openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
136 lines (135 loc) • 6.01 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { o as redactSensitiveUrlLikeString } from "./redact-sensitive-url-D-E7DGoc.mjs";
import { b as resolveGatewayPort, m as resolveConfigPath } from "./paths-DAcAg5aV.mjs";
import { r as normalizeTlsFingerprint } from "./client-address-utils-Clpzy8Fr.mjs";
import { s as isLoopbackHost, u as isSecureWebSocketUrl } from "./net-BgRayX-P.mjs";
import { n as gatewayOriginScope } from "./gateway-origin-scope-SP9yyLnR.mjs";
import { createHash } from "node:crypto";
//#region src/gateway/connection-details.ts
/** Resolve the forwarded Gateway port independently of the local tunnel port. */
function resolveGatewaySshRemotePort(e, t = e.gateway?.remote?.url) {
if (e.gateway?.remote?.remotePort !== void 0) return e.gateway.remote.remotePort;
const n = t?.trim();
if (n && URL.canParse(n)) {
const e = new URL(n.replace(/^wss?:/iu, "openclaw-ssh:")).port;
if (e) return Number(e);
}
return resolveGatewayPort(e);
}
/** Keep a retained SSH route on the same service when its URL spelling changes. */
function preserveGatewaySshRemotePort(e, t, n = e.gateway?.remote?.url) {
const r = e.gateway?.remote;
if (!normalizeOptionalString(r?.sshTarget) || r?.transport === "direct") return {};
const i = resolveGatewaySshRemotePort(e, n);
return i === resolveGatewaySshRemotePort(e, t) ? {} : { remotePort: i };
}
/** Select credential ownership independently of an SSH tunnel's reusable local port. */
function resolveGatewayDeviceAuthRoute(e) {
if (!e.remote) return { bound: false };
const t = e.url.trim();
const n = gatewayOriginScope(t);
if (!URL.canParse(t)) return {
deviceAuthScope: n,
bound: false
};
const r = new URL(t);
if (!isLoopbackHost(r.hostname)) return {
deviceAuthScope: n,
bound: true
};
const i = e.config.gateway?.remote;
const a = normalizeOptionalString(i?.sshTarget);
const o = normalizeOptionalString(i?.sshIdentity);
const s = e.configuredRemote && a && i?.transport !== "direct" && i?.url?.trim() === t ? {
target: a,
remotePort: resolveGatewaySshRemotePort(e.config, t),
...o ? { identity: o } : {},
...i.sshHostKeyPolicy ? { hostKeyPolicy: i.sshHostKeyPolicy } : {}
} : void 0;
const c = e.sshRoute ?? s;
if (c) {
const e = {
...c,
target: c.target.trim()
};
const t = `${e.target}:gateway-port:${e.remotePort}`;
return {
deviceAuthScope: `remote:ssh:${createHash("sha256").update(t).digest("hex")}`,
sshTunnel: e,
bound: true
};
}
const l = normalizeTlsFingerprint(e.tlsFingerprint);
if (r.protocol === "wss:" && l) return {
deviceAuthScope: `remote:tls:${createHash("sha256").update(`${n}:tls-sha256:${l}`).digest("hex")}`,
bound: true
};
return {
deviceAuthScope: n,
bound: false
};
}
/** Project raw transport details into the credential-safe CLI/report shape. */
function projectGatewayConnectionDetailsForDiagnostics(e) {
return {
...e,
url: redactSensitiveUrlLikeString(e.url),
message: redactSensitiveUrlLikeString(e.message)
};
}
/** Redact one Gateway URL before it crosses an operator-visible diagnostic boundary. */
function projectGatewayUrlForDiagnostics(e) {
return redactSensitiveUrlLikeString(e);
}
/** Build gateway target details and reject unsafe remote plaintext websocket URLs. */
function buildGatewayConnectionDetailsWithResolvers(e = {}, t = {}) {
const n = e.config ?? t.getRuntimeConfig?.() ?? {};
const r = e.configPath ?? t.resolveConfigPath?.(process.env) ?? resolveConfigPath(process.env);
const i = e.localPortOverride === void 0 && n.gateway?.mode === "remote";
const a = i ? n.gateway?.remote : void 0;
const o = n.gateway?.tls?.enabled === true;
const s = e.localPortOverride ?? t.resolveGatewayPort?.(n, process.env) ?? resolveGatewayPort(n);
const c = n.gateway?.bind ?? "loopback";
const l = `${o ? "wss" : "ws"}://127.0.0.1:${s}`;
const u = normalizeOptionalString(e.url);
const d = normalizeOptionalString(e.serviceTargetUrl);
const f = u || d || e.ignoreEnvUrlOverride || e.localPortOverride !== void 0 ? void 0 : normalizeOptionalString(process.env.OPENCLAW_GATEWAY_URL);
const p = u ?? f;
const m = normalizeOptionalString(a?.url);
const h = i && !p && !d && !m;
const g = e.urlSource ?? (u ? "cli" : f ? "env" : void 0);
const _ = p || d || m || l;
const v = redactSensitiveUrlLikeString(_);
const y = p ? g === "env" ? "env OPENCLAW_GATEWAY_URL" : "cli --url" : d ? "service target" : m ? "config gateway.remote.url" : h ? "missing gateway.remote.url (fallback local)" : "local loopback";
const b = !p && (d || !m) ? `Bind: ${c}` : void 0;
const x = h ? "Warn: gateway.mode=remote but gateway.remote.url is missing; set gateway.remote.url or switch gateway.mode=local." : void 0;
const S = process.env.OPENCLAW_ALLOW_INSECURE_PRIVATE_WS === "1";
if (!isSecureWebSocketUrl(_, { allowPrivateWs: S })) throw new Error([
`SECURITY ERROR: Gateway URL "${v}" uses plaintext ws:// to a non-loopback address.`,
"Both credentials and chat data would be exposed to network interception.",
`Source: ${y}`,
`Config: ${r}`,
"Fix: Use wss:// for remote gateway URLs.",
"Safe remote access defaults:",
"- keep gateway.bind=loopback and use an SSH tunnel (ssh -N -L 18789:127.0.0.1:18789 user@gateway-host)",
"- or use Tailscale Serve/Funnel for HTTPS remote access",
S ? void 0 : "Break-glass (trusted private networks only): set OPENCLAW_ALLOW_INSECURE_PRIVATE_WS=1",
"Doctor: openclaw doctor --fix",
"Docs: https://docs.openclaw.ai/gateway/remote"
].join("\n"));
return {
url: _,
urlSource: y,
bindDetail: b,
remoteFallbackNote: x,
message: [
`Gateway target: ${v}`,
`Source: ${y}`,
`Config: ${r}`,
b,
x
].filter(Boolean).join("\n")
};
}
//#endregion
export { resolveGatewayDeviceAuthRoute as a, projectGatewayUrlForDiagnostics as i, preserveGatewaySshRemotePort as n, resolveGatewaySshRemotePort as o, projectGatewayConnectionDetailsForDiagnostics as r, buildGatewayConnectionDetailsWithResolvers as t };