UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

194 lines (193 loc) • 7.16 kB
import { r as defaultRuntime } from "./runtime-B-SLYP-m.mjs"; import { r as terminalText } from "./safe-text-C80HAQ2e.mjs"; import { t as captureRuntimeConfigAsyncReader } from "./io.runtime-DsJ2dLCQ.mjs"; import { n as pickPrimaryTailnetIPv4, r as pickPrimaryTailnetIPv6 } from "./tailnet-Ec_4DLPQ.mjs"; import "./config-CMA-J084.mjs"; import { r as theme } from "./theme-BOI0xyRZ.mjs"; import { r as renderTerminalSafeTable, t as getTerminalTableWidth } from "./table-D2a-pD4_.mjs"; import { t as formatDocsHelp } from "./help-format-D2ytHW5E.mjs"; import { a as resolveWideAreaDiscoveryDomain, i as replaceWideAreaZoneFile, n as normalizeWideAreaDomain, t as getWideAreaZonePath } from "./widearea-dns-Clgx1Bw3.mjs"; import fs from "node:fs"; import path from "node:path"; import { spawnSync } from "node:child_process"; //#region src/cli/dns-cli.ts function assertSpawnSucceeded(e, t, n) { if (t.error) { if (n?.timeoutMs !== void 0 && "code" in t.error && t.error.code === "ETIMEDOUT") throw new Error(`${e} failed: timed out after ${n.timeoutMs / 1e3} seconds (signal ${t.signal ?? "SIGKILL"})`, { cause: t.error }); throw t.error; } if (n?.allowFailure || t.status === 0) return; const r = typeof t.stderr === "string" ? t.stderr.trim() : ""; const i = t.signal ? `signal ${t.signal}` : `exit ${t.status ?? "unknown"}`; throw new Error(`${e} failed: ${r || i}`); } function run(e, t, n) { const r = spawnSync(e, t, { encoding: "utf-8", stdio: n?.inherit ? "inherit" : "pipe", ...n?.timeoutMs === void 0 ? {} : { timeout: n.timeoutMs, killSignal: "SIGKILL" } }); assertSpawnSucceeded(`${e} ${t.join(" ")}`, r, n); return typeof r.stdout === "string" ? r.stdout : ""; } function writeFileSudoIfNeeded(e, t) { try { fs.writeFileSync(e, t, "utf-8"); return; } catch (e) { const t = e.code; if (t !== "EACCES" && t !== "EPERM") throw e instanceof Error ? e : new Error(String(e)); } const n = spawnSync("sudo", ["tee", e], { input: t, encoding: "utf-8", stdio: [ "pipe", "ignore", "inherit" ] }); assertSpawnSucceeded(`sudo tee ${e}`, n); } function mkdirSudoIfNeeded(e) { try { fs.mkdirSync(e, { recursive: true }); return; } catch (e) { const t = e.code; if (t !== "EACCES" && t !== "EPERM") throw e instanceof Error ? e : new Error(String(e)); } run("sudo", [ "mkdir", "-p", e ], { inherit: true }); } function zoneFileNeedsBootstrap(e) { if (!fs.existsSync(e)) return true; try { const t = fs.readFileSync(e, "utf-8"); return !/\bSOA\b/.test(t) || !/\bNS\b/.test(t); } catch { return true; } } function registerDnsCli(e) { e.command("dns").description("DNS helpers for wide-area discovery (Tailscale + CoreDNS)").addHelpText("after", () => formatDocsHelp("/cli/dns")).command("setup").description("Set up CoreDNS to serve your discovery domain for unicast DNS-SD (Wide-Area Bonjour)").option("--domain <domain>", "Wide-area discovery domain (e.g. openclaw.internal)").option("--apply", "Install/update CoreDNS config and (re)start the service (requires sudo)", false).action(async (e) => { const t = await captureRuntimeConfigAsyncReader()(); const n = pickPrimaryTailnetIPv4(); const r = pickPrimaryTailnetIPv6(); const i = e.domain ?? t.discovery?.wideArea?.domain; if (i) normalizeWideAreaDomain(i); const a = resolveWideAreaDiscoveryDomain({ configDomain: i }); if (!a) throw new Error("No wide-area domain configured. Set discovery.wideArea.domain or pass --domain."); const o = getWideAreaZonePath(a); const s = getTerminalTableWidth(); defaultRuntime.log(theme.heading("DNS setup")); defaultRuntime.log(renderTerminalSafeTable({ width: s, columns: [{ key: "Key", header: "Key", minWidth: 18 }, { key: "Value", header: "Value", minWidth: 24, flex: true }], rows: [ { Key: "Domain", Value: a }, { Key: "Zone file", Value: o }, { Key: "Tailnet IP", Value: `${n ?? "—"}${r ? ` (v6 ${r})` : ""}` } ] }).trimEnd()); defaultRuntime.log(""); defaultRuntime.log(theme.heading("Recommended config ($OPENCLAW_CONFIG_PATH, default ~/.openclaw/openclaw.json):")); defaultRuntime.writeJson({ gateway: { bind: "auto" }, discovery: { wideArea: { domain: a } } }); defaultRuntime.log(""); defaultRuntime.log(theme.heading("Tailscale admin (DNS → Nameservers):")); defaultRuntime.log(theme.muted(terminalText`- Add nameserver: ${n ?? "<this machine's tailnet IPv4>"}`)); defaultRuntime.log(theme.muted(terminalText`- Restrict to domain (Split DNS): ${a.replace(/\.$/, "")}`)); if (!e.apply) { defaultRuntime.log(""); defaultRuntime.log(theme.muted("Run with --apply to install CoreDNS and configure it.")); return; } if (process.platform !== "darwin") throw new Error("dns setup is currently supported on macOS only"); if (!n && !r) throw new Error("no tailnet IP detected; check that Tailscale is running on this machine"); const c = run("brew", ["--prefix"], { timeoutMs: 15e3 }).trim(); if (!c) throw new Error("failed to resolve Homebrew prefix"); const l = path.join(c, "etc", "coredns"); const u = path.join(l, "Corefile"); const d = path.join(l, "conf.d"); const f = path.join(d, "*.server"); const p = path.join(d, `${a.replace(/\.$/, "")}.server`); run("brew", ["list", "coredns"], { allowFailure: true }); run("brew", ["install", "coredns"], { inherit: true, allowFailure: true }); mkdirSudoIfNeeded(d); if (!fs.existsSync(u)) writeFileSudoIfNeeded(u, `import ${f}\n`); else { const e = fs.readFileSync(u, "utf-8"); if (!e.includes(f)) writeFileSudoIfNeeded(u, `${e.replace(/\s*$/, "")}\n\nimport ${f}\n`); } const m = [n, r].filter((e) => Boolean(e?.trim())); writeFileSudoIfNeeded(p, [ `${a.replace(/\.$/, "")}:53 {`, ` bind ${m.join(" ")}`, ` file ${o} {`, ` reload 10s`, ` }`, ` errors`, ` log`, `}`, `` ].join("\n")); if (zoneFileNeedsBootstrap(o)) { const e = `${(/* @__PURE__ */ new Date()).getUTCFullYear()}${String((/* @__PURE__ */ new Date()).getUTCMonth() + 1).padStart(2, "0")}${String((/* @__PURE__ */ new Date()).getUTCDate()).padStart(2, "0")}01`; const t = [ `; created by openclaw dns setup (will be overwritten by the gateway when wide-area discovery is enabled)`, `$ORIGIN ${a}`, `$TTL 60`, `@ IN SOA ns1 hostmaster ${e} 7200 3600 1209600 60`, `@ IN NS ns1`, n ? `ns1 IN A ${n}` : null, r ? `ns1 IN AAAA ${r}` : null, `` ].filter((e) => Boolean(e)); await replaceWideAreaZoneFile(o, t.join("\n")); } defaultRuntime.log(""); defaultRuntime.log(theme.heading("Starting CoreDNS (sudo)…")); run("sudo", [ "brew", "services", "restart", "coredns" ], { inherit: true }); if (!t.discovery?.wideArea?.domain?.trim()) { defaultRuntime.log(""); defaultRuntime.log(theme.muted("Note: set discovery.wideArea.domain in the active OpenClaw config ($OPENCLAW_CONFIG_PATH, default ~/.openclaw/openclaw.json) on the gateway and restart the gateway so it writes the DNS-SD zone.")); } }); } //#endregion export { registerDnsCli };