The best way to control Claude Code, Codex and more from any computer or phone

main
ts 81 lines 3.12 KB
Raw
1 /**
2 * Canonicalizes an IPv4 or IPv6 CIDR to its network address.
3 * Invalid input is returned unchanged; this function is not a validator.
4 */
5 export const canonicalCidr = (cidr: string | undefined): string | undefined => {
6 if (cidr === undefined) return undefined;
7 if (/\s/.test(cidr)) return cidr;
8 const [address, prefixText, extra] = cidr.split("/");
9 if (!address || prefixText === undefined || extra !== undefined) return cidr;
10 if (!/^\d+$/.test(prefixText)) return cidr;
11
12 const ipv6 = address.includes(":");
13 const width = ipv6 ? 128 : 32;
14 const prefix = Number(prefixText);
15 if (!Number.isInteger(prefix) || prefix > width) return cidr;
16 const value = ipv6 ? parseIpv6(address) : parseIpv4(address);
17 if (value === undefined) return cidr;
18
19 const hostBits = BigInt(width - prefix);
20 const network = (value >> hostBits) << hostBits;
21 return `${ipv6 ? formatIpv6(network) : formatIpv4(network)}/${prefix}`;
22 };
23
24 const parseIpv4 = (address: string): bigint | undefined => {
25 const octets = address.split(".");
26 if (
27 octets.length !== 4 ||
28 octets.some((octet) => !/^(0|[1-9]\d{0,2})$/.test(octet) || Number(octet) > 255)
29 )
30 return undefined;
31 return octets.reduce((value, octet) => (value << 8n) | BigInt(octet), 0n);
32 };
33
34 const parseIpv6 = (address: string): bigint | undefined => {
35 if (address.includes(".")) {
36 const lastColon = address.lastIndexOf(":");
37 const ipv4 = parseIpv4(address.slice(lastColon + 1));
38 if (ipv4 === undefined) return undefined;
39 address = `${address.slice(0, lastColon + 1)}${(ipv4 >> 16n).toString(16)}:${(ipv4 & 0xffffn).toString(16)}`;
40 }
41
42 const halves = address.split("::");
43 if (halves.length > 2) return undefined;
44 const left = halves[0] ? halves[0].split(":") : [];
45 const right = halves[1] ? halves[1].split(":") : [];
46 const count = left.length + right.length;
47 if (halves.length === 1 ? count !== 8 : count >= 8) return undefined;
48 if ([...left, ...right].some((word) => !/^[0-9a-f]{1,4}$/i.test(word))) {
49 return undefined;
50 }
51
52 // A double colon must replace at least one 16-bit word.
53 const words =
54 halves.length === 1 ? left : [...left, ...Array<string>(8 - count).fill("0"), ...right];
55 return words.reduce((value, word) => (value << 16n) | BigInt(`0x${word}`), 0n);
56 };
57
58 const formatIpv4 = (address: bigint): string =>
59 [24n, 16n, 8n, 0n].map((shift) => Number((address >> shift) & 255n)).join(".");
60
61 const formatIpv6 = (address: bigint): string => {
62 const words = Array.from({ length: 8 }, (_, index) =>
63 ((address >> BigInt((7 - index) * 16)) & 0xffffn).toString(16),
64 );
65 let longestStart = -1;
66 let longestLength = 1;
67 for (let index = 0; index < words.length; index++) {
68 if (words[index] !== "0") continue;
69 let end = index + 1;
70 while (end < words.length && words[end] === "0") end++;
71 // Compress the first longest run; never compress a single zero word.
72 if (end - index > longestLength) {
73 longestStart = index;
74 longestLength = end - index;
75 }
76 index = end - 1;
77 }
78 return longestStart < 0
79 ? words.join(":")
80 : `${words.slice(0, longestStart).join(":")}::${words.slice(longestStart + longestLength).join(":")}`;
81 };