跳到主要内容

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

浏览文件

cleanup-apps.mjs

107 行 · 3.5 KB历史下载
1#!/usr/bin/env node
2// Remove Cage bundles only after the caller has verified the replacement.
3import { execFileSync } from "node:child_process";
4import { lstat, readdir, realpath, rm } from "node:fs/promises";
5import path from "node:path";
6import { pathToFileURL } from "node:url";
7
8const bundleID = "io.github.lee-xeo.claude-cage";
9function isCage(bundle) {
10 try {
11 return (
12 execFileSync(
13 "/usr/libexec/PlistBuddy",
14 [
15 "-c",
16 "Print :CFBundleIdentifier",
17 path.join(bundle, "Contents/Info.plist"),
18 ],
19 { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] },
20 ).trim() === bundleID
21 );
22 } catch {
23 return false;
24 }
25}
26function inUse(bundle) {
27 const files = ["Contents/MacOS/Cage", "Contents/Resources/cage"].map((file) => path.join(bundle, file));
28 try {
29 return execFileSync("/usr/sbin/lsof", ["-t", ...files], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim() !== "";
30 } catch (error) {
31 if (String(error.stdout || "").trim()) return true;
32 if (error.status === 1) return false;
33 throw error;
34 }
35}
36function intact(bundle) {
37 try {
38 execFileSync('/usr/bin/codesign', ['--verify', '--deep', '--strict', bundle], { stdio: 'ignore' });
39 return true;
40 } catch { return false; }
41}
42
43export async function cleanupApps({ keep, roots, onInUse = () => {}, onModified = () => {} }) {
44 if (!keep.length)
45 throw new Error("At least one verified Cage.app must be kept.");
46 const retained = new Set();
47 for (const bundle of keep) {
48 if (!isCage(bundle) || !intact(bundle)) throw new Error(`Invalid replacement App: ${bundle}`);
49 retained.add(await realpath(bundle));
50 }
51 const removed = [];
52 async function visit(directory) {
53 let stat;
54 try {
55 stat = await lstat(directory);
56 } catch (error) {
57 if (error.code === "ENOENT") return;
58 throw error;
59 }
60 if (!stat.isDirectory() || stat.isSymbolicLink()) return;
61 const canonical = await realpath(directory);
62 if (retained.has(canonical)) return;
63 if (directory.endsWith(".app")) {
64 if (isCage(directory)) {
65 if (!intact(directory)) {
66 onModified(directory);
67 return;
68 }
69 if (inUse(directory)) {
70 onInUse(directory);
71 return;
72 }
73 await rm(directory, { recursive: true });
74 removed.push(directory);
75 }
76 return;
77 }
78 for (const entry of await readdir(directory))
79 await visit(path.join(directory, entry));
80 }
81 for (const root of roots) await visit(path.resolve(root));
82 return removed;
83}
84
85if (
86 process.argv[1] &&
87 import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href
88) {
89 const keep = [],
90 roots = [];
91 for (let i = 2; i < process.argv.length; i += 2) {
92 const flag = process.argv[i],
93 value = process.argv[i + 1];
94 if (!value || !["--keep", "--root"].includes(flag))
95 throw new Error(
96 "Usage: cleanup-apps.mjs --keep <verified-app> [--keep <app>] --root <directory> ...",
97 );
98 (flag === "--keep" ? keep : roots).push(value);
99 }
100 if (!roots.length) throw new Error("At least one cleanup root is required.");
101 for (const bundle of await cleanupApps({
102 keep, roots,
103 onInUse: (bundle) => console.error(`Old Cage App is still running; quit it and rerun cleanup: ${bundle}`),
104 onModified: (bundle) => console.error(`旧应用包含修改内容或无法验证完整性,文件已保留:${bundle}`),
105 }))
106 console.log(`Removed old App: ${bundle}`);
107}