跳到主要内容

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

浏览文件

cloud.go

247 行 · 6.5 KB历史下载
1//go:build linux || darwin || windows
2
3package reversesshfs
4
5import (
6 "errors"
7 "io"
8 "os"
9 "path"
10 "path/filepath"
11 "sync"
12
13 "github.com/pkg/sftp"
14)
15
16// CloudShare pins one local directory for one authenticated client-initiated
17// stream. Only a virtual "/" is visible to the peer. The caller authorizes and
18// validates the physical project directory before creating this grant.
19type CloudShare struct {
20 mu sync.Mutex
21 handlers *cloudHandlers
22 stream io.ReadWriteCloser
23 done chan struct{}
24 closed bool
25}
26
27// OpenCloudShare expects an absolute, symlink-resolved path supplied by the local picker.
28func OpenCloudShare(directory string, readOnly bool) (*CloudShare, error) {
29 if !filepath.IsAbs(directory) || filepath.Clean(directory) != directory || filepath.Dir(directory) == directory {
30 return nil, os.ErrPermission
31 }
32 sys, err := openCloudRoot(directory)
33 if err != nil {
34 return nil, err
35 }
36 h := &cloudHandlers{&rootedHandlers{rootPath: "/", guardRoot: directory, rootedSys: sys, readonly: readOnly, cloud: true}}
37 return &CloudShare{handlers: h}, nil
38}
39
40// Serve consumes stream, including when the grant has already been revoked.
41func (c *CloudShare) Serve(stream io.ReadWriteCloser) error {
42 c.mu.Lock()
43 if c.closed || c.stream != nil {
44 c.mu.Unlock()
45 stream.Close()
46 return os.ErrClosed
47 }
48 c.stream, c.done = stream, make(chan struct{})
49 h := c.handlers
50 c.mu.Unlock()
51
52 srv := sftp.NewRequestServer(stream, sftp.Handlers{FileGet: h, FilePut: h, FileCmd: h, FileList: h},
53 sftp.WithRSSequential(), sftp.WithRSMaxHandles(64), sftp.WithRSMaxTxPacket(64*1024))
54 err := srv.Serve()
55 stream.Close()
56 c.mu.Lock()
57 h.rootedSys.close()
58 c.closed = true
59 close(c.done)
60 c.mu.Unlock()
61 return err
62}
63
64// Close revokes the grant and waits until buffered requests and open file
65// handles have been retired. A closed grant cannot attach to another stream.
66func (c *CloudShare) Close() error {
67 c.mu.Lock()
68 if c.stream == nil {
69 defer c.mu.Unlock()
70 if c.closed {
71 return nil
72 }
73 c.closed = true
74 return c.handlers.rootedSys.close()
75 }
76 stream, done, closed := c.stream, c.done, c.closed
77 c.closed = true
78 c.mu.Unlock()
79 if !closed {
80 stream.Close()
81 }
82 <-done
83 return nil
84}
85
86// Use the confined server's handle-relative writes. All reads and metadata
87// requests use the same no-follow path walk, including the leaf. An internal
88// symlink could otherwise expose a protected path under another name.
89type cloudHandlers struct{ *rootedHandlers }
90
91func (h *cloudHandlers) accessible(p string) (string, error) {
92 rel, err := h.rel(p)
93 if err != nil || !writableName(rel) {
94 return "", errDenied
95 }
96 return rel, nil
97}
98
99// One desktop process can serve several grants. Bound long-lived descriptors
100// across all of them, leaving room for the SSH connections and the workbench.
101var cloudSlots = make(chan struct{}, 128)
102
103func acquireCloudSlot() error {
104 select {
105 case cloudSlots <- struct{}{}:
106 return nil
107 default:
108 return errors.New("too many open project files")
109 }
110}
111
112type cloudFile struct {
113 writableFile
114 writable bool
115}
116
117func (f cloudFile) Close() error {
118 err := f.File.Close()
119 <-cloudSlots
120 return err
121}
122
123func (h *cloudHandlers) fileHandle(r *sftp.Request, write bool) (cloudFile, error) {
124 if err := acquireCloudSlot(); err != nil {
125 return cloudFile{}, err
126 }
127 var f writableFile
128 var err error
129 if write {
130 f, err = h.openWritable(r)
131 } else {
132 var rel string
133 if rel, err = h.accessible(r.Filepath); err == nil {
134 f.File, err = h.cloudOpenRead(rel)
135 }
136 }
137 if err != nil {
138 <-cloudSlots
139 return cloudFile{}, err
140 }
141 return cloudFile{writableFile: f, writable: write}, nil
142}
143
144func (h *cloudHandlers) Fileread(r *sftp.Request) (io.ReaderAt, error) { return h.fileHandle(r, false) }
145func (h *cloudHandlers) Filewrite(r *sftp.Request) (io.WriterAt, error) { return h.fileHandle(r, true) }
146func (h *cloudHandlers) OpenFile(r *sftp.Request) (sftp.WriterAtReaderAt, error) {
147 return h.fileHandle(r, true)
148}
149
150func (h *cloudHandlers) Filelist(r *sftp.Request) (sftp.ListerAt, error) {
151 rel, err := h.accessible(r.Filepath)
152 if err != nil {
153 return nil, err
154 }
155 if r.Method == "Stat" {
156 if file, ok := r.HandleFile().(interface{ Stat() (os.FileInfo, error) }); ok {
157 info, err := file.Stat()
158 if err != nil {
159 return nil, err
160 }
161 return listerAt{info}, nil
162 }
163 }
164 f, err := h.cloudOpenRead(rel)
165 if err != nil {
166 return nil, err
167 }
168 if r.Method == "List" {
169 if err := acquireCloudSlot(); err != nil {
170 f.Close()
171 return nil, err
172 }
173 return &dirLister{f: f, cloud: true}, nil
174 }
175 defer f.Close()
176 info, err := f.Stat()
177 if err != nil {
178 return nil, err
179 }
180 return listerAt{info}, nil
181}
182
183func (h *cloudHandlers) Lstat(r *sftp.Request) (sftp.ListerAt, error) { return h.Filelist(r) }
184func (h *cloudHandlers) Readlink(string) (string, error) { return "", errDenied }
185func (h *cloudHandlers) RealPath(p string) (string, error) {
186 rel, err := h.accessible(p)
187 if err != nil {
188 return "", err
189 }
190 return path.Join("/", rel), nil
191}
192
193func (h *cloudHandlers) Filecmd(r *sftp.Request) error {
194 switch r.Method {
195 case "Setstat":
196 if h.readonly || r.AttrFlags().UidGid {
197 return errDenied
198 }
199 if f, ok := r.HandleFile().(cloudFile); ok && f.writable {
200 return h.fsetstat(f.File, r)
201 }
202 // A read handle cannot acquire write access through FSETSTAT.
203 if r.HandleFile() != nil {
204 return errDenied
205 }
206 f, err := h.openFile(&sftp.Request{Filepath: r.Filepath, Flags: 2 /* SSH_FXF_WRITE */})
207 if err != nil {
208 return err
209 }
210 defer f.Close()
211 return h.fsetstat(f, r)
212 case "Rename":
213 return h.rename(r, true)
214 case "Remove", "Rmdir":
215 return h.remove(r)
216 case "Mkdir":
217 return h.mkdir(r)
218 default:
219 // Refuse both hard-link and symbolic-link creation.
220 return errDenied
221 }
222}
223
224// The remote environment owns its own disk reports; querying the desktop
225// volume via an SFTP extension would disclose unrelated host information.
226func (h *cloudHandlers) StatVFS(*sftp.Request) (*sftp.StatVFS, error) {
227 return nil, sftp.ErrSSHFxOpUnsupported
228}
229
230func (h *cloudHandlers) PosixRename(r *sftp.Request) error { return h.rename(r, false) }
231
232// MatchesRoot compares the directory as picked before validation with the one
233// actually held by this grant. Replacing the picker path invalidates the grant.
234func (c *CloudShare) MatchesRoot(selected os.FileInfo) bool {
235 c.mu.Lock()
236 defer c.mu.Unlock()
237 if c.closed || selected == nil {
238 return false
239 }
240 f, err := c.handlers.cloudOpenRead(".")
241 if err != nil {
242 return false
243 }
244 defer f.Close()
245 info, err := f.Stat()
246 return err == nil && info.IsDir() && os.SameFile(info, selected)
247}