跳到主要内容

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

浏览文件

desktop_local_control.go

133 行 · 4.5 KB历史下载
1package workbench
2
3import (
4 "crypto/sha256"
5 "fmt"
6 "net/http"
7 "os"
8 "sort"
9)
10
11// The launcher authenticates these requests with the private controller record.
12// The browser-facing gateway never forwards this endpoint or exposes its token.
13type desktopLocalRequest struct {
14 Token string `json:"token"`
15 ControllerID string `json:"controller_id,omitempty"`
16 Stop bool `json:"stop,omitempty"`
17 Running bool `json:"running,omitempty"`
18 Terminals int `json:"terminals,omitempty"`
19 JobID string `json:"job_id,omitempty"`
20}
21
22type desktopLocalReply struct {
23 DesktopLocalStatus
24 Identity ControllerIdentity `json:"identity"`
25 OwnerID string `json:"owner_id"`
26 InstanceID string `json:"instance_id"`
27 LaunchID string `json:"launch_id,omitempty"`
28 JobID string `json:"job_id,omitempty"`
29 JobRunning bool `json:"job_running,omitempty"`
30 JobFailed bool `json:"job_failed,omitempty"`
31}
32
33func localControllerID(token string) string {
34 return fmt.Sprintf("%x", sha256.Sum256([]byte("desktop-local\x00"+token)))
35}
36
37func (s *Server) desktopLocalSnapshot() desktopLocalReply {
38 job := s.jobs.snapshot()
39 reply := desktopLocalReply{
40 DesktopLocalStatus: DesktopLocalStatus{Available: true, ServiceRunning: true},
41 Identity: s.identity, OwnerID: localControllerID(s.token), InstanceID: s.instanceID,
42 LaunchID: os.Getenv("CAGE_DESKTOP_START_ID"), JobID: job.ID, JobRunning: job.Running, JobFailed: job.Error != "",
43 }
44 owners, err := controllerVMOwners(s.profile)
45 reply.Running = len(owners) != 0
46 if err != nil {
47 reply.Message = "本机环境状态待确认"
48 return reply
49 }
50 reply.Known = true
51 var terminals []string
52 for _, session := range s.terminals.snapshot() {
53 if session["closed"] == false {
54 reply.LiveTerminals++
55 id, _ := session["id"].(string)
56 terminals = append(terminals, id)
57 }
58 }
59 s.mu.Lock()
60 reply.Busy = s.closing || job.Running
61 s.mu.Unlock()
62 if len(s.mobileCLI.activeAccounts()) != 0 || (s.remote != nil && s.remote.isRemote()) {
63 reply.Busy = true
64 }
65 if reply.Busy {
66 reply.Message = "本机控制服务正在执行操作或被其他入口使用"
67 }
68 // The same owner can acquire new work while the confirmation dialog is
69 // open. Bind its public stop confirmation to the observed workload too.
70 sort.Strings(terminals)
71 reply.ControllerID = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("%s\x00%q\x00%s\x00%t\x00%q", reply.OwnerID, owners, job.ID, reply.Busy, terminals))))
72 return reply
73}
74
75func (s *Server) desktopLocalControl(w http.ResponseWriter, r *http.Request) {
76 var request desktopLocalRequest
77 if !readJSON(w, r, &request) {
78 return
79 }
80 if s.clientOnly || !equalSecret(request.Token, s.token) {
81 http.Error(w, "Launcher refused", http.StatusUnauthorized)
82 return
83 }
84 switch r.PathValue("action") {
85 case "status":
86 jsonReply(w, s.desktopLocalSnapshot())
87 case "release":
88 if !s.op.TryLock() {
89 http.Error(w, "本机正在执行操作,请稍后重试", http.StatusConflict)
90 return
91 }
92 view := s.desktopLocalSnapshot()
93 if !view.Known || view.Busy || request.ControllerID != view.ControllerID || request.Running != view.Running || request.Terminals != view.LiveTerminals || (request.JobID != "" && request.JobID != view.JobID) {
94 s.op.Unlock()
95 http.Error(w, "本机任务或控制服务已变化,请重新确认", http.StatusConflict)
96 return
97 }
98 if !view.Running && view.LiveTerminals == 0 {
99 s.handoffLocked(w)
100 return
101 }
102 defer s.op.Unlock()
103 if !request.Stop {
104 http.Error(w, "本机环境仍在运行", http.StatusConflict)
105 return
106 }
107 if _, unlocked := s.account.access(); !unlocked {
108 http.Error(w, "请先返回本机解锁,再停止本机环境", http.StatusLocked)
109 return
110 }
111 // This stop may not cancel another admitted operation. Check and submit
112 // under the same admission lock used by submitJob, then let the existing
113 // job manager execute stopEnvironment under the operation lock.
114 s.mu.Lock()
115 if s.closing || s.jobs.snapshot().Running {
116 s.mu.Unlock()
117 http.Error(w, "本机正在执行操作,请稍后重试", http.StatusConflict)
118 return
119 }
120 job, err := s.jobs.submit(&s.op, "stop", s.safety.pauseVersion, nil, nil, func() error {
121 _ = s.pauseNetworkNow("正在停止本机环境")
122 return s.stopEnvironment()
123 })
124 s.mu.Unlock()
125 if err != nil {
126 requestError(w, err)
127 return
128 }
129 jsonReply(w, desktopLocalReply{JobID: job.ID, JobRunning: true})
130 default:
131 http.NotFound(w, r)
132 }
133}