跳到主要内容

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

浏览文件

macos_window_connection.go

144 行 · 4.4 KB历史下载
1package workbench
2
3import (
4 "bufio"
5 "context"
6 "encoding/json"
7 "math"
8 "net/http"
9 "time"
10)
11
12func validMacOSWindowRequest(input macOSWindowRequest) bool {
13 switch input.Action {
14 case "status", "launch", "stop", "request-screen", "request-input", "frame", "text", "key", "click", "scroll", "pointer", "ime", "input-reset":
15 default:
16 return false
17 }
18 if len(input.After) > 128 || len(input.Geometry) > 128 || input.Width < 0 || input.Width > 2560 || input.Height < 0 || input.Height > 1600 || input.Scale < 0 || input.Scale > 2 {
19 return false
20 }
21 if len(input.Text) > 8192 || len(input.Key) > 64 || len(input.Code) > 64 {
22 return false
23 }
24 if input.Action == "pointer" || input.Action == "ime" || (input.Action == "key" && input.Code != "") {
25 if input.PID <= 0 || input.Window == 0 || input.Geometry == "" {
26 return false
27 }
28 }
29 if input.Action == "pointer" {
30 if input.Phase != "down" && input.Phase != "move" && input.Phase != "up" {
31 return false
32 }
33 if input.Buttons < 0 || input.Buttons > 7 || input.X == nil || input.Y == nil ||
34 math.IsNaN(*input.X) || math.IsNaN(*input.Y) || *input.X < 0 || *input.X >= 1 || *input.Y < 0 || *input.Y >= 1 {
35 return false
36 }
37 if input.Phase != "move" && (input.Button == nil || *input.Button < 0 || *input.Button > 2 || input.Clicks < 1 || input.Clicks > 3) {
38 return false
39 }
40 }
41 if input.Action == "key" && input.Code != "" && input.Down == nil {
42 return false
43 }
44 if input.Action == "ime" {
45 if input.Mode != "status" && input.Mode != "begin" && input.Mode != "update" && input.Mode != "commit" && input.Mode != "cancel" {
46 return false
47 }
48 if input.Mode != "status" && (!validInputToken(input.Session) || input.Sequence < 0) {
49 return false
50 }
51 if input.Mode == "begin" && !validInputToken(input.Context) {
52 return false
53 }
54 }
55 return (input.Width == 0 && input.Height == 0 && input.Scale == 0) ||
56 (input.Action == "frame" && input.Width >= 320 && input.Height >= 240 && input.Scale >= 1)
57}
58
59func macOSWindowClientRequest(input macOSWindowRequest, keepAlive bool) macOSWindowRequest {
60 input.Project, input.Nonce = "", ""
61 input.Phone, input.PhoneToken = nil, ""
62 input.Transfer, input.Path, input.Error = "", "", ""
63 input.IDs = nil
64 input.Wait, input.Binary, input.KeepAlive = false, false, keepAlive
65 if input.Action != "ime" {
66 input.Sequence = 0
67 }
68 return input
69}
70
71// One authenticated WebSocket owns one ordinary-user SSH channel. Requests are
72// ordered on that channel, including input and capture geometry changes.
73func (s *Server) macOSWindowConnection(w http.ResponseWriter, r *http.Request) {
74 if s.macosGuest == nil {
75 http.NotFound(w, r)
76 return
77 }
78 ws, err := s.upgrade(w, r)
79 if err != nil {
80 return
81 }
82 defer ws.Close()
83 ws.SetReadLimit(64 << 10)
84 ctx, cancel := context.WithCancel(r.Context())
85 defer cancel()
86 stop := context.AfterFunc(s.lifetimeContext(), func() { cancel(); ws.Close() })
87 defer stop()
88 cmd := s.sshCommand(ctx, "-T", s.sshTarget(), "--", `exec /usr/bin/nc -U "$HOME/Library/Caches/dev.cage.GuestClaudeWindow/window.sock"`)
89 input, err := cmd.StdinPipe()
90 if err != nil {
91 return
92 }
93 defer input.Close()
94 output, err := cmd.StdoutPipe()
95 if err != nil {
96 return
97 }
98 defer output.Close()
99 if err = cmd.Start(); err != nil {
100 return
101 }
102 defer func() { cancel(); input.Close(); output.Close(); _ = cmd.Wait() }()
103 scanner := bufio.NewScanner(output)
104 scanner.Buffer(make([]byte, 64<<10), 12<<20)
105 for {
106 var message struct {
107 ID uint64 `json:"id"`
108 Request macOSWindowRequest `json:"request"`
109 }
110 if err = ws.ReadJSON(&message); err != nil {
111 return
112 }
113 if !validMacOSWindowRequest(message.Request) {
114 return
115 }
116 message.Request = macOSWindowClientRequest(message.Request, true)
117 body, err := json.Marshal(message.Request)
118 if err != nil || len(body) > 64<<10 {
119 return
120 }
121 limit := 12 * time.Second
122 if message.Request.Action == "launch" {
123 limit = 55 * time.Second
124 }
125 timer := time.AfterFunc(limit, func() { cancel(); output.Close(); ws.Close() })
126 _, err = input.Write(append(body, '\n'))
127 if err != nil || !scanner.Scan() {
128 timer.Stop()
129 return
130 }
131 timer.Stop()
132 data := append(json.RawMessage(nil), scanner.Bytes()...)
133 if !json.Valid(data) {
134 return
135 }
136 _ = ws.SetWriteDeadline(time.Now().Add(12 * time.Second))
137 if ws.WriteJSON(struct {
138 ID uint64 `json:"id"`
139 Result json.RawMessage `json:"result"`
140 }{message.ID, data}) != nil {
141 return
142 }
143 }
144}