formacao
This commit is contained in:
+75
-24
@@ -1,4 +1,3 @@
|
||||
"use strict";
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
@@ -23,11 +22,45 @@ __export(ip_restriction_exports, {
|
||||
module.exports = __toCommonJS(ip_restriction_exports);
|
||||
var import_http_exception = require("../../http-exception");
|
||||
var import_ipaddr = require("../../utils/ipaddr");
|
||||
const IS_CIDR_NOTATION_REGEX = /\/[0-9]{0,3}$/;
|
||||
const IS_CIDR_NOTATION_REGEX = /\/[^/]*$/;
|
||||
const parseCidrPrefix = (rule, prefix, max) => {
|
||||
if (!/^[0-9]{1,3}$/.test(prefix)) {
|
||||
throw new TypeError(`Invalid rule: ${rule}`);
|
||||
}
|
||||
const parsedPrefix = parseInt(prefix);
|
||||
if (parsedPrefix > max) {
|
||||
throw new TypeError(`Invalid rule: ${rule}`);
|
||||
}
|
||||
return parsedPrefix;
|
||||
};
|
||||
const buildMatcher = (rules) => {
|
||||
const functionRules = [];
|
||||
const staticRules = /* @__PURE__ */ new Set();
|
||||
const staticIPv4Rules = /* @__PURE__ */ new Set();
|
||||
const staticIPv6Rules = /* @__PURE__ */ new Set();
|
||||
const cidrRules = [];
|
||||
const registerStaticRule = (rule) => {
|
||||
const type = (0, import_ipaddr.distinctRemoteAddr)(rule);
|
||||
if (type === void 0) {
|
||||
throw new TypeError(`Invalid rule: ${rule}`);
|
||||
}
|
||||
if (type === "IPv4") {
|
||||
const ipv4binary = (0, import_ipaddr.convertIPv4ToBinary)(rule);
|
||||
staticRules.add(rule);
|
||||
staticRules.add(`::ffff:${rule}`);
|
||||
staticIPv4Rules.add(ipv4binary);
|
||||
staticIPv6Rules.add(0xffffn << 32n | ipv4binary);
|
||||
} else {
|
||||
const ipv6binary = (0, import_ipaddr.convertIPv6ToBinary)(rule);
|
||||
const ipv6Addr = (0, import_ipaddr.convertIPv6BinaryToString)(ipv6binary);
|
||||
staticRules.add(ipv6Addr);
|
||||
staticIPv6Rules.add(ipv6binary);
|
||||
if ((0, import_ipaddr.isIPv4MappedIPv6)(ipv6binary)) {
|
||||
staticRules.add(ipv6Addr.substring(7));
|
||||
staticIPv4Rules.add((0, import_ipaddr.convertIPv4MappedIPv6ToIPv4)(ipv6binary));
|
||||
}
|
||||
}
|
||||
};
|
||||
for (let rule of rules) {
|
||||
if (rule === "*") {
|
||||
return () => true;
|
||||
@@ -37,40 +70,51 @@ const buildMatcher = (rules) => {
|
||||
if (IS_CIDR_NOTATION_REGEX.test(rule)) {
|
||||
const separatedRule = rule.split("/");
|
||||
const addrStr = separatedRule[0];
|
||||
const type2 = (0, import_ipaddr.distinctRemoteAddr)(addrStr);
|
||||
if (type2 === void 0) {
|
||||
const type = (0, import_ipaddr.distinctRemoteAddr)(addrStr);
|
||||
if (type === void 0) {
|
||||
throw new TypeError(`Invalid rule: ${rule}`);
|
||||
}
|
||||
const isIPv4 = type2 === "IPv4";
|
||||
const prefix = parseInt(separatedRule[1]);
|
||||
let isIPv4 = type === "IPv4";
|
||||
let prefix = parseCidrPrefix(rule, separatedRule[1], isIPv4 ? 32 : 128);
|
||||
if (isIPv4 ? prefix === 32 : prefix === 128) {
|
||||
rule = addrStr;
|
||||
} else {
|
||||
const addr = (isIPv4 ? import_ipaddr.convertIPv4ToBinary : import_ipaddr.convertIPv6ToBinary)(addrStr);
|
||||
let addr = (isIPv4 ? import_ipaddr.convertIPv4ToBinary : import_ipaddr.convertIPv6ToBinary)(addrStr);
|
||||
if (type === "IPv6" && (0, import_ipaddr.isIPv4MappedIPv6)(addr) && prefix >= 96) {
|
||||
isIPv4 = true;
|
||||
addr = (0, import_ipaddr.convertIPv4MappedIPv6ToIPv4)(addr);
|
||||
prefix -= 96;
|
||||
}
|
||||
const mask = (1n << BigInt(prefix)) - 1n << BigInt((isIPv4 ? 32 : 128) - prefix);
|
||||
cidrRules.push([isIPv4, addr & mask, mask]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
const type = (0, import_ipaddr.distinctRemoteAddr)(rule);
|
||||
if (type === void 0) {
|
||||
throw new TypeError(`Invalid rule: ${rule}`);
|
||||
}
|
||||
staticRules.add(
|
||||
type === "IPv4" ? rule : (0, import_ipaddr.convertIPv6BinaryToString)((0, import_ipaddr.convertIPv6ToBinary)(rule))
|
||||
// normalize IPv6 address (e.g. 0000:0000:0000:0000:0000:0000:0000:0001 => ::1)
|
||||
);
|
||||
registerStaticRule(rule);
|
||||
}
|
||||
}
|
||||
return (remote) => {
|
||||
if (staticRules.has(remote.addr)) {
|
||||
return true;
|
||||
}
|
||||
const remoteAddr = remote.binaryAddr ||= (remote.isIPv4 ? import_ipaddr.convertIPv4ToBinary : import_ipaddr.convertIPv6ToBinary)(remote.addr);
|
||||
const remoteIPv4Addr = remote.isIPv4 || (0, import_ipaddr.isIPv4MappedIPv6)(remoteAddr) ? remote.isIPv4 ? remoteAddr : (0, import_ipaddr.convertIPv4MappedIPv6ToIPv4)(remoteAddr) : void 0;
|
||||
if ((remote.isIPv4 ? staticIPv4Rules : staticIPv6Rules).has(remoteAddr)) {
|
||||
return true;
|
||||
}
|
||||
for (const [isIPv4, addr, mask] of cidrRules) {
|
||||
if (isIPv4 !== remote.isIPv4) {
|
||||
if (isIPv4) {
|
||||
if (remoteIPv4Addr === void 0) {
|
||||
continue;
|
||||
}
|
||||
if ((remoteIPv4Addr & mask) === addr) {
|
||||
return true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (remote.isIPv4) {
|
||||
continue;
|
||||
}
|
||||
const remoteAddr = remote.binaryAddr ||= (isIPv4 ? import_ipaddr.convertIPv4ToBinary : import_ipaddr.convertIPv6ToBinary)(remote.addr);
|
||||
if ((remoteAddr & mask) === addr) {
|
||||
return true;
|
||||
}
|
||||
@@ -100,14 +144,21 @@ const ipRestriction = (getIP, { denyList = [], allowList = [] }, onError) => {
|
||||
}
|
||||
const type = typeof connInfo !== "string" && connInfo.remote.addressType || (0, import_ipaddr.distinctRemoteAddr)(addr);
|
||||
const remoteData = { addr, type, isIPv4: type === "IPv4" };
|
||||
if (denyMatcher(remoteData)) {
|
||||
if (onError) {
|
||||
return onError({ addr, type }, c);
|
||||
try {
|
||||
if (denyMatcher(remoteData)) {
|
||||
if (onError) {
|
||||
return onError({ addr, type }, c);
|
||||
}
|
||||
throw blockError(c);
|
||||
}
|
||||
throw blockError(c);
|
||||
}
|
||||
if (allowMatcher(remoteData)) {
|
||||
return await next();
|
||||
if (allowMatcher(remoteData)) {
|
||||
return await next();
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof TypeError && e.code === import_ipaddr.INVALID_IP_ADDRESS_ERROR_CODE) {
|
||||
throw blockError(c);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
if (allowLength === 0) {
|
||||
return await next();
|
||||
|
||||
Reference in New Issue
Block a user