retry ping domain with ipv6, if domain is not found

This commit is contained in:
LouisLam
2021-08-10 21:03:14 +08:00
parent 2a4695a774
commit 4d4d504d6e
2 changed files with 23 additions and 8 deletions

View File

@@ -45,15 +45,30 @@ exports.tcping = function (hostname, port) {
});
}
exports.ping = function (hostname) {
return new Promise((resolve, reject) => {
const ping = new Ping(hostname);
exports.ping = async (hostname) => {
try {
await exports.pingAsync(hostname);
} catch (e) {
// If the host cannot be resolved, try again with ipv6
if (e.message.includes("service not known")) {
await exports.pingAsync(hostname, true);
} else {
throw e;
}
}
}
ping.send(function (err, ms) {
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)
reject(err);
} else if (ms === null) {
reject(new Error("timeout"))
reject(new Error(stdout))
} else {
resolve(Math.round(ms))
}