diff options
author | Amin Bandali <bandali@gnu.org> | 2020-04-08 21:52:58 -0400 |
---|---|---|
committer | Amin Bandali <bandali@gnu.org> | 2020-04-08 21:52:58 -0400 |
commit | 61dd7225c7b6a2bb9346c76926b5e96264f831b8 (patch) | |
tree | 871f406fd2e3dfbfde8645615426e1c4ee15db23 /data/extensions/https-everywhere@eff.org/background-scripts/ip_utils.js | |
parent | f6e3adb6b2344ee2c7bb453a305fd2d6fb4c194c (diff) |
Update HTTPS Everywhere to 2020.3.16.
Diffstat (limited to 'data/extensions/https-everywhere@eff.org/background-scripts/ip_utils.js')
-rw-r--r-- | data/extensions/https-everywhere@eff.org/background-scripts/ip_utils.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/data/extensions/https-everywhere@eff.org/background-scripts/ip_utils.js b/data/extensions/https-everywhere@eff.org/background-scripts/ip_utils.js new file mode 100644 index 0000000..be7c1c8 --- /dev/null +++ b/data/extensions/https-everywhere@eff.org/background-scripts/ip_utils.js @@ -0,0 +1,53 @@ +'use strict'; + +(function (exports) { + +const parseIp = ip => { + if (!/^[0-9.]+$/.test(ip)) { + return -1; + } + + 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) | octet; + } + + return ipN >>> 0; +}; + +const isIpInRange = (ip, [rangeIp, mask]) => (ip & mask) >>> 0 === rangeIp; + +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], +]; + +const isLocalIp = ip => localRanges.some(range => isIpInRange(ip, range)); + +Object.assign(exports, { + parseIp, + isIpInRange, + isLocalIp +}); + +})(typeof exports !== 'undefined' ? exports : require.scopes.ip_utils = {}); |