Changed from ping-lite to ping module

Signed-off-by: Matthew Nickson <mnickson@sidingsmedia.com>

#Fixes 2126
This commit is contained in:
Matthew Nickson 2023-01-03 20:03:36 +00:00
parent 4819112e25
commit 73835f3328
No known key found for this signature in database
GPG key ID: BF229DCFD4748E05
4 changed files with 62 additions and 219 deletions

View file

@ -1,5 +1,5 @@
const tcpp = require("tcp-ping");
const Ping = require("./ping-lite");
const ping = require("ping");
const { R } = require("redbean-node");
const { log, genSecret } = require("../src/util");
const passwordHash = require("./password-hash");
@ -26,13 +26,6 @@ const {
} = require("node-radius-utils");
const dayjs = require("dayjs");
// From ping-lite
exports.WIN = /^win/.test(process.platform);
exports.LIN = /^linux/.test(process.platform);
exports.MAC = /^darwin/.test(process.platform);
exports.FBSD = /^freebsd/.test(process.platform);
exports.BSD = /bsd$/.test(process.platform);
/**
* Init or reset JWT secret
* @returns {Promise<Bean>}
@ -105,18 +98,20 @@ exports.ping = async (hostname) => {
*/
exports.pingAsync = function (hostname, ipv6 = false) {
return new Promise((resolve, reject) => {
const ping = new Ping(hostname, {
ipv6
});
ping.send(function (err, ms, stdout) {
if (err) {
reject(err);
} else if (ms === null) {
reject(new Error(stdout));
ping.promise.probe(hostname, {
v6: ipv6,
min_reply: 3
}).then((res) => {
// If ping failed, it will set field to unknown
if (res.host === "unknown") {
reject(new Error("Name or service not known"));
} else if (res.time === "unknown") {
reject(new Error(res.output));
} else {
resolve(Math.round(ms));
resolve(res.time);
}
}).catch((err) => {
reject(err);
});
});
};