跳到主要内容

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

浏览文件

desktop_local_legacy.go

78 行 · 3.1 KB历史下载
1package workbench
2
3import (
4 "context"
5 "crypto/sha256"
6 "errors"
7 "fmt"
8 "io"
9 "net/http"
10 "net/url"
11 "os"
12)
13
14const previousLocalWorkbench = "请用 cage desktop --existing 打开已有本机工作台,保存并停止任务后重试"
15
16// Only explicit local selection calls this path. An older full controller
17// can release an idle profile via its own authenticated update handoff. Its
18// process and workloads remain under its control throughout this exchange.
19func (d *desktopLocal) handoffOldLocal(ctx context.Context) (bool, error) {
20 c, err := connectController(d.profile)
21 if err != nil {
22 return false, errors.New("无法认证已有本机后台。" + previousLocalWorkbench)
23 }
24 defer c.close()
25 id := c.launch.Controller
26 if c.launch.DesktopGateway || id.Protocol != controlProtocol || id.PID != c.record.PID || !id.matches(c.record.Controller) || !remoteInstanceID.MatchString(id.Build) {
27 return false, errors.New("已有本机后台身份或协议不兼容。" + previousLocalWorkbench)
28 }
29 u, _ := url.Parse(c.launch.URL) // connectController verified the origin and ticket.
30 status, err := c.callContext(ctx, "POST", "/api/session", map[string]string{"token": u.Fragment}, nil)
31 if err != nil || status != http.StatusOK || len(c.client.Jar.Cookies(u)) == 0 {
32 return false, errors.New("无法建立已有本机后台的会话。" + previousLocalWorkbench)
33 }
34 // Old controllers can answer 401 before the cookie and 423 while locked,
35 // even for an unknown route. A working modern route must never be replaced.
36 status, err = c.callContext(ctx, "POST", "/api/desktop-local/status", desktopLocalRequest{Token: c.record.Token}, nil)
37 if err != nil {
38 return false, err
39 }
40 switch status {
41 case http.StatusOK:
42 return false, nil
43 case http.StatusNotFound, http.StatusMethodNotAllowed, http.StatusLocked:
44 default:
45 return false, errors.New("无法确认已有本机后台版本。" + previousLocalWorkbench)
46 }
47 f, err := os.Open(d.executable)
48 if err != nil {
49 return false, err
50 }
51 defer f.Close()
52 h := sha256.New()
53 if _, err = io.Copy(h, f); err != nil {
54 return false, err
55 }
56 if id.Build == fmt.Sprintf("%x", h.Sum(nil)) {
57 return false, errors.New("本机后台的启动器接口不可用。" + previousLocalWorkbench)
58 }
59 var selection ClientContext
60 status, err = c.callContext(ctx, "GET", "/api/client/context", nil, &selection)
61 if err != nil || (status != http.StatusOK && status != http.StatusNotFound && status != http.StatusLocked) {
62 return false, errors.New("无法确认旧工作台的连接状态。" + previousLocalWorkbench)
63 }
64 if status == http.StatusOK && (selection.ClientOnly || selection.Mode != "local") {
65 return false, errors.New("已有工作台正在管理其他连接。" + previousLocalWorkbench)
66 }
67 var ack struct {
68 Stopping bool `json:"stopping"`
69 }
70 status, err = c.callContext(ctx, "POST", "/api/handoff", map[string]string{"token": c.record.Token}, &ack)
71 if err != nil || status != http.StatusOK || !ack.Stopping {
72 return false, errors.New("旧版本机后台仍在使用,或尚未确认空闲退出。" + previousLocalWorkbench)
73 }
74 if err := c.waitForExit(); err != nil {
75 return false, err
76 }
77 return true, nil
78}