Skip to content

Commit

Permalink
Merge pull request #8771 from liranmauda/liran-code-clean
Browse files Browse the repository at this point in the history
Code maintenance | Clean dead code from net_utils | nope-ip removal - Phase 2
  • Loading branch information
liranmauda authored Feb 9, 2025
2 parents d9bfeea + db60c03 commit 8668b3c
Showing 1 changed file with 0 additions and 58 deletions.
58 changes: 0 additions & 58 deletions src/util/net_utils.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,12 @@
/* Copyright (C) 2016 NooBaa */
'use strict';

const _ = require('lodash');
const os = require('os');
const net = require('net');
const dns = require('dns');
const ip_module = require('ip');
const pinger = require('ping');

const P = require('./promise');
const dbg = require('./debug_module')(__filename);
const os_utils = require('./os_utils');
const hostname_regexp = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
const fqdn_regexp = /^(?=^.{1,253}$)(^(((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9])|((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63})$)/;

const DEFAULT_PING_OPTIONS = {
timeout: 5000,
retries: 0,
packetSize: 64
};

async function ping(target, options) {
dbg.log1('pinging', target);

options = options || DEFAULT_PING_OPTIONS;
_.defaults(options, DEFAULT_PING_OPTIONS);
const candidate_ip = new URL(target).hostname || target;

if (net.isIP(candidate_ip)) {
await _ping_ip(candidate_ip);
} else {
const ip_table = await dns_resolve(target);
await P.map_any(ip_table, ip => _ping_ip(ip));
}
}

function _ping_ip(session, ip) {
return new Promise((resolve, reject) => {
pinger.sys.probe(ip, (is_alive, error) => {
if (is_alive) {
resolve();
} else {
reject(error);
}
});
});
}

async function dns_resolve(target, options) {
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;
}

function is_hostname(target) {
if (hostname_regexp.test(target)) {
return true;
}

return false;
}

function is_fqdn(target) {
if (target && fqdn_regexp.test(target)) {
return true;
Expand Down Expand Up @@ -118,9 +63,6 @@ function find_ifc_containing_address(address) {
}
}

exports.ping = ping;
exports.dns_resolve = dns_resolve;
exports.is_hostname = is_hostname;
exports.is_ip = is_ip;
exports.is_fqdn = is_fqdn;
exports.is_localhost = is_localhost;
Expand Down

0 comments on commit 8668b3c

Please sign in to comment.