openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1,203 lines (1,202 loc) • 29.7 kB
JavaScript
import { s as __toESM, t as __commonJSMin } from "./rolldown-runtime-C_OpTf0N.mjs";
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
//#region node_modules/.pnpm/ipaddr.js@2.5.0/node_modules/ipaddr.js/lib/ipaddr.js
var require_ipaddr = /* @__PURE__ */ __commonJSMin(((e, t, n) => {
(function(e) {
"use strict";
const n = "(0?\\d+|0x[a-f0-9]+)";
const r = {
fourOctet: new RegExp(`^${n}\\.${n}\\.${n}\\.${n}$`, "i"),
threeOctet: new RegExp(`^${n}\\.${n}\\.${n}$`, "i"),
twoOctet: new RegExp(`^${n}\\.${n}$`, "i"),
longValue: new RegExp(`^${n}$`, "i")
};
const i = new RegExp(`^0[0-7]+$`, "i");
const a = new RegExp(`^0x[a-f0-9]+$`, "i");
const o = "%[0-9a-z]{1,}";
const s = "(?:[0-9a-f]+::?)+";
const c = {
zoneIndex: new RegExp(o, "i"),
"native": new RegExp(`^(::)?(${s})?([0-9a-f]+)?(::)?(${o})?$`, "i"),
deprecatedTransitional: new RegExp(`^(?:::)(${n}\\.${n}\\.${n}\\.${n}(${o})?)$`, "i"),
transitional: new RegExp(`^((?:${s})|(?:::)(?:${s})?)${n}\\.${n}\\.${n}\\.${n}(${o})?$`, "i")
};
function expandIPv6(e, t) {
if (e.indexOf("::") !== e.lastIndexOf("::")) return null;
let n = 0;
let r = -1;
let i = (e.match(c.zoneIndex) || [])[0];
let a, o;
if (i) {
i = i.substring(1);
e = e.replace(/%.+$/, "");
}
while ((r = e.indexOf(":", r + 1)) >= 0) n++;
if (e.substr(0, 2) === "::") n--;
if (e.substr(-2, 2) === "::") n--;
if (n >= t) return null;
o = t - n;
a = ":";
while (o--) a += "0:";
e = e.replace("::", a);
if (e[0] === ":") e = e.slice(1);
if (e[e.length - 1] === ":") e = e.slice(0, -1);
t = (function() {
const t = e.split(":");
const n = [];
for (let e = 0; e < t.length; e++) n.push(t[e].length > 4 ? NaN : parseInt(t[e], 16));
return n;
})();
return {
parts: t,
zoneId: i
};
}
function matchCIDR(e, t, n, r) {
if (e.length !== t.length) throw new Error("ipaddr: cannot match CIDR for objects with different lengths");
let i = 0;
let a;
while (r > 0) {
a = n - r;
if (a < 0) a = 0;
if (e[i] >> a !== t[i] >> a) return false;
r -= n;
i += 1;
}
return true;
}
function parseIntAuto(e) {
if (a.test(e)) return parseInt(e, 16);
if (e[0] === "0" && !isNaN(parseInt(e[1], 10))) {
if (i.test(e)) return parseInt(e, 8);
throw new Error(`ipaddr: cannot parse ${e} as octal`);
}
return parseInt(e, 10);
}
function padPart(e, t) {
while (e.length < t) e = `0${e}`;
return e;
}
const l = {};
l.IPv4 = (function() {
function IPv4(e) {
if (e.length !== 4) throw new Error("ipaddr: ipv4 octet count should be 4");
let t, n;
for (t = 0; t < e.length; t++) {
n = e[t];
if (!(0 <= n && n <= 255)) throw new Error("ipaddr: ipv4 octet should fit in 8 bits");
}
this.octets = e;
}
IPv4.prototype.SpecialRanges = {
unspecified: [[new IPv4([
0,
0,
0,
0
]), 8]],
broadcast: [[new IPv4([
255,
255,
255,
255
]), 32]],
multicast: [[new IPv4([
224,
0,
0,
0
]), 4]],
linkLocal: [[new IPv4([
169,
254,
0,
0
]), 16]],
loopback: [[new IPv4([
127,
0,
0,
0
]), 8]],
carrierGradeNat: [[new IPv4([
100,
64,
0,
0
]), 10]],
"private": [
[new IPv4([
10,
0,
0,
0
]), 8],
[new IPv4([
172,
16,
0,
0
]), 12],
[new IPv4([
192,
168,
0,
0
]), 16]
],
reserved: [
[new IPv4([
192,
0,
0,
0
]), 24],
[new IPv4([
192,
0,
2,
0
]), 24],
[new IPv4([
192,
88,
99,
0
]), 24],
[new IPv4([
198,
18,
0,
0
]), 15],
[new IPv4([
198,
51,
100,
0
]), 24],
[new IPv4([
203,
0,
113,
0
]), 24],
[new IPv4([
240,
0,
0,
0
]), 4]
],
as112: [[new IPv4([
192,
175,
48,
0
]), 24], [new IPv4([
192,
31,
196,
0
]), 24]],
amt: [[new IPv4([
192,
52,
193,
0
]), 24]]
};
IPv4.prototype.kind = function() {
return "ipv4";
};
IPv4.prototype.match = function(e, t) {
let n;
if (t === void 0) {
n = e;
e = n[0];
t = n[1];
}
if (e.kind() !== "ipv4") throw new Error("ipaddr: cannot match ipv4 address with non-ipv4 one");
return matchCIDR(this.octets, e.octets, 8, t);
};
IPv4.prototype.prefixLengthFromSubnetMask = function() {
let e = 0;
let t = false;
const n = {
0: 8,
128: 7,
192: 6,
224: 5,
240: 4,
248: 3,
252: 2,
254: 1,
255: 0
};
let r, i, a;
for (r = 3; r >= 0; r -= 1) {
i = this.octets[r];
if (i in n) {
a = n[i];
if (t && a !== 0) return null;
if (a !== 8) t = true;
e += a;
} else return null;
}
return 32 - e;
};
IPv4.prototype.range = function() {
return l.subnetMatch(this, this.SpecialRanges);
};
IPv4.prototype.toByteArray = function() {
return this.octets.slice(0);
};
IPv4.prototype.toIPv4MappedAddress = function() {
return l.IPv6.parse(`::ffff:${this.toString()}`);
};
IPv4.prototype.toNormalizedString = function() {
return this.toString();
};
IPv4.prototype.toString = function() {
return this.octets.join(".");
};
return IPv4;
})();
l.IPv4.broadcastAddressFromCIDR = function(e) {
try {
const t = this.parseCIDR(e);
const n = t[0].toByteArray();
const r = this.subnetMaskFromPrefixLength(t[1]).toByteArray();
const i = [];
let a = 0;
while (a < 4) {
i.push(parseInt(n[a], 10) | parseInt(r[a], 10) ^ 255);
a++;
}
return new this(i);
} catch (e) {
throw new Error("ipaddr: the address does not have IPv4 CIDR format", { cause: e });
}
};
l.IPv4.isIPv4 = function(e) {
return this.parser(e) !== null;
};
l.IPv4.isValid = function(e) {
try {
new this(this.parser(e));
return true;
} catch {
return false;
}
};
l.IPv4.isValidCIDR = function(e) {
try {
this.parseCIDR(e);
return true;
} catch {
return false;
}
};
l.IPv4.isValidFourPartDecimal = function(e) {
if (l.IPv4.isValid(e) && e.match(/^(0|[1-9]\d*)(\.(0|[1-9]\d*)){3}$/)) return true;
else return false;
};
l.IPv4.isValidCIDRFourPartDecimal = function(e) {
const t = e.match(/^(.+)\/(\d+)$/);
if (!l.IPv4.isValidCIDR(e) || !t) return false;
return l.IPv4.isValidFourPartDecimal(t[1]);
};
l.IPv4.networkAddressFromCIDR = function(e) {
let t, n, r, i, a;
try {
t = this.parseCIDR(e);
r = t[0].toByteArray();
a = this.subnetMaskFromPrefixLength(t[1]).toByteArray();
i = [];
n = 0;
while (n < 4) {
i.push(parseInt(r[n], 10) & parseInt(a[n], 10));
n++;
}
return new this(i);
} catch (e) {
throw new Error("ipaddr: the address does not have IPv4 CIDR format", { cause: e });
}
};
l.IPv4.parse = function(e) {
const t = this.parser(e);
if (t === null) throw new Error("ipaddr: string is not formatted like an IPv4 Address");
return new this(t);
};
l.IPv4.parseCIDR = function(e) {
let t;
if (t = e.match(/^(.+)\/(\d+)$/)) {
const e = parseInt(t[2]);
if (e >= 0 && e <= 32) {
const n = [this.parse(t[1]), e];
Object.defineProperty(n, "toString", { value: function() {
return this.join("/");
} });
return n;
}
}
throw new Error("ipaddr: string is not formatted like an IPv4 CIDR range");
};
l.IPv4.parser = function(e) {
let t, n, i;
if (t = e.match(r.fourOctet)) return (function() {
const e = t.slice(1, 6);
const r = [];
for (let t = 0; t < e.length; t++) {
n = e[t];
r.push(parseIntAuto(n));
}
return r;
})();
else if (t = e.match(r.longValue)) {
i = parseIntAuto(t[1]);
if (i > 4294967295 || i < 0) throw new Error("ipaddr: address outside defined range");
return (function() {
const e = [];
let t = 0;
for (; t <= 24; t += 8) e.push(i >> t & 255);
return e;
})().reverse();
} else if (t = e.match(r.twoOctet)) return (function() {
const e = t.slice(1, 4);
const n = [];
i = parseIntAuto(e[1]);
if (i > 16777215 || i < 0) throw new Error("ipaddr: address outside defined range");
n.push(parseIntAuto(e[0]));
n.push(i >> 16 & 255);
n.push(i >> 8 & 255);
n.push(i & 255);
return n;
})();
else if (t = e.match(r.threeOctet)) return (function() {
const e = t.slice(1, 5);
const n = [];
i = parseIntAuto(e[2]);
if (i > 65535 || i < 0) throw new Error("ipaddr: address outside defined range");
n.push(parseIntAuto(e[0]));
n.push(parseIntAuto(e[1]));
n.push(i >> 8 & 255);
n.push(i & 255);
return n;
})();
else return null;
};
l.IPv4.subnetMaskFromPrefixLength = function(e) {
e = parseInt(e);
if (Number.isNaN(e) || e < 0 || e > 32) throw new Error("ipaddr: invalid IPv4 prefix length");
const t = [
0,
0,
0,
0
];
let n = 0;
const r = Math.floor(e / 8);
while (n < r) {
t[n] = 255;
n++;
}
if (r < 4) t[r] = Math.pow(2, e % 8) - 1 << 8 - e % 8;
return new this(t);
};
l.IPv6 = (function() {
function IPv6(e, t) {
let n, r;
if (e.length === 16) {
this.parts = [];
for (n = 0; n <= 14; n += 2) this.parts.push(e[n] << 8 | e[n + 1]);
} else if (e.length === 8) this.parts = e;
else throw new Error("ipaddr: ipv6 part count should be 8 or 16");
for (n = 0; n < this.parts.length; n++) {
r = this.parts[n];
if (!(0 <= r && r <= 65535)) throw new Error("ipaddr: ipv6 part should fit in 16 bits");
}
if (t) this.zoneId = t;
}
IPv6.prototype.SpecialRanges = {
unspecified: [new IPv6([
0,
0,
0,
0,
0,
0,
0,
0
]), 128],
linkLocal: [new IPv6([
65152,
0,
0,
0,
0,
0,
0,
0
]), 10],
multicast: [new IPv6([
65280,
0,
0,
0,
0,
0,
0,
0
]), 8],
loopback: [new IPv6([
0,
0,
0,
0,
0,
0,
0,
1
]), 128],
uniqueLocal: [new IPv6([
64512,
0,
0,
0,
0,
0,
0,
0
]), 7],
ipv4Mapped: [new IPv6([
0,
0,
0,
0,
0,
65535,
0,
0
]), 96],
deprecatedSiteLocal: [new IPv6([
65216,
0,
0,
0,
0,
0,
0,
0
]), 10],
discard: [new IPv6([
256,
0,
0,
0,
0,
0,
0,
0
]), 64],
rfc6145: [new IPv6([
0,
0,
0,
0,
65535,
0,
0,
0
]), 96],
rfc6052: [[new IPv6([
100,
65435,
0,
0,
0,
0,
0,
0
]), 96], [new IPv6([
100,
65435,
1,
0,
0,
0,
0,
0
]), 48]],
"6to4": [new IPv6([
8194,
0,
0,
0,
0,
0,
0,
0
]), 16],
teredo: [new IPv6([
8193,
0,
0,
0,
0,
0,
0,
0
]), 32],
benchmarking: [new IPv6([
8193,
2,
0,
0,
0,
0,
0,
0
]), 48],
amt: [new IPv6([
8193,
3,
0,
0,
0,
0,
0,
0
]), 32],
as112v6: [[new IPv6([
8193,
4,
274,
0,
0,
0,
0,
0
]), 48], [new IPv6([
9760,
79,
32768,
0,
0,
0,
0,
0
]), 48]],
deprecatedOrchid: [new IPv6([
8193,
16,
0,
0,
0,
0,
0,
0
]), 28],
orchid2: [new IPv6([
8193,
32,
0,
0,
0,
0,
0,
0
]), 28],
droneRemoteIdProtocolEntityTags: [new IPv6([
8193,
48,
0,
0,
0,
0,
0,
0
]), 28],
segmentRouting: [new IPv6([
24320,
0,
0,
0,
0,
0,
0,
0
]), 16],
reserved: [
[new IPv6([
8193,
0,
0,
0,
0,
0,
0,
0
]), 23],
[new IPv6([
8193,
3512,
0,
0,
0,
0,
0,
0
]), 32],
[new IPv6([
16383,
0,
0,
0,
0,
0,
0,
0
]), 20]
]
};
IPv6.prototype.isIPv4MappedAddress = function() {
return this.range() === "ipv4Mapped";
};
IPv6.prototype.kind = function() {
return "ipv6";
};
IPv6.prototype.match = function(e, t) {
let n;
if (t === void 0) {
n = e;
e = n[0];
t = n[1];
}
if (e.kind() !== "ipv6") throw new Error("ipaddr: cannot match ipv6 address with non-ipv6 one");
return matchCIDR(this.parts, e.parts, 16, t);
};
IPv6.prototype.prefixLengthFromSubnetMask = function() {
let e = 0;
let t = false;
const n = {
0: 16,
32768: 15,
49152: 14,
57344: 13,
61440: 12,
63488: 11,
64512: 10,
65024: 9,
65280: 8,
65408: 7,
65472: 6,
65504: 5,
65520: 4,
65528: 3,
65532: 2,
65534: 1,
65535: 0
};
let r, i;
for (let a = 7; a >= 0; a -= 1) {
r = this.parts[a];
if (r in n) {
i = n[r];
if (t && i !== 0) return null;
if (i !== 16) t = true;
e += i;
} else return null;
}
return 128 - e;
};
IPv6.prototype.range = function() {
return l.subnetMatch(this, this.SpecialRanges);
};
IPv6.prototype.toByteArray = function() {
let e;
const t = [];
const n = this.parts;
for (let r = 0; r < n.length; r++) {
e = n[r];
t.push(e >> 8);
t.push(e & 255);
}
return t;
};
IPv6.prototype.toFixedLengthString = function() {
const e = (function() {
const e = [];
for (let t = 0; t < this.parts.length; t++) e.push(padPart(this.parts[t].toString(16), 4));
return e;
}).call(this).join(":");
let t = "";
if (this.zoneId) t = `%${this.zoneId}`;
return e + t;
};
IPv6.prototype.toIPv4Address = function() {
if (!this.isIPv4MappedAddress()) throw new Error("ipaddr: trying to convert a generic ipv6 address to ipv4");
const e = this.parts.slice(-2);
const t = e[0];
const n = e[1];
return new l.IPv4([
t >> 8,
t & 255,
n >> 8,
n & 255
]);
};
IPv6.prototype.toNormalizedString = function() {
const e = (function() {
const e = [];
for (let t = 0; t < this.parts.length; t++) e.push(this.parts[t].toString(16));
return e;
}).call(this).join(":");
let t = "";
if (this.zoneId) t = `%${this.zoneId}`;
return e + t;
};
IPv6.prototype.toRFC5952String = function() {
const e = /((^|:)(0(:|$)){2,})/g;
let t = "";
if (this.zoneId) t = `%${this.zoneId}`;
const n = this.toNormalizedString();
const r = n.slice(0, n.length - t.length);
let i = 0;
let a = -1;
let o = -1;
let s;
while (s = e.exec(r)) {
const e = (s[0].match(/0/g) || []).length;
if (e > o) {
o = e;
i = s.index;
a = s[0].length;
}
}
if (a < 0) return r + t;
return `${r.substring(0, i)}::${r.substring(i + a)}${t}`;
};
IPv6.prototype.toString = function() {
return this.toRFC5952String();
};
return IPv6;
})();
l.IPv6.broadcastAddressFromCIDR = function(e) {
try {
const t = this.parseCIDR(e);
const n = t[0].toByteArray();
const r = this.subnetMaskFromPrefixLength(t[1]).toByteArray();
const i = [];
let a = 0;
while (a < 16) {
i.push(parseInt(n[a], 10) | parseInt(r[a], 10) ^ 255);
a++;
}
return new this(i);
} catch (e) {
throw new Error("ipaddr: the address does not have IPv6 CIDR format", { cause: e });
}
};
l.IPv6.isIPv6 = function(e) {
return this.parser(e) !== null;
};
l.IPv6.isValid = function(e) {
if (typeof e === "string" && e.indexOf(":") === -1) return false;
try {
const t = this.parser(e);
new this(t.parts, t.zoneId);
return true;
} catch {
return false;
}
};
l.IPv6.isValidCIDR = function(e) {
if (typeof e === "string" && e.indexOf(":") === -1) return false;
try {
this.parseCIDR(e);
return true;
} catch {
return false;
}
};
l.IPv6.networkAddressFromCIDR = function(e) {
let t, n, r, i, a;
try {
t = this.parseCIDR(e);
r = t[0].toByteArray();
a = this.subnetMaskFromPrefixLength(t[1]).toByteArray();
i = [];
n = 0;
while (n < 16) {
i.push(parseInt(r[n], 10) & parseInt(a[n], 10));
n++;
}
return new this(i);
} catch (e) {
throw new Error("ipaddr: the address does not have IPv6 CIDR format", { cause: e });
}
};
l.IPv6.parse = function(e) {
const t = this.parser(e);
if (t === null) throw new Error("ipaddr: string is not formatted like an IPv6 Address");
return new this(t.parts, t.zoneId);
};
l.IPv6.parseCIDR = function(e) {
let t, n, r;
if (n = e.match(/^(.+)\/(\d+)$/)) {
t = parseInt(n[2]);
if (t >= 0 && t <= 128) {
r = [this.parse(n[1]), t];
Object.defineProperty(r, "toString", { value: function() {
return this.join("/");
} });
return r;
}
}
throw new Error("ipaddr: string is not formatted like an IPv6 CIDR range");
};
l.IPv6.parser = function(e) {
let t, n, r, i, a, o;
if (r = e.match(c.deprecatedTransitional)) return this.parser(`::ffff:${r[1]}`);
if (c.native.test(e)) return expandIPv6(e, 8);
if (r = e.match(c.transitional)) {
o = r[6] || "";
t = r[1];
if (!r[1].endsWith("::")) t = t.slice(0, -1);
t = expandIPv6(t + o, 6);
if (t && t.parts) {
a = [
parseInt(r[2]),
parseInt(r[3]),
parseInt(r[4]),
parseInt(r[5])
];
for (n = 0; n < a.length; n++) {
i = a[n];
if (!(0 <= i && i <= 255)) return null;
}
t.parts.push(a[0] << 8 | a[1]);
t.parts.push(a[2] << 8 | a[3]);
return {
parts: t.parts,
zoneId: t.zoneId
};
}
}
return null;
};
l.IPv6.subnetMaskFromPrefixLength = function(e) {
e = parseInt(e);
if (Number.isNaN(e) || e < 0 || e > 128) throw new Error("ipaddr: invalid IPv6 prefix length");
const t = [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
];
let n = 0;
const r = Math.floor(e / 8);
while (n < r) {
t[n] = 255;
n++;
}
if (r < 16) t[r] = Math.pow(2, e % 8) - 1 << 8 - e % 8;
return new this(t);
};
l.fromByteArray = function(e) {
const t = e.length;
if (t === 4) return new l.IPv4(e);
else if (t === 16) return new l.IPv6(e);
else throw new Error("ipaddr: the binary input is neither an IPv6 nor IPv4 address");
};
l.isValid = function(e) {
return l.IPv6.isValid(e) || l.IPv4.isValid(e);
};
l.isValidCIDR = function(e) {
return l.IPv6.isValidCIDR(e) || l.IPv4.isValidCIDR(e);
};
l.parse = function(e) {
if (l.IPv6.isValid(e)) return l.IPv6.parse(e);
else if (l.IPv4.isValid(e)) return l.IPv4.parse(e);
else throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format");
};
l.parseCIDR = function(e) {
try {
return l.IPv6.parseCIDR(e);
} catch {
try {
return l.IPv4.parseCIDR(e);
} catch (e) {
throw new Error("ipaddr: the address has neither IPv6 nor IPv4 CIDR format", { cause: e });
}
}
};
l.process = function(e) {
const t = this.parse(e);
if (t.kind() === "ipv6" && t.isIPv4MappedAddress()) return t.toIPv4Address();
else return t;
};
l.subnetMatch = function(e, t, n) {
let r, i, a, o;
if (n === void 0 || n === null) n = "unicast";
for (i in t) if (Object.prototype.hasOwnProperty.call(t, i)) {
a = t[i];
if (a[0] && !(a[0] instanceof Array)) a = [a];
for (r = 0; r < a.length; r++) {
o = a[r];
if (e.kind() === o[0].kind() && e.match.apply(e, o)) return i;
}
}
return n;
};
if (typeof t !== "undefined" && t.exports) t.exports = l;
else e.ipaddr = l;
})(n);
}));
//#endregion
//#region packages/net-policy/src/ip.ts
var import_ipaddr = /* @__PURE__ */ __toESM(require_ipaddr(), 1);
function expectIpv6Hextets(e) {
const [t, n, r, i, a, o, s, c] = e;
if (t === void 0 || n === void 0 || r === void 0 || i === void 0 || a === void 0 || o === void 0 || s === void 0 || c === void 0) throw new Error("expected IPv6 address to expose 8 hextets");
return [
t,
n,
r,
i,
a,
o,
s,
c
];
}
const BLOCKED_IPV4_SPECIAL_USE_RANGES = /* @__PURE__ */ new Set([
"unspecified",
"broadcast",
"multicast",
"linkLocal",
"loopback",
"carrierGradeNat",
"private",
"reserved"
]);
const PRIVATE_OR_LOOPBACK_IPV4_RANGES = /* @__PURE__ */ new Set([
"loopback",
"private",
"linkLocal",
"carrierGradeNat"
]);
const BLOCKED_IPV6_SPECIAL_USE_RANGES = /* @__PURE__ */ new Set([
"unspecified",
"loopback",
"linkLocal",
"uniqueLocal",
"multicast",
"reserved",
"benchmarking",
"discard",
"orchid2"
]);
const RFC2544_BENCHMARK_PREFIX = [import_ipaddr.default.IPv4.parse("198.18.0.0"), 15];
const CLOUD_METADATA_IP_ADDRESSES = /* @__PURE__ */ new Set(["100.100.100.200", "fd00:ec2::254"]);
/** Type guard for parsed IPv4 addresses. */
function isIpv4Address(e) {
return e.kind() === "ipv4";
}
/** Type guard for parsed IPv6 addresses. */
function isIpv6Address(e) {
return e.kind() === "ipv6";
}
function normalizeIpv4MappedAddress(e) {
return isIpv6Address(e) && e.isIPv4MappedAddress() ? e.toIPv4Address() : e;
}
function normalizeIpParseInput(e) {
const t = normalizeOptionalString(e);
if (!t) return;
return t.startsWith("[") && t.endsWith("]") ? t.slice(1, -1) : t;
}
/** Parses canonical IPv4/IPv6 literals, rejecting legacy IPv4 shorthand forms. */
function parseCanonicalIpAddress(e) {
const t = normalizeIpParseInput(e);
if (!t) return;
return import_ipaddr.default.IPv4.isValidFourPartDecimal(t) || import_ipaddr.default.IPv6.isValid(t) ? import_ipaddr.default.parse(t) : void 0;
}
/** Parses canonical IP literals plus legacy IPv4 forms needed for SSRF checks. */
function parseLooseIpAddress(e) {
const t = normalizeIpParseInput(e);
if (!t) return;
return import_ipaddr.default.isValid(t) ? import_ipaddr.default.parse(t) : void 0;
}
/** Normalizes canonical IP literals and maps IPv4-mapped IPv6 addresses to IPv4 text. */
function normalizeIpAddress(e) {
return parseNormalizedIpAddress(e)?.toString();
}
/** Parses canonical literals, normalizing mapped IPv4 and lowercase IPv6 zone IDs. */
function parseNormalizedIpAddress(e) {
const t = parseCanonicalIpAddress(e);
if (!t) return;
const n = normalizeIpv4MappedAddress(t);
if (isIpv6Address(n) && n.zoneId) n.zoneId = n.zoneId.toLowerCase();
return n;
}
/** True only for canonical four-part dotted-decimal IPv4 literals. */
function isCanonicalDottedDecimalIPv4(e) {
const t = normalizeIpParseInput(e);
return t !== void 0 && import_ipaddr.default.IPv4.isValidFourPartDecimal(t);
}
/** Detects legacy numeric IPv4 forms that canonical parsing deliberately rejects. */
function isLegacyIpv4Literal(e) {
const t = normalizeIpParseInput(e);
if (!t || t.includes(":")) return false;
if (isCanonicalDottedDecimalIPv4(t)) return false;
const n = t.split(".");
return n.length <= 4 && n.every((e) => /^[0-9]+$/.test(e) || /^0x[0-9a-f]+$/i.test(e));
}
/** True when a canonical IP literal is loopback, including IPv4-mapped IPv6. */
function isLoopbackIpAddress(e) {
const t = parseCanonicalIpAddress(e);
if (!t) return false;
return normalizeIpv4MappedAddress(t).range() === "loopback";
}
/** True for link-local IPs, including legacy and embedded-IPv4 forms. */
function isLinkLocalIpAddress(e) {
const t = parseLooseIpAddress(e);
if (!t) return false;
const n = normalizeIpv4MappedAddress(t);
if (isIpv4Address(n)) return n.range() === "linkLocal";
if (extractEmbeddedIpv4FromIpv6(n)?.range() === "linkLocal") return true;
return n.range() === "linkLocal";
}
/** True for unspecified IPs, including IPv4 embedded in IPv6 transition forms. */
function isUnspecifiedIpAddress(e) {
const t = parseCanonicalIpAddress(e);
if (!t || t.range() === "loopback") return false;
return (isIpv6Address(t) ? extractEmbeddedIpv4FromIpv6(t) ?? t : t).range() === "unspecified";
}
/** True for cloud metadata IP literals, including mapped and embedded forms. */
function isCloudMetadataIpAddress(e) {
const t = parseLooseIpAddress(e);
if (!t) return false;
const n = normalizeIpv4MappedAddress(t);
if (isIpv6Address(n)) {
const e = extractEmbeddedIpv4FromIpv6(n);
if (e && CLOUD_METADATA_IP_ADDRESSES.has(e.toString())) return true;
}
return CLOUD_METADATA_IP_ADDRESSES.has(n.toString());
}
/** True for canonical private, loopback, link-local, or blocked special-use IPs. */
function isPrivateOrLoopbackIpAddress(e) {
const t = parseCanonicalIpAddress(e);
if (!t) return false;
const n = normalizeIpv4MappedAddress(t);
if (isIpv4Address(n)) return PRIVATE_OR_LOOPBACK_IPV4_RANGES.has(n.range());
if (isBlockedSpecialUseIpv6Address(n)) return true;
const r = extractEmbeddedIpv4FromIpv6(n);
return r ? PRIVATE_OR_LOOPBACK_IPV4_RANGES.has(r.range()) : false;
}
/** True for RFC 8215 local-use NAT64 IPv6 literals (`64:ff9b:1::/48`). */
function isRfc8215LocalUseNat64Ipv6Address(e) {
const t = parseCanonicalIpAddress(e);
return Boolean(t && isIpv6Address(t) && isRfc8215Nat64LocalUseAddress(t));
}
/** Applies the SSRF block policy for parsed IPv6 special-use ranges. */
function isBlockedSpecialUseIpv6Address(e, t = {}) {
const n = e.range();
if (isRfc8215Nat64LocalUseAddress(e)) return true;
if (isCloudMetadataIpAddress(e.toString())) return true;
if (n === "uniqueLocal" && t.allowUniqueLocalRange === true) return false;
if (BLOCKED_IPV6_SPECIAL_USE_RANGES.has(n)) return true;
const [r] = expectIpv6Hextets(e.parts);
return (r & 65472) === 65216;
}
/** True for canonical IPv4 literals in RFC 1918 private ranges. */
function isRfc1918Ipv4Address(e) {
return parseCanonicalIpAddress(e)?.range() === "private";
}
/** True for canonical IPv4 literals in the carrier-grade NAT range. */
function isCarrierGradeNatIpv4Address(e) {
return parseCanonicalIpAddress(e)?.range() === "carrierGradeNat";
}
/** Applies the SSRF block policy for parsed IPv4 special-use ranges. */
function isBlockedSpecialUseIpv4Address(e, t = {}) {
const n = e.match(RFC2544_BENCHMARK_PREFIX);
if (n && t.allowRfc2544BenchmarkRange === true) return false;
return BLOCKED_IPV4_SPECIAL_USE_RANGES.has(e.range()) || n;
}
function decodeIpv4FromHextets(e, t) {
const n = [
e >>> 8 & 255,
e & 255,
t >>> 8 & 255,
t & 255
];
return import_ipaddr.default.IPv4.parse(n.join("."));
}
function isRfc8215Nat64LocalUseAddress(e) {
const t = expectIpv6Hextets(e.parts);
return t[0] === 100 && t[1] === 65435 && t[2] === 1;
}
/** Extracts the embedded IPv4 address from mapped and transition IPv6 prefixes. */
function extractEmbeddedIpv4FromIpv6(e) {
const t = expectIpv6Hextets(e.parts);
switch (e.range()) {
case "ipv4Mapped": return e.toIPv4Address();
case "rfc6145": return decodeIpv4FromHextets(t[6], t[7]);
case "rfc6052":
if (isRfc8215Nat64LocalUseAddress(e)) return;
return decodeIpv4FromHextets(t[6], t[7]);
case "6to4": return decodeIpv4FromHextets(t[1], t[2]);
case "teredo": return decodeIpv4FromHextets(t[6] ^ 65535, t[7] ^ 65535);
}
const n = t[0] === 0 && t[1] === 0 && t[2] === 0 && t[3] === 0 && t[4] === 0 && t[5] === 0;
const r = (t[4] & 64767) === 0 && t[5] === 24318;
if (n || r) return decodeIpv4FromHextets(t[6], t[7]);
}
/** Parses the exact-IP and CIDR forms accepted by runtime address matching. */
function parseIpAddressOrCidr(e) {
const t = normalizeOptionalString(e);
if (!t) return;
if (!t.includes("/")) {
const e = parseCanonicalIpAddress(t);
return e ? [e] : void 0;
}
try {
return import_ipaddr.default.parseCIDR(t);
} catch {
return;
}
}
/** Checks an IP literal against an exact IP or CIDR range, normalizing mapped IPv4. */
function isIpInCidr(e, t) {
return isParsedIpInCidr(parseCanonicalIpAddress(e), parseIpAddressOrCidr(t));
}
/** Matches already-parsed addresses with the same mapped-IPv4 rules as isIpInCidr. */
function isParsedIpInCidr(e, t) {
if (!e || !t) return false;
const [n, r] = t;
const i = normalizeIpv4MappedAddress(e);
const a = normalizeIpv4MappedAddress(n);
if (r === void 0) return i.kind() === a.kind() && i.toString() === a.toString();
if (isIpv4Address(i) && isIpv4Address(a)) {
const e = isIpv6Address(n) ? Math.max(0, r - 96) : r;
return i.match([a, e]);
}
if (isIpv6Address(i) && isIpv6Address(a)) return i.match([a, r]);
return false;
}
//#endregion
export { require_ipaddr as C, parseNormalizedIpAddress as S, isUnspecifiedIpAddress as _, isCarrierGradeNatIpv4Address as a, parseIpAddressOrCidr as b, isIpv4Address as c, isLinkLocalIpAddress as d, isLoopbackIpAddress as f, isRfc8215LocalUseNat64Ipv6Address as g, isRfc1918Ipv4Address as h, isCanonicalDottedDecimalIPv4 as i, isIpv6Address as l, isPrivateOrLoopbackIpAddress as m, isBlockedSpecialUseIpv4Address as n, isCloudMetadataIpAddress as o, isParsedIpInCidr as p, isBlockedSpecialUseIpv6Address as r, isIpInCidr as s, extractEmbeddedIpv4FromIpv6 as t, isLegacyIpv4Literal as u, normalizeIpAddress as v, parseLooseIpAddress as x, parseCanonicalIpAddress as y };