Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Change ping module to danielzzz/node-ping #2223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 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