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
|
'use strict';
(function (exports) {
/**
* Parse and convert literal IP address into numerical IP address.
* @param {string} ip
* @returns {number}
*/
const parseIp = ip => {
if (!/^[0-9.]+$/.test(ip)) {
return -1;
}
/** @type {string[]} */
const octets = ip.split('.');
if (octets.length !== 4) {
return -1;
}
let ipN = 0;
for (const octet of octets) {
if (octet === '') {
return -1;
}
const octetN = parseInt(octet);
if (octetN < 0 || octetN > 255) {
return -1;
}
ipN = (ipN << 8) | octetN;
}
return ipN >>> 0;
};
/**
* Check if the numeric IP address is within a certain range.
* @param {number} ip
* @param {number[]} range
* @returns {boolean}
*/
const isIpInRange = (ip, [rangeIp, mask]) => (ip & mask) >>> 0 === rangeIp;
// A list of local IP address ranges
const localRanges = [
[/* 0.0.0.0 */ 0x00000000, /* 255.255.255.255 */ 0xffffffff],
[/* 127.0.0.0 */ 0x7f000000, /* 255.0.0.0 */ 0xff000000],
[/* 10.0.0.0 */ 0x0a000000, /* 255.0.0.0 */ 0xff000000],
[/* 172.16.0.0 */ 0xac100000, /* 255.240.0.0 */ 0xfff00000],
[/* 192.168.0.0 */ 0xc0a80000, /* 255.255.0.0 */ 0xffff0000],
];
/**
* Check if the numeric IP address is inside the local IP address ranges.
* @param {number} ip
* @returns {boolean}
*/
const isLocalIp = ip => localRanges.some(range => isIpInRange(ip, range));
Object.assign(exports, {
parseIp,
isIpInRange,
isLocalIp
});
})(typeof exports !== 'undefined' ? exports : require.scopes.ip_utils = {});
|