|
1 | 1 | /* Copyright (C) 2016 NooBaa */
|
2 | 2 | 'use strict';
|
3 | 3 |
|
4 |
| -const _ = require('lodash'); |
5 | 4 | const os = require('os');
|
6 | 5 | const net = require('net');
|
7 |
| -const dns = require('dns'); |
8 | 6 | const ip_module = require('ip');
|
9 |
| -const pinger = require('ping'); |
10 | 7 |
|
11 |
| -const P = require('./promise'); |
12 |
| -const dbg = require('./debug_module')(__filename); |
13 |
| -const os_utils = require('./os_utils'); |
14 |
| -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]?)$/; |
15 | 8 | 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})$)/;
|
16 | 9 |
|
17 |
| -const DEFAULT_PING_OPTIONS = { |
18 |
| - timeout: 5000, |
19 |
| - retries: 0, |
20 |
| - packetSize: 64 |
21 |
| -}; |
22 |
| - |
23 |
| -async function ping(target, options) { |
24 |
| - dbg.log1('pinging', target); |
25 |
| - |
26 |
| - options = options || DEFAULT_PING_OPTIONS; |
27 |
| - _.defaults(options, DEFAULT_PING_OPTIONS); |
28 |
| - const candidate_ip = new URL(target).hostname || target; |
29 |
| - |
30 |
| - if (net.isIP(candidate_ip)) { |
31 |
| - await _ping_ip(candidate_ip); |
32 |
| - } else { |
33 |
| - const ip_table = await dns_resolve(target); |
34 |
| - await P.map_any(ip_table, ip => _ping_ip(ip)); |
35 |
| - } |
36 |
| -} |
37 |
| - |
38 |
| -function _ping_ip(session, ip) { |
39 |
| - return new Promise((resolve, reject) => { |
40 |
| - pinger.sys.probe(ip, (is_alive, error) => { |
41 |
| - if (is_alive) { |
42 |
| - resolve(); |
43 |
| - } else { |
44 |
| - reject(error); |
45 |
| - } |
46 |
| - }); |
47 |
| - }); |
48 |
| -} |
49 |
| - |
50 |
| -async function dns_resolve(target, options) { |
51 |
| - const modified_target = new URL(target).hostname || target; |
52 |
| - await os_utils.get_dns_config(); // unused? needed? |
53 |
| - const res = await dns.promises.resolve(modified_target, (options && options.rrtype) || 'A'); |
54 |
| - return res; |
55 |
| -} |
56 |
| - |
57 |
| -function is_hostname(target) { |
58 |
| - if (hostname_regexp.test(target)) { |
59 |
| - return true; |
60 |
| - } |
61 |
| - |
62 |
| - return false; |
63 |
| -} |
64 |
| - |
65 | 10 | function is_fqdn(target) {
|
66 | 11 | if (target && fqdn_regexp.test(target)) {
|
67 | 12 | return true;
|
@@ -118,9 +63,6 @@ function find_ifc_containing_address(address) {
|
118 | 63 | }
|
119 | 64 | }
|
120 | 65 |
|
121 |
| -exports.ping = ping; |
122 |
| -exports.dns_resolve = dns_resolve; |
123 |
| -exports.is_hostname = is_hostname; |
124 | 66 | exports.is_ip = is_ip;
|
125 | 67 | exports.is_fqdn = is_fqdn;
|
126 | 68 | exports.is_localhost = is_localhost;
|
|
0 commit comments