跳到主要内容

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

浏览文件

windows_ccs_launch.cjs

77 行 · 4.3 KB历史下载
1'use strict';
2
3// Runs inside the real CCS parent. Only its fixed native Claude spawn is
4// wrapped; all proxy and temporary-profile lifetime remains owned by CCS.
5const cp = require('node:child_process');
6const fs = require('node:fs');
7const path = require('node:path');
8const crypto = require('node:crypto');
9
10const uuid = /^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i;
11const profile = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/;
12function readObject(filename) {
13 const info = fs.lstatSync(filename);
14 if (!info.isFile() || info.isSymbolicLink() || info.size > 1 << 20) throw new Error('ccs_launch_file_invalid');
15 const value = JSON.parse(fs.readFileSync(filename, 'utf8'));
16 if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error('ccs_launch_file_invalid');
17 return value;
18}
19function samePath(a, b) {
20 const normalize = value => process.platform === 'win32' ? path.resolve(value).toLowerCase() : path.resolve(value);
21 return typeof a === 'string' && typeof b === 'string' && normalize(a) === normalize(b);
22}
23function settingsSource(args) {
24 let source = '';
25 for (let i = 0; i < args.length; i++) {
26 if (args[i] === '--settings') { if (typeof args[++i] !== 'string' || source) return ''; source = args[i]; }
27 else if (args[i].startsWith('--settings=')) { if (source) return ''; source = args[i].slice(11); }
28 }
29 return source && !source.trimStart().startsWith('{') && path.isAbsolute(source) ? path.resolve(source) : '';
30}
31function install(context, originalSpawn = cp.spawn) {
32 const previousSpawn = cp.spawn;
33 if (!uuid.test(context.id) || !uuid.test(context.session_id) || !profile.test(context.profile)) throw new Error('ccs_launch_context_invalid');
34 const folder = path.resolve(context.folder);
35 if (fs.lstatSync(folder).isSymbolicLink()) throw new Error('ccs_launch_directory_invalid');
36 let captured = false;
37 cp.spawn = function (command, args, options) {
38 if (!samePath(command, context.binary) || !Array.isArray(args) || options?.shell ||
39 !args.some((arg, index) => (arg === '--session-id' || arg === '--resume') && args[index + 1] === context.session_id)) {
40 return originalSpawn.apply(this, arguments);
41 }
42 if (captured) throw new Error('ccs_duplicate_claude_spawn');
43 captured = true;
44 if (args.length > 1024 || args.some(arg => typeof arg !== 'string' || arg.includes('\0'))) throw new Error('ccs_launch_arguments_invalid');
45 const source = settingsSource(args);
46 const metadata = {id:context.id, session_id:context.session_id, profile:context.profile,
47 actual_args:args, settings_source:source, expected_settings_source:context.settings_source || '',
48 profile_type:options?.env?.CCS_PROFILE_TYPE || ''};
49 const filename = path.join(folder, `ccs-actual-${crypto.randomUUID()}.json`);
50 if (Buffer.byteLength(JSON.stringify(metadata)) > 1 << 20) throw new Error('ccs_launch_arguments_limit');
51 fs.writeFileSync(filename, JSON.stringify(metadata), {flag:'wx', mode:0o600});
52 let child;
53 try {
54 child = originalSpawn(context.python, ['-X','utf8','-I',context.helper,'ccs-live',filename],
55 {...options, env:{...(options?.env || process.env), CAGE_CCS_ACTUAL:'1'}});
56 } catch (error) { fs.unlinkSync(filename); throw error; }
57 const cleanup = () => { try { fs.unlinkSync(filename); } catch (error) { if (error.code !== 'ENOENT') throw error; } };
58 child.once('exit', cleanup); child.once('error', cleanup);
59 return child;
60 };
61 return () => { cp.spawn = previousSpawn; };
62}
63
64if (process.env.CAGE_CCS_LAUNCH_FILE) {
65 const filename = process.env.CAGE_CCS_LAUNCH_FILE;
66 const context = readObject(filename);
67 if (!samePath(path.dirname(filename), context.folder) || path.basename(filename) !== 'ccs-launch.json') throw new Error('ccs_launch_context_invalid');
68 // This reads only the selected guest-owned CCS profile. No host configuration
69 // or credentials are imported. The actual child remains authoritative.
70 try {
71 const manager = require(path.join(path.dirname(context.main), 'utils', 'config-manager.js'));
72 const source = manager.getSettingsPath(context.profile);
73 if (typeof source === 'string' && path.isAbsolute(source)) context.settings_source = source;
74 } catch { context.settings_source = ''; }
75 install(context);
76}
77module.exports = {install, readObject, samePath, settingsSource};