跳到主要内容

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

浏览文件

tailnet.go

94 行 · 2.8 KB历史下载
1package workbench
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "net/http"
8 "net/url"
9 "time"
10)
11
12func validTailnetAuthURL(raw string) bool {
13 u, err := url.Parse(raw)
14 return err == nil && u.Scheme == "https" && u.Host == "login.tailscale.com" && u.User == nil && u.Fragment == "" && (len(u.Path) > 3 && (u.Path[:3] == "/a/" || u.Path[:3] == "/c/"))
15}
16func (s *Server) tailnet(w http.ResponseWriter, r *http.Request) {
17 w.Header().Set("Cache-Control", "no-store")
18 action := r.PathValue("action")
19 if r.Method == http.MethodGet {
20 action = "status"
21 }
22 if action == "install" {
23 var input struct{}
24 if !readJSON(w, r, &input) {
25 return
26 }
27 s.launch(w, "tailscale-install", s.installTailnet)
28 return
29 }
30 switch action {
31 case "status", "connect", "login", "disconnect", "logout", "add-key", "remove-key", "open-service":
32 default:
33 http.NotFound(w, r)
34 return
35 }
36 var input struct {
37 Key string `json:"key"`
38 Host string `json:"host,omitempty"`
39 Port int `json:"port,omitempty"`
40 }
41 if r.Method != http.MethodGet && !readJSON(w, r, &input) {
42 return
43 }
44 if len(input.Key) > 8192 {
45 http.Error(w, "SSH 公钥过长", 400)
46 return
47 }
48 if action == "open-service" && (len(input.Host) == 0 || len(input.Host) > 253 || input.Port < 1 || input.Port > 65535) {
49 http.Error(w, "请选择私网设备并填写 1–65535 之间的端口", 400)
50 return
51 }
52 s.mu.Lock()
53 ready, token := s.ready, s.credentials.Token
54 busy := s.jobs.snapshot().Running
55 s.mu.Unlock()
56 if !ready || busy {
57 http.Error(w, "请先启动 Cage 环境,并等待当前操作完成", 409)
58 return
59 }
60 ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
61 defer cancel()
62 body, _ := json.Marshal(struct {
63 Action string `json:"action"`
64 Key string `json:"key,omitempty"`
65 Host string `json:"host,omitempty"`
66 Port int `json:"port,omitempty"`
67 }{action, input.Key, input.Host, input.Port})
68 data, err := s.guestAPI(ctx, token, http.MethodPost, "/tailnet", body)
69 if err != nil {
70 http.Error(w, "Tailscale 操作未完成。请更新环境组件后重试;原设备身份和 SSH 公钥会保留。", 502)
71 return
72 }
73 var result map[string]json.RawMessage
74 if json.Unmarshal(data, &result) != nil {
75 http.Error(w, "Tailscale 组件返回了无效状态", 502)
76 return
77 }
78 if b := result["tailnet_error"]; len(b) > 0 {
79 var reason string
80 _ = json.Unmarshal(b, &reason)
81 http.Error(w, reason, 502)
82 return
83 }
84 var authURL string
85 _ = json.Unmarshal(result["auth_url"], &authURL)
86 if authURL != "" && !validTailnetAuthURL(authURL) {
87 http.Error(w, "Tailscale 登录地址无效,请重新发起登录", 502)
88 return
89 }
90 w.Header().Set("Content-Type", "application/json")
91 _, _ = w.Write(data)
92}
93
94var errTailnetNativeInstall = errors.New("请在设置中更新环境组件,Tailscale 随客机组件一同安装")