UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

107 lines (106 loc) 3.36 kB
import { r as normalizeTlsFingerprint } from "./client-address-utils-DUCasR_v.js"; import { TLSSocket } from "node:tls"; import { request } from "node:http"; import { request as request$1 } from "node:https"; //#region src/gateway/local-http-probe.ts const GATEWAY_HTTP_PROBE_MAX_RESPONSE_CHARS = 1024; function normalizeGatewayHttpProbeHost(host) { return host === "0.0.0.0" || host === "::" ? "127.0.0.1" : host; } async function requestGatewayLocalHttpProbe(params) { if (params.timeoutMs <= 0) return null; return await new Promise((resolve) => { let settled = false; const finish = (result) => { if (settled) return; settled = true; clearTimeout(deadline); resolve(result); }; const req = (params.tlsFingerprint ? request$1 : request)({ hostname: normalizeGatewayHttpProbeHost(params.host), port: params.port, path: params.pathname, method: "GET", timeout: params.timeoutMs, ...params.tlsFingerprint ? { rejectUnauthorized: false } : {} }, (res) => { if (params.tlsFingerprint) { if ((res.socket instanceof TLSSocket ? normalizeTlsFingerprint(res.socket.getPeerCertificate().fingerprint256 ?? "") : "") !== normalizeTlsFingerprint(params.tlsFingerprint)) { res.resume(); finish(null); return; } } let body = ""; res.setEncoding("utf8"); res.on("data", (chunk) => { if (body.length + chunk.length > GATEWAY_HTTP_PROBE_MAX_RESPONSE_CHARS) { res.destroy(); finish(null); return; } body += chunk; }); res.once("end", () => { finish({ statusCode: res.statusCode ?? 0, body }); }); res.once("error", () => { finish(null); }); }); const deadline = setTimeout(() => { req.destroy(); finish(null); }, params.timeoutMs); req.once("timeout", () => { req.destroy(); finish(null); }); req.once("error", () => { finish(null); }); req.end(); }); } function createConfiguredGatewayLocalProbe(config) { const tlsConfig = config.gateway?.tls; let tlsFingerprint; let tlsFingerprintLoad = null; const resolveTlsFingerprint = async () => { if (tlsConfig?.enabled !== true) return; if (!tlsFingerprint) { tlsFingerprintLoad ??= import("./gateway-DrcLSDwB.js").then(({ loadGatewayTlsServerRuntime }) => loadGatewayTlsServerRuntime({ ...tlsConfig, autoGenerate: false })).then((gatewayTls) => gatewayTls.fingerprintSha256).catch(() => void 0); const gatewayTls = await tlsFingerprintLoad; tlsFingerprintLoad = null; tlsFingerprint = gatewayTls; } return tlsFingerprint; }; return { async requestHttp(params) { const resolvedTlsFingerprint = await resolveTlsFingerprint(); if (tlsConfig?.enabled === true && !resolvedTlsFingerprint) return null; return await requestGatewayLocalHttpProbe({ ...params, ...resolvedTlsFingerprint ? { tlsFingerprint: resolvedTlsFingerprint } : {} }); }, async resolveWebSocketTarget(port) { const resolvedTlsFingerprint = await resolveTlsFingerprint(); if (tlsConfig?.enabled === true) return resolvedTlsFingerprint ? { url: `wss://127.0.0.1:${port}`, tlsFingerprint: resolvedTlsFingerprint } : null; return { url: `ws://127.0.0.1:${port}` }; } }; } //#endregion export { normalizeGatewayHttpProbeHost as n, requestGatewayLocalHttpProbe as r, createConfiguredGatewayLocalProbe as t };