跳到主要内容

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

浏览文件

backend.go

118 行 · 3.8 KB历史下载
1package mobilelink
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "net"
9 "net/http"
10 "net/http/cookiejar"
11 "net/url"
12 "strconv"
13 "strings"
14 "time"
15)
16
17type Workbench struct {
18 phoneRoom string
19 origin string
20 jar http.CookieJar
21 transport *http.Transport
22}
23
24func loopback(host string) bool {
25 ip := net.ParseIP(host)
26 return host == "localhost" || ip != nil && ip.IsLoopback()
27}
28
29func explicitPort(u *url.URL) bool {
30 port, err := strconv.Atoi(u.Port())
31 return err == nil && port >= 1 && port <= 65535
32}
33
34// OpenWorkbench consumes a ticket issued by the trusted local cage server-entry
35// command. Only the host process ever sees the ticket and the resulting cookie.
36func OpenWorkbench(ctx context.Context, origin, ticket string) (*Workbench, error) {
37 u, err := url.Parse(origin)
38 if err != nil || u.Scheme != "http" || !loopback(u.Hostname()) || !explicitPort(u) ||
39 u.User != nil || u.Path != "" || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" || len(ticket) < 32 || len(ticket) > 512 {
40 return nil, errors.New("invalid local Cage entry")
41 }
42 hostname := u.Hostname()
43 if hostname == "localhost" {
44 hostname = "127.0.0.1"
45 }
46 target := net.JoinHostPort(hostname, u.Port())
47 dialer := net.Dialer{Timeout: 5 * time.Second}
48 transport := &http.Transport{Proxy: nil, MaxIdleConnsPerHost: 8, IdleConnTimeout: time.Minute, ResponseHeaderTimeout: 35 * time.Second,
49 DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
50 if network != "tcp" || address != u.Host {
51 return nil, errors.New("nonlocal upstream refused")
52 }
53 return dialer.DialContext(ctx, "tcp", target)
54 },
55 }
56 jar, _ := cookiejar.New(nil)
57 b := &Workbench{origin: origin, transport: transport, jar: jar}
58 payload, err := json.Marshal(map[string]string{"token": ticket})
59 if err != nil {
60 return nil, errors.New("invalid entry ticket")
61 }
62 defer clear(payload)
63 ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
64 defer cancel()
65 req, err := http.NewRequestWithContext(ctx, http.MethodPost, origin+"/api/session", bytes.NewReader(payload))
66 if err != nil {
67 return nil, errors.New("invalid local Cage origin")
68 }
69 req.GetBody = nil
70 req.Header.Set("Content-Type", "application/json")
71 req.Header.Set("Origin", origin)
72 response, err := transport.RoundTrip(req)
73 if err != nil {
74 b.Close()
75 return nil, errors.New("Cage entry ticket exchange failed")
76 }
77 response.Body.Close()
78 if response.StatusCode == http.StatusOK {
79 for _, cookie := range response.Cookies() {
80 if cookie.Name == "cage_session_"+u.Port() && cookie.Value != "" {
81 jar.SetCookies(u, []*http.Cookie{cookie})
82 }
83 }
84 }
85 if len(jar.Cookies(u)) != 1 {
86 b.Close()
87 return nil, errors.New("Cage did not issue a valid local session cookie")
88 }
89 return b, nil
90}
91
92// SetPhoneRoom binds graphical and device metadata requests to this authenticated, UI-managed
93// pairing. The room proof is never accepted from the phone or sent to it.
94func (b *Workbench) SetPhoneRoom(proof string) { b.phoneRoom = proof }
95
96const PhoneRoomHeader = "X-Cage-Phone-Room"
97
98func (b *Workbench) Close() { b.transport.CloseIdleConnections() }
99
100// RoundTrip rebuilds requests in operations. This private transport receives
101// no caller-supplied headers and never follows redirects.
102type localTransport struct{ b *Workbench }
103
104func (t localTransport) RoundTrip(req *http.Request) (*http.Response, error) {
105 if t.b.phoneRoom != "" && strings.HasPrefix(req.URL.Path, "/api/mobile/") {
106 req.Header.Set(PhoneRoomHeader, t.b.phoneRoom)
107 }
108 for _, cookie := range t.b.jar.Cookies(req.URL) {
109 req.AddCookie(cookie)
110 }
111 return t.b.transport.RoundTrip(req)
112}
113
114func (b *Workbench) handler() http.Handler {
115 return operations(func(ctx context.Context, method, path string, body []byte) (*Reply, error) {
116 return request(ctx, localTransport{b}, b.origin, method, path, body, true)
117 })
118}