跳到主要内容

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

浏览文件

vault_http.go

241 行 · 7.2 KB历史下载
1package workbench
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "net/http"
8 "slices"
9 "time"
10)
11
12type vaultBrowserInput struct {
13 Action string `json:"action"`
14 Tab string `json:"tab"`
15 ID string `json:"id"`
16 Candidate string `json:"candidate"`
17 Document string `json:"document"`
18 Origin string `json:"origin"`
19 Lease string `json:"lease,omitempty"`
20 Username string `json:"username,omitempty"`
21 Password string `json:"password,omitempty"`
22 Blocked []string `json:"blocked,omitempty"`
23}
24type vaultCandidate struct {
25 ID string `json:"id"`
26 Tab string `json:"tab"`
27 Origin string `json:"origin"`
28 Username string `json:"username"`
29 Password string `json:"password,omitempty"`
30 Update bool `json:"update"`
31}
32type vaultBrowserState struct {
33 Available bool `json:"available"`
34 Origin string `json:"origin"`
35 Document string `json:"document"`
36 Candidates []vaultCandidate `json:"candidates"`
37 Entries []vaultEntry `json:"entries"`
38}
39
40func (s *Server) vaultList(w http.ResponseWriter, r *http.Request) {
41 var out vaultData
42 err := s.account.withVault(r.Context(), false, func(db *vaultData) error {
43 out = *db
44 out.GuestPasswords = nil
45 for i := range out.Entries {
46 out.Entries[i].Password = ""
47 }
48 return nil
49 })
50 if err != nil {
51 requestError(w, err)
52 return
53 }
54 jsonReply(w, out)
55}
56func (s *Server) vaultMutation(w http.ResponseWriter, r *http.Request) {
57 var input struct {
58 Action string `json:"action"`
59 Entry vaultEntry `json:"entry"`
60 ID string `json:"id"`
61 Origin string `json:"origin"`
62 AccountPassword string `json:"account_password"`
63 }
64 if !readJSON(w, r, &input, 32<<10) {
65 return
66 }
67 if input.Action == "reveal" {
68 if err := s.account.verifyVaultPassword(input.AccountPassword); err != nil {
69 requestError(w, err)
70 return
71 }
72 }
73 result := map[string]any{"ok": true}
74 err := s.account.withVault(r.Context(), input.Action != "reveal", func(db *vaultData) error {
75 switch input.Action {
76 case "save":
77 return db.save(input.Entry)
78 case "delete":
79 db.Entries = slices.DeleteFunc(db.Entries, func(e vaultEntry) bool { return e.ID == input.ID })
80 case "unblock":
81 db.Blocked = slices.DeleteFunc(db.Blocked, func(origin string) bool { return origin == input.Origin })
82 case "reveal":
83 for _, entry := range db.Entries {
84 if entry.ID == input.ID {
85 result["password"] = entry.Password
86 return nil
87 }
88 }
89 return errors.New("密码记录不存在")
90 default:
91 return errors.New("密码保险箱操作无效")
92 }
93 return nil
94 })
95 if err != nil {
96 requestError(w, err)
97 return
98 }
99 jsonReply(w, result)
100}
101
102func (s *Server) guestVault(ctx context.Context, input vaultBrowserInput, out any) error {
103 s.mu.Lock()
104 ready, token := s.ready, s.credentials.Token
105 s.mu.Unlock()
106 if !ready {
107 return errBrowserNotReady
108 }
109 body, _ := json.Marshal(input)
110 defer clear(body)
111 data, err := s.guestAPI(ctx, token, http.MethodPost, "/vault", body)
112 defer clear(data)
113 if err != nil {
114 return errors.New("浏览器密码功能暂不可用,请更新环境组件后重试")
115 }
116 var failure struct {
117 Error string `json:"vaultError"`
118 }
119 if json.Unmarshal(data, &failure) == nil && failure.Error != "" {
120 switch failure.Error {
121 case "登录记录已过期,请重新登录", "当前网页已切换", "未找到可填写的登录框,或页面已变化", "保险箱已锁定,请重试", "登录页面已变化,请稍后重试":
122 return errors.New(failure.Error)
123 default:
124 return errors.New("网页未能处理密码操作,请刷新后重试")
125 }
126 }
127 if json.Unmarshal(data, out) != nil {
128 return errors.New("浏览器密码响应无效")
129 }
130 return nil
131}
132
133func (s *Server) vaultBrowser(w http.ResponseWriter, r *http.Request) {
134 var input vaultBrowserInput
135 if !readJSON(w, r, &input, 16<<10) {
136 return
137 }
138 // Passwords and origins used for fill always come from the host database.
139 input.Password, input.Username, input.Origin, input.Lease, input.Blocked = "", "", "", "", nil
140 s.account.mu.Lock()
141 lease := s.account.vaultLease
142 s.account.mu.Unlock()
143 input.Lease = lease
144 var result any = map[string]bool{"ok": true}
145 err := s.account.withVault(r.Context(), input.Action == "save" || input.Action == "never", func(db *vaultData) error {
146 if lease == "" || lease != s.account.vaultLease {
147 return errors.New("请重新解锁后重试")
148 }
149 switch input.Action {
150 case "status":
151 input.Blocked = db.Blocked
152 state := vaultBrowserState{Candidates: []vaultCandidate{}, Entries: []vaultEntry{}}
153 if err := s.guestVault(r.Context(), input, &state); err != nil {
154 result = map[string]any{"available": false, "message": err.Error(), "candidates": []vaultCandidate{}, "entries": []vaultEntry{}}
155 return nil
156 }
157 pending := []vaultCandidate{}
158 for _, candidate := range state.Candidates {
159 if slices.Contains(db.Blocked, candidate.Origin) {
160 continue
161 }
162 same := false
163 for _, entry := range db.Entries {
164 if entry.Origin == candidate.Origin && entry.Username == candidate.Username {
165 candidate.Update = true
166 same = entry.Password == candidate.Password
167 }
168 }
169 candidate.Password = ""
170 if !same {
171 pending = append(pending, candidate)
172 }
173 }
174 state.Candidates = pending
175 state.Entries = []vaultEntry{}
176 for _, entry := range db.Entries {
177 if entry.Origin == state.Origin {
178 entry.Password = ""
179 state.Entries = append(state.Entries, entry)
180 }
181 }
182 result = state
183 case "fill":
184 for _, entry := range db.Entries {
185 if entry.ID == input.ID {
186 input.Origin, input.Username, input.Password = entry.Origin, entry.Username, entry.Password
187 var reply map[string]any
188 return s.guestVault(r.Context(), input, &reply)
189 }
190 }
191 return errors.New("密码记录不存在")
192 case "save", "never":
193 request := input
194 request.Action = "candidate"
195 var candidate vaultCandidate
196 if err := s.guestVault(r.Context(), request, &candidate); err != nil {
197 return err
198 }
199 origin, err := vaultOrigin(candidate.Origin)
200 if err != nil || origin != candidate.Origin || candidate.ID != input.Candidate {
201 return errors.New("登录记录已失效,请重新登录")
202 }
203 if input.Action == "never" {
204 if !slices.Contains(db.Blocked, origin) {
205 db.Blocked = append(db.Blocked, origin)
206 }
207 return nil
208 }
209 entry := vaultEntry{Origin: origin, Username: candidate.Username, Password: candidate.Password}
210 for _, old := range db.Entries {
211 if old.Origin == origin && old.Username == entry.Username {
212 entry.ID = old.ID
213 }
214 }
215 return db.save(entry)
216 case "dismiss":
217 var reply map[string]any
218 return s.guestVault(r.Context(), input, &reply)
219 default:
220 return errors.New("密码保险箱操作无效")
221 }
222 return nil
223 })
224 if err != nil {
225 requestError(w, err)
226 return
227 }
228 if input.Action == "save" || input.Action == "never" {
229 input.Action = "dismiss"
230 var ignored map[string]any
231 _ = s.guestVault(r.Context(), input, &ignored)
232 }
233 jsonReply(w, result)
234}
235
236func (s *Server) clearBrowserVault(lease string) {
237 ctx, cancel := context.WithTimeout(context.Background(), time.Second)
238 defer cancel()
239 var out map[string]any
240 _ = s.guestVault(ctx, vaultBrowserInput{Action: "lock", Lease: lease}, &out)
241}