






















@@ -558,9 +558,10 @@ async function resolveWindowsCommandLine(pid: number): Promise<string | undefine
558558return undefined;
559559}
560560561-async function readWindowsListeners(
561+async function readWindowsNetstatEntries<T extends PortListener>(
562562port: number,
563-): Promise<{ listeners: PortListener[]; detail?: string; errors: string[] }> {
563+parse: (output: string, port: number) => T[],
564+): Promise<{ entries: T[]; detail?: string; errors: string[] }> {
564565const errors: string[] = [];
565566const res = await runCommandSafe([getWindowsSystem32ExePath("netstat.exe"), "-ano", "-p", "tcp"]);
566567if (res.code !== 0) {
@@ -571,63 +572,42 @@ async function readWindowsListeners(
571572if (detail) {
572573errors.push(detail);
573574}
574-return { listeners: [], errors };
575+return { entries: [], errors };
575576}
576-const listeners = parseNetstatListeners(res.stdout, port);
577+578+const entries = parse(res.stdout, port);
577579await Promise.all(
578-listeners.map(async (listener) => {
579-if (!listener.pid) {
580+entries.map(async (entry) => {
581+if (!entry.pid) {
580582return;
581583}
582584const [imageName, commandLine] = await Promise.all([
583-resolveWindowsImageName(listener.pid),
584-resolveWindowsCommandLine(listener.pid),
585+resolveWindowsImageName(entry.pid),
586+resolveWindowsCommandLine(entry.pid),
585587]);
586588if (imageName) {
587-listener.command = imageName;
589+entry.command = imageName;
588590}
589591if (commandLine) {
590-listener.commandLine = commandLine;
592+entry.commandLine = commandLine;
591593}
592594}),
593595);
594-return { listeners, detail: res.stdout.trim() || undefined, errors };
596+return { entries, detail: res.stdout.trim() || undefined, errors };
597+}
598+599+async function readWindowsListeners(
600+port: number,
601+): Promise<{ listeners: PortListener[]; detail?: string; errors: string[] }> {
602+const result = await readWindowsNetstatEntries(port, parseNetstatListeners);
603+return { listeners: result.entries, detail: result.detail, errors: result.errors };
595604}
596605597606async function readWindowsEstablishedConnections(
598607port: number,
599608): Promise<{ connections: PortConnection[]; detail?: string; errors: string[] }> {
600-const errors: string[] = [];
601-const res = await runCommandSafe([getWindowsSystem32ExePath("netstat.exe"), "-ano", "-p", "tcp"]);
602-if (res.code !== 0) {
603-if (res.error) {
604-errors.push(res.error);
605-}
606-const detail = [res.stderr.trim(), res.stdout.trim()].filter(Boolean).join("\n");
607-if (detail) {
608-errors.push(detail);
609-}
610-return { connections: [], errors };
611-}
612-const connections = parseNetstatConnections(res.stdout, port);
613-await Promise.all(
614-connections.map(async (connection) => {
615-if (!connection.pid) {
616-return;
617-}
618-const [imageName, commandLine] = await Promise.all([
619-resolveWindowsImageName(connection.pid),
620-resolveWindowsCommandLine(connection.pid),
621-]);
622-if (imageName) {
623-connection.command = imageName;
624-}
625-if (commandLine) {
626-connection.commandLine = commandLine;
627-}
628-}),
629-);
630-return { connections, detail: res.stdout.trim() || undefined, errors };
609+const result = await readWindowsNetstatEntries(port, parseNetstatConnections);
610+return { connections: result.entries, detail: result.detail, errors: result.errors };
631611}
632612633613async function tryListenOnHost(port: number, host: string): Promise<PortUsageStatus | "skip"> {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。