Skip to content

Commit

Permalink
Merge pull request #2223 from Computroniks/feature/remove-hardcoded-p…
Browse files Browse the repository at this point in the history
…ing-path

feat: Change ping module to danielzzz/node-ping
  • Loading branch information
louislam authored Jan 5, 2023
2 parents 4c5e2ea + 0b95951 commit fbceefe
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 216 deletions.
46 changes: 46 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"password-hash": "~1.2.2",
"pg": "~8.8.0",
"pg-connection-string": "~2.5.0",
"ping": "~0.4.2",
"prom-client": "~13.2.0",
"prometheus-api-metrics": "~3.2.1",
"protobufjs": "~7.1.1",
Expand Down
199 changes: 0 additions & 199 deletions server/ping-lite.js

This file was deleted.

34 changes: 17 additions & 17 deletions server/util-server.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -26,12 +26,7 @@ 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);
const isWindows = process.platform === /^win/.test(process.platform);

/**
* Init or reset JWT secret
Expand Down Expand Up @@ -105,18 +100,23 @@ 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: 1,
timeout: 10,
}).then((res) => {
// If ping failed, it will set field to unknown
if (res.alive) {
resolve(res.time);
} else {
resolve(Math.round(ms));
if (isWindows) {
reject(new Error(exports.convertToUTF8(res.output)));
} else {
reject(new Error(res.output));
}
}
}).catch((err) => {
reject(err);
});
});
};
Expand Down

0 comments on commit fbceefe

Please sign in to comment.