跳到主要内容

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

浏览文件

remote_entry.go

108 行 · 4.0 KB历史下载
1package workbench
2
3import (
4 "encoding/json"
5 "errors"
6 "net/http"
7 "net/url"
8 "os"
9 "path/filepath"
10 "regexp"
11 "runtime"
12
13 "github.com/sylvinus/agent-vm/internal/host"
14 "github.com/sylvinus/agent-vm/internal/version"
15)
16
17const remoteAPIProtocol = 1
18
19var remoteInstanceID = regexp.MustCompile(`^[a-f0-9]{64}$`)
20
21type remoteInstance struct {
22 ID string `json:"id"`
23}
24
25// The instance identity belongs to persistent server data, not its address,
26// filesystem spelling, current process or the client's operating system.
27func ensureRemoteInstance(profile string) (string, error) {
28 path := filepath.Join(profile, "remote-instance.json")
29 if err := host.CheckPrivateFile(path); err == nil {
30 data, err := os.ReadFile(path)
31 var instance remoteInstance
32 if err != nil || json.Unmarshal(data, &instance) != nil || !remoteInstanceID.MatchString(instance.ID) {
33 return "", errors.New("远程实例身份记录无效,请保留数据并检查配置")
34 }
35 return instance.ID, nil
36 } else if !os.IsNotExist(err) {
37 return "", err
38 }
39 instance := remoteInstance{ID: randomToken()}
40 if err := writePrivate(path, instance); err != nil {
41 return "", err
42 }
43 return instance.ID, nil
44}
45
46type RemoteEntry struct {
47 APIProtocol int `json:"api_protocol"`
48 InstanceID string `json:"instance_id"`
49 Version string `json:"version"`
50 Platform string `json:"platform"`
51 Runtime string `json:"runtime"`
52 Origin string `json:"origin"`
53 Ticket string `json:"ticket,omitempty"`
54 Capabilities map[string]bool `json:"capabilities"`
55}
56
57func (s *Server) remoteManifest(w http.ResponseWriter, r *http.Request) {
58 if s.macosGuest != nil {
59 jsonReply(w, RemoteEntry{APIProtocol: remoteAPIProtocol, InstanceID: s.instanceID, Version: version.Version, Platform: runtime.GOOS,
60 Runtime: "lima", Origin: s.origin, Capabilities: map[string]bool{"terminal": true, "projects_browse": true}})
61 return
62 }
63 kind := "lima"
64 if _, err := os.Stat(filepath.Join(s.profile, "container.json")); err == nil {
65 kind = "container"
66 }
67 jsonReply(w, RemoteEntry{APIProtocol: remoteAPIProtocol, InstanceID: s.instanceID,
68 Version: version.Version, Platform: runtime.GOOS, Runtime: kind, Origin: s.origin,
69 Capabilities: map[string]bool{"projects_browse": true, "client_project_share": runtime.GOOS == "linux" && kind == "container", "terminal": true,
70 "browser": true, "desktop": true, "audio": true}})
71}
72
73// ServerEntry is used only over an authenticated SSH command channel. It
74// attaches to the existing server without triggering desktop build takeover.
75// The returned ticket must travel on that private pipe, never a shared log.
76func ServerEntry(configPath string) (RemoteEntry, error) {
77 var result RemoteEntry
78 c, err := connectController(filepath.Dir(configPath))
79 if err != nil {
80 return result, errors.New("云端 Cage 服务尚未运行或无法认证,请检查服务状态")
81 }
82 defer c.close()
83 entry, err := url.Parse(c.launch.URL)
84 if err != nil {
85 return result, err
86 }
87 status, err := c.call("POST", "/api/session", map[string]string{"token": entry.Fragment}, nil)
88 if err != nil || status != http.StatusOK {
89 return result, errors.New("无法建立云端入口会话")
90 }
91 status, err = c.call("GET", "/api/remote/manifest", nil, &result)
92 if err != nil || status != http.StatusOK || !remoteInstanceID.MatchString(result.InstanceID) {
93 return RemoteEntry{}, errors.New("云端服务不支持当前远程连接协议")
94 }
95 // The manifest lookup consumed the first ticket; issue another for the
96 // connecting client's private transport.
97 var launch launchResponse
98 status, err = c.call("POST", "/api/launch", map[string]string{"token": c.record.Token}, &launch)
99 if err != nil || status != http.StatusOK {
100 return RemoteEntry{}, errors.New("无法取得云端一次性入口")
101 }
102 u, err := url.Parse(launch.URL)
103 if err != nil || u.Scheme+"://"+u.Host != c.origin || u.Fragment == "" {
104 return RemoteEntry{}, errors.New("云端入口无效")
105 }
106 result.Ticket = u.Fragment
107 return result, nil
108}