跳到主要内容

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

浏览文件

proxy_darwin.go

118 行 · 4.4 KB历史下载
1//go:build darwin && cgo
2
3package hostproxy
4
5/*
6#cgo LDFLAGS: -framework CoreFoundation -framework CFNetwork
7#include <CFNetwork/CFNetwork.h>
8#include <stdlib.h>
9#include <string.h>
10
11typedef struct { char *url; int failed; } CageProxy;
12static char *cageString(CFStringRef value) {
13 if (!value) return NULL;
14 CFIndex size = CFStringGetMaximumSizeForEncoding(CFStringGetLength(value), kCFStringEncodingUTF8) + 1;
15 char *text = calloc(size, 1);
16 if (!CFStringGetCString(value, text, size, kCFStringEncodingUTF8)) { free(text); return NULL; }
17 return text;
18}
19static void cagePACCallback(void *client, CFArrayRef proxies, CFErrorRef error) {
20 CFTypeRef *result = client;
21 *result = CFRetain(error ? (CFTypeRef)error : (CFTypeRef)proxies);
22 CFRunLoopStop(CFRunLoopGetCurrent());
23}
24static CageProxy cageResolveArray(CFArrayRef proxies, CFURLRef target, int allowPAC) {
25 CageProxy result = {NULL, 1};
26 if (!proxies || !CFArrayGetCount(proxies)) return result;
27 CFDictionaryRef proxy = CFArrayGetValueAtIndex(proxies, 0);
28 CFStringRef type = CFDictionaryGetValue(proxy, kCFProxyTypeKey);
29 if (!type) return result;
30 if (CFEqual(type, kCFProxyTypeNone)) { result.failed = 0; return result; }
31 if (allowPAC && CFEqual(type, kCFProxyTypeAutoConfigurationURL)) {
32 CFURLRef pac = CFDictionaryGetValue(proxy, kCFProxyAutoConfigurationURLKey);
33 if (!pac) return result;
34 CFTypeRef answer = NULL;
35 CFStreamClientContext context = {0, &answer, NULL, NULL, NULL};
36 CFRunLoopSourceRef source = CFNetworkExecuteProxyAutoConfigurationURL(pac, target, cagePACCallback, &context);
37 if (source) {
38 CFStringRef mode = CFSTR("CageImageProxy");
39 CFRunLoopAddSource(CFRunLoopGetCurrent(), source, mode);
40 CFRunLoopRunInMode(mode, 15, false);
41 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), source, mode);
42 CFRunLoopSourceInvalidate(source);
43 CFRelease(source);
44 }
45 if (answer) {
46 if (CFGetTypeID(answer) == CFArrayGetTypeID()) result = cageResolveArray((CFArrayRef)answer, target, 0);
47 CFRelease(answer);
48 }
49 return result;
50 }
51 const char *scheme = NULL;
52 if (CFEqual(type, kCFProxyTypeHTTP)) scheme = "http";
53 // CFNetwork's HTTPS type means HTTP CONNECT, not TLS to the proxy.
54 if (CFEqual(type, kCFProxyTypeHTTPS)) scheme = "http";
55 if (CFEqual(type, kCFProxyTypeSOCKS)) scheme = "socks5";
56 if (!scheme) return result;
57 char *host = cageString(CFDictionaryGetValue(proxy, kCFProxyHostNameKey));
58 int port = 0;
59 CFNumberRef number = CFDictionaryGetValue(proxy, kCFProxyPortNumberKey);
60 if (number) CFNumberGetValue(number, kCFNumberIntType, &port);
61 if (host && port > 0 && port <= 65535) {
62 CFStringRef url = CFStringCreateWithFormat(NULL, NULL, CFSTR("%s://%s%s%s:%d"), scheme,
63 strchr(host, ':') ? "[" : "", host, strchr(host, ':') ? "]" : "", port);
64 result.url = cageString(url);
65 result.failed = result.url == NULL;
66 CFRelease(url);
67 }
68 free(host);
69 return result;
70}
71static CageProxy cageSystemProxy(char *raw) {
72 CageProxy result = {NULL, 1};
73 CFStringRef text = CFStringCreateWithCString(NULL, raw, kCFStringEncodingUTF8);
74 CFURLRef url = text ? CFURLCreateWithString(NULL, text, NULL) : NULL;
75 CFDictionaryRef settings = CFNetworkCopySystemProxySettings();
76 if (url && settings) {
77 CFArrayRef proxies = CFNetworkCopyProxiesForURL(url, settings);
78 result = cageResolveArray(proxies, url, 1);
79 if (proxies) CFRelease(proxies);
80 }
81 if (settings) CFRelease(settings);
82 if (url) CFRelease(url);
83 if (text) CFRelease(text);
84 return result;
85}
86*/
87import "C"
88
89import (
90 "errors"
91 "net/http"
92 "net/url"
93 "runtime"
94 "unsafe"
95)
96
97func systemProxy(req *http.Request) (*url.URL, error) {
98 if err := req.Context().Err(); err != nil {
99 return nil, err
100 }
101 // CFNetwork's PAC callback and run loop belong to the same OS thread.
102 runtime.LockOSThread()
103 defer runtime.UnlockOSThread()
104 raw := C.CString(req.URL.String())
105 defer C.free(unsafe.Pointer(raw))
106 result := C.cageSystemProxy(raw)
107 defer C.free(unsafe.Pointer(result.url))
108 if err := req.Context().Err(); err != nil {
109 return nil, err
110 }
111 if result.failed != 0 {
112 return nil, errors.New("无法解析主机系统代理或 PAC,请检查 Clash 等代理软件后重试;未切换直连")
113 }
114 if result.url == nil {
115 return nil, nil
116 }
117 return proxyURL(C.GoString(result.url))
118}