fix(gateway): add Windows-compatible port detection using netstat fallback (openclaw#29239) thanks @ajay99511

Verified:
- pnpm vitest src/cli/program.force.test.ts
- pnpm check
- pnpm build

Co-authored-by: ajay99511 <73169130+ajay99511@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
This commit is contained in:
Ajay Elika
2026-03-02 07:33:59 -07:00
committed by GitHub
parent d145518f94
commit e23b6fb2ba
2 changed files with 96 additions and 0 deletions

View File

@@ -158,6 +158,32 @@ export function parseLsofOutput(output: string): PortProcess[] {
}
export function listPortListeners(port: number): PortProcess[] {
if (process.platform === "win32") {
try {
const out = execFileSync("netstat", ["-ano", "-p", "TCP"], { encoding: "utf-8" });
const lines = out.split(/\r?\n/).filter(Boolean);
const results: PortProcess[] = [];
for (const line of lines) {
const parts = line.trim().split(/\s+/);
if (parts.length >= 5 && parts[3] === "LISTENING") {
const localAddress = parts[1];
const addressPort = localAddress.split(":").pop();
if (addressPort === String(port)) {
const pid = Number.parseInt(parts[4], 10);
if (!Number.isNaN(pid) && pid > 0) {
if (!results.some((p) => p.pid === pid)) {
results.push({ pid });
}
}
}
}
}
return results;
} catch (err: unknown) {
throw new Error(`netstat failed: ${String(err)}`, { cause: err });
}
}
try {
const lsof = resolveLsofCommandSync();
const out = execFileSync(lsof, ["-nP", `-iTCP:${port}`, "-sTCP:LISTEN", "-FpFc"], {