Skip to content

Commit 6d53494

Browse files
authored
Merge pull request #8787 from liranmauda/liran-remove-node-ip-2
Adding ading functions to `net_utils.js` | nope-ip removal - Phase 4
2 parents e2773ba + c8e4856 commit 6d53494

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

src/test/unit_tests/jest_tests/test_net_utils.test.js

+49
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,53 @@ describe('IP Utils', () => {
106106
expect(() => net_utils.ip_toBuffer('invalid_ip', Buffer.alloc(16), 0)).toThrow('Invalid IP address: invalid_ip');
107107
expect(() => net_utils.ip_toBuffer('10.0.0.1')).toThrow('Offset is required');
108108
});
109+
110+
it('normalize_family should return the proper family', () => {
111+
expect(net_utils.normalize_family(4)).toBe('ipv4');
112+
expect(net_utils.normalize_family(6)).toBe('ipv6');
113+
expect(net_utils.normalize_family('IPv4')).toBe('ipv4');
114+
expect(net_utils.normalize_family('IPv6')).toBe('ipv6');
115+
expect(net_utils.normalize_family('customFamily')).toBe('customfamily');
116+
expect(net_utils.normalize_family(undefined)).toBe('ipv4');
117+
expect(net_utils.normalize_family(null)).toBe('ipv4');
118+
});
119+
120+
it('should return true for loopback addresses and false if not', () => {
121+
expect(net_utils.is_loopback('127.0.0.1')).toBe(true);
122+
expect(net_utils.is_loopback('127.255.255.255')).toBe(true);
123+
expect(net_utils.is_loopback('::1')).toBe(true);
124+
expect(net_utils.is_loopback('192.168.1.1')).toBe(false);
125+
expect(net_utils.is_loopback('10.0.0.1')).toBe(false);
126+
expect(net_utils.is_loopback('8.8.8.8')).toBe(false);
127+
expect(net_utils.is_loopback('2001:db8::1')).toBe(false);
128+
});
129+
130+
it('should return true for localhost and loopback addresses and false if not', () => {
131+
expect(net_utils.is_localhost('127.0.0.1')).toBe(true);
132+
expect(net_utils.is_localhost('::1')).toBe(true);
133+
expect(net_utils.is_localhost('localhost')).toBe(true);
134+
expect(net_utils.is_localhost('192.168.1.1')).toBe(false);
135+
expect(net_utils.is_localhost('10.0.0.1')).toBe(false);
136+
expect(net_utils.is_localhost('google.com')).toBe(false);
137+
});
138+
139+
it('should return true for private IPv4 addresses or false to any other address', () => {
140+
expect(net_utils.is_private('10.0.0.1')).toBe(true);
141+
expect(net_utils.is_private('172.16.0.1')).toBe(true);
142+
expect(net_utils.is_private('192.168.1.1')).toBe(true);
143+
expect(net_utils.is_private('8.8.8.8')).toBe(false);
144+
expect(net_utils.is_private('1.1.1.1')).toBe(false);
145+
expect(net_utils.is_private('2001:db8::1')).toBe(false);
146+
});
147+
148+
149+
it('should return true for public addresses or false for private addresses', () => {
150+
expect(net_utils.is_public('10.0.0.1')).toBe(false);
151+
expect(net_utils.is_public('172.16.0.1')).toBe(false);
152+
expect(net_utils.is_public('192.168.1.1')).toBe(false);
153+
expect(net_utils.is_public('8.8.8.8')).toBe(true);
154+
expect(net_utils.is_public('1.1.1.1')).toBe(true);
155+
expect(net_utils.is_public('2001:db8::1')).toBe(true);
156+
});
157+
109158
});

src/util/net_utils.js

+46-3
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,58 @@ function is_cidr(ip) {
2828
return true;
2929
}
3030

31+
/**
32+
* normalize_family will normalize the family
33+
* @param {string|number} family
34+
* @returns {string}
35+
*/
36+
function normalize_family(family) {
37+
if (family === 4) return 'ipv4';
38+
if (family === 6) return 'ipv6';
39+
return family ? String(family).toLowerCase() : 'ipv4';
40+
}
41+
42+
/**
43+
* is_loopback will check if the address is a loop back address
44+
* any address that starts with 127. or is ::1 is considered loopback
45+
* @param {string} address
46+
* @returns {boolean}
47+
*/
48+
function is_loopback(address) {
49+
return address.startsWith('127.') || address === '::1';
50+
}
51+
3152
/**
3253
* is_localhost will check if the address is localhost
3354
* @param {string} address
3455
* @returns {boolean}
3556
*/
3657
function is_localhost(address) {
58+
return is_loopback(address) || address.toLowerCase() === 'localhost';
59+
}
60+
61+
/**
62+
* is_private will check if the address is private
63+
* @param {string} address
64+
* @returns {boolean}
65+
*/
66+
function is_private(address) {
3767
return (
38-
address === '127.0.0.1' ||
39-
address === '::1' ||
40-
address.toLowerCase() === 'localhost'
68+
address.startsWith('10.') ||
69+
address.startsWith('172.') ||
70+
address.startsWith('192.168.')
4171
);
4272
}
4373

74+
/**
75+
* is_public will check if the address is public
76+
* @param {string} address
77+
* @returns {boolean}
78+
*/
79+
function is_public(address) {
80+
return !is_private(address);
81+
}
82+
4483
function unwrap_ipv6(ip) {
4584
if (net.isIPv6(ip)) {
4685
if (ip.startsWith('::ffff:')) return ip.slice('::ffff:'.length);
@@ -201,6 +240,10 @@ exports.ip_to_long = ip_to_long;
201240
exports.find_ifc_containing_address = find_ifc_containing_address;
202241

203242
/// EXPORTS FOR TESTING:
243+
exports.normalize_family = normalize_family;
244+
exports.is_loopback = is_loopback;
245+
exports.is_private = is_private;
246+
exports.is_public = is_public;
204247
exports.ipv4_to_buffer = ipv4_to_buffer;
205248
exports.ipv6_to_buffer = ipv6_to_buffer;
206249
exports.expend_ipv6 = expend_ipv6;

0 commit comments

Comments
 (0)