From 2a4695a774414ac563bd758750c9b42b2c9dd941 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Tue, 10 Aug 2021 20:39:58 +0800 Subject: [PATCH] add -6 to ping cmd if ipv6 address --- server/ping-lite.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/server/ping-lite.js b/server/ping-lite.js index 0b9a7401a..4a8cc2419 100644 --- a/server/ping-lite.js +++ b/server/ping-lite.js @@ -1,7 +1,7 @@ // https://github.com/ben-bradley/ping-lite/blob/master/ping-lite.js // Fixed on Windows - -let spawn = require("child_process").spawn, +const net = require("net"); +const spawn = require("child_process").spawn, events = require("events"), fs = require("fs"), WIN = /^win/.test(process.platform), @@ -24,14 +24,24 @@ function Ping(host, options) { this._bin = "c:/windows/system32/ping.exe"; this._args = (options.args) ? options.args : [ "-n", "1", "-w", "5000", host ]; this._regmatch = /[><=]([0-9.]+?)ms/; + } else if (LIN) { this._bin = "/bin/ping"; - this._args = (options.args) ? options.args : [ "-n", "-w", "2", "-c", "1", host ]; - this._regmatch = /=([0-9.]+?) ms/; // need to verify this + + const defaultArgs = [ "-n", "-w", "2", "-c", "1", host ]; + + if (net.isIPv6(host)) { + defaultArgs.unshift("-6"); + } + + this._args = (options.args) ? options.args : defaultArgs; + this._regmatch = /=([0-9.]+?) ms/; + } else if (MAC) { this._bin = "/sbin/ping"; this._args = (options.args) ? options.args : [ "-n", "-t", "2", "-c", "1", host ]; this._regmatch = /=([0-9.]+?) ms/; + } else { throw new Error("Could not detect your ping binary."); }