From a3a39c48f4b0100c68411ed76ef40db4e8f097fc Mon Sep 17 00:00:00 2001 From: liranmauda Date: Wed, 22 Jan 2025 17:22:22 +0200 Subject: [PATCH] nope-ip removal - Phase 1 nope-ip removal - Phase 1 # `net_utils` - Will no longer use the deprecated `url.parse()` and use `new URL()` instead. It is possible because we use it with .hostname, which is equal for both. - Create ip_toLong to replace the node-ip toLong # Others - Replace the use of isV4Format to net.isIPv4 - Replace the use of isV6Format to net.isIPv6 - Calling ip_toLong instead of toLong Signed-off-by: liranmauda --- src/rpc/rpc.js | 1 - src/server/common_services/auth_server.js | 7 +++---- src/util/http_utils.js | 13 +++++++------ src/util/net_utils.js | 20 +++++++++++++------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/rpc/rpc.js b/src/rpc/rpc.js index 3e1e8c4946..2e8b8cf219 100644 --- a/src/rpc/rpc.js +++ b/src/rpc/rpc.js @@ -7,7 +7,6 @@ const _ = require('lodash'); const util = require('util'); const assert = require('assert'); -// const ip_module = require('ip'); const EventEmitter = require('events').EventEmitter; const P = require('../util/promise'); diff --git a/src/server/common_services/auth_server.js b/src/server/common_services/auth_server.js index 93160a1238..dd08d41dd0 100644 --- a/src/server/common_services/auth_server.js +++ b/src/server/common_services/auth_server.js @@ -3,7 +3,6 @@ const _ = require('lodash'); const bcrypt = require('bcrypt'); -const ip_module = require('ip'); const P = require('../../util/promise'); const dbg = require('../../util/debug_module')(__filename); @@ -578,10 +577,10 @@ function _prepare_auth_request(req) { const client_ip = net_utils.unwrap_ipv6(req.auth.client_ip); if (client_ip) { let is_allowed = false; - const client_ip_val = ip_module.toLong(client_ip); + const client_ip_val = net_utils.ip_toLong(client_ip); for (const ip_range of req.account.allowed_ips) { - const start = ip_module.toLong(ip_range.start); - const end = ip_module.toLong(ip_range.end); + const start = net_utils.ip_toLong(ip_range.start); + const end = net_utils.ip_toLong(ip_range.end); if (client_ip_val >= start && client_ip_val <= end) { is_allowed = true; break; diff --git a/src/util/http_utils.js b/src/util/http_utils.js index 3efc63b7d4..7c04f1407b 100644 --- a/src/util/http_utils.js +++ b/src/util/http_utils.js @@ -3,7 +3,8 @@ /* eslint-disable no-control-regex */ const _ = require('lodash'); -const ip = require('ip'); +const ip_module = require('ip'); +const net = require('net'); const url = require('url'); const http = require('http'); const https = require('https'); @@ -44,7 +45,7 @@ const unsecured_https_proxy_agent = HTTPS_PROXY ? const no_proxy_list = (NO_PROXY ? NO_PROXY.split(',') : []).map(addr => { - if (ip.isV4Format(addr) || ip.isV6Format(addr)) { + if (net.isIPv4(addr) || net.isIPv6(addr)) { return { kind: 'IP', addr @@ -52,7 +53,7 @@ const no_proxy_list = } try { - ip.cidr(addr); + ip_module.cidr(addr); return { kind: 'CIDR', addr @@ -383,16 +384,16 @@ function send_reply(req, res, reply, options) { * Check if a hostname should be proxied or not */ function should_proxy(hostname) { - const isIp = ip.isV4Format(hostname) || ip.isV6Format(hostname); + const isIp = net.isIPv4(hostname) || net.isIPv6(hostname); dbg.log2(`should_proxy: hostname ${hostname} isIp ${isIp}`); for (const { kind, addr } of no_proxy_list) { dbg.log3(`should_proxy: an item from no_proxy_list: kind ${kind} addr ${addr}`); if (isIp) { - if (kind === 'IP' && ip.isEqual(addr, hostname)) { + if (kind === 'IP' && ip_module.isEqual(addr, hostname)) { return false; } - if (kind === 'CIDR' && ip.cidrSubnet(addr).contains(hostname)) { + if (kind === 'CIDR' && ip_module.cidrSubnet(addr).contains(hostname)) { return false; } diff --git a/src/util/net_utils.js b/src/util/net_utils.js index 38832c08cf..85baf5e3fe 100644 --- a/src/util/net_utils.js +++ b/src/util/net_utils.js @@ -3,7 +3,6 @@ const _ = require('lodash'); const os = require('os'); -const url = require('url'); const net = require('net'); const dns = require('dns'); const ip_module = require('ip'); @@ -26,7 +25,7 @@ async function ping(target, options) { options = options || DEFAULT_PING_OPTIONS; _.defaults(options, DEFAULT_PING_OPTIONS); - const candidate_ip = url.parse(target).hostname || target; + const candidate_ip = new URL(target).hostname || target; if (net.isIP(candidate_ip)) { await _ping_ip(candidate_ip); @@ -49,7 +48,7 @@ function _ping_ip(session, ip) { } async function dns_resolve(target, options) { - const modified_target = url.parse(target).hostname || target; + const modified_target = new URL(target).hostname || target; await os_utils.get_dns_config(); // unused? needed? const res = await dns.promises.resolve(modified_target, (options && options.rrtype) || 'A'); return res; @@ -90,18 +89,24 @@ function unwrap_ipv6(ip) { return ip; } +//the name ip_toLong consist of camel case and underscore, to indicate that toLong is the function we had in node-ip +function ip_toLong(ip) { + // eslint-disable-next-line no-bitwise + return ip.split('.').reduce((acc, octet) => (acc << 8) + parseInt(octet, 10), 0) >>> 0; +} + function ip_to_long(ip) { - return ip_module.toLong(unwrap_ipv6(ip)); + return ip_toLong(unwrap_ipv6(ip)); } function is_ip(address) { - return ip_module.isV4Format(address) || ip_module.isV6Format(address); + return net.isIPv4(address) || net.isIPv6(address); } function find_ifc_containing_address(address) { const family = - (ip_module.isV4Format(address) && 'IPv4') || - (ip_module.isV6Format(address) && 'IPv6') || + (net.isIPv4(address) && 'IPv4') || + (net.isIPv6(address) && 'IPv6') || ''; if (!family) return; for (const [ifc, arr] of Object.entries(os.networkInterfaces())) { @@ -120,5 +125,6 @@ exports.is_ip = is_ip; exports.is_fqdn = is_fqdn; exports.is_localhost = is_localhost; exports.unwrap_ipv6 = unwrap_ipv6; +exports.ip_toLong = ip_toLong; exports.ip_to_long = ip_to_long; exports.find_ifc_containing_address = find_ifc_containing_address;