From 4d4d504d6e099268477adf07de08d691db1adc83 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Tue, 10 Aug 2021 21:03:14 +0800 Subject: [PATCH] retry ping domain with ipv6, if domain is not found --- server/ping-lite.js | 4 ++-- server/util-server.js | 27 +++++++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/server/ping-lite.js b/server/ping-lite.js index 4a8cc2419..9f350cac7 100644 --- a/server/ping-lite.js +++ b/server/ping-lite.js @@ -30,7 +30,7 @@ function Ping(host, options) { const defaultArgs = [ "-n", "-w", "2", "-c", "1", host ]; - if (net.isIPv6(host)) { + if (net.isIPv6(host) || options.ipv6) { defaultArgs.unshift("-6"); } @@ -115,7 +115,7 @@ Ping.prototype.send = function(callback) { ms = stdout.match(self._regmatch); // parse out the ##ms response ms = (ms && ms[1]) ? Number(ms[1]) : ms; - callback(null, ms); + callback(null, ms, stdout); } }; diff --git a/server/util-server.js b/server/util-server.js index 1baec2a0f..e52e20881 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -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)) }