跳到主要内容

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

浏览文件

macos-phone-browser.mjs

78 行 · 3.5 KB历史下载
1// The phone owns a separate browser profile and the shared public-web policy.
2import { homedir } from "node:os";
3import path from "node:path";
4import { pathToFileURL } from "node:url";
5import { PhoneBrowser } from "./modules/browser-phone.mjs";
6import { phoneError } from "./modules/browser-phone-network.mjs";
7import { lines } from "./windows-browser.mjs";
8
9export async function runMacOSPhoneBrowser(input = process.stdin, output = process.stdout, options = {}) {
10 if (process.platform !== "darwin" && options.testing !== true) throw new Error("此浏览器需要 macOS 客机");
11 const phone = new PhoneBrowser({
12 binary: options.binary || "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
13 directory: options.directory || path.join(homedir(), "Library", "Application Support", "Cage", "PhoneBrowser"),
14 language: options.language || Intl.DateTimeFormat().resolvedOptions().locale,
15 connect: options.connect,
16 });
17 // This controller is private to one paired phone. Keep its cold launch
18 // alive between frame polls, while close, SSH EOF and timeout can stop it.
19 let opening;
20 async function cancelOpen() {
21 if (opening) {
22 opening.abort.abort();
23 await opening.done;
24 opening = undefined;
25 }
26 await phone.dispose();
27 }
28 async function operate(operation, input) {
29 if (opening && input?.id !== opening.id) throw phoneError(410, "手机浏览器会话已结束");
30 if (opening && operation === "close") {
31 await cancelOpen();
32 return { id: input.id };
33 }
34 if (!opening && operation === "open") {
35 const entry = { id: input?.id, abort: new AbortController(), settled: false };
36 opening = entry;
37 const deadline = setTimeout(() => entry.abort.abort(), 75000);
38 entry.done = phone.run("open", input, entry.abort.signal).catch(error => {
39 entry.error = entry.abort.signal.aborted ? phoneError(503, "客机浏览器启动超时") : error;
40 }).finally(() => {
41 entry.settled = true;
42 clearTimeout(deadline);
43 });
44 // Return quick URL/parameter rejections on the original open request.
45 let timer;
46 try { await Promise.race([entry.done, new Promise(resolve => { timer = setTimeout(resolve, 200); })]); }
47 finally { clearTimeout(timer); }
48 }
49 if (opening) {
50 if (opening.settled) {
51 const { error } = opening;
52 opening = undefined;
53 if (error) throw error;
54 } else if (operation !== "open") {
55 throw phoneError(425, "正在启动虚拟 Mac 中的浏览器");
56 }
57 if (operation === "open") return { id: input.id };
58 }
59 return phone.run(operation, input);
60 }
61 try {
62 for await (const line of lines(input)) {
63 let result;
64 try {
65 const request = JSON.parse(line);
66 if (request.method !== "POST" || !/^\/phone-browser\/(open|frame|action|close)$/.test(request.path || "")) throw new Error("手机浏览器请求无效");
67 result = await operate(request.path.split("/").at(-1), request.input);
68 } catch (error) {
69 result = { error: "手机浏览器操作未完成,请核对授权和最新画面", code: "phone_browser_error", status: [403, 409, 410, 425, 503].includes(error.status) ? error.status : 400 };
70 }
71 await new Promise((resolve, reject) => output.write(JSON.stringify(result) + "\n", error => error ? reject(error) : resolve()));
72 }
73 } finally { await cancelOpen(); }
74}
75
76if (process.argv[1] && pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url) {
77 await runMacOSPhoneBrowser().catch(() => { process.exitCode = 1; });
78}