index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // src/middleware/ip-restriction/index.ts
  2. import { HTTPException } from "../../http-exception.js";
  3. import {
  4. convertIPv4MappedIPv6ToIPv4,
  5. convertIPv4ToBinary,
  6. convertIPv6BinaryToString,
  7. convertIPv6ToBinary,
  8. distinctRemoteAddr,
  9. isIPv4MappedIPv6
  10. } from "../../utils/ipaddr.js";
  11. var IS_CIDR_NOTATION_REGEX = /\/[0-9]{0,3}$/;
  12. var buildMatcher = (rules) => {
  13. const functionRules = [];
  14. const staticRules = /* @__PURE__ */ new Set();
  15. const cidrRules = [];
  16. for (let rule of rules) {
  17. if (rule === "*") {
  18. return () => true;
  19. } else if (typeof rule === "function") {
  20. functionRules.push(rule);
  21. } else {
  22. if (IS_CIDR_NOTATION_REGEX.test(rule)) {
  23. const separatedRule = rule.split("/");
  24. const addrStr = separatedRule[0];
  25. const type2 = distinctRemoteAddr(addrStr);
  26. if (type2 === void 0) {
  27. throw new TypeError(`Invalid rule: ${rule}`);
  28. }
  29. let isIPv4 = type2 === "IPv4";
  30. let prefix = parseInt(separatedRule[1]);
  31. if (isIPv4 ? prefix === 32 : prefix === 128) {
  32. rule = addrStr;
  33. } else {
  34. let addr = (isIPv4 ? convertIPv4ToBinary : convertIPv6ToBinary)(addrStr);
  35. if (type2 === "IPv6" && isIPv4MappedIPv6(addr) && prefix >= 96) {
  36. isIPv4 = true;
  37. addr = convertIPv4MappedIPv6ToIPv4(addr);
  38. prefix -= 96;
  39. }
  40. const mask = (1n << BigInt(prefix)) - 1n << BigInt((isIPv4 ? 32 : 128) - prefix);
  41. cidrRules.push([isIPv4, addr & mask, mask]);
  42. continue;
  43. }
  44. }
  45. const type = distinctRemoteAddr(rule);
  46. if (type === void 0) {
  47. throw new TypeError(`Invalid rule: ${rule}`);
  48. }
  49. if (type === "IPv4") {
  50. staticRules.add(rule);
  51. staticRules.add(`::ffff:${rule}`);
  52. } else {
  53. const ipv6binary = convertIPv6ToBinary(rule);
  54. const ipv6Addr = convertIPv6BinaryToString(ipv6binary);
  55. staticRules.add(ipv6Addr);
  56. if (isIPv4MappedIPv6(ipv6binary)) {
  57. staticRules.add(ipv6Addr.substring(7));
  58. }
  59. }
  60. }
  61. }
  62. return (remote) => {
  63. if (staticRules.has(remote.addr)) {
  64. return true;
  65. }
  66. const remoteAddr = remote.binaryAddr ||= (remote.isIPv4 ? convertIPv4ToBinary : convertIPv6ToBinary)(remote.addr);
  67. const remoteIPv4Addr = remote.isIPv4 || isIPv4MappedIPv6(remoteAddr) ? remote.isIPv4 ? remoteAddr : convertIPv4MappedIPv6ToIPv4(remoteAddr) : void 0;
  68. for (const [isIPv4, addr, mask] of cidrRules) {
  69. if (isIPv4) {
  70. if (remoteIPv4Addr === void 0) {
  71. continue;
  72. }
  73. if ((remoteIPv4Addr & mask) === addr) {
  74. return true;
  75. }
  76. continue;
  77. }
  78. if (remote.isIPv4) {
  79. continue;
  80. }
  81. if ((remoteAddr & mask) === addr) {
  82. return true;
  83. }
  84. }
  85. for (const rule of functionRules) {
  86. if (rule({ addr: remote.addr, type: remote.type })) {
  87. return true;
  88. }
  89. }
  90. return false;
  91. };
  92. };
  93. var ipRestriction = (getIP, { denyList = [], allowList = [] }, onError) => {
  94. const allowLength = allowList.length;
  95. const denyMatcher = buildMatcher(denyList);
  96. const allowMatcher = buildMatcher(allowList);
  97. const blockError = (c) => new HTTPException(403, {
  98. res: c.text("Forbidden", {
  99. status: 403
  100. })
  101. });
  102. return async function ipRestriction2(c, next) {
  103. const connInfo = getIP(c);
  104. const addr = typeof connInfo === "string" ? connInfo : connInfo.remote.address;
  105. if (!addr) {
  106. throw blockError(c);
  107. }
  108. const type = typeof connInfo !== "string" && connInfo.remote.addressType || distinctRemoteAddr(addr);
  109. const remoteData = { addr, type, isIPv4: type === "IPv4" };
  110. if (denyMatcher(remoteData)) {
  111. if (onError) {
  112. return onError({ addr, type }, c);
  113. }
  114. throw blockError(c);
  115. }
  116. if (allowMatcher(remoteData)) {
  117. return await next();
  118. }
  119. if (allowLength === 0) {
  120. return await next();
  121. } else {
  122. if (onError) {
  123. return await onError({ addr, type }, c);
  124. }
  125. throw blockError(c);
  126. }
  127. };
  128. };
  129. export {
  130. ipRestriction
  131. };