File size: 4,013 Bytes
45d1682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b58a2db
45d1682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b58a2db
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
import { isIPv4, isIPv6 } from 'net';

export function parseIp(ip: string): Buffer {
    if (isIPv4(ip)) {
        const [a, b, c, d] = ip.split('.').map(Number);

        const buf = Buffer.alloc(4);
        buf.writeUInt8(a, 0);
        buf.writeUInt8(b, 1);
        buf.writeUInt8(c, 2);
        buf.writeUInt8(d, 3);

        return buf;
    }

    if (isIPv6(ip)) {
        if (ip.includes('.')) {
            const parts = ip.split(':');
            const ipv4Part = parts.pop();
            if (!ipv4Part) throw new Error('Invalid IPv6 address');
            const ipv4Bytes = parseIp(ipv4Part);
            parts.push('0');
            const ipv6Bytes = parseIp(parts.join(':'));
            ipv6Bytes.writeUInt32BE(ipv4Bytes.readUInt32BE(0), 12);

            return ipv6Bytes;
        }

        const buf = Buffer.alloc(16);

        // Expand :: notation
        let expanded = ip;
        if (ip.includes('::')) {
            const sides = ip.split('::');
            const left = sides[0] ? sides[0].split(':') : [];
            const right = sides[1] ? sides[1].split(':') : [];
            const middle = Array(8 - left.length - right.length).fill('0');
            expanded = [...left, ...middle, ...right].join(':');
        }

        // Convert to buffer
        const parts = expanded.split(':');
        let offset = 0;
        for (const part of parts) {
            buf.writeUInt16BE(parseInt(part, 16), offset);
            offset += 2;
        }

        return buf;
    }

    throw new Error('Invalid IP address');
}


export function parseCIDR(cidr: string): [Buffer, Buffer] {
    const [ip, prefixTxt] = cidr.split('/');
    const buf = parseIp(ip);
    const maskBuf = Buffer.alloc(buf.byteLength, 0xff);
    const prefixBits = parseInt(prefixTxt);

    let offsetBits = 0;
    while (offsetBits < (buf.byteLength * 8)) {
        if (offsetBits <= (prefixBits - 8)) {
            offsetBits += 8;
            continue;
        }
        const bitsRemain = prefixBits - offsetBits;
        const byteOffset = Math.floor(offsetBits / 8);

        if (bitsRemain > 0) {
            const theByte = buf[byteOffset];
            const mask = 0xff << (8 - bitsRemain);
            maskBuf[byteOffset] = mask;
            buf[byteOffset] = theByte & mask;

            offsetBits += 8;
            continue;
        };
        buf[byteOffset] = 0;
        maskBuf[byteOffset] = 0;

        offsetBits += 8;
    }

    return [buf, maskBuf];
}

export class CIDR {
    buff: Buffer;
    mask: Buffer;
    text: string;
    constructor(cidr: string) {
        this.text = cidr;
        [this.buff, this.mask] = parseCIDR(cidr);
    }

    toString() {
        return this.text;
    }

    get family() {
        return this.buff.byteLength === 4 ? 4 : 6;
    }

    test(ip: string | Buffer): boolean {
        const parsedIp = typeof ip === 'string' ? parseIp(ip) : ip;

        if (parsedIp.byteLength !== this.buff.byteLength) {
            return false;
        }

        for (const i of Array(this.buff.byteLength).keys()) {
            const t = parsedIp[i];
            const m = this.mask[i];

            if (m === 0) {
                return true;
            }

            const r = this.buff[i];
            if ((t & m) !== r) {
                return false;
            }
        }

        return true;
    }
}

const nonPublicNetworks4 = [
    '10.0.0.0/8',
    '172.16.0.0/12',
    '192.168.0.0/16',

    '127.0.0.0/8',
    '255.255.255.255/32',
    '169.254.0.0/16',
    '224.0.0.0/4',

    '100.64.0.0/10',
    '0.0.0.0/32',
];


const nonPublicNetworks6 = [
    'fc00::/7',
    'fe80::/10',
    'ff00::/8',

    '::127.0.0.0/104',
    '::/128',
];

const nonPublicCIDRs = [...nonPublicNetworks4, ...nonPublicNetworks6].map(cidr => new CIDR(cidr));

export function isIPInNonPublicRange(ip: string) {
    const parsed = parseIp(ip);

    for (const cidr of nonPublicCIDRs) {
        if (cidr.test(parsed)) {
            return true;
        }
    }

    return false;
}