跳到主要内容

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

浏览文件

archive.go

240 行 · 7.1 KB历史下载
1// Package clipboardfiles streams selected ordinary files between guest systems.
2// It has no host/guest process or authentication policy; callers own the selection.
3package clipboardfiles
4
5import (
6 "archive/tar"
7 "errors"
8 "io"
9 "os"
10 "path/filepath"
11 "strings"
12)
13
14const FileLimit int64 = 2 << 30
15const SelectionLimit = 100
16
17// LinkCachedFiles groups already frozen guest-cache files for one native file
18// chooser without copying every attachment again. GUI callers validate ownership.
19func LinkCachedFiles(paths []string) (result []string, err error) {
20 return linkCachedFiles(paths, temporaryDirectory)
21}
22
23func temporaryDirectory() (string, error) { return os.MkdirTemp("", "cage-clipboard-") }
24
25func linkCachedFiles(paths []string, directory func() (string, error)) (result []string, err error) {
26 if len(paths) == 0 || len(paths) > SelectionLimit {
27 return nil, errors.New("请选择 1 至 100 个文件")
28 }
29 root, err := directory()
30 if err != nil {
31 return nil, err
32 }
33 defer func() {
34 if err != nil {
35 _ = os.RemoveAll(root)
36 }
37 }()
38 for _, source := range paths {
39 name := filepath.Base(source)
40 info, err := os.Lstat(source)
41 if err != nil || !filepath.IsAbs(source) || !strings.HasPrefix(filepath.Base(filepath.Dir(source)), "cage-clipboard-") || !validName(name) || !info.Mode().IsRegular() || info.Size() > FileLimit {
42 return nil, errors.New("附件缓存文件无效")
43 }
44 target := filepath.Join(root, name)
45 if err := os.Link(source, target); err != nil {
46 return nil, errors.New("无法准备附件选择,请核对文件名是否重复")
47 }
48 linked, err := os.Lstat(target)
49 if err != nil || !linked.Mode().IsRegular() || !os.SameFile(info, linked) {
50 return nil, errors.New("附件缓存已变化,请重新选择文件")
51 }
52 result = append(result, target)
53 }
54 return result, nil
55}
56
57// Save receives one file into a newly owned cache directory. The caller can
58// publish the returned path only after the complete bounded write succeeds.
59func Save(input io.Reader, name string) (path string, err error) {
60 return save(input, name, temporaryDirectory)
61}
62
63func save(input io.Reader, name string, directory func() (string, error)) (path string, err error) {
64 if !validName(name) || strings.Contains(name, ":") {
65 return "", errors.New("剪贴板文件名称无效")
66 }
67 root, err := directory()
68 if err != nil {
69 return "", err
70 }
71 defer func() {
72 if err != nil {
73 _ = os.RemoveAll(root)
74 }
75 }()
76 path = filepath.Join(root, name)
77 file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
78 if err != nil {
79 return "", err
80 }
81 count, writeErr := io.Copy(file, io.LimitReader(input, FileLimit+1))
82 if count > FileLimit {
83 writeErr = errors.New("每个文件不能超过 2 GB")
84 }
85 if writeErr == nil {
86 writeErr = file.Sync()
87 }
88 if err = errors.Join(writeErr, file.Close()); err != nil {
89 return "", err
90 }
91 return path, nil
92}
93
94func validName(name string) bool {
95 return name != "" && len(name) <= 255 && name != "." && name != ".." && !strings.ContainsAny(name, "/\\\x00") &&
96 !strings.ContainsFunc(name, func(r rune) bool { return r < 32 || r == 127 })
97}
98
99func Write(output io.Writer, paths []string) error {
100 if len(paths) == 0 || len(paths) > SelectionLimit {
101 return errors.New("请选择 1 至 100 个文件或文件夹")
102 }
103 writer := tar.NewWriter(output)
104 names := map[string]bool{}
105 for _, selected := range paths {
106 name := filepath.Base(selected)
107 if !filepath.IsAbs(selected) || !validName(name) || names[name] {
108 return errors.New("剪贴板文件名称无效或重复")
109 }
110 names[name] = true
111 err := filepath.Walk(selected, func(path string, info os.FileInfo, err error) error {
112 if err != nil {
113 return errors.New("无法读取所选剪贴板文件")
114 }
115 if !(info.Mode().IsRegular() || info.IsDir()) || info.Size() > FileLimit {
116 return errors.New("剪贴板支持普通文件和文件夹,每个文件最多 2 GB")
117 }
118 relative, err := filepath.Rel(filepath.Dir(selected), path)
119 if err != nil {
120 return err
121 }
122 header, err := tar.FileInfoHeader(info, "")
123 if err != nil {
124 return err
125 }
126 header.Name = filepath.ToSlash(relative)
127 if err := writer.WriteHeader(header); err != nil {
128 return err
129 }
130 if !info.Mode().IsRegular() {
131 return nil
132 }
133 file, err := os.Open(path)
134 if err != nil {
135 return errors.New("无法读取所选剪贴板文件")
136 }
137 defer file.Close()
138 after, err := file.Stat()
139 if err != nil || !os.SameFile(info, after) || after.Size() != info.Size() {
140 return errors.New("文件已变化,请重新复制")
141 }
142 _, err = io.CopyN(writer, file, info.Size())
143 return err
144 })
145 if err != nil {
146 return err
147 }
148 }
149 return writer.Close()
150}
151
152func Read(input io.Reader) (paths []string, err error) {
153 return read(input, temporaryDirectory)
154}
155
156func read(input io.Reader, directory func() (string, error)) (paths []string, err error) {
157 root, err := directory()
158 if err != nil {
159 return nil, err
160 }
161 defer func() {
162 if err != nil {
163 _ = os.RemoveAll(root)
164 }
165 }()
166 reader := tar.NewReader(input)
167 names := map[string]bool{}
168 for {
169 header, readErr := reader.Next()
170 if readErr == io.EOF {
171 break
172 }
173 if readErr != nil {
174 return nil, readErr
175 }
176 name := strings.TrimSuffix(header.Name, "/")
177 parts := strings.Split(name, "/")
178 if !filepath.IsLocal(filepath.FromSlash(name)) || header.Size < 0 || header.Size > FileLimit {
179 return nil, errors.New("剪贴板文件位置或大小无效")
180 }
181 for _, part := range parts {
182 if !validName(part) || strings.Contains(part, ":") {
183 return nil, errors.New("剪贴板文件名称无效")
184 }
185 }
186 if !names[parts[0]] {
187 names[parts[0]] = true
188 paths = append(paths, filepath.Join(root, parts[0]))
189 if len(paths) > SelectionLimit {
190 return nil, errors.New("剪贴板文件数量超过 100 个")
191 }
192 }
193 destination := filepath.Join(root, filepath.FromSlash(name))
194 if err = os.MkdirAll(filepath.Dir(destination), 0700); err != nil {
195 return nil, err
196 }
197 switch header.Typeflag {
198 case tar.TypeDir:
199 if err = os.MkdirAll(destination, 0700); err != nil {
200 return nil, err
201 }
202 case tar.TypeReg, tar.TypeRegA:
203 file, createErr := os.OpenFile(destination, os.O_WRONLY|os.O_CREATE|os.O_EXCL, os.FileMode(header.Mode)&0700|0600)
204 if createErr != nil {
205 return nil, createErr
206 }
207 _, copyErr := io.CopyN(file, reader, header.Size)
208 closeErr := file.Close()
209 if copyErr != nil || closeErr != nil {
210 return nil, errors.Join(copyErr, closeErr)
211 }
212 default:
213 return nil, errors.New("剪贴板不支持链接或特殊文件")
214 }
215 }
216 if len(paths) == 0 {
217 return nil, errors.New("剪贴板文件为空")
218 }
219 return paths, nil
220}
221
222func Copy(paths []string) ([]string, error) {
223 return copyFiles(paths, temporaryDirectory)
224}
225
226func copyFiles(paths []string, directory func() (string, error)) ([]string, error) {
227 reader, writer := io.Pipe()
228 defer reader.Close()
229 done := make(chan error, 1)
230 go func() { err := Write(writer, paths); writer.CloseWithError(err); done <- err }()
231 copied, err := read(reader, directory)
232 reader.Close()
233 if writeErr := <-done; writeErr != nil {
234 if len(copied) > 0 {
235 _ = os.RemoveAll(filepath.Dir(copied[0]))
236 }
237 return nil, writeErr
238 }
239 return copied, err
240}