File size: 4,112 Bytes
f316cce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CIDR = void 0;
exports.parseIp = parseIp;
exports.parseCIDR = parseCIDR;
exports.isIPInNonPublicRange = isIPInNonPublicRange;
const net_1 = require("net");
function parseIp(ip) {
    if ((0, net_1.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 ((0, net_1.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');
}
function parseCIDR(cidr) {
    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];
}
class CIDR {
    constructor(cidr) {
        this.text = cidr;
        [this.buff, this.mask] = parseCIDR(cidr);
    }
    toString() {
        return this.text;
    }
    get family() {
        return this.buff.byteLength === 4 ? 4 : 6;
    }
    test(ip) {
        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;
    }
}
exports.CIDR = CIDR;
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));
function isIPInNonPublicRange(ip) {
    const parsed = parseIp(ip);
    for (const cidr of nonPublicCIDRs) {
        if (cidr.test(parsed)) {
            return true;
        }
    }
    return false;
}
//# sourceMappingURL=ip.js.map