forked from mattn/go-ieproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pac_darwin.go
141 lines (115 loc) · 3.81 KB
/
pac_darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package ieproxy
/*
#cgo LDFLAGS: -framework CoreFoundation
#cgo LDFLAGS: -framework CFNetwork
#include <strings.h>
#include <CFNetwork/CFProxySupport.h>
#define STR_LEN 128
void proxyAutoConfCallback(void* client, CFArrayRef proxies, CFErrorRef error) {
CFTypeRef* result_ptr = (CFTypeRef*)client;
if (error != NULL) {
*result_ptr = CFRetain(error);
} else {
*result_ptr = CFRetain(proxies);
}
CFRunLoopStop(CFRunLoopGetCurrent());
}
int intCFNumber(CFNumberRef num) {
int ret;
CFNumberGetValue(num, kCFNumberIntType, &ret);
return ret;
}
char* _getProxyUrlFromPac(char* pac, char* reqCs) {
char* retCString = (char*)calloc(STR_LEN, sizeof(char));
CFStringRef reqStr = CFStringCreateWithCString(NULL, reqCs, kCFStringEncodingUTF8);
CFStringRef pacStr = CFStringCreateWithCString(NULL, pac, kCFStringEncodingUTF8);
CFURLRef pacUrl = CFURLCreateWithString(NULL, pacStr, NULL);
CFURLRef reqUrl = CFURLCreateWithString(NULL, reqStr, NULL);
CFTypeRef result = NULL;
CFStreamClientContext context = { 0, &result, NULL, NULL, NULL };
CFRunLoopSourceRef runloop_src = CFNetworkExecuteProxyAutoConfigurationURL(pacUrl, reqUrl, proxyAutoConfCallback, &context);
if (runloop_src) {
const CFStringRef private_runloop_mode = CFSTR("go-ieproxy");
CFRunLoopAddSource(CFRunLoopGetCurrent(), runloop_src, private_runloop_mode);
CFRunLoopRunInMode(private_runloop_mode, DBL_MAX, false);
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runloop_src, kCFRunLoopCommonModes);
if (CFGetTypeID(result) == CFArrayGetTypeID()) {
CFArrayRef resultArray = (CFTypeRef)result;
if (CFArrayGetCount(resultArray) > 0) {
CFDictionaryRef pxy = (CFDictionaryRef)CFArrayGetValueAtIndex(resultArray, 0);
CFStringRef pxyType = CFDictionaryGetValue(pxy, kCFProxyTypeKey);
if (CFEqual(pxyType, kCFProxyTypeNone)) {
// noop
}
if (CFEqual(pxyType, kCFProxyTypeHTTP)) {
CFStringRef host = (CFStringRef)CFDictionaryGetValue(pxy, kCFProxyHostNameKey);
CFNumberRef port = (CFNumberRef)CFDictionaryGetValue(pxy, kCFProxyPortNumberKey);
char host_str[STR_LEN - 16];
CFStringGetCString(host, host_str, STR_LEN - 16, kCFStringEncodingUTF8);
int port_int = 80;
if (port) {
CFNumberGetValue(port, kCFNumberIntType, &port_int);
}
sprintf(retCString, "%s:%d", host_str, port_int);
}
}
} else {
// error
}
}
CFRelease(result);
CFRelease(reqStr);
CFRelease(reqUrl);
CFRelease(pacStr);
CFRelease(pacUrl);
return retCString;
}
char* _getPacUrl() {
char* retCString = (char*)calloc(STR_LEN, sizeof(char));
CFDictionaryRef proxyDict = CFNetworkCopySystemProxySettings();
CFNumberRef pacEnable = (CFNumberRef)CFDictionaryGetValue(proxyDict, kCFNetworkProxiesProxyAutoConfigEnable);
if (pacEnable && intCFNumber(pacEnable)) {
CFStringRef pacUrlStr = (CFStringRef)CFDictionaryGetValue(proxyDict, kCFNetworkProxiesProxyAutoConfigURLString);
if (pacUrlStr) {
CFStringGetCString(pacUrlStr, retCString, STR_LEN, kCFStringEncodingUTF8);
}
}
CFRelease(proxyDict);
return retCString;
}
*/
import "C"
import (
"net/url"
"unsafe"
)
func (psc *ProxyScriptConf) findProxyForURL(URL string) string {
if !psc.Active {
return ""
}
proxy := getProxyForURL(psc.PreConfiguredURL, URL)
return proxy
}
func getProxyForURL(pacFileURL, targetURL string) string {
if pacFileURL == "" {
pacFileURL = getPacUrl()
}
if pacFileURL == "" {
return ""
}
if u, err := url.Parse(pacFileURL); err != nil || u.Scheme == "" {
return ""
}
csUrl := C.CString(targetURL)
csPac := C.CString(pacFileURL)
csRet := C._getProxyUrlFromPac(csPac, csUrl)
defer C.free(unsafe.Pointer(csUrl))
defer C.free(unsafe.Pointer(csPac))
defer C.free(unsafe.Pointer(csRet))
return C.GoString(csRet)
}
func getPacUrl() string {
csRet := C._getPacUrl()
defer C.free(unsafe.Pointer(csRet))
return C.GoString(csRet)
}