跳到主要内容

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

浏览文件

macos_desktop_files.go

161 行 · 5.8 KB历史下载
1package workbench
2
3import (
4 "bufio"
5 "bytes"
6 "context"
7 "encoding/json"
8 "errors"
9 "io"
10 "path"
11 "strings"
12 "time"
13)
14
15func macOSFileFailure(reason string) error {
16 if err := desktopDownloadFailure(reason); err != nil {
17 return err
18 }
19 switch reason {
20 case "requires_gui_login":
21 return errors.New("请先登录并解锁虚拟 Mac 的普通账户")
22 case "accessibility_permission":
23 return errors.New("请在虚拟 Mac 中允许 Cage Guest Window 使用辅助功能,再重新传递文件")
24 case "window_changed", "file_dialog_changed", "gui_session_changed", "attachment_cancelled":
25 return errors.New("接收文件的 Claude 窗口已变化,请回到原窗口重新选择文件")
26 case "file_dialog_navigation_failed", "file_dialog_still_open", "file_dialog_selection_mismatch":
27 return errors.New("客机文件选择框尚未接收文件,请检查该选择框后重试")
28 case "attachment_incomplete":
29 return errors.New("附件尚未完整传入,请重新选择文件")
30 case "attachment_limit":
31 return errors.New("一次最多传递 100 个附件,请分批选择文件")
32 default:
33 return errors.New("虚拟 Mac 未能接收附件,请确认 Claude Desktop 已打开,且客机组件已更新")
34 }
35}
36
37func (s *Server) macOSDesktopFileCall(ctx context.Context, input macOSWindowRequest) (map[string]any, error) {
38 if input.Action == "files-start" && (input.Window == 0 || input.PID <= 0) {
39 return nil, macOSFileFailure("window_changed")
40 }
41 data, err := s.macOSWindowCall(ctx, input)
42 if err != nil {
43 return nil, err
44 }
45 var result map[string]any
46 if json.Unmarshal(data, &result) != nil {
47 return nil, errors.New("虚拟 Mac 附件响应无效")
48 }
49 if reason, _ := result["error"].(string); reason != "" {
50 return nil, macOSFileFailure(reason)
51 }
52 return result, nil
53}
54
55func validMacOSClipboardAttachment(value, name string) bool {
56 if path.Clean(value) != value || strings.ContainsAny(value, "\\\x00\r\n") || path.Base(value) != name {
57 return false
58 }
59 if !(strings.HasPrefix(value, "/var/folders/") || strings.HasPrefix(value, "/private/var/folders/") || strings.HasPrefix(value, "/tmp/") || strings.HasPrefix(value, "/private/tmp/")) {
60 return false
61 }
62 directory := path.Base(path.Dir(value))
63 suffix, ok := strings.CutPrefix(directory, "cage-clipboard-")
64 return ok && suffix != "" && strings.Trim(suffix, "0123456789") == ""
65}
66
67func (s *Server) macOSUploadDesktopAttachment(ctx context.Context, name, id string, body io.Reader) (map[string]any, error) {
68 if _, err := s.macOSDesktopFileCall(ctx, macOSWindowRequest{Action: "files-validate", ID: id}); err != nil {
69 return nil, err
70 }
71 cmd, _, err := s.clipboardCommand(ctx, "upload", clipboardFileSelection{})
72 if err != nil {
73 return nil, err
74 }
75 input, err := cmd.StdinPipe()
76 if err != nil {
77 return nil, err
78 }
79 output, err := cmd.StdoutPipe()
80 if err != nil {
81 input.Close()
82 return nil, err
83 }
84 cmd.Stderr = io.Discard
85 if err = cmd.Start(); err != nil {
86 input.Close()
87 output.Close()
88 return nil, err
89 }
90 finished := false
91 defer func() {
92 input.Close()
93 output.Close()
94 if !finished {
95 _ = cmd.Process.Kill()
96 _ = cmd.Wait()
97 }
98 }()
99 if err = json.NewEncoder(input).Encode(map[string]string{"name": name}); err == nil {
100 err = writeWindowsAttachmentFrames(input, body)
101 }
102 if err != nil {
103 return nil, err
104 }
105 input.Close()
106 var reply struct {
107 Paths []string `json:"paths"`
108 }
109 reader := bufio.NewScanner(output)
110 reader.Buffer(make([]byte, 4096), 65536)
111 if !reader.Scan() || json.Unmarshal(reader.Bytes(), &reply) != nil || len(reply.Paths) != 1 || !validMacOSClipboardAttachment(reply.Paths[0], name) {
112 return nil, errors.New("虚拟 Mac 附件没有完整保存,请重新传递")
113 }
114 err = cmd.Wait()
115 finished = true
116 if err != nil {
117 return nil, errors.New("虚拟 Mac 附件传递中断,请重新选择文件")
118 }
119 if _, err = s.macOSDesktopFileCall(ctx, macOSWindowRequest{Action: "files-add", ID: id, Files: reply.Paths}); err != nil {
120 // An upload may finish after its window or batch was canceled. Remove
121 // only the private file just returned by this successful upload.
122 cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
123 defer cancel()
124 request, _ := json.Marshal(map[string]any{"operation": "discard", "files": reply.Paths})
125 command := s.sshCommand(cleanup, "-T", s.sshTarget(), "--", "exec '/Applications/Cage Guest Clipboard.app/Contents/MacOS/CageGuestClipboard' --client request")
126 command.Stdin = bytes.NewReader(append(request, '\n'))
127 command.Stdout, command.Stderr = io.Discard, io.Discard
128 _ = command.Run()
129 return nil, err
130 }
131 return map[string]any{"ok": true, "id": id, "name": name}, nil
132}
133
134func (s *Server) macOSPasteDesktopImage(ctx context.Context, data []byte, window macOSWindowRequest) (map[string]any, error) {
135 if len(data) == 0 || len(data) > maxImageBytes {
136 return nil, errors.New("图片大小无效")
137 }
138 if window.Window == 0 || window.PID <= 0 {
139 return nil, macOSFileFailure("window_changed")
140 }
141 header, _ := json.Marshal(map[string]any{"action": "paste-image", "length": len(data), "pid": window.PID, "window": window.Window})
142 cmd := s.sshCommand(ctx, "-T", s.sshTarget(), "--", macOSWindowSocketCommand)
143 cmd.Stdin = io.MultiReader(bytes.NewReader(append(header, '\n')), bytes.NewReader(data))
144 cmd.Stderr = io.Discard
145 var reply bytes.Buffer
146 cmd.Stdout = &boundedWindowsStatusWriter{buffer: &reply, remaining: 65536}
147 if cmd.Run() != nil {
148 return nil, errors.New("图片未能传入虚拟 Mac,请确认 Claude Desktop 已打开")
149 }
150 var result map[string]any
151 if json.Unmarshal(reply.Bytes(), &result) != nil {
152 return nil, errors.New("虚拟 Mac 图片响应无效")
153 }
154 if reason, _ := result["error"].(string); reason != "" {
155 return nil, macOSFileFailure(reason)
156 }
157 if result["ok"] != true {
158 return nil, errors.New("虚拟 Mac 未确认图片粘贴")
159 }
160 return result, nil
161}