uptime-kuma/server/ping-lite.js

200 lines
5 KiB
JavaScript
Raw Normal View History

2021-07-01 12:00:23 +03:00
// https://github.com/ben-bradley/ping-lite/blob/master/ping-lite.js
// Fixed on Windows
2021-08-10 15:39:58 +03:00
const net = require("net");
const spawn = require("child_process").spawn;
const events = require("events");
const fs = require("fs");
const util = require("./util-server");
2021-07-01 12:00:23 +03:00
module.exports = Ping;
/**
* Constructor for ping class
* @param {string} host Host to ping
* @param {object} [options] Options for the ping command
2021-11-10 08:24:31 +03:00
* @param {array|string} [options.args] - Arguments to pass to the ping command
*/
2021-08-10 17:00:29 +03:00
function Ping(host, options) {
2021-07-27 20:47:13 +03:00
if (!host) {
throw new Error("You must specify a host to ping!");
}
2021-07-01 12:00:23 +03:00
this._host = host;
this._options = options = (options || {});
events.EventEmitter.call(this);
const timeout = 10;
2021-10-14 13:48:40 +03:00
if (util.WIN) {
2021-07-27 20:47:13 +03:00
this._bin = "c:/windows/system32/ping.exe";
this._args = (options.args) ? options.args : [ "-n", "1", "-w", timeout * 1000, host ];
2021-07-01 12:00:23 +03:00
this._regmatch = /[><=]([0-9.]+?)ms/;
2021-08-10 15:39:58 +03:00
2021-10-14 13:48:40 +03:00
} else if (util.LIN) {
2021-07-27 20:47:13 +03:00
this._bin = "/bin/ping";
2021-08-10 15:39:58 +03:00
const defaultArgs = [ "-n", "-w", timeout, "-c", "1", host ];
2021-08-10 15:39:58 +03:00
if (net.isIPv6(host) || options.ipv6) {
2021-08-10 15:39:58 +03:00
defaultArgs.unshift("-6");
}
this._args = (options.args) ? options.args : defaultArgs;
this._regmatch = /=([0-9.]+?) ms/;
2021-10-14 13:48:40 +03:00
} else if (util.MAC) {
2021-08-10 16:07:11 +03:00
if (net.isIPv6(host) || options.ipv6) {
this._bin = "/sbin/ping6";
} else {
this._bin = "/sbin/ping";
}
this._args = (options.args) ? options.args : [ "-n", "-t", timeout, "-c", "1", host ];
2021-07-01 12:00:23 +03:00
this._regmatch = /=([0-9.]+?) ms/;
2022-01-09 19:27:24 +03:00
} else if (util.BSD) {
2021-08-17 05:48:37 +03:00
this._bin = "/sbin/ping";
const defaultArgs = [ "-n", "-t", timeout, "-c", "1", host ];
2021-08-17 05:48:37 +03:00
if (net.isIPv6(host) || options.ipv6) {
defaultArgs.unshift("-6");
}
this._args = (options.args) ? options.args : defaultArgs;
this._regmatch = /=([0-9.]+?) ms/;
2021-08-10 15:39:58 +03:00
2021-07-27 20:47:13 +03:00
} else {
throw new Error("Could not detect your ping binary.");
2021-07-01 12:00:23 +03:00
}
2021-07-27 20:47:13 +03:00
if (!fs.existsSync(this._bin)) {
throw new Error("Could not detect " + this._bin + " on your system");
}
2021-07-01 12:00:23 +03:00
this._i = 0;
return this;
}
Ping.prototype.__proto__ = events.EventEmitter.prototype;
/**
* Callback for send
* @callback pingCB
* @param {any} err Any error encountered
* @param {number} ms Ping time in ms
*/
/**
* Send a ping
* @param {pingCB} callback Callback to call with results
*/
2021-08-10 16:47:14 +03:00
Ping.prototype.send = function (callback) {
2021-07-27 20:47:13 +03:00
let self = this;
2021-08-10 16:47:14 +03:00
callback = callback || function (err, ms) {
2021-07-27 20:47:13 +03:00
if (err) {
return self.emit("error", err);
}
return self.emit("result", ms);
2021-07-01 12:00:23 +03:00
};
let _ended;
let _exited;
let _errored;
2021-07-01 12:00:23 +03:00
this._ping = spawn(this._bin, this._args, { windowsHide: true }); // spawn the binary
2021-07-01 12:00:23 +03:00
2021-08-10 16:47:14 +03:00
this._ping.on("error", function (err) { // handle binary errors
2021-07-01 12:00:23 +03:00
_errored = true;
callback(err);
});
2021-08-10 16:47:14 +03:00
this._ping.stdout.on("data", function (data) { // log stdout
2021-10-14 13:48:40 +03:00
if (util.WIN) {
data = convertOutput(data);
}
2021-07-27 20:47:13 +03:00
this._stdout = (this._stdout || "") + data;
2021-07-01 12:00:23 +03:00
});
2021-08-10 16:47:14 +03:00
this._ping.stdout.on("end", function () {
2021-07-01 12:00:23 +03:00
_ended = true;
2021-07-27 20:47:13 +03:00
if (_exited && !_errored) {
onEnd.call(self._ping);
}
2021-07-01 12:00:23 +03:00
});
2021-08-10 16:47:14 +03:00
this._ping.stderr.on("data", function (data) { // log stderr
2021-10-14 13:48:40 +03:00
if (util.WIN) {
data = convertOutput(data);
}
2021-07-27 20:47:13 +03:00
this._stderr = (this._stderr || "") + data;
2021-07-01 12:00:23 +03:00
});
2021-08-10 16:47:14 +03:00
this._ping.on("exit", function (code) { // handle complete
2021-07-01 12:00:23 +03:00
_exited = true;
2021-07-27 20:47:13 +03:00
if (_ended && !_errored) {
onEnd.call(self._ping);
}
2021-07-01 12:00:23 +03:00
});
2021-11-10 08:24:31 +03:00
/**
* @param {Function} callback
*
* Generated by Trelent
*/
2021-08-10 17:00:29 +03:00
function onEnd() {
let stdout = this.stdout._stdout;
let stderr = this.stderr._stderr;
let ms;
2021-07-01 12:00:23 +03:00
2021-07-27 20:47:13 +03:00
if (stderr) {
2021-07-01 12:00:23 +03:00
return callback(new Error(stderr));
2021-07-27 20:47:13 +03:00
}
2021-07-27 21:02:20 +03:00
2021-07-27 20:47:13 +03:00
if (!stdout) {
return callback(new Error("No stdout detected"));
}
2021-07-01 12:00:23 +03:00
ms = stdout.match(self._regmatch); // parse out the ##ms response
ms = (ms && ms[1]) ? Number(ms[1]) : ms;
callback(null, ms, stdout);
2021-07-01 12:00:23 +03:00
}
};
/**
* Ping every interval
* @param {pingCB} callback Callback to call with results
*/
2021-08-10 16:47:14 +03:00
Ping.prototype.start = function (callback) {
2021-07-27 20:47:13 +03:00
let self = this;
2021-08-10 16:47:14 +03:00
this._i = setInterval(function () {
2021-07-01 12:00:23 +03:00
self.send(callback);
}, (self._options.interval || 5000));
self.send(callback);
};
/** Stop sending pings */
2021-08-10 16:47:14 +03:00
Ping.prototype.stop = function () {
2021-07-01 12:00:23 +03:00
clearInterval(this._i);
};
/**
* Try to convert to UTF-8 for Windows, as the ping's output on Windows is not UTF-8 and could be in other languages
* Thank @pemassi
* https://github.com/louislam/uptime-kuma/issues/570#issuecomment-941984094
* @param {any} data
* @returns {string}
*/
function convertOutput(data) {
2021-10-14 13:48:40 +03:00
if (util.WIN) {
if (data) {
return util.convertToUTF8(data);
}
}
return data;
}