From 0e02b7c7eadb102fc84f405108b60a08589f2328 Mon Sep 17 00:00:00 2001 From: Mirek Laus Date: Thu, 26 Sep 2019 14:14:17 +0200 Subject: [PATCH] Fix ping on FreeBSD --- lib/nbi.ts | 8 +++++++- lib/ui/api-functions.ts | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/nbi.ts b/lib/nbi.ts index 93a6742..4189f84 100644 --- a/lib/nbi.ts +++ b/lib/nbi.ts @@ -28,6 +28,7 @@ import * as query from "./query"; import * as apiFunctions from "./api-functions"; import * as cache from "./cache"; import { version as VERSION } from "../package.json"; +import * as os from "os"; const DEVICE_TASKS_REGEX = /^\/devices\/([a-zA-Z0-9\-_%]+)\/tasks\/?$/; const TASKS_REGEX = /^\/tasks\/([a-zA-Z0-9\-_%]+)(\/[a-zA-Z_]*)?$/; @@ -42,6 +43,10 @@ const PROVISIONS_REGEX = /^\/provisions\/([a-zA-Z0-9\-_%]+)\/?$/; const VIRTUAL_PARAMETERS_REGEX = /^\/virtual_parameters\/([a-zA-Z0-9\-_%]+)\/?$/; const FAULTS_REGEX = /^\/faults\/([a-zA-Z0-9\-_%:]+)\/?$/; +function pingVariant(): string { + return os.platform() === "freebsd" ? "-t 1 -c 3" : "-w 1 -i 0.2 -c 3"; +} + function throwError(err, httpResponse?): never { if (httpResponse) { httpResponse.writeHead(500, { Connection: "close" }); @@ -591,7 +596,8 @@ export function listener(request, response): void { } } else if (PING_REGEX.test(urlParts.pathname)) { const host = querystring.unescape(PING_REGEX.exec(urlParts.pathname)[1]); - exec(`ping -w 1 -i 0.2 -c 3 ${host}`, (err, stdout) => { + const pingArgs = pingVariant(); + exec(`ping ${pingArgs} ${host}`, (err, stdout) => { if (err) { response.writeHead(404, { "Cache-Control": "no-cache" }); response.end(`${err.name}: ${err.message}`); diff --git a/lib/ui/api-functions.ts b/lib/ui/api-functions.ts index 92c461f..1383f44 100644 --- a/lib/ui/api-functions.ts +++ b/lib/ui/api-functions.ts @@ -29,6 +29,17 @@ import { connectionRequest, deleteDevice } from "../api-functions"; +import * as os from "os"; + +function pingVariant(): string { + return os.platform() === "freebsd" ? "-t 1 -c 3" : "-w 1 -i 0.2 -c 3"; +} + +function pingOutRegex(): RegExp { + return os.platform() === "freebsd" + ? /(\d) packets transmitted, (\d) packets received, ([\d.%]+) packet loss\nround-trip min\/avg\/max\/stddev = ([\d.]+)\/([\d.]+)\/([\d.]+)\/([\d.]+) ms/ + : /(\d) packets transmitted, (\d) received, ([\d.%]+) packet loss[^]*([\d.]+)\/([\d.]+)\/([\d.]+)\/([\d.]+)/; +} async function deleteFault(id): Promise { const deviceId = id.split(":", 1)[0]; @@ -151,12 +162,11 @@ interface PingResponse { export function ping(host): Promise { return new Promise((resolve, reject) => { - exec(`ping -w 1 -i 0.2 -c 3 ${host}`, (err, stdout) => { + const pingArgs = pingVariant(); + exec(`ping ${pingArgs} ${host}`, (err, stdout) => { if (err) return void reject(err); - const m = stdout.match( - /(\d) packets transmitted, (\d) received, ([\d.%]+) packet loss[^]*([\d.]+)\/([\d.]+)\/([\d.]+)\/([\d.]+)/ - ); + const m = stdout.match(pingOutRegex()); if (!m) return void reject(new Error("Could not parse ping response")); resolve({