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 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}`);