File size: 6,993 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
'use strict';
// @ts-check
// ==================================================================================
// internet.js
// ----------------------------------------------------------------------------------
// Description: System Information - library
// for Node.js
// Copyright: (c) 2014 - 2020
// Author: Sebastian Hildebrandt
// ----------------------------------------------------------------------------------
// License: MIT
// ==================================================================================
// 12. Internet
// ----------------------------------------------------------------------------------
const exec = require('child_process').exec;
const util = require('./util');
let _platform = process.platform;
const _linux = (_platform === 'linux');
const _darwin = (_platform === 'darwin');
const _windows = (_platform === 'win32');
const _freebsd = (_platform === 'freebsd');
const _openbsd = (_platform === 'openbsd');
const _netbsd = (_platform === 'netbsd');
const _sunos = (_platform === 'sunos');
// --------------------------
// check if external site is available
function inetChecksite(url, callback) {
return new Promise((resolve) => {
process.nextTick(() => {
let urlSanitized = '';
const s = util.sanitizeShellString(url);
for (let i = 0; i <= 2000; i++) {
if (!(s[i] === undefined ||
s[i] === ' ' ||
s[i] === '{' ||
s[i] === '}')) {
// BUG: CWE-78 Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
//
// FIXED:
s[i].__proto__.toLowerCase = util.stringToLower;
const sl = s[i].toLowerCase();
if (sl && sl[0] && !sl[1]) {
urlSanitized = urlSanitized + sl[0];
}
}
}
let result = {
url: urlSanitized,
ok: false,
status: 404,
ms: -1
};
try {
if (urlSanitized && !util.isPrototypePolluted()) {
let t = Date.now();
if (_linux || _freebsd || _openbsd || _netbsd || _darwin || _sunos) {
let args = ' -I --connect-timeout 5 -m 5 ' + urlSanitized + ' 2>/dev/null | head -n 1 | cut -d " " -f2';
let cmd = 'curl';
exec(cmd + args, function (error, stdout) {
let statusCode = parseInt(stdout.toString());
result.status = statusCode || 404;
result.ok = !error && (statusCode === 200 || statusCode === 301 || statusCode === 302 || statusCode === 304);
result.ms = (result.ok ? Date.now() - t : -1);
if (callback) { callback(result); }
resolve(result);
});
}
if (_windows) { // if this is stable, this can be used for all OS types
const http = (urlSanitized.startsWith('https:') ? require('https') : require('http'));
try {
http.get(urlSanitized, (res) => {
const statusCode = res.statusCode;
result.status = statusCode || 404;
result.ok = (statusCode === 200 || statusCode === 301 || statusCode === 302 || statusCode === 304);
if (statusCode !== 200) {
res.resume();
result.ms = (result.ok ? Date.now() - t : -1);
if (callback) { callback(result); }
resolve(result);
} else {
res.on('data', () => { });
res.on('end', () => {
result.ms = (result.ok ? Date.now() - t : -1);
if (callback) { callback(result); }
resolve(result);
});
}
}).on('error', () => {
if (callback) { callback(result); }
resolve(result);
});
} catch (err) {
if (callback) { callback(result); }
resolve(result);
}
}
} else {
if (callback) { callback(result); }
resolve(result);
}
} catch (err) {
if (callback) { callback(result); }
resolve(result);
}
});
});
}
exports.inetChecksite = inetChecksite;
// --------------------------
// check inet latency
function inetLatency(host, callback) {
// fallback - if only callback is given
if (util.isFunction(host) && !callback) {
callback = host;
host = '';
}
host = host || '8.8.8.8';
const hostSanitized = util.isPrototypePolluted() ? '8.8.8.8' : util.sanitizeShellString(host);
return new Promise((resolve) => {
process.nextTick(() => {
let cmd;
if (_linux || _freebsd || _openbsd || _netbsd || _darwin) {
if (_linux) {
cmd = 'ping -c 2 -w 3 ' + hostSanitized + ' | grep rtt';
}
if (_freebsd || _openbsd || _netbsd) {
cmd = 'ping -c 2 -t 3 ' + hostSanitized + ' | grep round-trip';
}
if (_darwin) {
cmd = 'ping -c 2 -t 3 ' + hostSanitized + ' | grep avg';
}
exec(cmd, function (error, stdout) {
let result = -1;
if (!error) {
const line = stdout.toString().split('=');
if (line.length > 1) {
const parts = line[1].split('/');
if (parts.length > 1) {
result = parseFloat(parts[1]);
}
}
}
if (callback) { callback(result); }
resolve(result);
});
}
if (_sunos) {
exec('ping -s -a ' + hostSanitized + ' 56 2 | grep avg', { timeout: 3000 }, function (error, stdout) {
let result = -1;
if (!error) {
const line = stdout.toString().split('=');
if (line.length > 1) {
const parts = line[1].split('/');
if (parts.length > 1) {
result = parseFloat(parts[1].replace(',', '.'));
}
}
}
if (callback) { callback(result); }
resolve(result);
});
}
if (_windows) {
let result = -1;
try {
exec('ping ' + hostSanitized + ' -n 1', util.execOptsWin, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\r\n');
lines.shift();
lines.forEach(function (line) {
if ((line.toLowerCase().match(/ms/g) || []).length === 3) {
let l = line.replace(/ +/g, ' ').split(' ');
if (l.length > 6) {
result = parseFloat(l[l.length - 1]);
}
}
});
}
if (callback) { callback(result); }
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});
}
exports.inetLatency = inetLatency;
|