跳到主要内容

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

浏览文件

main.go

142 行 · 3.6 KB历史下载
1// cage-guest-files runs as the guest GUI agent's own user. File selections are
2// transient stdin data, never host paths or commands selected by a web page.
3package main
4
5import (
6 "encoding/binary"
7 "encoding/json"
8 "errors"
9 "fmt"
10 "io"
11 "os"
12
13 "github.com/sylvinus/agent-vm/internal/clipboardfiles"
14)
15
16func run(args []string, input io.Reader, output io.Writer) error {
17 if len(args) < 1 || len(args) > 2 {
18 return errors.New("guest file operation invalid")
19 }
20 if args[0] == "cache" && len(args) == 2 {
21 var request clipboardfiles.CacheRequest
22 if json.Unmarshal([]byte(args[1]), &request) != nil {
23 return errors.New("invalid clipboard cache request")
24 }
25 store, err := clipboardfiles.UserCacheStore()
26 if err != nil {
27 return err
28 }
29 result, err := store.Control(request)
30 if err != nil {
31 return err
32 }
33 return json.NewEncoder(output).Encode(result)
34 }
35 var store clipboardfiles.CacheStore
36 if args[0] != "export" {
37 var err error
38 store, err = clipboardfiles.UserCacheStore()
39 if err != nil {
40 return err
41 }
42 }
43 if args[0] == "save" {
44 if len(args) != 2 {
45 return errors.New("file name required")
46 }
47 selection, err := store.Save(&framedInput{reader: input}, args[1])
48 if err != nil {
49 return err
50 }
51 return returnCachedSelection(output, store, selection)
52 }
53 if args[0] == "import" {
54 selection, err := store.Read(input)
55 if err != nil {
56 return err
57 }
58 return returnCachedSelection(output, store, selection)
59 }
60 var paths []string
61 if len(args) == 2 && args[1] != "--paths-stdin" {
62 if len(args[1]) > 1<<20 || json.Unmarshal([]byte(args[1]), &paths) != nil {
63 return errors.New("file selection invalid")
64 }
65 } else {
66 data, err := io.ReadAll(io.LimitReader(input, (1<<20)+1))
67 if err != nil || len(data) > 1<<20 || json.Unmarshal(data, &paths) != nil {
68 return errors.New("file selection invalid")
69 }
70 }
71 switch args[0] {
72 case "link-cached":
73 linked, err := store.LinkCachedFiles(paths)
74 if err != nil {
75 return err
76 }
77 return returnCachedSelection(output, store, linked)
78 case "export":
79 return clipboardfiles.Write(output, paths)
80 case "copy":
81 copied, err := store.Copy(paths)
82 if err != nil {
83 return err
84 }
85 return returnCachedSelection(output, store, copied)
86 default:
87 return errors.New("guest file operation invalid")
88 }
89}
90
91func returnCachedSelection(output io.Writer, store clipboardfiles.CacheStore, selection clipboardfiles.CachedSelection) error {
92 if err := json.NewEncoder(output).Encode(selection); err != nil {
93 _ = store.Release(selection.ID)
94 return err
95 }
96 return nil
97}
98
99// The zero-length frame confirms that the sender finished the file. A closed
100// connection before that marker must not publish a truncated attachment.
101type framedInput struct {
102 reader io.Reader
103 remaining uint32
104 ended bool
105}
106
107func (f *framedInput) Read(buffer []byte) (int, error) {
108 if len(buffer) == 0 {
109 return 0, nil
110 }
111 if f.ended {
112 return 0, io.EOF
113 }
114 if f.remaining == 0 {
115 var header [4]byte
116 if _, err := io.ReadFull(f.reader, header[:]); err != nil {
117 return 0, io.ErrUnexpectedEOF
118 }
119 f.remaining = binary.LittleEndian.Uint32(header[:])
120 if f.remaining > 64<<10 {
121 return 0, errors.New("file frame exceeds limit")
122 }
123 if f.remaining == 0 {
124 f.ended = true
125 return 0, io.EOF
126 }
127 }
128 n, err := f.reader.Read(buffer[:min(len(buffer), int(f.remaining))])
129 f.remaining -= uint32(n)
130 if err == io.EOF {
131 err = io.ErrUnexpectedEOF
132 }
133 return n, err
134}
135
136func main() {
137 if err := run(os.Args[1:], os.Stdin, os.Stdout); err != nil {
138 // Do not expose selected file names or OS error strings in agent logs.
139 fmt.Fprintln(os.Stderr, "Clipboard file transfer failed; check the selection, permissions and limits.")
140 os.Exit(1)
141 }
142}