跳到主要内容

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

浏览文件

terminal_login_link.go

94 行 · 2.5 KB历史下载
1package workbench
2
3import (
4 "context"
5 "encoding/json"
6 "net/http"
7 "net/url"
8 "strings"
9 "time"
10)
11
12func claudeAuthorizationURL(raw string) bool {
13 if len(raw) > 8192 || strings.ContainsAny(raw, "\r\n\t \\") {
14 return false
15 }
16 u, err := url.Parse(raw)
17 if err != nil || u.Scheme != "https" || u.User != nil || u.RawQuery == "" ||
18 (u.EscapedPath() != "/oauth/authorize" && u.EscapedPath() != "/oauth/authorize/") {
19 return false
20 }
21 switch strings.ToLower(u.Host) {
22 case "claude.ai", "claude.com", "console.anthropic.com", "platform.claude.com", "platform.anthropic.com":
23 return true
24 }
25 return false
26}
27
28func (s *Server) terminalLoginLink(w http.ResponseWriter, r *http.Request) {
29 var input struct {
30 URL string `json:"url"`
31 }
32 if !readJSON(w, r, &input) {
33 return
34 }
35 if !claudeAuthorizationURL(input.URL) {
36 http.Error(w, "请选择 Claude 官方授权链接", 400)
37 return
38 }
39 terminal := s.terminals.get(r.PathValue("id"))
40 if terminal == nil {
41 http.NotFound(w, r)
42 return
43 }
44 terminal.loginLinkMu.Lock()
45 defer terminal.loginLinkMu.Unlock()
46 if !terminal.ownsConversation() {
47 http.Error(w, "登录终端已关闭,请重新发起登录", 409)
48 return
49 }
50 s.mu.Lock()
51 ready, token := s.ready, s.credentials.Token
52 s.mu.Unlock()
53 if !ready {
54 http.Error(w, "请先启动工作环境", 409)
55 return
56 }
57
58 login := terminal.id
59 if terminal.loginGuestID != "" {
60 // Match the official CLI helper's private browser and cleanup key.
61 login = terminal.loginGuestID
62 }
63 ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
64 defer cancel()
65 payload, _ := json.Marshal(map[string]string{"login": login, "url": input.URL})
66 data, err := s.guestAPI(ctx, token, "POST", "/login-open", payload)
67 if terminal.loginGuestID == "" {
68 terminal.loginLinkCleanup.Do(func() {
69 go func() {
70 lifetime := s.lifetimeContext()
71 select {
72 case <-lifetime.Done():
73 return
74 case <-terminal.done:
75 }
76 terminal.loginLinkMu.Lock()
77 defer terminal.loginLinkMu.Unlock()
78 ctx, cancel := context.WithTimeout(lifetime, 8*time.Second)
79 defer cancel()
80 body, _ := json.Marshal(map[string]string{"login": login})
81 _, _ = s.guestAPI(ctx, token, "POST", "/login-finish", body)
82 }()
83 })
84 }
85 var tab struct {
86 ID string `json:"id"`
87 }
88 if err != nil || json.Unmarshal(data, &tab) != nil || tab.ID == "" {
89 http.Error(w, "Cage 浏览器未能打开授权页,请检查浏览器连接并重试", 502)
90 return
91 }
92 w.Header().Set("Cache-Control", "no-store")
93 jsonReply(w, tab)
94}