跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

tailnet-client.mjs

50 行 · 3.2 KB历史下载
1// Private stdio control of the Cage-owned guest node. No browser page can
2// select an executable, destination command, data directory or auth key.
3import { spawn } from "node:child_process";
4import { access } from "node:fs/promises";
5import { fileURLToPath } from "node:url";
6import { createInterface } from "node:readline";
7
8export function createTailnetClient({ executable, launch, start = spawn } = {}) {
9 executable ||= process.platform === "linux" ? "/usr/local/bin/cage-tailnet"
10 : fileURLToPath(new URL(process.platform === "win32" ? "./cage-tailnet.exe" : "./cage-tailnet", import.meta.url));
11 let child, lines, pending, queue = Promise.resolve();
12 async function open() {
13 if (child) return true;
14 try { await access(executable); } catch { return false; }
15 const command = launch || (process.platform === "linux" ? { file: "/usr/bin/sudo", args: ["-n", "-H", "-u", "cage", executable] } : { file: executable, args: [] });
16 const current = start(command.file, command.args, { stdio: ["pipe", "pipe", "ignore"], windowsHide: true });
17 child = current;
18 const reset = () => {
19 if (child !== current) return;
20 pending?.reject(new Error("Tailscale 组件连接中断,请重新打开面板")); pending = undefined;
21 lines?.close(); child = undefined;
22 };
23 lines = createInterface({ input: current.stdout });
24 lines.on("line", line => {
25 const active = pending; pending = undefined;
26 if (!active) return;
27 try { const value = JSON.parse(line); if (value.error) { value.tailnet_error = value.error; delete value.error; } active.resolve(value); }
28 catch (error) { active.reject(error); }
29 });
30 current.once("error", reset); current.once("exit", reset);
31 current.stdin.on("error", reset);
32 return true;
33 }
34 async function run(input) {
35 if (!input || !["status", "connect", "login", "disconnect", "logout", "add-key", "remove-key", "open-service"].includes(input.action) || typeof input.key !== "undefined" && (typeof input.key !== "string" || input.key.length > 8192)) throw new Error("Tailscale 操作无效");
36 if (input.action === "open-service" && (typeof input.host !== "string" || !/^[a-zA-Z0-9.-]{1,253}$/.test(input.host) || !Number.isInteger(input.port) || input.port < 1 || input.port > 65535)) throw new Error("请选择私网设备并填写有效端口");
37 if (!await open()) return { installed: false, enabled: false, backend: "NotInstalled", ips: [], peers: [], keys: [] };
38 return new Promise((resolve, reject) => {
39 const current = child;
40 const timer = setTimeout(() => { current.kill(); reject(new Error("Tailscale 暂未响应,请稍后重试")); }, 12000);
41 pending = { resolve: value => { clearTimeout(timer); resolve(value); }, reject: error => { clearTimeout(timer); reject(error); } };
42 current.stdin.write(JSON.stringify({ action: input.action, ...(input.key === undefined ? {} : { key: input.key }), ...(input.action === "open-service" ? { host: input.host, port: input.port } : {}) }) + "\n");
43 });
44 }
45 return {
46 request(input) { const result = queue.then(() => run(input)); queue = result.catch(() => {}); return result; },
47 close() { child?.stdin.end(); },
48 restore() { return this.request({ action: "status" }); },
49 };
50}