1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const exec = require('child_process').exec;
|
17 | const util = require('./util');
|
18 |
|
19 | let _platform = process.platform;
|
20 |
|
21 | const _linux = (_platform === 'linux');
|
22 | const _darwin = (_platform === 'darwin');
|
23 | const _windows = (_platform === 'win32');
|
24 | const _freebsd = (_platform === 'freebsd');
|
25 | const _openbsd = (_platform === 'openbsd');
|
26 | const _netbsd = (_platform === 'netbsd');
|
27 | const _sunos = (_platform === 'sunos');
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function inetChecksite(url, callback) {
|
33 |
|
34 | return new Promise((resolve) => {
|
35 | process.nextTick(() => {
|
36 |
|
37 | const urlSanitized = util.sanitizeShellString(url).toLowerCase();
|
38 | let result = {
|
39 | url: urlSanitized,
|
40 | ok: false,
|
41 | status: 404,
|
42 | ms: -1
|
43 | };
|
44 | if (urlSanitized) {
|
45 | let t = Date.now();
|
46 | if (_linux || _freebsd || _openbsd || _netbsd || _darwin || _sunos) {
|
47 | let args = ' -I --connect-timeout 5 -m 5 ' + urlSanitized + ' 2>/dev/null | head -n 1 | cut -d " " -f2';
|
48 | let cmd = 'curl';
|
49 | exec(cmd + args, function (error, stdout) {
|
50 | let statusCode = parseInt(stdout.toString());
|
51 | result.status = statusCode || 404;
|
52 | result.ok = !error && (statusCode === 200 || statusCode === 301 || statusCode === 302 || statusCode === 304);
|
53 | result.ms = (result.ok ? Date.now() - t : -1);
|
54 | if (callback) { callback(result); }
|
55 | resolve(result);
|
56 | });
|
57 | }
|
58 | if (_windows) {
|
59 | const http = (urlSanitized.startsWith('https:') ? require('https') : require('http'));
|
60 | try {
|
61 | http.get(urlSanitized, (res) => {
|
62 | const statusCode = res.statusCode;
|
63 |
|
64 | result.status = statusCode || 404;
|
65 | result.ok = (statusCode === 200 || statusCode === 301 || statusCode === 302 || statusCode === 304);
|
66 |
|
67 | if (statusCode !== 200) {
|
68 | res.resume();
|
69 | result.ms = (result.ok ? Date.now() - t : -1);
|
70 | if (callback) { callback(result); }
|
71 | resolve(result);
|
72 | } else {
|
73 | res.on('data', () => { });
|
74 | res.on('end', () => {
|
75 | result.ms = (result.ok ? Date.now() - t : -1);
|
76 | if (callback) { callback(result); }
|
77 | resolve(result);
|
78 | });
|
79 | }
|
80 | }).on('error', () => {
|
81 | if (callback) { callback(result); }
|
82 | resolve(result);
|
83 | });
|
84 | } catch (err) {
|
85 | if (callback) { callback(result); }
|
86 | resolve(result);
|
87 | }
|
88 | }
|
89 | } else {
|
90 | if (callback) { callback(result); }
|
91 | resolve(result);
|
92 | }
|
93 | });
|
94 | });
|
95 | }
|
96 |
|
97 | exports.inetChecksite = inetChecksite;
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | function inetLatency(host, callback) {
|
103 |
|
104 |
|
105 | if (util.isFunction(host) && !callback) {
|
106 | callback = host;
|
107 | host = '';
|
108 | }
|
109 |
|
110 | host = host || '8.8.8.8';
|
111 | const hostSanitized = util.sanitizeShellString(host);
|
112 |
|
113 | return new Promise((resolve) => {
|
114 | process.nextTick(() => {
|
115 | let cmd;
|
116 | if (_linux || _freebsd || _openbsd || _netbsd || _darwin) {
|
117 | if (_linux) {
|
118 | cmd = 'ping -c 2 -w 3 ' + hostSanitized + ' | grep rtt';
|
119 | }
|
120 | if (_freebsd || _openbsd || _netbsd) {
|
121 | cmd = 'ping -c 2 -t 3 ' + hostSanitized + ' | grep round-trip';
|
122 | }
|
123 | if (_darwin) {
|
124 | cmd = 'ping -c 2 -t 3 ' + hostSanitized + ' | grep avg';
|
125 | }
|
126 |
|
127 | exec(cmd, function (error, stdout) {
|
128 | let result = -1;
|
129 | if (!error) {
|
130 | const line = stdout.toString().split('=');
|
131 | if (line.length > 1) {
|
132 | const parts = line[1].split('/');
|
133 | if (parts.length > 1) {
|
134 | result = parseFloat(parts[1]);
|
135 | }
|
136 | }
|
137 | }
|
138 | if (callback) { callback(result); }
|
139 | resolve(result);
|
140 | });
|
141 | }
|
142 | if (_sunos) {
|
143 | exec('ping -s -a ' + hostSanitized + ' 56 2 | grep avg', { timeout: 3000 }, function (error, stdout) {
|
144 | let result = -1;
|
145 | if (!error) {
|
146 | const line = stdout.toString().split('=');
|
147 | if (line.length > 1) {
|
148 | const parts = line[1].split('/');
|
149 | if (parts.length > 1) {
|
150 | result = parseFloat(parts[1].replace(',', '.'));
|
151 | }
|
152 | }
|
153 | }
|
154 | if (callback) { callback(result); }
|
155 | resolve(result);
|
156 | });
|
157 | }
|
158 | if (_windows) {
|
159 | let result = -1;
|
160 | try {
|
161 | exec('ping ' + hostSanitized + ' -n 1', util.execOptsWin, function (error, stdout) {
|
162 | if (!error) {
|
163 | let lines = stdout.toString().split('\r\n');
|
164 | lines.shift();
|
165 | lines.forEach(function (line) {
|
166 | if ((line.toLowerCase().match(/ms/g) || []).length === 3) {
|
167 | let l = line.replace(/ +/g, ' ').split(' ');
|
168 | if (l.length > 6) {
|
169 | result = parseFloat(l[l.length - 1]);
|
170 | }
|
171 | }
|
172 | });
|
173 | }
|
174 | if (callback) { callback(result); }
|
175 | resolve(result);
|
176 | });
|
177 | } catch (e) {
|
178 | if (callback) { callback(result); }
|
179 | resolve(result);
|
180 | }
|
181 | }
|
182 | });
|
183 | });
|
184 | }
|
185 |
|
186 | exports.inetLatency = inetLatency;
|