openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
181 lines (180 loc) • 6.61 kB
JavaScript
import { E as parseStrictNonNegativeInteger } from "./number-coercion-D6u-4YD8.mjs";
import { r as terminalText } from "./safe-text-C80HAQ2e.mjs";
import { u as isSecureWebSocketUrl } from "./net-BgRayX-P.mjs";
import { t as formatHostForUrl } from "./hostname-DQVb2xzD.mjs";
import { t as detectBinary } from "./detect-binary-B596fFLF.mjs";
import { n as gatewayOriginScope, t as gatewayCredentialScope } from "./gateway-origin-scope-SP9yyLnR.mjs";
import { n as preserveGatewaySshRemotePort } from "./connection-details-BWzGtdxz.mjs";
import { r as t } from "./i18n-AiHKCZaZ.mjs";
import "./onboard-helpers-I6fwzX78.mjs";
import { t as resolveSecretInputModeForEnvSelection } from "./provider-auth-mode-BEzA0d9m.mjs";
import { a as resolveWideAreaDiscoveryDomain } from "./widearea-dns-Clgx1Bw3.mjs";
import { t as discoverGatewayBeacons } from "./bonjour-discovery-B1DGhRj_.mjs";
import { n as buildGatewayDiscoveryTarget, t as buildGatewayDiscoveryLabel } from "./gateway-discovery-targets-BLlhMUdW.mjs";
import { n as promptSecretRefForSetup } from "./provider-auth-ref-DmucC_mM.mjs";
//#region src/commands/onboard-remote.ts
const DEFAULT_GATEWAY_URL = "ws://127.0.0.1:18789";
function validateGatewayWebSocketUrl(e) {
const n = e.trim();
if (!n.startsWith("ws://") && !n.startsWith("wss://")) return t("wizard.remote.validWebSocketUrl");
if (!isSecureWebSocketUrl(n, { allowPrivateWs: process.env.OPENCLAW_ALLOW_INSECURE_PRIVATE_WS === "1" })) return t("wizard.remote.insecureRemoteUrl");
}
async function promptRemoteGatewayConfig(e, n, r) {
let i = null;
let a = e.gateway?.remote?.url ?? DEFAULT_GATEWAY_URL;
let o;
const s = await detectBinary("dns-sd") || await detectBinary("avahi-browse");
const c = s ? await n.confirm({
message: t("wizard.remote.bonjour"),
initialValue: true
}) : false;
if (!s) await n.note(["Bonjour discovery requires dns-sd (macOS) or avahi-browse (Linux).", "Docs: https://docs.openclaw.ai/gateway/discovery"].join("\n"), "Discovery");
if (c) {
const r = resolveWideAreaDiscoveryDomain({ configDomain: e.discovery?.wideArea?.domain });
const a = n.progress(t("wizard.remote.searchProgress"));
const o = await discoverGatewayBeacons({
timeoutMs: 2e3,
wideAreaDomain: r
});
a.stop(o.length > 0 ? t("wizard.remote.foundGateways", { count: o.length }) : t("wizard.remote.noGatewaysFound"));
if (o.length > 0) {
const e = await n.select({
message: t("wizard.remote.selectGateway"),
options: [...o.map((e, n) => ({
value: String(n),
label: buildGatewayDiscoveryLabel(e)
})), {
value: "manual",
label: t("wizard.remote.enterUrlManually")
}]
});
if (e !== "manual") {
const n = parseStrictNonNegativeInteger(e);
i = n === void 0 ? null : o[n] ?? null;
}
}
}
if (i) {
const e = buildGatewayDiscoveryTarget(i);
if (e.endpoint) {
const { host: r, port: i } = e.endpoint;
const s = formatHostForUrl(r);
if (await n.select({
message: t("wizard.remote.connectionMethod"),
options: [{
value: "direct",
label: terminalText`Direct gateway WS (${s}:${i})`
}, {
value: "ssh",
label: t("wizard.remote.sshTunnel")
}]
}) === "direct") {
a = `wss://${s}:${i}`;
const r = e.endpoint.gatewayTlsFingerprintSha256;
if (await n.confirm({
message: t("wizard.remote.trustGateway", {
host: terminalText`${s}:${i}`,
fingerprint: r ? terminalText`${r}` : t("wizard.remote.fingerprintMissing")
}),
initialValue: false
})) {
o = {
url: a,
transport: "direct",
...r ? { tlsFingerprint: r } : {}
};
await n.note([
t("wizard.remote.directDefaultsTls"),
terminalText`Using: ${a}`,
...r ? [terminalText`TLS pin: ${r}`] : [],
t("wizard.remote.loopbackSshHint")
].join("\n"), t("wizard.remote.directAccessTitle"));
} else a = DEFAULT_GATEWAY_URL;
} else {
a = DEFAULT_GATEWAY_URL;
o = {
url: a,
transport: "ssh"
};
await n.note([
"Start a tunnel before using the CLI:",
terminalText`ssh -N -L 18789:127.0.0.1:${i} <user>@${r}${e.sshPort ? ` -p ${e.sshPort}` : ""}`,
"Docs: https://docs.openclaw.ai/gateway/remote"
].join("\n"), t("wizard.remote.sshTunnelTitle"));
}
}
}
const l = (await n.text({
message: t("wizard.remote.websocketUrl"),
initialValue: a,
validate: validateGatewayWebSocketUrl
})).trim() || DEFAULT_GATEWAY_URL;
const u = o && gatewayCredentialScope(o.url) === gatewayCredentialScope(l) ? o : void 0;
const d = (!e.gateway?.remote?.url || gatewayCredentialScope(l) === gatewayCredentialScope(e.gateway.remote.url)) && u?.transport !== "ssh" ? e.gateway?.remote?.token ?? e.gateway?.remote?.password : void 0;
let f;
if (await resolveSecretInputModeForEnvSelection({
prompter: n,
explicitMode: r?.secretInputMode,
copy: {
modeMessage: t("wizard.gateway.remoteTokenMode"),
plaintextLabel: t("wizard.remote.plaintextTokenLabel"),
plaintextHint: t("wizard.remote.plaintextTokenHint")
}
}) === "ref") {
if (!await n.confirm({
message: t("wizard.remote.noSecretConfirm"),
initialValue: false
})) f = (await promptSecretRefForSetup({
provider: "gateway-remote-token",
config: e,
prompter: n,
preferredEnvVar: "OPENCLAW_GATEWAY_TOKEN",
copy: {
sourceMessage: t("wizard.remote.gatewayTokenStoredMessage"),
envVarPlaceholder: "OPENCLAW_GATEWAY_TOKEN"
}
})).ref;
} else while (true) {
const e = (await n.text({
message: t("wizard.remote.tokenPrompt"),
placeholder: t("wizard.remote.secretPlaceholder"),
sensitive: true
})).trim();
if (e) {
f = e;
break;
}
if (d && await n.confirm({
message: t("wizard.remote.keepSecretConfirm"),
initialValue: true
})) {
f = d;
break;
}
if (await n.confirm({
message: t("wizard.remote.noSecretConfirm"),
initialValue: false
})) break;
}
const p = r && "remoteOriginUrl" in r ? r.remoteOriginUrl : e.gateway?.remote?.url;
const m = p && gatewayOriginScope(l) === gatewayOriginScope(p) ? e.gateway?.remote?.edgeAuth : void 0;
const h = gatewayCredentialScope(l) === gatewayCredentialScope(p ?? "") && u?.transport !== "ssh" ? e.gateway?.remote : void 0;
return {
...e,
gateway: {
...e.gateway,
mode: "remote",
remote: {
...h,
...h && u?.transport !== "direct" ? preserveGatewaySshRemotePort(e, l, p) : {},
url: l,
edgeAuth: m,
token: f,
password: void 0,
...u?.transport === "direct" ? u : {}
}
}
};
}
//#endregion
export { validateGatewayWebSocketUrl as n, promptRemoteGatewayConfig as t };