openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
150 lines (149 loc) • 4.08 kB
JavaScript
import { r as normalizeTlsFingerprint } from "./client-address-utils-Clpzy8Fr.mjs";
import { r as registerManagedProxyGatewayLoopbackBypass } from "./proxy-lifecycle-78PDzQG7.mjs";
import { n as createNodeProxyAgent } from "./node-proxy-agent-CKkgcWcJ.mjs";
import { TLSSocket } from "node:tls";
import { Agent, request } from "node:http";
import { Agent as Agent$1, request as request$1 } from "node:https";
//#region src/gateway/local-http-probe.ts
const GATEWAY_HTTP_PROBE_MAX_RESPONSE_CHARS = 65536;
const directHttpAgent = new Agent({ keepAlive: false });
const directHttpsAgent = new Agent$1({
keepAlive: false,
maxCachedSessions: 0
});
function normalizeGatewayHttpProbeHost(e) {
return e === "0.0.0.0" || e === "::" ? "127.0.0.1" : e;
}
async function requestGatewayLocalHttpProbe(e) {
e.signal?.throwIfAborted();
if (e.timeoutMs <= 0) return null;
const t = await new Promise((t) => {
let n = false;
const finish = (e) => {
if (n) return;
n = true;
clearTimeout(l);
t(e);
};
const r = e.tlsFingerprints?.map(normalizeTlsFingerprint);
const i = normalizeGatewayHttpProbeHost(e.host);
const a = r ? "https" : "http";
const o = `${a}://${i.includes(":") ? `[${i}]` : i}:${e.port}${e.pathname}`;
registerManagedProxyGatewayLoopbackBypass(o);
const s = createNodeProxyAgent({
mode: "env",
targetUrl: o,
protocol: a,
agentOptions: {
keepAlive: false,
maxCachedSessions: 0
}
});
const c = (r ? request$1 : request)({
hostname: i,
port: e.port,
path: e.pathname,
method: "GET",
agent: s ?? (r ? directHttpsAgent : directHttpAgent),
...e.signal ? { signal: e.signal } : {},
...r ? { rejectUnauthorized: false } : {}
}, (e) => {
let t;
if (r) {
t = e.socket instanceof TLSSocket ? normalizeTlsFingerprint(e.socket.getPeerCertificate().fingerprint256 ?? "") : "";
if (!t || !r.includes(t)) {
e.resume();
finish(null);
return;
}
}
let n = "";
e.setEncoding("utf8");
e.on("data", (r) => {
if (n.length + r.length > GATEWAY_HTTP_PROBE_MAX_RESPONSE_CHARS) {
finish({
statusCode: e.statusCode ?? 0,
body: "",
...t ? { tlsFingerprint: t } : {}
});
e.destroy();
return;
}
n += r;
});
e.once("end", () => {
finish({
statusCode: e.statusCode ?? 0,
body: n,
...t ? { tlsFingerprint: t } : {}
});
});
e.once("error", () => {
finish(null);
});
});
const l = setTimeout(() => {
c.destroy();
finish(null);
}, e.timeoutMs);
c.once("error", () => {
finish(null);
});
c.once("close", () => s?.destroy());
c.end();
});
e.signal?.throwIfAborted();
return t;
}
function createConfiguredGatewayLocalProbe(e) {
const t = e.gateway?.tls;
let n;
let r;
const inspectCertificate = () => n ??= import("./gateway-6xEaCcr5.mjs").then(async ({ inspectGatewayTlsCertificate: e }) => {
const n = await e(t);
return n.ok ? n.value.fingerprintSha256 : void 0;
}).catch(() => void 0).finally(() => {
n = void 0;
});
const requestHttp = async (e) => {
e.signal?.throwIfAborted();
const n = `${normalizeGatewayHttpProbeHost(e.host)}:${e.port}`;
const i = r;
let a;
if (t?.enabled === true) {
const t = await inspectCertificate();
e.signal?.throwIfAborted();
a = [...t ? [t] : [], ...i?.endpoint === n ? [i.fingerprint] : []];
if (a.length === 0) return null;
}
const o = await requestGatewayLocalHttpProbe({
...e,
tlsFingerprints: a
});
if (o?.tlsFingerprint) r = {
fingerprint: o.tlsFingerprint,
endpoint: n
};
return o;
};
return {
requestHttp,
async resolveWebSocketTarget(e, n) {
n?.throwIfAborted();
if (t?.enabled !== true) return { url: `ws://127.0.0.1:${e}` };
const r = await requestHttp({
host: "127.0.0.1",
port: e,
pathname: "/healthz",
timeoutMs: 3e3,
signal: n
});
return r?.tlsFingerprint ? {
url: `wss://127.0.0.1:${e}`,
tlsFingerprint: r.tlsFingerprint
} : null;
}
};
}
//#endregion
export { createConfiguredGatewayLocalProbe as t };