跳到主要内容

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

浏览文件

proxy_windows.go

120 行 · 3.3 KB历史下载
1package hostproxy
2
3import (
4 "fmt"
5 "net/http"
6 "net/url"
7 "strings"
8 "unsafe"
9
10 "golang.org/x/sys/windows"
11)
12
13var winhttp = windows.NewLazySystemDLL("winhttp.dll")
14var globalFree = windows.NewLazySystemDLL("kernel32.dll").NewProc("GlobalFree")
15
16type ieConfig struct {
17 AutoDetect int32
18 URL, Proxy, Bypass *uint16
19}
20type autoOptions struct {
21 Flags, DetectFlags uint32
22 URL *uint16
23 Reserved uintptr
24 ReservedFlags uint32
25 AutoLogon int32
26}
27type proxyInfo struct {
28 AccessType uint32
29 Proxy, Bypass *uint16
30}
31
32func freeString(p *uint16) {
33 if p != nil {
34 globalFree.Call(uintptr(unsafe.Pointer(p)))
35 }
36}
37func proxyString(p *uint16) string { return windows.UTF16PtrToString(p) }
38
39func systemProxy(req *http.Request) (*url.URL, error) {
40 var config ieConfig
41 ok, _, err := winhttp.NewProc("WinHttpGetIEProxyConfigForCurrentUser").Call(uintptr(unsafe.Pointer(&config)))
42 if ok == 0 {
43 if err == windows.ERROR_FILE_NOT_FOUND {
44 return nil, nil
45 }
46 return nil, fmt.Errorf("无法读取主机系统代理,未切换直连:%w", err)
47 }
48 defer freeString(config.URL)
49 defer freeString(config.Proxy)
50 defer freeString(config.Bypass)
51 if config.URL != nil || config.AutoDetect != 0 {
52 handle, _, err := winhttp.NewProc("WinHttpOpen").Call(0, 1, 0, 0, 0)
53 if handle == 0 {
54 return nil, fmt.Errorf("无法解析主机自动代理:%w", err)
55 }
56 defer winhttp.NewProc("WinHttpCloseHandle").Call(handle)
57 winhttp.NewProc("WinHttpSetTimeouts").Call(handle, 15000, 15000, 15000, 15000)
58 options := autoOptions{URL: config.URL}
59 if config.URL != nil {
60 options.Flags = 2
61 } else {
62 options.Flags = 1
63 options.DetectFlags = 3
64 }
65 target, err := windows.UTF16PtrFromString(req.URL.String())
66 if err != nil {
67 return nil, err
68 }
69 var info proxyInfo
70 ok, _, err := winhttp.NewProc("WinHttpGetProxyForUrl").Call(handle, uintptr(unsafe.Pointer(target)), uintptr(unsafe.Pointer(&options)), uintptr(unsafe.Pointer(&info)))
71 defer freeString(info.Proxy)
72 defer freeString(info.Bypass)
73 if ok != 0 {
74 if info.AccessType == 1 {
75 return nil, nil
76 } // PAC explicitly selected DIRECT.
77 if info.Proxy == nil {
78 return nil, fmt.Errorf("主机自动代理返回空地址,未切换直连")
79 }
80 return windowsProxy(req, proxyString(info.Proxy), proxyString(info.Bypass))
81 }
82 // Windows commonly enables WPAD even when this network has no PAC.
83 // An explicitly configured PAC that fails must never become DIRECT.
84 if config.URL != nil || err != windows.Errno(12180) {
85 return nil, fmt.Errorf("主机 PAC/自动代理解析失败,未切换直连,请检查代理软件:%w", err)
86 }
87 }
88 return windowsProxy(req, proxyString(config.Proxy), proxyString(config.Bypass))
89}
90
91func windowsProxy(req *http.Request, list, bypass string) (*url.URL, error) {
92 if list == "" {
93 return nil, nil
94 }
95 var fallback, httpProxy, httpsProxy string
96 for _, entry := range strings.Split(list, ";") {
97 key, value, specific := strings.Cut(strings.TrimSpace(entry), "=")
98 if !specific {
99 if fallback == "" {
100 fallback = key
101 }
102 continue
103 }
104 switch strings.ToLower(key) {
105 case "http":
106 httpProxy = value
107 case "https":
108 httpsProxy = value
109 case "socks":
110 fallback = "socks5://" + value
111 }
112 }
113 if httpProxy == "" {
114 httpProxy = fallback
115 }
116 if httpsProxy == "" {
117 httpsProxy = fallback
118 }
119 return staticProxy(req, httpProxy, httpsProxy, bypass)
120}