Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

haraka-net-utils

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

haraka-net-utils - npm Package Compare versions

Comparing version 1.0.0 to 1.0.2

.codeclimate.yml

381

index.js

@@ -15,90 +15,90 @@ 'use strict';

exports.long_to_ip = function (n) {
var d = n%256;
for (var i=3; i>0; i--) {
n = Math.floor(n/256);
d = n%256 + '.' + d;
}
return d;
var d = n%256;
for (var i=3; i>0; i--) {
n = Math.floor(n/256);
d = n%256 + '.' + d;
}
return d;
};
exports.dec_to_hex = function (d) {
return d.toString(16);
return d.toString(16);
};
exports.hex_to_dec = function (h) {
return parseInt(h, 16);
return parseInt(h, 16);
};
exports.ip_to_long = function (ip) {
if (!net.isIPv4(ip)) { return false; }
if (!net.isIPv4(ip)) { return false; }
var d = ip.split('.');
return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]);
var d = ip.split('.');
return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]);
};
exports.octets_in_string = function (str, oct1, oct2) {
var oct1_idx;
var oct2_idx;
var oct1_idx;
var oct2_idx;
// test the largest of the two octets first
if (oct2.length >= oct1.length) {
oct2_idx = str.lastIndexOf(oct2);
if (oct2_idx === -1) { return false; }
if (oct2.length >= oct1.length) {
oct2_idx = str.lastIndexOf(oct2);
if (oct2_idx === -1) { return false; }
oct1_idx = (str.substring(0, oct2_idx) +
oct1_idx = (str.substring(0, oct2_idx) +
str.substring(oct2_idx + oct2.length)).lastIndexOf(oct1);
if (oct1_idx === -1) { return false; }
if (oct1_idx === -1) { return false; }
return true; // both were found
}
return true; // both were found
}
oct1_idx = str.indexOf(oct1);
if (oct1_idx === -1) { return false; }
oct1_idx = str.indexOf(oct1);
if (oct1_idx === -1) { return false; }
oct2_idx = (str.substring(0, oct1_idx) +
oct2_idx = (str.substring(0, oct1_idx) +
str.substring(oct1_idx + oct1.length)).lastIndexOf(oct2);
if (oct2_idx === -1) { return false; }
if (oct2_idx === -1) { return false; }
return true;
return true;
};
exports.is_ip_in_str = function (ip, str) {
if (!str) { return false; }
if (!ip) { return false; }
if (!net.isIPv4(ip)) {
return false; // IPv4 only, for now
}
if (!str) { return false; }
if (!ip) { return false; }
if (!net.isIPv4(ip)) {
return false; // IPv4 only, for now
}
var host_part = (tlds.split_hostname(str,1))[0].toString();
var octets = ip.split('.');
var host_part = (tlds.split_hostname(str,1))[0].toString();
var octets = ip.split('.');
// See if the 3rd and 4th octets appear in the string
if (this.octets_in_string(host_part, octets[2], octets[3])) {
return true;
}
if (this.octets_in_string(host_part, octets[2], octets[3])) {
return true;
}
// then the 1st and 2nd octets
if (this.octets_in_string(host_part, octets[0], octets[1])) {
return true;
}
if (this.octets_in_string(host_part, octets[0], octets[1])) {
return true;
}
// Whole IP in hex
var host_part_copy = host_part;
var ip_hex = this.dec_to_hex(this.ip_to_long(ip));
for (var i=0; i<4; i++) {
var part = host_part_copy.indexOf(ip_hex.substring(i*2, (i*2)+2));
if (part === -1) break;
if (i === 3) return true;
host_part_copy = host_part_copy.substring(0, part) +
var host_part_copy = host_part;
var ip_hex = this.dec_to_hex(this.ip_to_long(ip));
for (var i=0; i<4; i++) {
var part = host_part_copy.indexOf(ip_hex.substring(i*2, (i*2)+2));
if (part === -1) break;
if (i === 3) return true;
host_part_copy = host_part_copy.substring(0, part) +
host_part_copy.substring(part+2);
}
return false;
}
return false;
};
var re_ipv4 = {
loopback: /^127\./,
link_local: /^169\.254\./,
loopback: /^127\./,
link_local: /^169\.254\./,
private10: /^10\./, // 10/8
private192: /^192\.168\./, // 192.168/16
private10: /^10\./, // 10/8
private192: /^192\.168\./, // 192.168/16
// 172.16/16 .. 172.31/16
private172: /^172\.(1[6-9]|2[0-9]|3[01])\./, // 172.16/12
private172: /^172\.(1[6-9]|2[0-9]|3[01])\./, // 172.16/12
};

@@ -109,7 +109,7 @@

// RFC 1918, reserved as "private" IP space
if (re_ipv4.private10.test(ip)) return true;
if (re_ipv4.private192.test(ip)) return true;
if (re_ipv4.private172.test(ip)) return true;
if (re_ipv4.private10.test(ip)) return true;
if (re_ipv4.private192.test(ip)) return true;
if (re_ipv4.private172.test(ip)) return true;
return false;
return false;
};

@@ -119,46 +119,46 @@

// 127/8 (loopback) # RFC 1122
if (re_ipv4.loopback.test(ip)) return true;
if (re_ipv4.loopback.test(ip)) return true;
// link local: 169.254/16 RFC 3927
if (re_ipv4.link_local.test(ip)) return true;
if (re_ipv4.link_local.test(ip)) return true;
return false;
return false;
};
var re_ipv6 = {
loopback: /^(0{1,4}:){7}0{0,3}1$/,
link_local: /^fe80::/i,
unique_local: /^f(c|d)[a-f0-9]{2}:/i,
loopback: /^(0{1,4}:){7}0{0,3}1$/,
link_local: /^fe80::/i,
unique_local: /^f(c|d)[a-f0-9]{2}:/i,
};
exports.is_local_ipv6 = function (ip) {
if (ip === '::1') return true; // RFC 4291
if (ip === '::1') return true; // RFC 4291
// 2 more IPv6 notations for ::1
// 0:0:0:0:0:0:0:1 or 0000:0000:0000:0000:0000:0000:0000:0001
if (re_ipv6.loopback.test(ip)) return true;
if (re_ipv6.loopback.test(ip)) return true;
// link local: fe80::/10, RFC 4862
if (re_ipv6.link_local.test(ip)) return true;
if (re_ipv6.link_local.test(ip)) return true;
// unique local (fc00::/7) -> fc00: - fd00:
if (re_ipv6.unique_local.test(ip)) return true;
if (re_ipv6.unique_local.test(ip)) return true;
return false;
return false;
};
exports.is_private_ip = function (ip) {
if (net.isIPv4(ip)) {
if (this.is_private_ipv4(ip)) return true;
if (this.is_local_ipv4(ip)) return true;
return false;
}
if (net.isIPv4(ip)) {
if (this.is_private_ipv4(ip)) return true;
if (this.is_local_ipv4(ip)) return true;
return false;
}
if (net.isIPv6(ip)) {
if (this.is_local_ipv6(ip)) return true;
return false;
}
if (net.isIPv6(ip)) {
if (this.is_local_ipv6(ip)) return true;
return false;
}
console.error('invalid IP address: ' + ip);
return false;
console.error('invalid IP address: ' + ip);
return false;
};

@@ -170,67 +170,67 @@

exports.is_ip_literal = function (host) {
return exports.get_ipany_re('^\\[(IPv6:)?','\\]$','').test(host) ? true : false;
return exports.get_ipany_re('^\\[(IPv6:)?','\\]$','').test(host) ? true : false;
};
exports.is_ipv4_literal = function (host) {
return /^\[(\d{1,3}\.){3}\d{1,3}\]$/.test(host) ? true : false;
return /^\[(\d{1,3}\.){3}\d{1,3}\]$/.test(host) ? true : false;
};
exports.same_ipv4_network = function (ip, ipList) {
if (!ipList || !ipList.length) {
console.error('same_ipv4_network, no ip list!');
return false;
}
if (!net.isIPv4(ip)) {
console.error('same_ipv4_network, IP is not IPv4!');
return false;
}
if (!ipList || !ipList.length) {
console.error('same_ipv4_network, no ip list!');
return false;
}
if (!net.isIPv4(ip)) {
console.error('same_ipv4_network, IP is not IPv4!');
return false;
}
var first3 = ip.split('.').slice(0,3).join('.');
var first3 = ip.split('.').slice(0,3).join('.');
for (var i=0; i < ipList.length; i++) {
if (!net.isIPv4(ipList[i])) {
console.error('same_ipv4_network, IP in list is not IPv4!');
continue;
}
if (first3 === ipList[i].split('.').slice(0,3).join('.'))
return true;
for (var i=0; i < ipList.length; i++) {
if (!net.isIPv4(ipList[i])) {
console.error('same_ipv4_network, IP in list is not IPv4!');
continue;
}
return false;
if (first3 === ipList[i].split('.').slice(0,3).join('.'))
return true;
}
return false;
};
exports.get_public_ip = function (cb) {
var nu = this;
if (nu.public_ip !== undefined) {
return cb(null, nu.public_ip); // cache
}
var nu = this;
if (nu.public_ip !== undefined) {
return cb(null, nu.public_ip); // cache
}
// manual config override, for the cases where we can't figure it out
var smtpIni = config.get('smtp.ini').main;
if (smtpIni.public_ip) {
nu.public_ip = smtpIni.public_ip;
return cb(null, nu.public_ip);
}
var smtpIni = config.get('smtp.ini').main;
if (smtpIni.public_ip) {
nu.public_ip = smtpIni.public_ip;
return cb(null, nu.public_ip);
}
// Initialise cache value to null to prevent running
// should we hit a timeout or the module isn't installed.
nu.public_ip = null;
nu.public_ip = null;
try {
nu.stun = require('vs-stun');
}
try {
nu.stun = require('vs-stun');
}
catch (e) {
e.install = 'Please install stun: "npm install -g vs-stun"';
console.error(e.msg + "\n" + e.install);
return cb(e);
e.install = 'Please install stun: "npm install -g vs-stun"';
console.error(e.msg + "\n" + e.install);
return cb(e);
}
var timeout = 10;
var timer;
var timeout = 10;
var timer;
var st_cb = function (error, socket) {
if (timer) clearTimeout(timer);
if (error) {
return cb(error);
}
socket.close();
var st_cb = function (error, socket) {
if (timer) clearTimeout(timer);
if (error) {
return cb(error);
}
socket.close();
/* sample socket.stun response

@@ -243,15 +243,15 @@ *

*/
if (!socket.stun.public) {
return cb(new Error('invalid STUN result'));
}
nu.public_ip = socket.stun.public.host;
return cb(null, socket.stun.public.host);
};
if (!socket.stun.public) {
return cb(new Error('invalid STUN result'));
}
nu.public_ip = socket.stun.public.host;
return cb(null, socket.stun.public.host);
};
// Connect to STUN Server
nu.stun.connect({ host: get_stun_server(), port: 19302 }, st_cb);
nu.stun.connect({ host: get_stun_server(), port: 19302 }, st_cb);
timer = setTimeout(function () {
return cb(new Error('STUN timeout'));
}, (timeout || 10) * 1000);
timer = setTimeout(function () {
return cb(new Error('STUN timeout'));
}, (timeout || 10) * 1000);
};

@@ -261,10 +261,10 @@

// STUN servers by Google
var servers = [
'stun.l.google.com',
'stun1.l.google.com',
'stun2.l.google.com',
'stun3.l.google.com',
'stun4.l.google.com',
];
return servers[Math.floor(Math.random()*servers.length)];
var servers = [
'stun.l.google.com',
'stun1.l.google.com',
'stun2.l.google.com',
'stun3.l.google.com',
'stun4.l.google.com',
];
return servers[Math.floor(Math.random()*servers.length)];
}

@@ -274,6 +274,6 @@

/* jshint maxlen: false */
if (prefix === undefined) prefix = '';
if (suffix === undefined) suffix = '';
if (modifier === undefined) modifier = 'mg';
return new RegExp(
if (prefix === undefined) prefix = '';
if (suffix === undefined) suffix = '';
if (modifier === undefined) modifier = 'mg';
return new RegExp(
prefix +

@@ -289,32 +289,32 @@ '(' + // capture group

exports.get_ips_by_host = function (hostname, done) {
var ips = [];
var errors = [];
var ips = [];
var errors = [];
async.parallel(
[
function (iter_done) {
dns.resolve4(hostname, function resolve_cb (err, res) {
if (err) {
errors.push(err);
return iter_done();
}
for (var i=0; i<res.length; i++) {
ips.push(res[i]);
}
iter_done(null, true);
});
},
function (iter_done) {
dns.resolve6(hostname, function resolve_cb (err, res) {
if (err) {
errors.push(err);
return iter_done();
}
for (var j=0; j<res.length; j++) {
ips.push(res[j]);
}
iter_done(null, true);
});
},
],
async.parallel(
[
function (iter_done) {
dns.resolve4(hostname, function resolve_cb (err, res) {
if (err) {
errors.push(err);
return iter_done();
}
for (var i=0; i<res.length; i++) {
ips.push(res[i]);
}
iter_done(null, true);
});
},
function (iter_done) {
dns.resolve6(hostname, function resolve_cb (err, res) {
if (err) {
errors.push(err);
return iter_done();
}
for (var j=0; j<res.length; j++) {
ips.push(res[j]);
}
iter_done(null, true);
});
},
],
function (err, async_list) {

@@ -324,3 +324,3 @@ // if multiple IPs are included in the iterations, then the async

// we want. Return the merged ips array.
done(errors, ips);
done(errors, ips);
}

@@ -331,7 +331,7 @@ );

exports.ipv6_reverse = function(ipv6){
var ipv6 = ipaddr.parse(ipv6);
return ipv6.toNormalizedString()
ipv6 = ipaddr.parse(ipv6);
return ipv6.toNormalizedString()
.split(':')
.map(function (n) {
return sprintf('%04x', parseInt(n, 16));
return sprintf('%04x', parseInt(n, 16));
})

@@ -345,5 +345,28 @@ .join('')

exports.ipv6_bogus = function(ipv6){
var ipCheck = ipaddr.parse(ipv6);
if (ipCheck.range() !== 'unicast') { return true; }
return false;
var ipCheck = ipaddr.parse(ipv6);
if (ipCheck.range() !== 'unicast') { return true; }
return false;
};
exports.ip_in_list = function (list, ip) {
if (!net.isIP(ip)) return (ip in list); // domain
for (var string in list) {
if (string === ip) return true; // exact match
var cidr = string.split('/');
var c_net = cidr[0];
if (!net.isIP(c_net)) continue; // bad config entry
if (net.isIPv4(ip) && net.isIPv6(c_net)) continue;
if (net.isIPv6(ip) && net.isIPv4(c_net)) continue;
var c_mask = parseInt(cidr[1], 10) || (net.isIPv6(c_net) ? 128 : 32);
if (ipaddr.parse(ip).match(ipaddr.parse(c_net), c_mask)) {
return true;
}
}
return false;
}
{
"name": "haraka-net-utils",
"version": "1.0.0",
"version": "1.0.2",
"description": "haraka network utilities",

@@ -8,3 +8,3 @@ "main": "index.js",

"test": "./run_tests",
"lint": "eslint index",
"lint": "./node_modules/.bin/eslint *.js test",
"coverage": "./node_modules/.bin/istanbul cover ./run_tests"

@@ -29,4 +29,4 @@ },

"devDependencies": {
"eslint": "^3.7.1",
"nodeunit": "^0.10.2"
"eslint": "*",
"nodeunit": "*"
},

@@ -33,0 +33,0 @@ "dependencies": {

@@ -62,4 +62,10 @@ [![Build Status][ci-img]][ci-url]

### ip_in_list
// searches for 'ip' as a hash key in the list object
// ip can be a host, an IP, or an IPv4 or IPv6 range
net_utils.ip_in_list(object, ip);
net_utils.ip_in_list(tls.no_tls_hosts, '127.0.0.5');
[ci-img]: https://travis-ci.org/haraka/haraka-net-utils.svg

@@ -72,2 +78,2 @@ [ci-url]: https://travis-ci.org/haraka/haraka-net-utils

[npm-img]: https://nodei.co/npm/haraka-net-utils.png
[npm-url]: https://www.npmjs.com/package/haraka-net-utils
[npm-url]: https://www.npmjs.com/package/haraka-net-utils

@@ -8,291 +8,299 @@

function _check(test, ip, host, res) {
test.expect(1);
test.equals(net_utils.is_ip_in_str(ip, host), res);
test.done();
}
exports.long_to_ip = {
'185999660': function (test) {
test.expect(1);
test.equals(net_utils.is_ip_in_str(ip, host), res);
test.equals(net_utils.long_to_ip(185999660), '11.22.33.44');
test.done();
}
}
};
exports.static_rdns = {
'74.125.82.182': function (test) {
_check(test, '74.125.82.182', 'mail-we0-f182.google.com', false);
},
'74.125.82.53': function (test) {
_check(test, '74.125.82.53', 'mail-ww0-f53.google.com', false);
}
'74.125.82.182': function (test) {
_check(test, '74.125.82.182', 'mail-we0-f182.google.com', false);
},
'74.125.82.53': function (test) {
_check(test, '74.125.82.53', 'mail-ww0-f53.google.com', false);
}
};
exports.dynamic_rdns = {
'109.168.232.131': function (test) {
_check(test, '109.168.232.131', 'host-109-168-232-131.stv.ru', true);
},
'62.198.236.129': function (test) {
_check(test, '62.198.236.129', '0x3ec6ec81.inet.dsl.telianet.dk', true);
},
'123.58.178.17': function (test) {
_check(test, '123.58.178.17', 'm17-178.vip.126.com', true);
},
'100.42.67.92': function (test) {
_check(test, '100.42.67.92', '92-67-42-100-dedicated.multacom.com',
'109.168.232.131': function (test) {
_check(test, '109.168.232.131', 'host-109-168-232-131.stv.ru', true);
},
'62.198.236.129': function (test) {
_check(test, '62.198.236.129', '0x3ec6ec81.inet.dsl.telianet.dk', true);
},
'123.58.178.17': function (test) {
_check(test, '123.58.178.17', 'm17-178.vip.126.com', true);
},
'100.42.67.92': function (test) {
_check(test, '100.42.67.92', '92-67-42-100-dedicated.multacom.com',
true);
},
'101.0.57.5': function (test) {
_check(test, '101.0.57.5', 'static-bpipl-101.0.57-5.com', true);
}
},
'101.0.57.5': function (test) {
_check(test, '101.0.57.5', 'static-bpipl-101.0.57-5.com', true);
}
};
function _same_ipv4_network(test, addr, addrList, expected) {
test.expect(1);
test.equals(expected, net_utils.same_ipv4_network(addr, addrList));
test.done();
test.expect(1);
test.equals(expected, net_utils.same_ipv4_network(addr, addrList));
test.done();
}
exports.same_ipv4_network = {
'199.176.179.3 <-> [199.176.179.4]': function (test) {
_same_ipv4_network(test, '199.176.179.3', ['199.176.179.4'], true);
},
'199.176.179.3 <-> [199.177.179.4': function (test) {
_same_ipv4_network(test, '199.176.179.3', ['199.177.179.4'], false);
},
'199.176.179.3 <-> [199.176.179.4]': function (test) {
_same_ipv4_network(test, '199.176.179.3', ['199.176.179.4'], true);
},
'199.176.179.3 <-> [199.177.179.4': function (test) {
_same_ipv4_network(test, '199.176.179.3', ['199.177.179.4'], false);
},
'199.176.179 <-> [199.176.179.4] (missing octet)': function (test) {
_same_ipv4_network(test, '199.176.179', ['199.176.179.4'], false);
},
'199.176.179.3.5 <-> [199.176.179.4] (extra octet)': function (test) {
_same_ipv4_network(test, '199.176.179.3.5', ['199.176.179.4'], false);
},
'199.176.179 <-> [199.176.179.4] (missing octet)': function (test) {
_same_ipv4_network(test, '199.176.179', ['199.176.179.4'], false);
},
'199.176.179.3.5 <-> [199.176.179.4] (extra octet)': function (test) {
_same_ipv4_network(test, '199.176.179.3.5', ['199.176.179.4'], false);
},
};
exports.is_ipv4_literal = {
'3 ways ': function (test) {
test.expect(3);
test.equal(true, net_utils.is_ipv4_literal('[127.0.0.1]'));
test.equal(false, net_utils.is_ipv4_literal('127.0.0.1'));
test.equal(false, net_utils.is_ipv4_literal('test.host'));
test.done();
},
'3 ways ': function (test) {
test.expect(3);
test.equal(true, net_utils.is_ipv4_literal('[127.0.0.1]'));
test.equal(false, net_utils.is_ipv4_literal('127.0.0.1'));
test.equal(false, net_utils.is_ipv4_literal('test.host'));
test.done();
},
};
function _is_private_ip(test, ip, expected) {
test.expect(1);
test.equals(expected, net_utils.is_private_ip(ip));
test.done();
test.expect(1);
test.equals(expected, net_utils.is_private_ip(ip));
test.done();
}
exports.is_private_ip = {
'127.0.0.1': function (test) {
_is_private_ip(test, '127.0.0.1', true);
},
'10.255.31.23': function (test) {
_is_private_ip(test, '10.255.31.23', true);
},
'172.16.255.254': function (test) {
_is_private_ip(test, '172.16.255.254', true);
},
'192.168.123.123': function (test) {
_is_private_ip(test, '192.168.123.123', true);
},
'169.254.23.54 (APIPA)': function (test) {
_is_private_ip(test, '169.254.23.54', true);
},
'::1': function (test) {
_is_private_ip(test, '::1', true);
},
'0:0:0:0:0:0:0:1': function (test) {
_is_private_ip(test, '0:0:0:0:0:0:0:1', true);
},
'0000:0000:0000:0000:0000:0000:0000:0001': function (test) {
_is_private_ip(test, '0000:0000:0000:0000:0000:0000:0000:0001', true);
},
'123.123.123.123': function (test) {
_is_private_ip(test, '123.123.123.123', false);
},
'dead::beef': function (test) {
_is_private_ip(test, 'dead::beef', false);
},
'192.168.1 (missing octet)': function (test) {
_is_private_ip(test, '192.168.1', false);
},
'239.0.0.1 (multicast; not currently considered rfc1918)': function (test) {
_is_private_ip(test, '239.0.0.1', false);
},
'127.0.0.1': function (test) {
_is_private_ip(test, '127.0.0.1', true);
},
'10.255.31.23': function (test) {
_is_private_ip(test, '10.255.31.23', true);
},
'172.16.255.254': function (test) {
_is_private_ip(test, '172.16.255.254', true);
},
'192.168.123.123': function (test) {
_is_private_ip(test, '192.168.123.123', true);
},
'169.254.23.54 (APIPA)': function (test) {
_is_private_ip(test, '169.254.23.54', true);
},
'::1': function (test) {
_is_private_ip(test, '::1', true);
},
'0:0:0:0:0:0:0:1': function (test) {
_is_private_ip(test, '0:0:0:0:0:0:0:1', true);
},
'0000:0000:0000:0000:0000:0000:0000:0001': function (test) {
_is_private_ip(test, '0000:0000:0000:0000:0000:0000:0000:0001', true);
},
'123.123.123.123': function (test) {
_is_private_ip(test, '123.123.123.123', false);
},
'dead::beef': function (test) {
_is_private_ip(test, 'dead::beef', false);
},
'192.168.1 (missing octet)': function (test) {
_is_private_ip(test, '192.168.1', false);
},
'239.0.0.1 (multicast; not currently considered rfc1918)': function (test) {
_is_private_ip(test, '239.0.0.1', false);
},
};
exports.get_public_ip = {
setUp: function (callback) {
this.net_utils = require('../index');
callback();
},
'cached': function (test) {
test.expect(2);
this.net_utils.public_ip='1.1.1.1';
var cb = function (err, ip) {
test.equal(null, err);
test.equal('1.1.1.1', ip);
test.done();
};
this.net_utils.get_public_ip(cb);
},
'normal': function (test) {
this.net_utils.public_ip=undefined;
var cb = function (err, ip) {
setUp: function (callback) {
this.net_utils = require('../index');
callback();
},
'cached': function (test) {
test.expect(2);
this.net_utils.public_ip='1.1.1.1';
var cb = function (err, ip) {
test.equal(null, err);
test.equal('1.1.1.1', ip);
test.done();
};
this.net_utils.get_public_ip(cb);
},
'normal': function (test) {
this.net_utils.public_ip=undefined;
var cb = function (err, ip) {
// console.log('ip: ' + ip);
// console.log('err: ' + err);
if (has_stun()) {
if (err) {
console.log(err);
test.expect(0);
}
else {
console.log("stun success: " + ip);
test.expect(2);
test.equal(null, err);
test.ok(ip, ip);
}
}
else {
console.log("stun skipped");
test.expect(0);
}
test.done();
};
this.net_utils.get_public_ip(cb);
},
if (has_stun()) {
if (err) {
console.log(err);
test.expect(0);
}
else {
console.log("stun success: " + ip);
test.expect(2);
test.equal(null, err);
test.ok(ip, ip);
}
}
else {
console.log("stun skipped");
test.expect(0);
}
test.done();
};
this.net_utils.get_public_ip(cb);
},
};
function has_stun () {
try {
require('vs-stun');
}
try {
require('vs-stun');
}
catch (e) {
return false;
return false;
}
return true;
return true;
}
exports.octets_in_string = {
'c-24-18-98-14.hsd1.wa.comcast.net': function (test) {
var str = 'c-24-18-98-14.hsd1.wa.comcast.net';
test.expect(3);
test.equal(net_utils.octets_in_string(str, 98, 14), true );
test.equal(net_utils.octets_in_string(str, 24, 18), true );
test.equal(net_utils.octets_in_string(str, 2, 7), false );
test.done();
},
'149.213.210.203.in-addr.arpa': function (test) {
var str = '149.213.210.203.in-addr.arpa';
test.expect(3);
test.equal(net_utils.octets_in_string(str, 149, 213), true );
test.equal(net_utils.octets_in_string(str, 210, 20), true );
test.equal(net_utils.octets_in_string(str, 2, 7), false );
test.done();
}
'c-24-18-98-14.hsd1.wa.comcast.net': function (test) {
var str = 'c-24-18-98-14.hsd1.wa.comcast.net';
test.expect(3);
test.equal(net_utils.octets_in_string(str, 98, 14), true );
test.equal(net_utils.octets_in_string(str, 24, 18), true );
test.equal(net_utils.octets_in_string(str, 2, 7), false );
test.done();
},
'149.213.210.203.in-addr.arpa': function (test) {
var str = '149.213.210.203.in-addr.arpa';
test.expect(3);
test.equal(net_utils.octets_in_string(str, 149, 213), true );
test.equal(net_utils.octets_in_string(str, 210, 20), true );
test.equal(net_utils.octets_in_string(str, 2, 7), false );
test.done();
}
};
exports.is_ip_literal = {
'ipv4 is_ip_literal': function (test) {
test.expect(6);
test.equal(net_utils.is_ip_literal('[127.0.0.0]'), true);
test.equal(net_utils.is_ip_literal('[127.0.0.1]'), true);
test.equal(net_utils.is_ip_literal('[127.1.0.255]'), true);
test.equal(net_utils.is_ip_literal('127.0.0.0'), false);
test.equal(net_utils.is_ip_literal('127.0.0.1'), false);
test.equal(net_utils.is_ip_literal('127.1.0.255'), false);
'ipv4 is_ip_literal': function (test) {
test.expect(6);
test.equal(net_utils.is_ip_literal('[127.0.0.0]'), true);
test.equal(net_utils.is_ip_literal('[127.0.0.1]'), true);
test.equal(net_utils.is_ip_literal('[127.1.0.255]'), true);
test.equal(net_utils.is_ip_literal('127.0.0.0'), false);
test.equal(net_utils.is_ip_literal('127.0.0.1'), false);
test.equal(net_utils.is_ip_literal('127.1.0.255'), false);
test.done();
},
'ipv6 is_ip_literal': function (test) {
test.expect(7);
test.equal(net_utils.is_ip_literal('[::5555:6666:7777:8888]'), true);
test.equal(net_utils.is_ip_literal('[1111::4444:5555:6666:7777:8888]'), true);
test.equal(net_utils.is_ip_literal('[2001:0:1234::C1C0:ABCD:876]'), true);
test.equal(net_utils.is_ip_literal('[IPv6:2607:fb90:4c28:f9e9:4ca2:2658:db85:f1a]'), true);
test.equal(net_utils.is_ip_literal('::5555:6666:7777:8888'), false);
test.equal(net_utils.is_ip_literal('1111::4444:5555:6666:7777:8888'), false);
test.equal(net_utils.is_ip_literal('2001:0:1234::C1C0:ABCD:876'), false);
test.done();
},
'ipv6 is_ip_literal': function (test) {
test.expect(7);
test.equal(net_utils.is_ip_literal('[::5555:6666:7777:8888]'), true);
test.equal(net_utils.is_ip_literal('[1111::4444:5555:6666:7777:8888]'), true);
test.equal(net_utils.is_ip_literal('[2001:0:1234::C1C0:ABCD:876]'), true);
test.equal(net_utils.is_ip_literal('[IPv6:2607:fb90:4c28:f9e9:4ca2:2658:db85:f1a]'), true);
test.equal(net_utils.is_ip_literal('::5555:6666:7777:8888'), false);
test.equal(net_utils.is_ip_literal('1111::4444:5555:6666:7777:8888'), false);
test.equal(net_utils.is_ip_literal('2001:0:1234::C1C0:ABCD:876'), false);
test.done();
}
test.done();
}
};
exports.is_local_ipv4 = {
'127/8': function (test) {
test.expect(3);
test.equal(net_utils.is_local_ipv4('127.0.0.0'), true);
test.equal(net_utils.is_local_ipv4('127.0.0.1'), true);
test.equal(net_utils.is_local_ipv4('127.1.0.255'), true);
'127/8': function (test) {
test.expect(3);
test.equal(net_utils.is_local_ipv4('127.0.0.0'), true);
test.equal(net_utils.is_local_ipv4('127.0.0.1'), true);
test.equal(net_utils.is_local_ipv4('127.1.0.255'), true);
test.done();
},
'0/8': function (test) {
test.expect(4);
test.equal(net_utils.is_local_ipv4('0.0.0.1'), false);
test.equal(net_utils.is_local_ipv4('0.255.0.1'), false);
test.equal(net_utils.is_local_ipv4('1.255.0.1'), false);
test.equal(net_utils.is_local_ipv4('10.255.0.1'), false);
test.done();
},
test.done();
},
'0/8': function (test) {
test.expect(4);
test.equal(net_utils.is_local_ipv4('0.0.0.1'), false);
test.equal(net_utils.is_local_ipv4('0.255.0.1'), false);
test.equal(net_utils.is_local_ipv4('1.255.0.1'), false);
test.equal(net_utils.is_local_ipv4('10.255.0.1'), false);
test.done();
},
};
exports.is_private_ipv4 = {
'10/8': function (test) {
test.expect(4);
test.equal(net_utils.is_private_ipv4('10.0.0.0'), true);
test.equal(net_utils.is_private_ipv4('10.255.0.0'), true);
test.equal(net_utils.is_private_ipv4('9.255.0.0'), false);
test.equal(net_utils.is_private_ipv4('11.255.0.0'), false);
test.done();
},
'192.168/16': function (test) {
test.expect(3);
test.equal(net_utils.is_private_ipv4('192.168.0.0'), true);
test.equal(net_utils.is_private_ipv4('192.169.0.0'), false);
test.equal(net_utils.is_private_ipv4('192.167.0.0'), false);
test.done();
},
'172.16-31': function (test) {
test.expect(5);
test.equal(net_utils.is_private_ipv4('172.16.0.0'), true);
test.equal(net_utils.is_private_ipv4('172.20.0.0'), true);
test.equal(net_utils.is_private_ipv4('172.31.0.0'), true);
test.equal(net_utils.is_private_ipv4('172.15.0.0'), false);
test.equal(net_utils.is_private_ipv4('172.32.0.0'), false);
test.done();
},
'10/8': function (test) {
test.expect(4);
test.equal(net_utils.is_private_ipv4('10.0.0.0'), true);
test.equal(net_utils.is_private_ipv4('10.255.0.0'), true);
test.equal(net_utils.is_private_ipv4('9.255.0.0'), false);
test.equal(net_utils.is_private_ipv4('11.255.0.0'), false);
test.done();
},
'192.168/16': function (test) {
test.expect(3);
test.equal(net_utils.is_private_ipv4('192.168.0.0'), true);
test.equal(net_utils.is_private_ipv4('192.169.0.0'), false);
test.equal(net_utils.is_private_ipv4('192.167.0.0'), false);
test.done();
},
'172.16-31': function (test) {
test.expect(5);
test.equal(net_utils.is_private_ipv4('172.16.0.0'), true);
test.equal(net_utils.is_private_ipv4('172.20.0.0'), true);
test.equal(net_utils.is_private_ipv4('172.31.0.0'), true);
test.equal(net_utils.is_private_ipv4('172.15.0.0'), false);
test.equal(net_utils.is_private_ipv4('172.32.0.0'), false);
test.done();
},
};
exports.is_local_ipv6 = {
'::1': function (test) {
test.expect(3);
test.equal(net_utils.is_local_ipv6('::1'), true);
test.equal(net_utils.is_local_ipv6('0:0:0:0:0:0:0:1'), true);
test.equal(net_utils.is_local_ipv6(
'::1': function (test) {
test.expect(3);
test.equal(net_utils.is_local_ipv6('::1'), true);
test.equal(net_utils.is_local_ipv6('0:0:0:0:0:0:0:1'), true);
test.equal(net_utils.is_local_ipv6(
'0000:0000:0000:0000:0000:0000:0000:0001'), true);
test.done();
},
'fe80::/10': function (test) {
test.expect(4);
test.equal(net_utils.is_local_ipv6('fe80::'), true);
test.equal(net_utils.is_local_ipv6('fe80:'), false);
test.equal(net_utils.is_local_ipv6('fe8:'), false);
test.equal(net_utils.is_local_ipv6(':fe80:'), false);
test.done();
},
'fc80::/7': function (test) {
test.expect(10);
test.equal(net_utils.is_local_ipv6('fc00:'), true);
test.equal(net_utils.is_local_ipv6('fcff:'), true);
test.done();
},
'fe80::/10': function (test) {
test.expect(4);
test.equal(net_utils.is_local_ipv6('fe80::'), true);
test.equal(net_utils.is_local_ipv6('fe80:'), false);
test.equal(net_utils.is_local_ipv6('fe8:'), false);
test.equal(net_utils.is_local_ipv6(':fe80:'), false);
test.done();
},
'fc80::/7': function (test) {
test.expect(10);
test.equal(net_utils.is_local_ipv6('fc00:'), true);
test.equal(net_utils.is_local_ipv6('fcff:'), true);
// examples from https://en.wikipedia.org/wiki/Unique_local_address
test.equal(net_utils.is_local_ipv6('fde4:8dba:82e1::'), true);
test.equal(net_utils.is_local_ipv6('fde4:8dba:82e1:ffff::'), true);
test.equal(net_utils.is_local_ipv6('fde4:8dba:82e1::'), true);
test.equal(net_utils.is_local_ipv6('fde4:8dba:82e1:ffff::'), true);
test.equal(net_utils.is_local_ipv6('fd00:'), true);
test.equal(net_utils.is_local_ipv6('fdff:'), true);
test.equal(net_utils.is_local_ipv6('fd00:'), true);
test.equal(net_utils.is_local_ipv6('fdff:'), true);
test.equal(net_utils.is_local_ipv6('fb00:'), false);
test.equal(net_utils.is_local_ipv6('fe00:'), false);
test.equal(net_utils.is_local_ipv6('fb00:'), false);
test.equal(net_utils.is_local_ipv6('fe00:'), false);
test.equal(net_utils.is_local_ipv6('fe8:'), false);
test.equal(net_utils.is_local_ipv6(':fe80:'), false);
test.done();
},
test.equal(net_utils.is_local_ipv6('fe8:'), false);
test.equal(net_utils.is_local_ipv6(':fe80:'), false);
test.done();
},
};

@@ -794,111 +802,156 @@

/* jshint maxlen: false */
'IPv6, Prefix': function (test) {
'IPv6, Prefix': function (test) {
/* for x-*-ip headers */
test.expect(2);
test.expect(2);
// it must fail as of not valide
test.ok(!net.isIPv6('IPv6:2001:db8:85a3::8a2e:370:7334'));
test.ok(!net.isIPv6('IPv6:2001:db8:85a3::8a2e:370:7334'));
// must okay!
test.ok(net.isIPv6('2001:db8:85a3::8a2e:370:7334'));
test.done();
},
'IP fixtures check': function (test) {
test.expect(ip_fixtures.length);
for (var i in ip_fixtures) {
var match = net_utils.get_ipany_re('^','$').test(ip_fixtures[i][1]);
test.ok(net.isIPv6('2001:db8:85a3::8a2e:370:7334'));
test.done();
},
'IP fixtures check': function (test) {
test.expect(ip_fixtures.length);
for (var i in ip_fixtures) {
var match = net_utils.get_ipany_re('^','$').test(ip_fixtures[i][1]);
// console.log('IP:', "'"+ip_fixtures[i][1]+"'" , 'Expected:', ip_fixtures[i][0] , 'Match:' , match);
test.ok((match===ip_fixtures[i][0]), ip_fixtures[i][1] + ' - Expected: ' + ip_fixtures[i][0] + ' - Match: ' + match);
}
test.done();
},
'IPv4, bare': function (test) {
test.ok((match===ip_fixtures[i][0]), ip_fixtures[i][1] + ' - Expected: ' + ip_fixtures[i][0] + ' - Match: ' + match);
}
test.done();
},
'IPv4, bare': function (test) {
/* for x-*-ip headers */
test.expect(2);
var match = net_utils.get_ipany_re().exec('127.0.0.1');
test.equal(match[1], '127.0.0.1');
test.equal(match.length, 2);
test.done();
},
'IPv4, Received header, parens': function (test) {
test.expect(2);
var received_re = net_utils.get_ipany_re('^Received:.*?[\\[\\(]', '[\\]\\)]');
var match = received_re.exec('Received: from unknown (HELO mail.theartfarm.com) (127.0.0.30) by mail.theartfarm.com with SMTP; 5 Sep 2015 14:29:00 -0000');
test.equal(match[1], '127.0.0.30');
test.equal(match.length, 2);
test.done();
},
'IPv4, Received header, bracketed, expedia': function (test) {
test.expect(2);
var received_header = 'Received: from mta2.expediamail.com (mta2.expediamail.com [66.231.89.19]) by mail.theartfarm.com (Haraka/2.6.2-toaster) with ESMTPS id C669CF18-1C1C-484C-8A5B-A89088B048CB.1 envelope-from <bounce-857_HTML-202764435-1098240-260085-60@bounce.global.expediamail.com> (version=TLSv1/SSLv3 cipher=AES256-SHA verify=NO); Sat, 05 Sep 2015 07:28:57 -0700';
var received_re = net_utils.get_ipany_re('^Received:.*?[\\[\\(]', '[\\]\\)]');
var match = received_re.exec(received_header);
test.equal(match[1], '66.231.89.19');
test.equal(match.length, 2);
test.done();
},
'IPv4, Received header, bracketed, github': function (test) {
test.expect(2);
var received_re = net_utils.get_ipany_re('^Received:.*?[\\[\\(]', '[\\]\\)]');
var match = received_re.exec('Received: from github-smtp2a-ext-cp1-prd.iad.github.net (github-smtp2-ext5.iad.github.net [192.30.252.196])');
test.equal(match[1], '192.30.252.196');
test.equal(match.length, 2);
test.done();
},
'IPv6, Received header, bracketed': function (test) {
test.expect(2);
var received_header = 'Received: from ?IPv6:2601:184:c001:5cf7:a53f:baf7:aaf3:bce7? ([2601:184:c001:5cf7:a53f:baf7:aaf3:bce7])';
var received_re = net_utils.get_ipany_re('^Received:.*?[\\[\\(]', '[\\]\\)]');
var match = received_re.exec(received_header);
test.equal(match[1], '2601:184:c001:5cf7:a53f:baf7:aaf3:bce7');
test.equal(match.length, 2);
test.done();
},
'IPv6, Received header, bracketed, IPv6 prefix': function (test) {
test.expect(2);
var received_re = net_utils.get_ipany_re('^Received:.*?[\\[\\(](?:IPv6:)?', '[\\]\\)]');
var match = received_re.exec('Received: from hub.freebsd.org (hub.freebsd.org [IPv6:2001:1900:2254:206c::16:88])');
test.equal(match[1], '2001:1900:2254:206c::16:88');
test.equal(match.length, 2);
test.done();
},
'IPv6, folded Received header, bracketed, IPv6 prefix': function (test) {
test.expect(2);
test.expect(2);
var match = net_utils.get_ipany_re().exec('127.0.0.1');
test.equal(match[1], '127.0.0.1');
test.equal(match.length, 2);
test.done();
},
'IPv4, Received header, parens': function (test) {
test.expect(2);
var received_re = net_utils.get_ipany_re('^Received:.*?[\\[\\(]', '[\\]\\)]');
var match = received_re.exec('Received: from unknown (HELO mail.theartfarm.com) (127.0.0.30) by mail.theartfarm.com with SMTP; 5 Sep 2015 14:29:00 -0000');
test.equal(match[1], '127.0.0.30');
test.equal(match.length, 2);
test.done();
},
'IPv4, Received header, bracketed, expedia': function (test) {
test.expect(2);
var received_header = 'Received: from mta2.expediamail.com (mta2.expediamail.com [66.231.89.19]) by mail.theartfarm.com (Haraka/2.6.2-toaster) with ESMTPS id C669CF18-1C1C-484C-8A5B-A89088B048CB.1 envelope-from <bounce-857_HTML-202764435-1098240-260085-60@bounce.global.expediamail.com> (version=TLSv1/SSLv3 cipher=AES256-SHA verify=NO); Sat, 05 Sep 2015 07:28:57 -0700';
var received_re = net_utils.get_ipany_re('^Received:.*?[\\[\\(]', '[\\]\\)]');
var match = received_re.exec(received_header);
test.equal(match[1], '66.231.89.19');
test.equal(match.length, 2);
test.done();
},
'IPv4, Received header, bracketed, github': function (test) {
test.expect(2);
var received_re = net_utils.get_ipany_re('^Received:.*?[\\[\\(]', '[\\]\\)]');
var match = received_re.exec('Received: from github-smtp2a-ext-cp1-prd.iad.github.net (github-smtp2-ext5.iad.github.net [192.30.252.196])');
test.equal(match[1], '192.30.252.196');
test.equal(match.length, 2);
test.done();
},
'IPv6, Received header, bracketed': function (test) {
test.expect(2);
var received_header = 'Received: from ?IPv6:2601:184:c001:5cf7:a53f:baf7:aaf3:bce7? ([2601:184:c001:5cf7:a53f:baf7:aaf3:bce7])';
var received_re = net_utils.get_ipany_re('^Received:.*?[\\[\\(]', '[\\]\\)]');
var match = received_re.exec(received_header);
test.equal(match[1], '2601:184:c001:5cf7:a53f:baf7:aaf3:bce7');
test.equal(match.length, 2);
test.done();
},
'IPv6, Received header, bracketed, IPv6 prefix': function (test) {
test.expect(2);
var received_re = net_utils.get_ipany_re('^Received:.*?[\\[\\(](?:IPv6:)?', '[\\]\\)]');
var match = received_re.exec('Received: from hub.freebsd.org (hub.freebsd.org [IPv6:2001:1900:2254:206c::16:88])');
test.equal(match[1], '2001:1900:2254:206c::16:88');
test.equal(match.length, 2);
test.done();
},
'IPv6, folded Received header, bracketed, IPv6 prefix': function (test) {
test.expect(2);
/* note the use of [\s\S], '.' doesn't match newlines in JS regexp */
var received_re = net_utils.get_ipany_re('^Received:[\\s\\S]*?[\\[\\(](?:IPv6:)?', '[\\]\\)]');
var match = received_re.exec('Received: from freefall.freebsd.org (freefall.freebsd.org\r\n [IPv6:2001:1900:2254:206c::16:87])');
if (match) {
test.equal(match[1], '2001:1900:2254:206c::16:87');
test.equal(match.length, 2);
}
test.done();
},
'IPv6, Received header, bracketed, IPv6 prefix, localhost compressed': function (test) {
test.expect(2);
var received_re = net_utils.get_ipany_re('^Received:.*?[\\[\\(](?:IPv6:)?', '[\\]\\)]');
var match = received_re.exec('Received: from ietfa.amsl.com (localhost [IPv6:::1])');
test.equal(match[1], '::1');
test.equal(match.length, 2);
test.done();
},
var received_re = net_utils.get_ipany_re('^Received:[\\s\\S]*?[\\[\\(](?:IPv6:)?', '[\\]\\)]');
var match = received_re.exec('Received: from freefall.freebsd.org (freefall.freebsd.org\r\n [IPv6:2001:1900:2254:206c::16:87])');
if (match) {
test.equal(match[1], '2001:1900:2254:206c::16:87');
test.equal(match.length, 2);
}
test.done();
},
'IPv6, Received header, bracketed, IPv6 prefix, localhost compressed': function (test) {
test.expect(2);
var received_re = net_utils.get_ipany_re('^Received:.*?[\\[\\(](?:IPv6:)?', '[\\]\\)]');
var match = received_re.exec('Received: from ietfa.amsl.com (localhost [IPv6:::1])');
test.equal(match[1], '::1');
test.equal(match.length, 2);
test.done();
},
};
exports.get_ips_by_host = {
'get_ips_by_host, servedby.tnpi.net': function (test) {
test.expect(2);
net_utils.get_ips_by_host('servedby.tnpi.net', function (err, res) {
'get_ips_by_host, servedby.tnpi.net': function (test) {
test.expect(2);
net_utils.get_ips_by_host('servedby.tnpi.net', function (err, res) {
// console.log(arguments);
if (err && err.length) {
console.error(err);
}
test.deepEqual(err, []);
test.deepEqual(res.sort(), [
'192.48.85.146',
'192.48.85.147',
'192.48.85.148',
'192.48.85.149',
'2607:f060:b008:feed::2'
].sort());
test.done();
});
},
if (err && err.length) {
console.error(err);
}
test.deepEqual(err, []);
test.deepEqual(res.sort(), [
'192.48.85.146',
'192.48.85.147',
'192.48.85.148',
'192.48.85.149',
'2607:f060:b008:feed::2'
].sort());
test.done();
});
},
};
function _check_list(test, list, ip, res) {
test.expect(1);
test.equals(net_utils.ip_in_list(list, ip), res);
test.done();
}
exports.ip_in_list = {
'domain.com': function (test) {
_check_list(test, { 'domain.com': undefined }, 'domain.com', true);
},
'foo.com': function (test) {
_check_list(test, { }, 'foo.com', false);
},
'1.2.3.4': function (test) {
_check_list(test, { '1.2.3.4': undefined }, '1.2.3.4', true);
},
'1.2.3.4/32': function (test) {
_check_list(test, { '1.2.3.4/32': undefined }, '1.2.3.4', true);
},
'1.2.0.0/16 <-> 1.2.3.4': function (test) {
_check_list(test, { '1.2.0.0/16': undefined }, '1.2.3.4', true);
},
'1.2.0.0/16 <-> 5.6.7.8': function (test) {
_check_list(test, { '1.2.0.0/16': undefined }, '5.6.7.8', false);
},
'0000:0000:0000:0000:0000:0000:0000:0001': function (test) {
_check_list(test, { '0000:0000:0000:0000:0000:0000:0000:0001': undefined }, '0000:0000:0000:0000:0000:0000:0000:0001', true);
},
'0:0:0:0:0:0:0:1': function (test) {
_check_list(test, { '0:0:0:0:0:0:0:1': undefined }, '0000:0000:0000:0000:0000:0000:0000:0001', true);
},
'1.2 (bad config)': function (test) {
_check_list(test, { '1.2': undefined }, '1.2.3.4', false);
},
'1.2.3.4/ (mask ignored)': function (test) {
_check_list(test, { '1.2.3.4/': undefined }, '1.2.3.4', true);
},
'1.2.3.4/gr (mask ignored)': function (test) {
_check_list(test, { '1.2.3.4/gr': undefined }, '1.2.3.4', true);
},
'1.2.3.4/400 (mask read as 400 bits)': function (test) {
_check_list(test, { '1.2.3.4/400': undefined }, '1.2.3.4', true);
}
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc