index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var ip_restriction_exports = {};
  19. __export(ip_restriction_exports, {
  20. ipRestriction: () => ipRestriction
  21. });
  22. module.exports = __toCommonJS(ip_restriction_exports);
  23. var import_http_exception = require("../../http-exception");
  24. var import_ipaddr = require("../../utils/ipaddr");
  25. const IS_CIDR_NOTATION_REGEX = /\/[0-9]{0,3}$/;
  26. const buildMatcher = (rules) => {
  27. const functionRules = [];
  28. const staticRules = /* @__PURE__ */ new Set();
  29. const cidrRules = [];
  30. for (let rule of rules) {
  31. if (rule === "*") {
  32. return () => true;
  33. } else if (typeof rule === "function") {
  34. functionRules.push(rule);
  35. } else {
  36. if (IS_CIDR_NOTATION_REGEX.test(rule)) {
  37. const separatedRule = rule.split("/");
  38. const addrStr = separatedRule[0];
  39. const type2 = (0, import_ipaddr.distinctRemoteAddr)(addrStr);
  40. if (type2 === void 0) {
  41. throw new TypeError(`Invalid rule: ${rule}`);
  42. }
  43. let isIPv4 = type2 === "IPv4";
  44. let prefix = parseInt(separatedRule[1]);
  45. if (isIPv4 ? prefix === 32 : prefix === 128) {
  46. rule = addrStr;
  47. } else {
  48. let addr = (isIPv4 ? import_ipaddr.convertIPv4ToBinary : import_ipaddr.convertIPv6ToBinary)(addrStr);
  49. if (type2 === "IPv6" && (0, import_ipaddr.isIPv4MappedIPv6)(addr) && prefix >= 96) {
  50. isIPv4 = true;
  51. addr = (0, import_ipaddr.convertIPv4MappedIPv6ToIPv4)(addr);
  52. prefix -= 96;
  53. }
  54. const mask = (1n << BigInt(prefix)) - 1n << BigInt((isIPv4 ? 32 : 128) - prefix);
  55. cidrRules.push([isIPv4, addr & mask, mask]);
  56. continue;
  57. }
  58. }
  59. const type = (0, import_ipaddr.distinctRemoteAddr)(rule);
  60. if (type === void 0) {
  61. throw new TypeError(`Invalid rule: ${rule}`);
  62. }
  63. if (type === "IPv4") {
  64. staticRules.add(rule);
  65. staticRules.add(`::ffff:${rule}`);
  66. } else {
  67. const ipv6binary = (0, import_ipaddr.convertIPv6ToBinary)(rule);
  68. const ipv6Addr = (0, import_ipaddr.convertIPv6BinaryToString)(ipv6binary);
  69. staticRules.add(ipv6Addr);
  70. if ((0, import_ipaddr.isIPv4MappedIPv6)(ipv6binary)) {
  71. staticRules.add(ipv6Addr.substring(7));
  72. }
  73. }
  74. }
  75. }
  76. return (remote) => {
  77. if (staticRules.has(remote.addr)) {
  78. return true;
  79. }
  80. const remoteAddr = remote.binaryAddr ||= (remote.isIPv4 ? import_ipaddr.convertIPv4ToBinary : import_ipaddr.convertIPv6ToBinary)(remote.addr);
  81. const remoteIPv4Addr = remote.isIPv4 || (0, import_ipaddr.isIPv4MappedIPv6)(remoteAddr) ? remote.isIPv4 ? remoteAddr : (0, import_ipaddr.convertIPv4MappedIPv6ToIPv4)(remoteAddr) : void 0;
  82. for (const [isIPv4, addr, mask] of cidrRules) {
  83. if (isIPv4) {
  84. if (remoteIPv4Addr === void 0) {
  85. continue;
  86. }
  87. if ((remoteIPv4Addr & mask) === addr) {
  88. return true;
  89. }
  90. continue;
  91. }
  92. if (remote.isIPv4) {
  93. continue;
  94. }
  95. if ((remoteAddr & mask) === addr) {
  96. return true;
  97. }
  98. }
  99. for (const rule of functionRules) {
  100. if (rule({ addr: remote.addr, type: remote.type })) {
  101. return true;
  102. }
  103. }
  104. return false;
  105. };
  106. };
  107. const ipRestriction = (getIP, { denyList = [], allowList = [] }, onError) => {
  108. const allowLength = allowList.length;
  109. const denyMatcher = buildMatcher(denyList);
  110. const allowMatcher = buildMatcher(allowList);
  111. const blockError = (c) => new import_http_exception.HTTPException(403, {
  112. res: c.text("Forbidden", {
  113. status: 403
  114. })
  115. });
  116. return async function ipRestriction2(c, next) {
  117. const connInfo = getIP(c);
  118. const addr = typeof connInfo === "string" ? connInfo : connInfo.remote.address;
  119. if (!addr) {
  120. throw blockError(c);
  121. }
  122. const type = typeof connInfo !== "string" && connInfo.remote.addressType || (0, import_ipaddr.distinctRemoteAddr)(addr);
  123. const remoteData = { addr, type, isIPv4: type === "IPv4" };
  124. if (denyMatcher(remoteData)) {
  125. if (onError) {
  126. return onError({ addr, type }, c);
  127. }
  128. throw blockError(c);
  129. }
  130. if (allowMatcher(remoteData)) {
  131. return await next();
  132. }
  133. if (allowLength === 0) {
  134. return await next();
  135. } else {
  136. if (onError) {
  137. return await onError({ addr, type }, c);
  138. }
  139. throw blockError(c);
  140. }
  141. };
  142. };
  143. // Annotate the CommonJS export names for ESM import in node:
  144. 0 && (module.exports = {
  145. ipRestriction
  146. });