Skip to content

Commit 56563a0

Browse files
authored
Merge pull request #8714 from liranmauda/liran-remove-node-ip
nope-ip removal - Phase 1
2 parents d580748 + a3a39c4 commit 56563a0

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

src/rpc/rpc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const _ = require('lodash');
88
const util = require('util');
99
const assert = require('assert');
10-
// const ip_module = require('ip');
1110
const EventEmitter = require('events').EventEmitter;
1211

1312
const P = require('../util/promise');

src/server/common_services/auth_server.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
const _ = require('lodash');
55
const bcrypt = require('bcrypt');
6-
const ip_module = require('ip');
76

87
const P = require('../../util/promise');
98
const dbg = require('../../util/debug_module')(__filename);
@@ -578,10 +577,10 @@ function _prepare_auth_request(req) {
578577
const client_ip = net_utils.unwrap_ipv6(req.auth.client_ip);
579578
if (client_ip) {
580579
let is_allowed = false;
581-
const client_ip_val = ip_module.toLong(client_ip);
580+
const client_ip_val = net_utils.ip_toLong(client_ip);
582581
for (const ip_range of req.account.allowed_ips) {
583-
const start = ip_module.toLong(ip_range.start);
584-
const end = ip_module.toLong(ip_range.end);
582+
const start = net_utils.ip_toLong(ip_range.start);
583+
const end = net_utils.ip_toLong(ip_range.end);
585584
if (client_ip_val >= start && client_ip_val <= end) {
586585
is_allowed = true;
587586
break;

src/util/http_utils.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
/* eslint-disable no-control-regex */
44

55
const _ = require('lodash');
6-
const ip = require('ip');
6+
const ip_module = require('ip');
7+
const net = require('net');
78
const url = require('url');
89
const http = require('http');
910
const https = require('https');
@@ -44,15 +45,15 @@ const unsecured_https_proxy_agent = HTTPS_PROXY ?
4445

4546
const no_proxy_list =
4647
(NO_PROXY ? NO_PROXY.split(',') : []).map(addr => {
47-
if (ip.isV4Format(addr) || ip.isV6Format(addr)) {
48+
if (net.isIPv4(addr) || net.isIPv6(addr)) {
4849
return {
4950
kind: 'IP',
5051
addr
5152
};
5253
}
5354

5455
try {
55-
ip.cidr(addr);
56+
ip_module.cidr(addr);
5657
return {
5758
kind: 'CIDR',
5859
addr
@@ -383,16 +384,16 @@ function send_reply(req, res, reply, options) {
383384
* Check if a hostname should be proxied or not
384385
*/
385386
function should_proxy(hostname) {
386-
const isIp = ip.isV4Format(hostname) || ip.isV6Format(hostname);
387+
const isIp = net.isIPv4(hostname) || net.isIPv6(hostname);
387388
dbg.log2(`should_proxy: hostname ${hostname} isIp ${isIp}`);
388389

389390
for (const { kind, addr } of no_proxy_list) {
390391
dbg.log3(`should_proxy: an item from no_proxy_list: kind ${kind} addr ${addr}`);
391392
if (isIp) {
392-
if (kind === 'IP' && ip.isEqual(addr, hostname)) {
393+
if (kind === 'IP' && ip_module.isEqual(addr, hostname)) {
393394
return false;
394395
}
395-
if (kind === 'CIDR' && ip.cidrSubnet(addr).contains(hostname)) {
396+
if (kind === 'CIDR' && ip_module.cidrSubnet(addr).contains(hostname)) {
396397
return false;
397398
}
398399

src/util/net_utils.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
const _ = require('lodash');
55
const os = require('os');
6-
const url = require('url');
76
const net = require('net');
87
const dns = require('dns');
98
const ip_module = require('ip');
@@ -26,7 +25,7 @@ async function ping(target, options) {
2625

2726
options = options || DEFAULT_PING_OPTIONS;
2827
_.defaults(options, DEFAULT_PING_OPTIONS);
29-
const candidate_ip = url.parse(target).hostname || target;
28+
const candidate_ip = new URL(target).hostname || target;
3029

3130
if (net.isIP(candidate_ip)) {
3231
await _ping_ip(candidate_ip);
@@ -49,7 +48,7 @@ function _ping_ip(session, ip) {
4948
}
5049

5150
async function dns_resolve(target, options) {
52-
const modified_target = url.parse(target).hostname || target;
51+
const modified_target = new URL(target).hostname || target;
5352
await os_utils.get_dns_config(); // unused? needed?
5453
const res = await dns.promises.resolve(modified_target, (options && options.rrtype) || 'A');
5554
return res;
@@ -90,18 +89,24 @@ function unwrap_ipv6(ip) {
9089
return ip;
9190
}
9291

92+
//the name ip_toLong consist of camel case and underscore, to indicate that toLong is the function we had in node-ip
93+
function ip_toLong(ip) {
94+
// eslint-disable-next-line no-bitwise
95+
return ip.split('.').reduce((acc, octet) => (acc << 8) + parseInt(octet, 10), 0) >>> 0;
96+
}
97+
9398
function ip_to_long(ip) {
94-
return ip_module.toLong(unwrap_ipv6(ip));
99+
return ip_toLong(unwrap_ipv6(ip));
95100
}
96101

97102
function is_ip(address) {
98-
return ip_module.isV4Format(address) || ip_module.isV6Format(address);
103+
return net.isIPv4(address) || net.isIPv6(address);
99104
}
100105

101106
function find_ifc_containing_address(address) {
102107
const family =
103-
(ip_module.isV4Format(address) && 'IPv4') ||
104-
(ip_module.isV6Format(address) && 'IPv6') ||
108+
(net.isIPv4(address) && 'IPv4') ||
109+
(net.isIPv6(address) && 'IPv6') ||
105110
'';
106111
if (!family) return;
107112
for (const [ifc, arr] of Object.entries(os.networkInterfaces())) {
@@ -120,5 +125,6 @@ exports.is_ip = is_ip;
120125
exports.is_fqdn = is_fqdn;
121126
exports.is_localhost = is_localhost;
122127
exports.unwrap_ipv6 = unwrap_ipv6;
128+
exports.ip_toLong = ip_toLong;
123129
exports.ip_to_long = ip_to_long;
124130
exports.find_ifc_containing_address = find_ifc_containing_address;

0 commit comments

Comments
 (0)