Fix ping on FreeBSD

This commit is contained in:
Mirek Laus authored and Zaid Abdulla committed 2019-10-21 22:58:12 -07:00
1 parent f966194e22
commit 0e02b7c7ea
2 files changed
+21 -5

No files matched your search

+7 -1
View File
@@ -28,6 +28,7 @@ import * as query from "./query";
import * as apiFunctions from "./api-functions"; import * as apiFunctions from "./api-functions";
import * as cache from "./cache"; import * as cache from "./cache";
import { version as VERSION } from "../package.json"; import { version as VERSION } from "../package.json";
import * as os from "os";
const DEVICE_TASKS_REGEX = /^\/devices\/([a-zA-Z0-9\-_%]+)\/tasks\/?$/; const DEVICE_TASKS_REGEX = /^\/devices\/([a-zA-Z0-9\-_%]+)\/tasks\/?$/;
const TASKS_REGEX = /^\/tasks\/([a-zA-Z0-9\-_%]+)(\/[a-zA-Z_]*)?$/; 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 VIRTUAL_PARAMETERS_REGEX = /^\/virtual_parameters\/([a-zA-Z0-9\-_%]+)\/?$/;
const FAULTS_REGEX = /^\/faults\/([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 { function throwError(err, httpResponse?): never {
if (httpResponse) { if (httpResponse) {
httpResponse.writeHead(500, { Connection: "close" }); httpResponse.writeHead(500, { Connection: "close" });
@@ -591,7 +596,8 @@ export function listener(request, response): void {
} }
} else if (PING_REGEX.test(urlParts.pathname)) { } else if (PING_REGEX.test(urlParts.pathname)) {
const host = querystring.unescape(PING_REGEX.exec(urlParts.pathname)[1]); 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) { if (err) {
response.writeHead(404, { "Cache-Control": "no-cache" }); response.writeHead(404, { "Cache-Control": "no-cache" });
response.end(`${err.name}: ${err.message}`); response.end(`${err.name}: ${err.message}`);
+14 -4
View File
@@ -29,6 +29,17 @@ import {
connectionRequest, connectionRequest,
deleteDevice deleteDevice
} from "../api-functions"; } 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<void> { async function deleteFault(id): Promise<void> {
const deviceId = id.split(":", 1)[0]; const deviceId = id.split(":", 1)[0];
@@ -151,12 +162,11 @@ interface PingResponse {
export function ping(host): Promise<PingResponse> { export function ping(host): Promise<PingResponse> {
return new Promise((resolve, reject) => { 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); if (err) return void reject(err);
const m = stdout.match( const m = stdout.match(pingOutRegex());
/(\d) packets transmitted, (\d) received, ([\d.%]+) packet loss[^]*([\d.]+)\/([\d.]+)\/([\d.]+)\/([\d.]+)/
);
if (!m) return void reject(new Error("Could not parse ping response")); if (!m) return void reject(new Error("Could not parse ping response"));
resolve({ resolve({