跳到主要内容

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

浏览文件

browser-settings-fixture.mjs

68 行 · 5.0 KB历史下载
1import { execFileSync } from 'node:child_process';
2import http from 'node:http';
3import { createWorkbench } from './workbench-fixture.mjs';
4import { createPrivacyController } from '../../scripts/browser-privacy-fixture.mjs';
5export async function createSettingsWorkbench(options = {}) {
6 const clientContext = options.clientContext || { mode: 'local', connection_id: 'local', client_platform: 'darwin', server_platform: 'darwin', connection: { state: 'disconnected' } };
7 const controller = await createPrivacyController();
8 const fixtureState = { failSave: false, home: controller.origin + '/home' };
9 const archive = execFileSync('uv', ['run', '--no-project', 'python', '-c', `import io,json,sys,zipfile
10b=io.BytesIO()
11with zipfile.ZipFile(b,'w') as z:
12 z.writestr('manifest.json',json.dumps({'manifest_version':3,'name':'阅读助手','version':'1.0','description':'调整网页阅读体验。','permissions':['storage'],'content_scripts':[{'matches':['http://localhost/*'],'js':['content.js']}]}))
13 z.writestr('content.js','document.documentElement.dataset.cageSettingsExtension="active";')
14sys.stdout.buffer.write(b.getvalue())`], { stdio: ['ignore', 'pipe', 'ignore'], maxBuffer: 1 << 20 });
15 const profileID = 'a'.repeat(32), extensionID = 'b'.repeat(32);
16 // WebKit's routing protocol omits Blob upload bodies. Keep those uploads on
17 // real HTTP, so the controller receives the actual File bytes in both engines.
18 const proxy = http.createServer(async (request, response) => {
19 if (request.url === '/api/browser/extensions/upload' && request.method === 'POST') {
20 try {
21 const chunks = []; for await (const chunk of request) chunks.push(chunk);
22 const result = await controller.uploadExtension(Buffer.concat(chunks));
23 response.setHeader('Content-Type', 'application/json'); response.end(JSON.stringify(result));
24 } catch (error) { response.writeHead(400); response.end(error.message); }
25 return;
26 }
27 if (request.url.startsWith('/api/')) { response.writeHead(404); response.end(); return; }
28 const target = new URL(request.url, process.env.CAGE_UI_URL || 'http://127.0.0.1:5198');
29 const upstream = http.request(target, { headers: { ...request.headers, host: target.host } }, source => { response.writeHead(source.statusCode, source.headers); source.pipe(response); });
30 upstream.on('error', error => { response.writeHead(502); response.end(error.message); }); request.pipe(upstream);
31 });
32 await new Promise(resolve => proxy.listen(0, '127.0.0.1', resolve));
33 let workbench;
34 try {
35 workbench = await createWorkbench({ ...options, clientContext, url: `http://127.0.0.1:${proxy.address().port}`,
36 browserRequest: async (route, path, body) => {
37 if (path === '/browser/extensions/upload') return route.continue();
38 if (path === '/browser/resize') return route.fulfill({ json: { ok: true } });
39 if (path === '/browser/preferences') {
40 if (body) {
41 if (fixtureState.failSave) return route.fulfill({ status: 500, contentType: 'text/plain; charset=utf-8', body: '保存浏览器设置失败,请重试' });
42 await controller.api('/homepage', { url: body.browser_home_url }); fixtureState.home = body.browser_home_url;
43 }
44 return route.fulfill({ json: { browser_home_url: fixtureState.home } });
45 }
46 try {
47 const result = await controller.api(path.replace('/browser', ''), body);
48 return route.fulfill({ json: result });
49 } catch (error) { return route.fulfill({ status: 400, contentType: 'text/plain; charset=utf-8', body: error.message }); }
50 },
51 clientRequest: async (route, path, body) => {
52 if (path === '/client/context') return route.fulfill({ json: clientContext });
53 if (path === '/client/browser-extensions') {
54 if (body) return route.fulfill({ contentType: 'application/zip', body: archive });
55 return route.fulfill({ json: { profiles: [{ id: profileID, browser: 'Microsoft Edge', name: '个人配置', extensions: [
56 { id: extensionID, name: '阅读助手', version: '1.0', enabled: false, state_known: true, compatible: true, identity_preserved: false, permissions: ['storage'], hostPermissions: ['http://localhost/*'] },
57 { id: 'c'.repeat(32), name: '旧版扩展', version: '2.0', enabled: true, state_known: true, compatible: false, identity_preserved: false, permissions: [], hostPermissions: [] },
58 ] }] } });
59 }
60 return route.fulfill({ json: path === '/client/hosts' ? { hosts: [] } : {} });
61 },
62 });
63 workbench.state.status.ready = workbench.state.status.vm_running = true;
64 workbench.state.settings.browser_home_url = fixtureState.home;
65 await workbench.page.reload();
66 return { ...workbench, controller, fixtureState, archive, profileID, extensionID, close: async () => { await workbench.browser.close(); await new Promise(resolve => proxy.close(resolve)); await controller.close(); } };
67 } catch (error) { await workbench?.browser.close(); await new Promise(resolve => proxy.close(resolve)); await controller.close(); throw error; }
68}