openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 5.17 kB
JavaScript
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{o as redactSensitiveUrlLikeString}from"./redact-sensitive-url-Byg68d6v.mjs";import{b as resolveGatewayPort,m as resolveConfigPath}from"./paths-D_FOvGon.mjs";import{r as normalizeTlsFingerprint}from"./client-address-utils-CIkL4kpz.mjs";import{s as isLoopbackHost,u as isSecureWebSocketUrl}from"./net-Bu4cO5Pq.mjs";import{n as gatewayOriginScope}from"./gateway-origin-scope-XnuMqfjZ.mjs";import{createHash}from"node:crypto";
//!src/gateway/connection-details.ts
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)}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}}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}}function projectGatewayConnectionDetailsForDiagnostics(e){return{...e,url:redactSensitiveUrlLikeString(e.url),message:redactSensitiveUrlLikeString(e.message)}}function projectGatewayUrlForDiagnostics(e){return redactSensitiveUrlLikeString(e)}const GATEWAY_PAIRING_APPROVAL_HINT="On the Gateway host, run `openclaw devices approve --latest` to preview the pending request. Review the preview, then run the printed `openclaw devices approve <requestId>` command to approve it. Or approve it in that Gateway's Control UI (Settings -> Devices). Then retry.";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(`
`));return{url:_,urlSource:y,bindDetail:b,remoteFallbackNote:x,message:[`Gateway target: ${v}`,`Source: ${y}`,`Config: ${r}`,b,x].filter(Boolean).join(`
`)}}export{projectGatewayUrlForDiagnostics as a,projectGatewayConnectionDetailsForDiagnostics as i,buildGatewayConnectionDetailsWithResolvers as n,resolveGatewayDeviceAuthRoute as o,preserveGatewaySshRemotePort as r,resolveGatewaySshRemotePort as s,GATEWAY_PAIRING_APPROVAL_HINT as t};