跳到主要内容

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

浏览文件

rooted_relay.go

159 行 · 4.4 KB历史下载
1//go:build linux || darwin || windows
2
3package reversesshfs
4
5import (
6 "errors"
7 "io/fs"
8 "time"
9)
10
11// noopRemovalTTL bounds how long an ExpectRemove token waits for the guest.
12const noopRemovalTTL = 5 * time.Second
13
14// ownRemovalTTL bounds how long a removal made by the server hides the host's event for it.
15const ownRemovalTTL = 2 * time.Second
16
17// maxRelayEntries bounds each table, whatever the host does.
18const maxRelayEntries = 1 << 20
19
20// expiring is a set of keys that each expire ttl after being added. Entries
21// are queued in the order they expire, so purging costs O(1) per entry.
22type expiring struct {
23 ttl time.Duration
24 m map[string]time.Time
25 q []expiringKey
26 front int
27}
28
29type expiringKey struct {
30 key string
31 expiry time.Time
32}
33
34func newExpiring(ttl time.Duration) *expiring {
35 return &expiring{ttl: ttl, m: make(map[string]time.Time)}
36}
37
38// add reports false when the set is full.
39func (e *expiring) add(key string, now time.Time) bool {
40 e.purge(now)
41 if len(e.q)-e.front >= maxRelayEntries {
42 return false
43 }
44 expiry := now.Add(e.ttl)
45 e.m[key] = expiry
46 e.q = append(e.q, expiringKey{key, expiry})
47 return true
48}
49
50func (e *expiring) has(key string, now time.Time) bool {
51 e.purge(now)
52 _, ok := e.m[key]
53 return ok
54}
55
56// remove reports whether key was there. Its queue entry stays until it expires.
57func (e *expiring) remove(key string, now time.Time) bool {
58 ok := e.has(key, now)
59 delete(e.m, key)
60 return ok
61}
62
63func (e *expiring) purge(now time.Time) {
64 for e.front < len(e.q) && !now.Before(e.q[e.front].expiry) {
65 k := e.q[e.front]
66 // A key added again has a later expiry, and its own queue entry.
67 if e.m[k.key].Equal(k.expiry) {
68 delete(e.m, k.key)
69 }
70 e.q[e.front] = expiringKey{}
71 e.front++
72 }
73 if e.front >= 1024 && e.front*2 >= len(e.q) {
74 e.q = append(e.q[:0], e.q[e.front:]...)
75 e.front = 0
76 }
77}
78
79// relay is the state behind ExpectRemove.
80//
81// The hostagent relays a deletion made on the host by having the guest remove the path, which
82// makes the guest emit an event. The server must then answer that removal without touching the host:
83// the path may have been created again there since. Two things must not be mistaken for it:
84// the host's event for a removal that the guest itself asked for, and a removal of a path the
85// guest created after the host deleted it.
86type relay struct {
87 tokens *expiring // paths whose next removal is the relayed one
88 own *expiring // paths the server just removed or renamed away
89}
90
91func newRelay() *relay {
92 return &relay{tokens: newExpiring(noopRemovalTTL), own: newExpiring(ownRemovalTTL)}
93}
94
95// expectRemove makes the next Remove or Rmdir request for p, within noopRemovalTTL, succeed without
96// touching the host, and reports true. p is a host path under the root. It reports false, and
97// the removal must not be relayed, when the server removed p itself just now.
98func (h *rootedHandlers) expectRemove(p string) bool {
99 rel, err := h.rel(slashPath(p))
100 if err != nil {
101 return false
102 }
103 now := h.now()
104 h.mu.Lock()
105 defer h.mu.Unlock()
106 if h.relay.own.has(rel, now) {
107 return false
108 }
109 return h.relay.tokens.add(rel, now)
110}
111
112func (h *rootedHandlers) consumeNoopRemoval(p string) bool {
113 rel, err := h.rel(p)
114 if err != nil {
115 return false
116 }
117 h.mu.Lock()
118 defer h.mu.Unlock()
119 return h.relay.tokens.remove(rel, h.now())
120}
121
122// removing records that the server is about to remove or move away p, a request path.
123// This is before the removal, as the host's event can come right after it.
124func (h *rootedHandlers) removing(p string) {
125 rel, err := h.rel(p)
126 if err != nil {
127 return
128 }
129 h.mu.Lock()
130 defer h.mu.Unlock()
131 h.relay.own.add(rel, h.now())
132}
133
134// removalFailed takes back removing(p) when the removal or move did not happen: nothing of the host's
135// changed, so a later deletion of p there is not the server's own. A path that was not there stays: an
136// earlier removal of it may be the one the host's event is about.
137func (h *rootedHandlers) removalFailed(p string, err error) error {
138 if err == nil || errors.Is(err, fs.ErrNotExist) {
139 return err
140 }
141 if rel, rerr := h.rel(p); rerr == nil {
142 h.mu.Lock()
143 h.relay.own.remove(rel, h.now())
144 h.mu.Unlock()
145 }
146 return err
147}
148
149// creating records that the server is about to create p, a request path: a removal of it that follows
150// is the guest's own, not a relayed one.
151func (h *rootedHandlers) creating(p string) {
152 rel, err := h.rel(p)
153 if err != nil {
154 return
155 }
156 h.mu.Lock()
157 defer h.mu.Unlock()
158 h.relay.tokens.remove(rel, h.now())
159}