openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
129 lines (128 loc) • 4.55 kB
JavaScript
import { a as matchesNoProxy, o as resolveEnvHttpProxyAgentOptions } from "./proxy-env-CZP0dXDG.mjs";
import { t as resolveActiveManagedProxyTlsOptions } from "./active-managed-proxy-tls-NikAcZOT.mjs";
import { t as loadProxyline } from "./proxyline-runtime-Df2Nb5Zp.mjs";
import { isIPv6 } from "node:net";
//#region src/infra/net/node-proxy-agent.ts
const UNSUPPORTED_PROXY_PROTOCOL_MESSAGE = "Unsupported proxy protocol. SOCKS and PAC proxy URLs are not supported; use an HTTP or HTTPS proxy URL.";
function proxyUrlWithDefaultScheme(e, t) {
const n = e.includes("://") ? e : `${t}://${e}`;
const r = URL.parse(n);
if (!r) throw new Error("Invalid proxy URL. Use an HTTP or HTTPS proxy URL.");
if (r.protocol !== "http:" && r.protocol !== "https:") throw new Error(`${UNSUPPORTED_PROXY_PROTOCOL_MESSAGE} Got ${r.protocol}`);
return r;
}
/** Resolves the env proxy URL that should be used for a specific Node target. */
function resolveEnvNodeProxyUrlForTarget(e, t = process.env) {
return resolveEnvNodeProxyTarget(e, t)?.proxyUrl;
}
function resolveEnvNodeProxyTarget(e, t = process.env) {
const n = URL.parse(e instanceof URL ? e.href : e);
if (!n) return;
if (n.protocol === "ws:") n.protocol = "http:";
else if (n.protocol === "wss:") n.protocol = "https:";
if (n.protocol !== "http:" && n.protocol !== "https:") return;
const r = n.protocol === "https:" ? "https" : "http";
if (matchesNoProxy(n, t)) return;
const i = resolveEnvHttpProxyAgentOptions(t);
const a = r === "https" ? i?.httpsProxy : i?.httpProxy;
return a ? {
proxyUrl: proxyUrlWithDefaultScheme(a, r),
protocol: r
} : void 0;
}
function createFixedNodeProxyAgent(e, t = {}) {
const n = proxyUrlWithDefaultScheme(e instanceof URL ? e.href : e, t.protocol ?? "https").href;
const r = t.proxyConnect;
const { ProxylineNodeProxyAgent: i } = loadProxyline();
return new i({
...t.agentOptions,
defaultProtocol: t.protocol ?? "https",
getProxyForUrl: () => n,
proxyTls: t.proxyTls,
resolveProxyConnectOptions: r !== void 0 ? () => r : void 0
});
}
function createPerRequestEnvProxyAgent(e) {
const t = { ...process.env };
const n = resolveEnvHttpProxyAgentOptions(t);
if (!n) return;
const r = /* @__PURE__ */ new Map();
const i = [];
for (const a of ["http", "https"]) {
const o = a === "http" ? n.httpProxy : n.httpsProxy;
if (!o) continue;
try {
const n = proxyUrlWithDefaultScheme(o, a);
const s = createFixedNodeProxyAgent(n, {
protocol: e.protocol,
proxyTls: resolveActiveManagedProxyTlsOptions({
proxyUrl: n.href,
env: t
}),
agentOptions: e.agentOptions,
proxyConnect: e.proxyConnect
});
r.set(a, s.addRequest.bind(s));
i.push(s);
} catch (e) {
r.set(a, () => {
throw e;
});
}
}
const { ProxylineNodeProxyAgent: a } = loadProxyline();
const o = new a({
...e.agentOptions,
defaultProtocol: e.protocol ?? "https",
getProxyForUrl: () => ""
});
const s = o.addRequest.bind(o);
o.addRequest = (e, n) => {
const i = e.protocol === "https:" ? "https" : "http";
const a = isIPv6(e.host) ? `[${e.host}]` : e.host;
const o = new URL(`${e.protocol}//${a}`);
if (n.port) o.port = String(n.port);
const c = matchesNoProxy(o, t) ? void 0 : r.get(i);
if (c) c(e, n);
else s(e, n);
};
const c = o.destroy.bind(o);
o.destroy = () => {
for (const e of i) e.destroy();
c();
};
return o;
}
function createNodeProxyAgent(e) {
if (e.mode === "explicit") return createFixedNodeProxyAgent(e.proxyUrl, {
protocol: e.protocol,
agentOptions: e.agentOptions,
proxyConnect: e.proxyConnect
});
if (e.targetUrl === void 0) return createPerRequestEnvProxyAgent(e);
const t = resolveEnvNodeProxyTarget(e.targetUrl);
if (t === void 0) return;
return createFixedNodeProxyAgent(t.proxyUrl, {
protocol: e.protocol ?? t.protocol,
proxyTls: resolveActiveManagedProxyTlsOptions({ proxyUrl: t.proxyUrl.href }),
agentOptions: e.agentOptions,
proxyConnect: e.proxyConnect
});
}
/** Builds paired HTTP and HTTPS agents for libraries that require both slots. */
function createFixedNodeProxyAgentPair(e) {
const t = e instanceof URL ? e : proxyUrlWithDefaultScheme(e, "https");
const n = resolveActiveManagedProxyTlsOptions({ proxyUrl: t.href });
return {
httpAgent: createFixedNodeProxyAgent(t, {
protocol: "http",
proxyTls: n
}),
httpsAgent: createFixedNodeProxyAgent(t, {
protocol: "https",
proxyTls: n
})
};
}
//#endregion
export { createNodeProxyAgent as n, resolveEnvNodeProxyUrlForTarget as r, createFixedNodeProxyAgentPair as t };