Skip to content

Commit

Permalink
Merge pull request #8714 from liranmauda/liran-remove-node-ip
Browse files Browse the repository at this point in the history
nope-ip removal - Phase 1
  • Loading branch information
liranmauda authored Feb 5, 2025
2 parents d580748 + a3a39c4 commit 56563a0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/rpc/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
7 changes: 3 additions & 4 deletions src/server/common_services/auth_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions src/util/http_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -44,15 +45,15 @@ 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
};
}

try {
ip.cidr(addr);
ip_module.cidr(addr);
return {
kind: 'CIDR',
addr
Expand Down Expand Up @@ -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;
}

Expand Down
20 changes: 13 additions & 7 deletions src/util/net_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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())) {
Expand All @@ -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;

0 comments on commit 56563a0

Please sign in to comment.