openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
147 lines (146 loc) • 5.08 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { u as rewriteUpdateFlagArgv } from "./cli-root-options-xlQx6o4R.mjs";
import "./argv-CbnKpH-r.mjs";
import { t as resolveCliArgvInvocation } from "./argv-invocation-C9SmXUsh.mjs";
import { n as scanCliRootOptions, t as takeCliRootOptionValue } from "./root-option-value-xhHNIC3W.mjs";
import "./src-H3RMXyFL.mjs";
import { t as expectDefined } from "./expect-DDakjvvQ.mjs";
import { t as resolveSubprocessExitCode } from "./subprocess-exit-code-Sobl-u-2.mjs";
import { spawnSync } from "node:child_process";
import { isIP } from "node:net";
//#region src/cli/container-target.ts
const CONTAINER_RUNTIMES = ["podman", "docker"];
const CONTAINER_ALLOW_LOOPBACK_PROXY_URL_ENV = "OPENCLAW_CONTAINER_ALLOW_LOOPBACK_PROXY_URL";
const CONTAINER_RUNTIME_PROBE_TIMEOUT_MS = 1e4;
function parseCliContainerArgs(e) {
let t = null;
const n = scanCliRootOptions(e, ({ arg: e, args: n, index: r }) => {
if (e === "--container" || e.startsWith("--container=")) {
const i = n[r + 1];
const { value: a, consumedNext: o } = takeCliRootOptionValue(e, i);
if (!a) return {
kind: "error",
error: "--container requires a value"
};
t = a;
return {
kind: "handled",
consumedNext: o
};
}
return { kind: "pass" };
});
return n.ok ? {
ok: true,
container: t,
argv: n.argv
} : n;
}
function resolveCliContainerTarget(e, t = process.env) {
const n = parseCliContainerArgs(e);
if (!n.ok) throw new Error(n.error);
return n.container ?? normalizeOptionalString(t.OPENCLAW_CONTAINER) ?? null;
}
function assertContainerProxyUrlIsReachable(e) {
if (process.env[CONTAINER_ALLOW_LOOPBACK_PROXY_URL_ENV] === "1") return;
const t = URL.parse(e);
if (!t || !isLoopbackProxyHostname(t.hostname)) return;
throw new Error(`OPENCLAW_PROXY_URL=${redactProxyUrlForMessage(e)} is loopback; 127.0.0.1 inside a container points at the container, not the host. Use a container-reachable proxy address, or set ${CONTAINER_ALLOW_LOOPBACK_PROXY_URL_ENV}=1 if this is intentional.`);
}
function isLoopbackProxyHostname(e) {
const t = e.toLowerCase().replace(/\.+$/, "");
if (t === "localhost") return true;
if (isIP(t) === 4) return t.startsWith("127.");
const n = t.replace(/^\[|\]$/g, "");
if (isIP(n) !== 6) return false;
if (n === "::1" || n === "0:0:0:0:0:0:0:1") return true;
const r = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i.exec(n);
if (!r) return false;
const i = Number.parseInt(expectDefined(r[1], "mapped capture group 1"), 16);
return Number.isInteger(i) && i >= 32512 && i <= 32767;
}
function redactProxyUrlForMessage(e) {
const t = URL.parse(e);
if (!t) return "<invalid URL>";
if (t.username || t.password) {
t.username = "redacted";
t.password = t.password ? "redacted" : "";
}
t.search = "";
t.hash = "";
return t.toString();
}
function maybeRunCliInContainer(e) {
if (process.env.OPENCLAW_CLI_CONTAINER_BYPASS === "1") return {
handled: false,
argv: e
};
const t = parseCliContainerArgs(e);
if (!t.ok) throw new Error(t.error);
const n = t.container ?? normalizeOptionalString(process.env.OPENCLAW_CONTAINER);
if (!n) return {
handled: false,
argv: t.argv
};
const r = [
"node",
"openclaw",
...t.argv.slice(2)
];
if (resolveCliArgvInvocation(r).primary === "update" || rewriteUpdateFlagArgv(r) !== r) throw new Error("openclaw update is not supported with --container; rebuild or restart the container image instead.");
const i = CONTAINER_RUNTIMES.filter((e) => {
const t = spawnSync(e, [
"inspect",
"--format",
"{{.State.Running}}",
n
], {
encoding: "utf8",
killSignal: "SIGKILL",
timeout: CONTAINER_RUNTIME_PROBE_TIMEOUT_MS
});
return t.status === 0 && t.stdout.trim() === "true";
});
if (i.length === 0) throw new Error(`No running container matched "${n}" under podman or docker.`);
if (i.length > 1) {
const e = i.join(", ");
throw new Error(`Container "${n}" is running under multiple runtimes (${e}); use a unique container name.`);
}
const a = expectDefined(i[0], "matches capture group 0");
const o = t.argv.slice(2);
const s = a === "docker" ? "-e" : "--env";
const c = normalizeOptionalString(process.env.OPENCLAW_PROXY_URL);
if (c) assertContainerProxyUrlIsReachable(c);
const l = c ? [s, `OPENCLAW_PROXY_URL=${c}`] : [];
const u = [
"exec",
"-i",
...process.stdin.isTTY && process.stdout.isTTY ? ["-t"] : [],
s,
`OPENCLAW_CONTAINER_HINT=${n}`,
s,
"OPENCLAW_CLI_CONTAINER_BYPASS=1",
...l,
n,
"openclaw",
...o
];
const d = { ...process.env };
delete d.OPENCLAW_PROFILE;
delete d.OPENCLAW_GATEWAY_PORT;
delete d.OPENCLAW_GATEWAY_URL;
delete d.OPENCLAW_GATEWAY_TOKEN;
delete d.OPENCLAW_GATEWAY_PASSWORD;
d.OPENCLAW_CONTAINER = "";
const f = spawnSync(a, u, {
stdio: "inherit",
env: d
});
if (f.error) throw f.error;
return {
handled: true,
exitCode: resolveSubprocessExitCode(f.status, f.signal)
};
}
//#endregion
export { parseCliContainerArgs as n, resolveCliContainerTarget as r, maybeRunCliInContainer as t };