This commit is contained in:
xovoxy 2026-01-31 00:16:17 +08:00 committed by GitHub
commit ee8ea4b845
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import { loadDotEnv } from "../infra/dotenv.js";
import { normalizeEnv } from "../infra/env.js";
import { isMainModule } from "../infra/is-main.js";
import { ensureOpenClawCliOnPath } from "../infra/path-env.js";
import { configureGlobalProxy } from "../infra/proxy.js";
import { assertSupportedRuntime } from "../infra/runtime-guard.js";
import { formatUncaughtError } from "../infra/errors.js";
import { installUnhandledRejectionHandler } from "../infra/unhandled-rejections.js";
@ -27,6 +28,7 @@ export async function runCli(argv: string[] = process.argv) {
const normalizedArgv = stripWindowsNodeExec(argv);
loadDotEnv({ quiet: true });
normalizeEnv();
configureGlobalProxy();
ensureOpenClawCliOnPath();
// Enforce the minimum supported runtime before doing any work.

41
src/infra/proxy.ts Normal file
View File

@ -0,0 +1,41 @@
import { setGlobalDispatcher, EnvHttpProxyAgent } from "undici";
import { createSubsystemLogger } from "../logging/subsystem.js";
const log = createSubsystemLogger("infra/proxy");
/**
* Configures the global undici dispatcher to respect standard proxy environment variables:
* - HTTP_PROXY / http_proxy
* - HTTPS_PROXY / https_proxy
* - NO_PROXY / no_proxy
*
* This affects all global fetch() calls and undici-based requests.
*/
export function configureGlobalProxy(): void {
const hasProxyEnv =
process.env.HTTPS_PROXY ||
process.env.https_proxy ||
process.env.HTTP_PROXY ||
process.env.http_proxy;
if (!hasProxyEnv) {
return;
}
try {
// EnvHttpProxyAgent is the industry-standard way in undici to handle env-based proxies.
// It automatically manages the logic for NO_PROXY and selecting the right proxy per protocol.
const agent = new EnvHttpProxyAgent();
setGlobalDispatcher(agent);
// We don't log the full proxy URL by default to avoid leaking potential credentials
// but we log that the proxy system is active.
log.debug("global proxy dispatcher initialized via environment variables");
} catch (err) {
// If proxy configuration fails, we log it but don't crash the app.
// Standard behavior is to fallback to direct connection if possible.
log.error(
`failed to initialize global proxy dispatcher: ${err instanceof Error ? err.message : String(err)}`,
);
}
}