mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 20:14:49 +00:00
sdk: checkPortListening: check tcp6/udp6 ports (#2763)
* sdk: checkPortListening: check tcp6/udp6 ports * allow ipv6 if unspecified address --------- Co-authored-by: Aiden McClelland <me@drbonez.dev>
This commit is contained in:
@@ -6,16 +6,17 @@ import * as CP from "node:child_process"
|
|||||||
|
|
||||||
const cpExec = promisify(CP.exec)
|
const cpExec = promisify(CP.exec)
|
||||||
|
|
||||||
export function containsAddress(x: string, port: number) {
|
export function containsAddress(x: string, port: number, address?: bigint) {
|
||||||
const readPorts = x
|
const readPorts = x
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.splice(1)
|
.splice(1)
|
||||||
.map((x) => x.split(" ").filter(Boolean)[1]?.split(":")?.[1])
|
.map((x) => x.split(" ").filter(Boolean)[1]?.split(":"))
|
||||||
.filter(Boolean)
|
.filter((x) => x?.length > 1)
|
||||||
.map((x) => Number.parseInt(x, 16))
|
.map(([addr, p]) => [BigInt(`0x${addr}`), Number.parseInt(p, 16)] as const)
|
||||||
.filter(Number.isFinite)
|
return !!readPorts.find(
|
||||||
return readPorts.indexOf(port) >= 0
|
([addr, p]) => (address === undefined || address === addr) && port === p,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,9 +40,19 @@ export async function checkPortListening(
|
|||||||
await cpExec(`cat /proc/net/tcp`, {}).then(stringFromStdErrOut),
|
await cpExec(`cat /proc/net/tcp`, {}).then(stringFromStdErrOut),
|
||||||
port,
|
port,
|
||||||
) ||
|
) ||
|
||||||
|
containsAddress(
|
||||||
|
await cpExec(`cat /proc/net/tcp6`, {}).then(stringFromStdErrOut),
|
||||||
|
port,
|
||||||
|
BigInt(0),
|
||||||
|
) ||
|
||||||
containsAddress(
|
containsAddress(
|
||||||
await cpExec("cat /proc/net/udp", {}).then(stringFromStdErrOut),
|
await cpExec("cat /proc/net/udp", {}).then(stringFromStdErrOut),
|
||||||
port,
|
port,
|
||||||
|
) ||
|
||||||
|
containsAddress(
|
||||||
|
await cpExec("cat /proc/net/udp6", {}).then(stringFromStdErrOut),
|
||||||
|
port,
|
||||||
|
BigInt(0),
|
||||||
)
|
)
|
||||||
if (hasAddress) {
|
if (hasAddress) {
|
||||||
return { result: "success", message: options.successMessage }
|
return { result: "success", message: options.successMessage }
|
||||||
|
|||||||
Reference in New Issue
Block a user