diff --git a/server/system-proxy.test.ts b/server/system-proxy.test.ts index 86dd4ab1b..37dfdecfa 100644 --- a/server/system-proxy.test.ts +++ b/server/system-proxy.test.ts @@ -106,7 +106,7 @@ describe("installSystemProxy", () => { it("sends the harness's own fetch through the proxy the computer's settings name", async () => { const proxy = await fakeProxy(); const parent = fakeParent(() => `PROXY 127.0.0.1:${proxy.port}; DIRECT`); - installSystemProxy(parent, {}); + await installSystemProxy(parent, {}); const response = await fetch("http://models.example.test/api.json"); expect(await response.text()).toBe("via proxy"); @@ -122,7 +122,7 @@ describe("installSystemProxy", () => { const proxy = await fakeProxy(); const target = await listen((_request, response) => response.end("direct")); const parent = fakeParent((request) => (request.url.includes("bypassed") ? "DIRECT" : `PROXY 127.0.0.1:${proxy.port}`)); - installSystemProxy(parent, {}); + await installSystemProxy(parent, {}); await fetch("http://models.example.test/a").then((response) => response.text()); await fetch("http://models.example.test/b").then((response) => response.text()); @@ -143,7 +143,7 @@ describe("installSystemProxy", () => { const own = await fakeProxy(); const target = await listen((_request, response) => response.end("direct")); const parent = fakeParent(() => `PROXY 127.0.0.1:${system.port}`); - installSystemProxy(parent, { HTTP_PROXY: `http://127.0.0.1:${own.port}` }); + await installSystemProxy(parent, { HTTP_PROXY: `http://127.0.0.1:${own.port}` }); expect(await (await fetch("http://models.example.test/")).text()).toBe("via proxy"); expect(await (await fetch(`http://127.0.0.1:${target.port}/`)).text()).toBe("direct"); diff --git a/server/system-proxy.ts b/server/system-proxy.ts index b3c328c6e..45f5cbe62 100644 --- a/server/system-proxy.ts +++ b/server/system-proxy.ts @@ -1,8 +1,10 @@ import { randomUUID } from "node:crypto"; -import { Agent, EnvHttpProxyAgent, ProxyAgent, Socks5ProxyAgent, setGlobalDispatcher, type Dispatcher } from "undici"; import { isIP } from "node:net"; +import type { Dispatcher } from "undici"; import { addressClass } from "../electron/egress-addresses.mjs"; +type Undici = typeof import("undici"); + /** asked of the desktop main process: `rule` is Chromium's `session.resolveProxy` answer for `url` (PAC syntax, * "PROXY host:port; DIRECT"), so the computer's own proxy settings, PAC script and WPAD apply; null when it has none */ export type ProxyResolveRequest = { type: "duskbot:resolve-proxy"; requestId: string; url: string }; @@ -63,12 +65,14 @@ export const envProxySet = (env: NodeJS.ProcessEnv) => PROXY_ENV.some((name) => * environment wins, otherwise each origin goes where Chromium's resolver says (cached for `ttlMs`). Engines, MCP * servers and the bot box are separate processes and keep their own env. */ -export function systemProxyDispatcher({ resolve, env = process.env, ttlMs = 60_000, now = Date.now }: { +export function systemProxyDispatcher({ resolve, env = process.env, ttlMs = 60_000, now = Date.now, undici }: { resolve: (url: string) => Promise; env?: NodeJS.ProcessEnv; ttlMs?: number; now?: () => number; + undici: Undici; }): Dispatcher { + const { Agent, EnvHttpProxyAgent, ProxyAgent, Socks5ProxyAgent } = undici; const direct = new Agent(); const fromEnv = envProxySet(env) ? new EnvHttpProxyAgent({ httpProxy: env.http_proxy || env.HTTP_PROXY || "", @@ -107,14 +111,15 @@ export function systemProxyDispatcher({ resolve, env = process.env, ttlMs = 60_0 type ParentPort = { on(event: "message", listener: (event: { data?: unknown }) => void): void; postMessage(message: object): void }; -export function installSystemProxy(port: ParentPort, env: NodeJS.ProcessEnv = process.env): void { +export async function installSystemProxy(port: ParentPort, env: NodeJS.ProcessEnv = process.env): Promise { + const undici = await import("undici"); const resolver = createProxyResolver((message) => { try { port.postMessage(message); return true; } catch { return false; } }); port.on("message", ({ data }) => { resolver.receive(data); }); - setGlobalDispatcher(systemProxyDispatcher({ resolve: resolver.resolve, env })); + undici.setGlobalDispatcher(systemProxyDispatcher({ resolve: resolver.resolve, env, undici })); } // Only Electron's utility process supplies parentPort (the desktop app's harness); plain Node leaves it absent. const parentPort = (process as NodeJS.Process & { parentPort?: ParentPort }).parentPort; -if (parentPort) installSystemProxy(parentPort); +if (parentPort) await installSystemProxy(parentPort);