Skip to content

Commit 9d26ed0

Browse files
authored
Merge pull request #8779 from liranmauda/liran-remove-node-ip
nope-ip removal - Phase 3
2 parents 23aec8d + 4514fb7 commit 9d26ed0

File tree

4 files changed

+265
-40
lines changed

4 files changed

+265
-40
lines changed

src/rpc/stun.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
/* eslint-disable no-bitwise */
33
'use strict';
44

5+
const url = require('url');
56
const _ = require('lodash');
67
const util = require('util');
7-
const P = require('../util/promise');
8-
const url = require('url');
98
const dgram = require('dgram');
109
const crypto = require('crypto');
11-
const ip_module = require('ip');
1210
const chance = require('chance')();
11+
const P = require('../util/promise');
12+
const net_utils = require('../util/net_utils');
1313

1414
// https://tools.ietf.org/html/rfc5389
1515
const stun = {
@@ -435,7 +435,7 @@ function encode_attrs(buffer, attrs) {
435435
function decode_attr_mapped_addr(buffer, start, end) {
436436
const family = (buffer.readUInt16BE(start) === 0x02) ? 6 : 4;
437437
const port = buffer.readUInt16BE(start + 2);
438-
const address = ip_module.toString(buffer, start + 4, family);
438+
const address = net_utils.ip_toString(buffer, start + 4, family);
439439

440440
return {
441441
family: 'IPv' + family,
@@ -463,7 +463,7 @@ function decode_attr_xor_mapped_addr(buffer, start, end) {
463463
xor_buf[i] = addr_buf[i] ^ buffer[k];
464464
k += 1;
465465
}
466-
const address = ip_module.toString(xor_buf, 0, family);
466+
const address = net_utils.ip_toString(xor_buf, 0, family);
467467

468468
return {
469469
family: 'IPv' + family,
@@ -510,7 +510,7 @@ function encode_attr_mapped_addr(addr, buffer, offset, end) {
510510
// xor the port against the magic key
511511
buffer.writeUInt16BE(addr.port, offset + 2);
512512

513-
ip_module.toBuffer(addr.address, buffer, offset + 4);
513+
net_utils.ip_toBuffer(addr.address, buffer, offset + 4);
514514
}
515515

516516

@@ -524,7 +524,7 @@ function encode_attr_xor_mapped_addr(addr, buffer, offset, end) {
524524
// xor the port against the magic key
525525
buffer.writeUInt16BE(addr.port ^ buffer.readUInt16BE(stun.XOR_KEY_OFFSET), offset + 2);
526526

527-
ip_module.toBuffer(addr.address, buffer, offset + 4);
527+
net_utils.ip_toBuffer(addr.address, buffer, offset + 4);
528528
let k = stun.XOR_KEY_OFFSET;
529529
for (let i = offset + 4; i < end; ++i) {
530530
buffer[i] ^= buffer[k];
@@ -615,7 +615,8 @@ function test() {
615615
}
616616
return Promise.all([
617617
P.ninvoke(socket, 'send', req, 0, req.length, stun_url.port, stun_url.hostname),
618-
P.ninvoke(socket, 'send', ind, 0, ind.length, stun_url.port, stun_url.hostname)])
618+
P.ninvoke(socket, 'send', ind, 0, ind.length, stun_url.port, stun_url.hostname)
619+
])
619620
.then(() => P.delay(stun.INDICATION_INTERVAL * chance.floating(stun.INDICATION_JITTER)))
620621
.then(loop);
621622
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* Copyright (C) 2016 NooBaa */
2+
'use strict';
3+
4+
const os = require('os');
5+
const net_utils = require('../../../util/net_utils');
6+
7+
describe('IP Utils', () => {
8+
it('is_ip should correctly identify IP addresses', () => {
9+
expect(net_utils.is_ip('192.168.1.1')).toBe(true);
10+
expect(net_utils.is_ip('::1')).toBe(true);
11+
expect(net_utils.is_ip('not_an_ip')).toBe(false);
12+
expect(net_utils.is_ip('256.256.256.256')).toBe(false);
13+
});
14+
15+
it('is_fqdn should correctly identify FQDNs', () => {
16+
expect(net_utils.is_fqdn('example.com')).toBe(true);
17+
expect(net_utils.is_fqdn('sub.example.com')).toBe(true);
18+
expect(net_utils.is_fqdn('localhost')).toBe(false);
19+
expect(net_utils.is_fqdn('invalid_domain-.com')).toBe(false);
20+
});
21+
22+
it('is_cidr should correctly identify CIDR notation', () => {
23+
expect(net_utils.is_cidr('192.168.1.0/24')).toBe(true);
24+
expect(net_utils.is_cidr('10.0.0.0/8')).toBe(true);
25+
expect(net_utils.is_cidr('2001:db8::/32')).toBe(true);
26+
expect(net_utils.is_cidr('192.168.1.300/24')).toBe(false);
27+
expect(net_utils.is_cidr('invalid_cidr')).toBe(false);
28+
expect(net_utils.is_cidr('192.168.1.1')).toBe(false);
29+
expect(net_utils.is_cidr('282.150.0.0/12')).toBe(false);
30+
expect(net_utils.is_cidr('192.168.0.0/35')).toBe(false);
31+
});
32+
33+
it('is_localhost should correctly identify localhost addresses', () => {
34+
expect(net_utils.is_localhost('127.0.0.1')).toBe(true);
35+
expect(net_utils.is_localhost('::1')).toBe(true);
36+
expect(net_utils.is_localhost('localhost')).toBe(true);
37+
expect(net_utils.is_localhost('192.168.1.1')).toBe(false);
38+
});
39+
40+
it('unwrap_ipv6 should remove IPv6 prefix', () => {
41+
expect(net_utils.unwrap_ipv6('::ffff:192.168.1.1')).toBe('192.168.1.1');
42+
expect(net_utils.unwrap_ipv6('::1')).toBe('::1');
43+
expect(net_utils.unwrap_ipv6('2001:db8::ff00:42:8329')).toBe('2001:db8::ff00:42:8329');
44+
});
45+
46+
it('ip_toLong should convert IPv4 to long', () => {
47+
expect(net_utils.ip_toLong('192.168.1.1')).toBe(3232235777);
48+
expect(net_utils.ip_toLong('0.0.0.0')).toBe(0);
49+
expect(net_utils.ip_toLong('255.255.255.255')).toBe(4294967295);
50+
});
51+
52+
it('ip_to_long should handle both IPv4 and IPv6-mapped IPv4', () => {
53+
expect(net_utils.ip_to_long('192.168.1.1')).toBe(3232235777);
54+
expect(net_utils.ip_to_long('::ffff:192.168.1.1')).toBe(3232235777);
55+
});
56+
57+
it('find_ifc_containing_address should find interface containing the address', () => {
58+
const networkInterfacesMock = {
59+
eth0: [
60+
{ family: 'IPv4', cidr: '192.168.1.0/24', address: '192.168.1.2' },
61+
{ family: 'IPv6', cidr: 'fe80::/64', address: 'fe80::1' },
62+
],
63+
lo: [{ family: 'IPv4', cidr: '127.0.0.0/8', address: '127.0.0.1' }],
64+
};
65+
jest.spyOn(os, 'networkInterfaces').mockReturnValue(networkInterfacesMock);
66+
67+
expect(net_utils.find_ifc_containing_address('192.168.1.5')).toEqual({ ifc: 'eth0', info: networkInterfacesMock.eth0[0] });
68+
expect(net_utils.find_ifc_containing_address('127.0.0.1')).toEqual({ ifc: 'lo', info: networkInterfacesMock.lo[0] });
69+
expect(net_utils.find_ifc_containing_address('8.8.8.8')).toBeUndefined();
70+
});
71+
72+
it('ip_toString should convert buffers to IP strings', () => {
73+
expect(net_utils.ip_toString(Buffer.from([192, 168, 1, 1]), 0, 4)).toBe('192.168.1.1');
74+
expect(net_utils.ip_toString(Buffer.from([0, 0, 0, 0]), 0, 4)).toBe('0.0.0.0');
75+
expect(net_utils.ip_toString(Buffer.from([255, 255, 255, 255]), 0, 4)).toBe('255.255.255.255');
76+
expect(net_utils.ip_toString(Buffer.from([32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), 0, 16)).toBe('2001:db8::1');
77+
expect(() => net_utils.ip_toString(Buffer.from([192, 168, 1, 1]), undefined, 4)).toThrow('Offset is required');
78+
});
79+
80+
it('ipv4_to_buffer should convert IPv4 string to buffer', () => {
81+
const buff = Buffer.alloc(4);
82+
expect(net_utils.ipv4_to_buffer('192.168.1.1', buff, 0)).toEqual(Buffer.from([192, 168, 1, 1]));
83+
});
84+
85+
it('ipv6_to_buffer should convert expanded IPv6 string to buffer', () => {
86+
const buff = Buffer.alloc(16);
87+
expect(net_utils.ipv6_to_buffer('2001:0db8:0000:0000:0000:0000:0000:0001', buff, 0)).toEqual(
88+
Buffer.from([32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
89+
);
90+
});
91+
92+
it('expend_ipv6 should expand IPv6 shorthand notation', () => {
93+
expect(net_utils.expend_ipv6('::')).toEqual(['0', '0', '0', '0', '0', '0', '0', '0']);
94+
expect(net_utils.expend_ipv6('2001:db8::1')).toEqual(['2001', 'db8', '0', '0', '0', '0', '0', '1']);
95+
expect(net_utils.expend_ipv6('2001:db8::ff00:42:8329')).toEqual(['2001', 'db8', '0', '0', '0', 'ff00', '42', '8329']);
96+
expect(net_utils.expend_ipv6('::1')).toEqual(['0', '0', '0', '0', '0', '0', '0', '1']);
97+
expect(net_utils.expend_ipv6('2001:0db8:85a3::8a2e:370:7334')).toEqual(['2001', '0db8', '85a3', '0', '0', '8a2e', '370', '7334']);
98+
expect(net_utils.expend_ipv6('::ffff:192.168.1.1')).toEqual(['0', '0', '0', '0', '0', 'ffff', 'c0a8', '0101']);
99+
});
100+
101+
it('ip_toBuffer should convert IP strings to buffer', () => {
102+
expect(net_utils.ip_toBuffer('10.0.0.1', Buffer.alloc(4), 0)).toEqual(Buffer.from([10, 0, 0, 1]));
103+
expect(net_utils.ip_toBuffer('2001:db8::1', Buffer.alloc(16), 0)).toEqual(
104+
Buffer.from([32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
105+
);
106+
expect(() => net_utils.ip_toBuffer('invalid_ip', Buffer.alloc(16), 0)).toThrow('Invalid IP address: invalid_ip');
107+
expect(() => net_utils.ip_toBuffer('10.0.0.1')).toThrow('Offset is required');
108+
});
109+
});

src/util/http_utils.js

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const dbg = require('./debug_module')(__filename);
1919
const config = require('../../config');
2020
const xml_utils = require('./xml_utils');
2121
const jwt_utils = require('./jwt_utils');
22+
const net_utils = require('./net_utils');
2223
const time_utils = require('./time_utils');
2324
const cloud_utils = require('./cloud_utils');
2425
const ssl_utils = require('../util/ssl_utils');
@@ -45,38 +46,18 @@ const https_proxy_agent = HTTPS_PROXY ?
4546
const unsecured_https_proxy_agent = HTTPS_PROXY ?
4647
new HttpsProxyAgent(HTTPS_PROXY, { rejectUnauthorized: false }) : null;
4748

48-
const no_proxy_list =
49-
(NO_PROXY ? NO_PROXY.split(',') : []).map(addr => {
50-
if (net.isIPv4(addr) || net.isIPv6(addr)) {
51-
return {
52-
kind: 'IP',
53-
addr
54-
};
55-
}
56-
57-
try {
58-
ip_module.cidr(addr);
59-
return {
60-
kind: 'CIDR',
61-
addr
62-
};
63-
} catch {
64-
// noop
65-
}
66-
67-
if (addr.startsWith('.')) {
68-
return {
69-
kind: 'FQDN_SUFFIX',
70-
addr
71-
};
72-
}
73-
74-
return {
75-
kind: 'FQDN',
76-
addr
77-
};
78-
});
49+
const no_proxy_list = (NO_PROXY ? NO_PROXY.split(',') : []).map(addr => {
50+
let kind = 'FQDN';
51+
if (net.isIPv4(addr) || net.isIPv6(addr)) {
52+
kind = 'IP';
53+
} else if (net_utils.is_cidr(addr)) {
54+
kind = 'CIDR';
55+
} else if (addr.startsWith('.')) {
56+
kind = 'FQDN_SUFFIX';
57+
}
7958

59+
return { kind, addr };
60+
});
8061

8162
const parse_xml_to_js = xml2js.parseStringPromise;
8263
const non_printable_regexp = /[\x00-\x1F]/;

0 commit comments

Comments
 (0)