-
Notifications
You must be signed in to change notification settings - Fork 2
/
resolver_mac.c
276 lines (236 loc) · 9.55 KB
/
resolver_mac.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CFNetwork/CFNetwork.h>
#include "config.h"
#include "event.h"
#include "log.h"
#include "resolver.h"
#include "resolver_i.h"
#include "resolver_mac.h"
#include "util.h"
#ifdef LEAK_SANITIZER
# include <sanitizer/lsan_interface.h>
#endif
#define PROXY_RESOLVER_RUN_LOOP CFSTR("proxy_resolver_mac.run_loop")
#define PROXY_RESOLVER_TIMEOUT_SEC 10
typedef struct g_proxy_resolver_mac_s {
bool reserved;
} g_proxy_resolver_mac_s;
g_proxy_resolver_mac_s g_proxy_resolver_mac;
typedef struct proxy_resolver_mac_s {
// Last system error
int64_t error;
// Complete event
void *complete;
// Proxy list
char *list;
} proxy_resolver_mac_s;
static const char *proxy_resolver_mac_get_proxy_scheme(CFStringRef proxy_type) {
const char *scheme = NULL;
if (CFEqual(proxy_type, kCFProxyTypeNone))
scheme = "direct://";
else if (CFEqual(proxy_type, kCFProxyTypeHTTP))
scheme = "http://";
else if (CFEqual(proxy_type, kCFProxyTypeHTTPS))
// "HTTPS Proxy" on macOS means "use CONNENCT verb for a https:// URL", the proxy still should be an HTTP proxy.
scheme = "http://";
else if (CFEqual(proxy_type, kCFProxyTypeSOCKS))
scheme = "socks://";
else if (CFEqual(proxy_type, kCFProxyTypeFTP))
scheme = "ftp://";
return scheme;
}
static void proxy_resolver_mac_auto_config_result_callback(void *client, CFArrayRef proxy_array, CFErrorRef error) {
proxy_resolver_mac_s *proxy_resolver = (proxy_resolver_mac_s *)client;
if (error) {
// Get error code
proxy_resolver->error = CFErrorGetCode(error);
} else {
// Convert proxy array into PAC file return format
const size_t proxy_count = CFArrayGetCount(proxy_array);
const size_t max_list = proxy_count * MAX_PROXY_URL + 1;
size_t list_len = 0;
proxy_resolver->list = (char *)calloc(max_list, sizeof(char));
// Enumerate through each proxy in the array
for (size_t i = 0; proxy_resolver->list && i < proxy_count && list_len < max_list; i++) {
CFDictionaryRef proxy = CFArrayGetValueAtIndex(proxy_array, i);
CFStringRef proxy_type = (CFStringRef)CFDictionaryGetValue(proxy, kCFProxyTypeKey);
// Copy type of connection
const char *scheme = proxy_resolver_mac_get_proxy_scheme(proxy_type);
if (scheme) {
strncpy(proxy_resolver->list + list_len, scheme, max_list - list_len);
list_len += strlen(scheme);
} else {
LOG_WARN("Unknown proxy type encountered\n");
continue;
}
if (!CFEqual(proxy_type, kCFProxyTypeNone) && list_len < max_list) {
// Copy proxy host
CFStringRef host = (CFStringRef)CFDictionaryGetValue(proxy, kCFProxyHostNameKey);
if (host && list_len < max_list) {
const char *host_utf8 = CFStringGetCStringPtr(host, kCFStringEncodingUTF8);
if (host_utf8) {
strncpy(proxy_resolver->list + list_len, host_utf8, max_list - list_len);
list_len += strlen(host_utf8);
} else if (CFStringGetCString(host, proxy_resolver->list + list_len, max_list - list_len,
kCFStringEncodingUTF8)) {
list_len = strlen(proxy_resolver->list);
} else {
list_len = max_list;
}
}
// Copy proxy port
CFNumberRef port = (CFNumberRef)CFDictionaryGetValue(proxy, kCFProxyPortNumberKey);
if (port && list_len < max_list) {
int32_t port_number = 0;
CFNumberGetValue(port, kCFNumberSInt32Type, &port_number);
list_len +=
snprintf(proxy_resolver->list + list_len, max_list - list_len, ":%" PRId32, port_number);
}
}
if (i != proxy_count - 1 && list_len < max_list) {
// Separate each proxy with a comma
strncat(proxy_resolver->list, ",", max_list - list_len);
list_len++;
}
}
if (list_len >= max_list) {
proxy_resolver->error = ERANGE;
LOG_WARN("Proxy list limit exceeded\n");
free(proxy_resolver->list);
proxy_resolver->list = NULL;
} else if (!proxy_resolver->list) {
proxy_resolver->error = ENOMEM;
}
}
CFRunLoopStop(CFRunLoopGetCurrent());
return;
}
bool proxy_resolver_mac_get_proxies_for_url(void *ctx, const char *url) {
proxy_resolver_mac_s *proxy_resolver = (proxy_resolver_mac_s *)ctx;
CFURLRef target_url_ref = NULL;
CFURLRef url_ref = NULL;
char *auto_config_url = NULL;
bool is_ok = false;
if (!proxy_resolver || !url)
return false;
auto_config_url = proxy_config_get_auto_config_url();
if (!auto_config_url) {
proxy_resolver->error = EINVAL;
LOG_ERROR("Auto configuration url not specified");
goto mac_done;
}
url_ref = CFURLCreateWithBytes(NULL, (const UInt8 *)auto_config_url, strlen(auto_config_url), kCFStringEncodingUTF8,
NULL);
if (!url_ref) {
proxy_resolver->error = ENOMEM;
LOG_ERROR("Unable to allocate memory for %s (%" PRId64 ")\n", "auto config url reference",
proxy_resolver->error);
goto mac_done;
}
target_url_ref = CFURLCreateWithBytes(NULL, (const UInt8 *)url, strlen(url), kCFStringEncodingUTF8, NULL);
if (!target_url_ref) {
proxy_resolver->error = ENOMEM;
LOG_ERROR("Unable to allocate memory for %s (%" PRId64 ")\n", "target url reference", proxy_resolver->error);
goto mac_done;
}
CFStreamClientContext context = {0, proxy_resolver, NULL, NULL, NULL};
CFRunLoopSourceRef run_loop = CFNetworkExecuteProxyAutoConfigurationURL(
url_ref, target_url_ref, proxy_resolver_mac_auto_config_result_callback, &context);
if (!run_loop) {
proxy_resolver->error = ELOOP;
LOG_ERROR("Failed to execute pac url (%" PRId64 ")\n", proxy_resolver->error);
goto mac_done;
}
#ifdef LEAK_SANITIZER
// There is a known issue mentioned in Chromium source, that the run loop instance
// returned by CFNetworkExecuteProxyAutoConfigurationURL leaks.
// Additionally it is discussed on these forums:
// http://www.openradar.appspot.com/20974299
// https://forums.developer.apple.com/forums/thread/724883
// https://stackoverflow.com/questions/53290871
__lsan_ignore_object(run_loop);
#endif
CFRunLoopAddSource(CFRunLoopGetCurrent(), run_loop, PROXY_RESOLVER_RUN_LOOP);
CFRunLoopRunInMode(PROXY_RESOLVER_RUN_LOOP, PROXY_RESOLVER_TIMEOUT_SEC, false);
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), run_loop, PROXY_RESOLVER_RUN_LOOP);
CFRunLoopSourceInvalidate(run_loop);
CFRelease(run_loop);
mac_done:
is_ok = proxy_resolver->error == 0;
event_set(proxy_resolver->complete);
free(auto_config_url);
if (url_ref)
CFRelease(url_ref);
if (target_url_ref)
CFRelease(target_url_ref);
return is_ok;
}
const char *proxy_resolver_mac_get_list(void *ctx) {
proxy_resolver_mac_s *proxy_resolver = (proxy_resolver_mac_s *)ctx;
if (!proxy_resolver)
return NULL;
return proxy_resolver->list;
}
int32_t proxy_resolver_mac_get_error(void *ctx) {
proxy_resolver_mac_s *proxy_resolver = (proxy_resolver_mac_s *)ctx;
return (int32_t)proxy_resolver->error;
}
bool proxy_resolver_mac_wait(void *ctx, int32_t timeout_ms) {
proxy_resolver_mac_s *proxy_resolver = (proxy_resolver_mac_s *)ctx;
if (!proxy_resolver)
return false;
return event_wait(proxy_resolver->complete, timeout_ms);
}
bool proxy_resolver_mac_cancel(void *ctx) {
return false;
}
void *proxy_resolver_mac_create(void) {
proxy_resolver_mac_s *proxy_resolver = (proxy_resolver_mac_s *)calloc(1, sizeof(proxy_resolver_mac_s));
if (!proxy_resolver)
return NULL;
proxy_resolver->complete = event_create();
if (!proxy_resolver->complete) {
free(proxy_resolver);
return NULL;
}
return proxy_resolver;
}
bool proxy_resolver_mac_delete(void **ctx) {
if (!ctx)
return false;
proxy_resolver_mac_s *proxy_resolver = (proxy_resolver_mac_s *)*ctx;
if (!proxy_resolver)
return false;
proxy_resolver_mac_cancel(ctx);
event_delete(&proxy_resolver->complete);
free(proxy_resolver->list);
free(proxy_resolver);
return true;
}
bool proxy_resolver_mac_global_init(void) {
return true;
}
bool proxy_resolver_mac_global_cleanup(void) {
memset(&g_proxy_resolver_mac, 0, sizeof(g_proxy_resolver_mac));
return true;
}
const proxy_resolver_i_s *proxy_resolver_mac_get_interface(void) {
static const proxy_resolver_i_s proxy_resolver_mac_i = {
proxy_resolver_mac_get_proxies_for_url,
proxy_resolver_mac_get_list,
proxy_resolver_mac_get_error,
proxy_resolver_mac_wait,
proxy_resolver_mac_cancel,
proxy_resolver_mac_create,
proxy_resolver_mac_delete,
false, // get_proxies_for_url should be spooled to another thread
false, // get_proxies_for_url does not take into account system config
proxy_resolver_mac_global_init,
proxy_resolver_mac_global_cleanup};
return &proxy_resolver_mac_i;
}