From eb1f3a0ced982d16d0d77eef0cb9b8b1a0fc54e0 Mon Sep 17 00:00:00 2001 From: Remco Ros Date: Fri, 10 Jan 2025 00:40:41 +0100 Subject: [PATCH] sdk: checkPortListening: check tcp6/udp6 ports (#2763) * sdk: checkPortListening: check tcp6/udp6 ports * allow ipv6 if unspecified address --------- Co-authored-by: Aiden McClelland --- .../lib/health/checkFns/checkPortListening.ts | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/sdk/package/lib/health/checkFns/checkPortListening.ts b/sdk/package/lib/health/checkFns/checkPortListening.ts index 0d6792b86..59cd9717f 100644 --- a/sdk/package/lib/health/checkFns/checkPortListening.ts +++ b/sdk/package/lib/health/checkFns/checkPortListening.ts @@ -6,16 +6,17 @@ import * as CP from "node:child_process" const cpExec = promisify(CP.exec) -export function containsAddress(x: string, port: number) { +export function containsAddress(x: string, port: number, address?: bigint) { const readPorts = x .split("\n") .filter(Boolean) .splice(1) - .map((x) => x.split(" ").filter(Boolean)[1]?.split(":")?.[1]) - .filter(Boolean) - .map((x) => Number.parseInt(x, 16)) - .filter(Number.isFinite) - return readPorts.indexOf(port) >= 0 + .map((x) => x.split(" ").filter(Boolean)[1]?.split(":")) + .filter((x) => x?.length > 1) + .map(([addr, p]) => [BigInt(`0x${addr}`), Number.parseInt(p, 16)] as const) + return !!readPorts.find( + ([addr, p]) => (address === undefined || address === addr) && port === p, + ) } /** @@ -39,9 +40,19 @@ export async function checkPortListening( await cpExec(`cat /proc/net/tcp`, {}).then(stringFromStdErrOut), port, ) || + containsAddress( + await cpExec(`cat /proc/net/tcp6`, {}).then(stringFromStdErrOut), + port, + BigInt(0), + ) || containsAddress( await cpExec("cat /proc/net/udp", {}).then(stringFromStdErrOut), port, + ) || + containsAddress( + await cpExec("cat /proc/net/udp6", {}).then(stringFromStdErrOut), + port, + BigInt(0), ) if (hasAddress) { return { result: "success", message: options.successMessage }