Skip to content

Commit

Permalink
sdk: checkPortListening: check tcp6/udp6 ports (#2763)
Browse files Browse the repository at this point in the history
* sdk: checkPortListening: check tcp6/udp6 ports

* allow ipv6 if unspecified address

---------

Co-authored-by: Aiden McClelland <[email protected]>
  • Loading branch information
remcoros and dr-bonez authored Jan 9, 2025
1 parent 29e8210 commit eb1f3a0
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions sdk/package/lib/health/checkFns/checkPortListening.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

/**
Expand All @@ -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 }
Expand Down

0 comments on commit eb1f3a0

Please sign in to comment.