File size: 8,235 Bytes
a5d718a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | // src/utils/ipaddr.ts
var expandIPv6 = (ipV6) => {
const sections = ipV6.split(":");
if (IPV4_REGEX.test(sections.at(-1))) {
sections.splice(
-1,
1,
...convertIPv6BinaryToString(convertIPv4ToBinary(sections.at(-1))).substring(2).split(":")
// => ['7f00', '0001']
);
}
for (let i = 0; i < sections.length; i++) {
const node = sections[i];
if (node !== "") {
sections[i] = node.padStart(4, "0");
} else {
while (sections[i + 1] === "") {
sections.splice(i + 1, 1);
}
sections[i] = new Array(8 - sections.length + 1).fill("0000").join(":");
}
}
return sections.join(":");
};
var IPV4_OCTET_PART = "(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])";
var IPV4_REGEX = new RegExp(`^(?:${IPV4_OCTET_PART}\\.){3}${IPV4_OCTET_PART}$`);
var INVALID_IP_ADDRESS_ERROR_CODE = "ERR_INVALID_IP_ADDRESS";
var CHAR_CODE_0 = 48;
var CHAR_CODE_9 = 57;
var CHAR_CODE_A = 65;
var CHAR_CODE_F = 70;
var CHAR_CODE_a = 97;
var CHAR_CODE_f = 102;
var CHAR_CODE_DOT = 46;
var CHAR_CODE_COLON = 58;
var CHAR_CODE_PERCENT = 37;
var distinctRemoteAddr = (remoteAddr) => {
if (IPV4_REGEX.test(remoteAddr)) {
return "IPv4";
}
if (remoteAddr.includes(":")) {
return "IPv6";
}
};
var createInvalidIPAddressError = (message) => {
const error = new TypeError(message);
error.code = INVALID_IP_ADDRESS_ERROR_CODE;
return error;
};
var throwInvalidIPv4Address = (ipv4) => {
throw createInvalidIPAddressError(`Invalid IPv4 address: ${ipv4}`);
};
var throwInvalidIPv6Address = (ipv6) => {
throw createInvalidIPAddressError(`Invalid IPv6 address: ${ipv6}`);
};
var parseIPv4ToBinary = (ipv4, start, end, onInvalid) => {
let result = 0n;
let octets = 0;
let octet = 0;
let digits = 0;
let firstDigit = 0;
for (let i = start; i <= end; i++) {
const code = i < end ? ipv4.charCodeAt(i) : CHAR_CODE_DOT;
if (code >= CHAR_CODE_0 && code <= CHAR_CODE_9) {
if (digits === 0) {
firstDigit = code;
} else if (firstDigit === CHAR_CODE_0) {
onInvalid();
}
octet = octet * 10 + code - CHAR_CODE_0;
if (octet > 255) {
onInvalid();
}
digits++;
continue;
}
if (code !== CHAR_CODE_DOT || digits === 0 || octets === 4) {
onInvalid();
}
result = (result << 8n) + BigInt(octet);
octets++;
octet = 0;
digits = 0;
}
if (octets !== 4) {
onInvalid();
}
return result;
};
var parseIPv6HexCode = (code) => {
if (code >= CHAR_CODE_0 && code <= CHAR_CODE_9) {
return code - CHAR_CODE_0;
}
if (code >= CHAR_CODE_A && code <= CHAR_CODE_F) {
return code - CHAR_CODE_A + 10;
}
if (code >= CHAR_CODE_a && code <= CHAR_CODE_f) {
return code - CHAR_CODE_a + 10;
}
return -1;
};
var isIPv6LinkLocal = (ipv6binary) => ipv6binary >> 118n === 0x3fan;
var convertIPv4ToBinary = (ipv4) => {
return parseIPv4ToBinary(ipv4, 0, ipv4.length, () => throwInvalidIPv4Address(ipv4));
};
var convertIPv6ToBinary = (ipv6) => {
const length = ipv6.length;
const sections = [];
let hasZoneId = false;
let compressAt = -1;
let index = 0;
if (length === 0) {
throwInvalidIPv6Address(ipv6);
}
while (index < length) {
if (sections.length > 8) {
throwInvalidIPv6Address(ipv6);
}
let code = ipv6.charCodeAt(index);
if (code === CHAR_CODE_PERCENT) {
if (index + 1 === length) {
throwInvalidIPv6Address(ipv6);
}
hasZoneId = true;
break;
}
if (code === CHAR_CODE_COLON) {
if (index + 1 < length && ipv6.charCodeAt(index + 1) === CHAR_CODE_COLON) {
if (compressAt !== -1) {
throwInvalidIPv6Address(ipv6);
}
compressAt = sections.length;
index += 2;
continue;
}
throwInvalidIPv6Address(ipv6);
}
let value = 0;
let digits = 0;
const sectionStart = index;
while (index < length) {
code = ipv6.charCodeAt(index);
const hex = parseIPv6HexCode(code);
if (hex === -1) {
break;
}
if (digits === 4) {
throwInvalidIPv6Address(ipv6);
}
value = value << 4 | hex;
digits++;
index++;
}
if (index < length && ipv6.charCodeAt(index) === CHAR_CODE_DOT) {
let ipv4End = length;
for (let i = index; i < length; i++) {
if (ipv6.charCodeAt(i) === CHAR_CODE_PERCENT) {
if (i + 1 === length) {
throwInvalidIPv6Address(ipv6);
}
hasZoneId = true;
ipv4End = i;
break;
}
}
const ipv4 = parseIPv4ToBinary(
ipv6,
sectionStart,
ipv4End,
() => throwInvalidIPv6Address(ipv6)
);
sections.push(Number(ipv4 >> 16n & 0xffffn), Number(ipv4 & 0xffffn));
index = length;
break;
}
if (digits === 0) {
throwInvalidIPv6Address(ipv6);
}
sections.push(value);
if (index === length) {
break;
}
code = ipv6.charCodeAt(index);
if (code === CHAR_CODE_PERCENT) {
if (index + 1 === length) {
throwInvalidIPv6Address(ipv6);
}
hasZoneId = true;
break;
}
if (code !== CHAR_CODE_COLON) {
throwInvalidIPv6Address(ipv6);
}
if (index + 1 < length && ipv6.charCodeAt(index + 1) === CHAR_CODE_COLON) {
if (compressAt !== -1) {
throwInvalidIPv6Address(ipv6);
}
compressAt = sections.length;
index += 2;
continue;
}
index++;
if (index === length) {
throwInvalidIPv6Address(ipv6);
}
}
if (compressAt === -1 ? sections.length !== 8 : sections.length >= 8) {
throwInvalidIPv6Address(ipv6);
}
let result = 0n;
const zeros = compressAt === -1 ? 0 : 8 - sections.length;
const firstSectionEnd = compressAt === -1 ? sections.length : compressAt;
for (let i = 0; i < firstSectionEnd; i++) {
result <<= 16n;
result += BigInt(sections[i]);
}
for (let i = 0; i < zeros; i++) {
result <<= 16n;
}
for (let i = firstSectionEnd; i < sections.length; i++) {
result <<= 16n;
result += BigInt(sections[i]);
}
if (hasZoneId && !isIPv6LinkLocal(result)) {
throwInvalidIPv6Address(ipv6);
}
return result;
};
var convertIPv4BinaryToString = (ipV4) => {
const sections = [];
for (let i = 0; i < 4; i++) {
sections.push(ipV4 >> BigInt(8 * (3 - i)) & 0xffn);
}
return sections.join(".");
};
var isIPv4MappedIPv6 = (ipv6binary) => ipv6binary >> 32n === 0xffffn;
var convertIPv4MappedIPv6ToIPv4 = (ipv6binary) => ipv6binary & 0xffffffffn;
var convertIPv6BinaryToString = (ipV6) => {
if (ipV6 === 0n) {
return "::";
}
if (isIPv4MappedIPv6(ipV6)) {
return `::ffff:${convertIPv4BinaryToString(convertIPv4MappedIPv6ToIPv4(ipV6))}`;
}
const sections = [];
for (let i = 0; i < 8; i++) {
sections.push((ipV6 >> BigInt(16 * (7 - i)) & 0xffffn).toString(16));
}
let currentZeroStart = -1;
let maxZeroStart = -1;
let maxZeroEnd = -1;
for (let i = 0; i < 8; i++) {
if (sections[i] === "0") {
if (currentZeroStart === -1) {
currentZeroStart = i;
}
} else {
if (currentZeroStart > -1) {
if (i - currentZeroStart > maxZeroEnd - maxZeroStart) {
maxZeroStart = currentZeroStart;
maxZeroEnd = i;
}
currentZeroStart = -1;
}
}
}
if (currentZeroStart > -1) {
if (8 - currentZeroStart > maxZeroEnd - maxZeroStart) {
maxZeroStart = currentZeroStart;
maxZeroEnd = 8;
}
}
if (maxZeroStart !== -1 && maxZeroEnd - maxZeroStart > 1) {
sections.splice(maxZeroStart, maxZeroEnd - maxZeroStart, ":");
}
return sections.join(":").replace(/:{2,}/g, "::");
};
export {
INVALID_IP_ADDRESS_ERROR_CODE,
convertIPv4BinaryToString,
convertIPv4MappedIPv6ToIPv4,
convertIPv4ToBinary,
convertIPv6BinaryToString,
convertIPv6ToBinary,
distinctRemoteAddr,
expandIPv6,
isIPv4MappedIPv6
};
|