跳到主要内容

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

浏览文件

transport.go

135 行 · 3.5 KB历史下载
1// Package hostproxy routes host-side image and application requests through the host's
2// proxy settings. Guest traffic must continue to use cage.Egress instead.
3package hostproxy
4
5import (
6 "errors"
7 "net"
8 "net/http"
9 "net/url"
10 "os"
11 "strings"
12 "sync"
13 "time"
14
15 "golang.org/x/net/http/httpproxy"
16)
17
18var environment struct {
19 sync.Mutex
20 config *httpproxy.Config
21}
22
23// CaptureEnvironment preserves host proxy settings before the CLI removes them
24// from child/guest environments. It never restores them to the process env.
25func CaptureEnvironment() {
26 environment.Lock()
27 defer environment.Unlock()
28 if environment.config == nil {
29 environment.config = readEnvironment()
30 }
31}
32
33func readEnvironment() *httpproxy.Config {
34 cfg := httpproxy.FromEnvironment()
35 all := os.Getenv("ALL_PROXY")
36 if all == "" {
37 all = os.Getenv("all_proxy")
38 }
39 if cfg.HTTPProxy == "" {
40 cfg.HTTPProxy = all
41 }
42 if cfg.HTTPSProxy == "" {
43 cfg.HTTPSProxy = all
44 }
45 return cfg
46}
47
48func NewTransport() *http.Transport {
49 environment.Lock()
50 cfg := environment.config
51 if cfg == nil {
52 cfg = readEnvironment()
53 }
54 copy := *cfg
55 environment.Unlock()
56 return transport(&copy, systemProxy)
57}
58
59// DownloadEnvironment restores only the captured proxy variables for a host
60// downloader subprocess. Callers must not pass these into guest operations.
61func DownloadEnvironment() []string {
62 environment.Lock()
63 defer environment.Unlock()
64 cfg := environment.config
65 if cfg == nil {
66 cfg = readEnvironment()
67 }
68 return []string{
69 "HTTP_PROXY=" + cfg.HTTPProxy, "http_proxy=" + cfg.HTTPProxy,
70 "HTTPS_PROXY=" + cfg.HTTPSProxy, "https_proxy=" + cfg.HTTPSProxy,
71 "NO_PROXY=" + cfg.NoProxy, "no_proxy=" + cfg.NoProxy,
72 "ALL_PROXY=", "all_proxy=",
73 }
74}
75
76func transport(env *httpproxy.Config, system func(*http.Request) (*url.URL, error)) *http.Transport {
77 envProxy := env.ProxyFunc()
78 return &http.Transport{
79 Proxy: func(req *http.Request) (*url.URL, error) {
80 value := env.HTTPProxy
81 if req.URL.Scheme == "https" {
82 value = env.HTTPSProxy
83 }
84 if value != "" {
85 if _, err := proxyURL(value); err != nil {
86 return nil, err
87 }
88 return envProxy(req.URL)
89 }
90 return system(req)
91 },
92 // No interface binding: host TUN/routing rules apply to these sockets.
93 DialContext: (&net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}).DialContext,
94 ForceAttemptHTTP2: true, TLSHandshakeTimeout: 15 * time.Second,
95 ResponseHeaderTimeout: 30 * time.Second, IdleConnTimeout: 90 * time.Second,
96 }
97}
98
99func proxyURL(value string) (*url.URL, error) {
100 value = strings.TrimSpace(value)
101 if !strings.Contains(value, "://") {
102 value = "http://" + value
103 }
104 u, err := url.Parse(value)
105 if err != nil || u.Hostname() == "" {
106 return nil, errors.New("主机代理地址无效,请检查系统代理设置")
107 }
108 switch u.Scheme {
109 case "http", "https", "socks5", "socks5h":
110 return u, nil
111 default:
112 return nil, errors.New("主机代理协议不受支持,请使用 HTTP 或 SOCKS5 系统代理")
113 }
114}
115
116func staticProxy(req *http.Request, httpProxy, httpsProxy, bypass string) (*url.URL, error) {
117 selected := httpProxy
118 if req.URL.Scheme == "https" {
119 selected = httpsProxy
120 }
121 if selected != "" {
122 if _, err := proxyURL(selected); err != nil {
123 return nil, err
124 }
125 }
126 cfg := httpproxy.Config{HTTPProxy: httpProxy, HTTPSProxy: httpsProxy, NoProxy: strings.ReplaceAll(bypass, ";", ",")}
127 u, err := cfg.ProxyFunc()(req.URL)
128 if err != nil {
129 return nil, errors.New("主机代理设置无效,请检查系统代理设置")
130 }
131 if u != nil {
132 return proxyURL(u.String())
133 }
134 return nil, nil
135}