跳到主要内容

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

浏览文件

proxy.go

94 行 · 2.6 KB历史下载
1package main
2
3import (
4 "context"
5 "encoding/json"
6 "github.com/sylvinus/agent-vm/internal/cage"
7 "github.com/sylvinus/agent-vm/internal/guestos"
8 "github.com/sylvinus/agent-vm/internal/managedproxy"
9 "os"
10 "path/filepath"
11)
12
13func proxyCommand(command, profile string, cfg cage.Config) error {
14 if command == "proxy-stop" {
15 if err := closeManagedGates(profile); err != nil {
16 return err
17 }
18 return managedproxy.Stop(profile)
19 }
20 p, err := managedproxy.Applied(profile, cfg.ProxyURL, cfg.ProxyRevision)
21 if err != nil {
22 return err
23 }
24 name, err := managedproxy.PhysicalInterface(context.Background())
25 if err != nil {
26 _ = closeManagedGates(profile)
27 return err
28 }
29 bound, _ := managedproxy.BoundInterface(filepath.Join(managedproxy.Dir(profile), "config.yaml"))
30 if bound == name && managedproxy.Running(context.Background(), profile, p) {
31 return nil
32 }
33 if err = closeManagedGates(profile); err != nil {
34 return err
35 }
36 p.Revision = managedproxy.Token()
37 cfg.ProxyRevision = p.Revision
38 if err = p.Save(profile); err != nil {
39 return err
40 }
41 path := os.Getenv(cage.ConfigEnv)
42 if path == "" {
43 path = filepath.Join(profile, "config.json")
44 }
45 data, _ := json.MarshalIndent(cfg, "", " ")
46 if err = managedproxy.Write(path, data); err != nil {
47 return err
48 }
49 files, err := managedGateFiles(profile)
50 if err != nil {
51 return err
52 }
53 for _, file := range files {
54 if client, e := cage.OpenGateClient(filepath.Dir(file)); e == nil {
55 if _, e = client.Status(context.Background()); e == nil {
56 if _, e = client.SyncProxyRevision(context.Background()); e != nil {
57 return e
58 }
59 }
60 }
61 }
62 return managedproxy.Start(context.Background(), profile, p)
63}
64
65func closeManagedGates(profile string) error {
66 files, err := managedGateFiles(profile)
67 if err != nil {
68 return err
69 }
70 for _, file := range files {
71 if client, e := cage.OpenGateClient(filepath.Dir(file)); e == nil {
72 if _, e = client.Status(context.Background()); e == nil {
73 if _, e = client.Close(context.Background(), "代理核心停止或重启,等待重新检测"); e != nil {
74 return e
75 }
76 }
77 }
78 }
79 return nil
80}
81
82func managedGateFiles(profile string) ([]string, error) {
83 files, _ := filepath.Glob(filepath.Join(profile, "state", "lima", "*", "cage-gate.json"))
84 containers, _ := filepath.Glob(filepath.Join(profile, "state", "container", "*", "cage-gate.json"))
85 files = append(files, containers...)
86 binding, err := guestos.LoadWindowsProfile(profile)
87 if err != nil {
88 return nil, err
89 }
90 if binding != nil {
91 files = append(files, filepath.Join(binding.InstanceDirectory, "cage-gate.json"))
92 }
93 return files, nil
94}