forked from aarond10/https_dns_proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps_client.c
More file actions
346 lines (322 loc) · 11.4 KB
/
https_client.c
File metadata and controls
346 lines (322 loc) · 11.4 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <curl/curl.h>
#include <errno.h>
#include <grp.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pwd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "https_client.h"
#include "json_to_dns.h"
#include "logging.h"
#include "options.h"
static size_t write_buffer(void *buf, size_t size, size_t nmemb, void *userp) {
struct https_fetch_ctx *ctx = (struct https_fetch_ctx *)userp;
unsigned char *new_buf = (unsigned char *)realloc(
ctx->buf, ctx->buflen + size * nmemb + 1);
if (new_buf == NULL) {
ELOG("Out of memory!");
return 0;
}
ctx->buf = new_buf;
memcpy(&(ctx->buf[ctx->buflen]), buf, size * nmemb);
ctx->buflen += size * nmemb;
// We always expect to receive valid non-null ASCII but just to be safe...
ctx->buf[ctx->buflen] = '\0';
return size * nmemb;
}
static void https_fetch_ctx_init(https_client_t *client,
struct https_fetch_ctx *ctx, const char *url,
struct curl_slist *resolv,
https_response_cb cb, void *cb_data) {
ctx->curl = curl_easy_init();
ctx->cb = cb;
ctx->cb_data = cb_data;
ctx->buf = NULL;
ctx->buflen = 0;
ctx->next = client->fetches;
client->fetches = ctx;
CURLcode res;
if ((res = curl_easy_setopt(ctx->curl, CURLOPT_RESOLVE, resolv)) !=
CURLE_OK) {
FLOG("CURLOPT_RESOLV error: %s", curl_easy_strerror(res));
}
DLOG("Requesting HTTP/1.1: %d\n", client->opt->use_http_1_1);
curl_easy_setopt(ctx->curl, CURLOPT_HTTP_VERSION,
client->opt->use_http_1_1 ?
CURL_HTTP_VERSION_1_1 :
CURL_HTTP_VERSION_2_0);
curl_easy_setopt(ctx->curl, CURLOPT_URL, url);
curl_easy_setopt(ctx->curl, CURLOPT_WRITEFUNCTION, &write_buffer);
curl_easy_setopt(ctx->curl, CURLOPT_WRITEDATA, ctx);
curl_easy_setopt(ctx->curl, CURLOPT_TCP_KEEPALIVE, 5L);
curl_easy_setopt(ctx->curl, CURLOPT_USERAGENT, "dns-to-https-proxy/0.2");
curl_easy_setopt(ctx->curl, CURLOPT_NOSIGNAL, 0);
curl_easy_setopt(ctx->curl, CURLOPT_TIMEOUT, 2 /* seconds */);
// We know Google supports this, so force it.
curl_easy_setopt(ctx->curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
if (client->opt->curl_proxy) {
DLOG("Using curl proxy: %s", client->opt->curl_proxy);
if ((res = curl_easy_setopt(ctx->curl, CURLOPT_PROXY,
client->opt->curl_proxy)) != CURLE_OK) {
FLOG("CURLOPT_PROXY error: %s", curl_easy_strerror(res));
}
}
curl_multi_add_handle(client->curlm, ctx->curl);
}
static void https_fetch_ctx_cleanup(https_client_t *client,
struct https_fetch_ctx *ctx) {
struct https_fetch_ctx *last = NULL;
struct https_fetch_ctx *cur = client->fetches;
while (cur) {
if (cur == ctx) {
curl_multi_remove_handle(client->curlm, ctx->curl);
if (client->opt->loglevel <= LOG_DEBUG) {
CURLcode res;
long long_resp = 0;
char *str_resp = NULL;
if ((res = curl_easy_getinfo(
ctx->curl, CURLINFO_EFFECTIVE_URL, &str_resp)) != CURLE_OK) {
ELOG("CURLINFO_EFFECTIVE_URL: %s", curl_easy_strerror(res));
} else {
DLOG("CURLINFO_EFFECTIVE_URL: %s", str_resp);
}
if ((res = curl_easy_getinfo(
ctx->curl, CURLINFO_REDIRECT_URL, &str_resp)) != CURLE_OK) {
ELOG("CURLINFO_REDIRECT_URL: %s", curl_easy_strerror(res));
} else if (str_resp != NULL) {
DLOG("CURLINFO_REDIRECT_URL: %s", str_resp);
}
if ((res = curl_easy_getinfo(
ctx->curl, CURLINFO_RESPONSE_CODE, &long_resp)) != CURLE_OK) {
ELOG("CURLINFO_RESPONSE_CODE: %s", curl_easy_strerror(res));
} else if (long_resp != 200) {
DLOG("CURLINFO_RESPONSE_CODE: %d", long_resp);
}
if ((res = curl_easy_getinfo(
ctx->curl, CURLINFO_SSL_VERIFYRESULT, &long_resp)) != CURLE_OK) {
ELOG("CURLINFO_SSL_VERIFYRESULT: %s", curl_easy_strerror(res));
} else if (long_resp != CURLE_OK) {
ELOG("CURLINFO_SSL_VERIFYRESULT: %s", curl_easy_strerror(long_resp));
}
if ((res = curl_easy_getinfo(
ctx->curl, CURLINFO_OS_ERRNO, &long_resp)) != CURLE_OK) {
ELOG("CURLINFO_OS_ERRNO: %s", curl_easy_strerror(res));
} else if (long_resp != 0) {
ELOG("CURLINFO_OS_ERRNO: %d", long_resp);
}
#ifdef CURLINFO_HTTP_VERSION
if ((res = curl_easy_getinfo(
ctx->curl, CURLINFO_HTTP_VERSION, &long_resp)) != CURLE_OK) {
ELOG("CURLINFO_HTTP_VERSION: %s", curl_easy_strerror(res));
} else {
switch (long_resp) {
case CURL_HTTP_VERSION_1_0:
DLOG("CURLINFO_HTTP_VERSION: %s", "1.0");
break;
case CURL_HTTP_VERSION_1_1:
DLOG("CURLINFO_HTTP_VERSION: %s", "1.1");
break;
case CURL_HTTP_VERSION_2_0:
DLOG("CURLINFO_HTTP_VERSION: %s", "2");
break;
default:
DLOG("CURLINFO_HTTP_VERSION: %d", long_resp);
}
}
#endif
#ifdef CURLINFO_PROTOCOL
if ((res = curl_easy_getinfo(
ctx->curl, CURLINFO_PROTOCOL, &long_resp)) != CURLE_OK) {
ELOG("CURLINFO_PROTOCOL: %s", curl_easy_strerror(res));
} else if (long_resp != CURLPROTO_HTTPS) {
DLOG("CURLINFO_PROTOCOL: %d", long_resp);
}
#endif
double namelookup_time;
double connect_time;
double appconnect_time;
double pretransfer_time;
double starttransfer_time;
double total_time;
if (curl_easy_getinfo(ctx->curl,
CURLINFO_NAMELOOKUP_TIME, &namelookup_time) != CURLE_OK ||
curl_easy_getinfo(ctx->curl,
CURLINFO_CONNECT_TIME, &connect_time) != CURLE_OK ||
curl_easy_getinfo(ctx->curl,
CURLINFO_APPCONNECT_TIME, &appconnect_time) != CURLE_OK ||
curl_easy_getinfo(ctx->curl,
CURLINFO_PRETRANSFER_TIME, &pretransfer_time) != CURLE_OK ||
curl_easy_getinfo(ctx->curl,
CURLINFO_STARTTRANSFER_TIME, &starttransfer_time) != CURLE_OK ||
curl_easy_getinfo(ctx->curl,
CURLINFO_TOTAL_TIME, &total_time) != CURLE_OK) {
ELOG("Err getting timing");
} else {
DLOG("Times: %lf, %lf, %lf, %lf, %lf, %lf",
namelookup_time, connect_time, appconnect_time, pretransfer_time,
starttransfer_time, total_time);
}
}
curl_easy_cleanup(ctx->curl);
ctx->cb(ctx->cb_data, ctx->buf, ctx->buflen);
free(ctx->buf);
if (last) {
last->next = cur->next;
} else {
client->fetches = cur->next;
}
return;
}
last = cur;
cur = cur->next;
}
}
static void check_multi_info(https_client_t *c) {
CURLMsg *msg;
int msgs_left;
while ((msg = curl_multi_info_read(c->curlm, &msgs_left))) {
if (msg->msg == CURLMSG_DONE) {
struct https_fetch_ctx *n = c->fetches;
while (n) {
if (n->curl == msg->easy_handle) {
https_fetch_ctx_cleanup(c, n);
free(n);
break;
}
n = n->next;
}
}
}
}
static void sock_cb(struct ev_loop *loop, struct ev_io *w, int revents) {
https_client_t *c = (https_client_t *)w->data;
if (c == NULL) {
FLOG("c is NULL");
}
CURLMcode rc = curl_multi_socket_action(
c->curlm, w->fd, (revents & EV_READ ? CURL_CSELECT_IN : 0) |
(revents & EV_WRITE ? CURL_CSELECT_OUT : 0),
&c->still_running);
if (rc != CURLM_OK) {
FLOG("curl_multi_socket_action: %d", rc);
}
check_multi_info(c);
}
static void timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents) {
https_client_t *c = (https_client_t *)w->data;
CURLMcode rc = curl_multi_socket_action(c->curlm, CURL_SOCKET_TIMEOUT, 0,
&c->still_running);
if (rc != CURLM_OK) {
FLOG("curl_multi_socket_action: %d", rc);
}
check_multi_info(c);
}
static int multi_sock_cb(CURL *curl, curl_socket_t sock, int what,
https_client_t *c, void *sockp) {
if (!curl) {
FLOG("Unexpected NULL pointer for CURL");
}
if (!c) {
FLOG("Unexpected NULL pointer for https_client_t");
}
#ifndef NO_LIBCURL_BUG_WORKAROUND
static int curl_bug = -1;
if (curl_bug == -1) {
if (what == CURL_POLL_IN) {
curl_bug = 0;
} else if (what == CURL_POLL_REMOVE) {
ELOG("libcurl bug detected: socket closed without ever being read.");
ELOG("Activating workaround. PERFORMANCE WILL BE GREATLY DEGRADED!");
curl_bug = 1;
}
}
if (curl_bug == 1 && what == CURL_POLL_REMOVE && c != NULL) {
do {
curl_multi_perform(c->curlm, &c->still_running);
} while (c->still_running != 0);
}
#endif
if (what == CURL_POLL_REMOVE) {
ev_io_stop(c->loop, &c->fd[sock]);
c->fd[sock].fd = 0;
return 0;
}
if (c->fd[sock].fd) {
ev_io_stop(c->loop, &c->fd[sock]);
}
ev_io_init(&c->fd[sock], sock_cb, sock,
((what & CURL_POLL_IN) ? EV_READ : 0) |
((what & CURL_POLL_OUT) ? EV_WRITE : 0));
c->fd[sock].data = c;
ev_io_start(c->loop, &c->fd[sock]);
return 0;
}
static int multi_timer_cb(CURLM *multi, long timeout_ms, https_client_t *c) {
ev_timer_stop(c->loop, &c->timer);
if (timeout_ms > 0) {
ev_timer_init(&c->timer, timer_cb, timeout_ms / 1000.0, 0);
ev_timer_start(c->loop, &c->timer);
} else {
timer_cb(c->loop, &c->timer, 0);
}
return 0;
}
void https_client_init(https_client_t *c, options_t *opt, struct ev_loop *loop) {
int i = 0;
memset(c, 0, sizeof(*c));
c->loop = loop;
c->curlm = curl_multi_init();
c->fetches = NULL;
c->timer.data = c;
for (i = 0; i < FD_SETSIZE; i++) {
c->fd[i].fd = 0;
}
c->opt = opt;
#if defined(CURLMOPT_PIPELINING) && defined(CURLPIPE_HTTP1) && \
defined(CURLPIPE_MULTIPLEX)
if (c->opt->use_http_1_1) {
curl_multi_setopt(c->curlm, CURLMOPT_PIPELINING, CURLPIPE_HTTP1);
} else {
curl_multi_setopt(c->curlm, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
}
#endif
curl_multi_setopt(c->curlm, CURLMOPT_MAX_TOTAL_CONNECTIONS, 8);
curl_multi_setopt(c->curlm, CURLMOPT_MAXCONNECTS, 8);
curl_multi_setopt(c->curlm, CURLMOPT_SOCKETDATA, c);
curl_multi_setopt(c->curlm, CURLMOPT_SOCKETFUNCTION, multi_sock_cb);
curl_multi_setopt(c->curlm, CURLMOPT_TIMERDATA, c);
curl_multi_setopt(c->curlm, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
}
void https_client_fetch(https_client_t *c, const char *url,
struct curl_slist *resolv, https_response_cb cb,
void *data) {
struct https_fetch_ctx *new_ctx =
(struct https_fetch_ctx *)calloc(1, sizeof(struct https_fetch_ctx));
if (!new_ctx) {
FLOG("Out of mem");
}
https_fetch_ctx_init(c, new_ctx, url, resolv, cb, data);
}
void https_client_cleanup(https_client_t *c) {
int i;
while (c->fetches) {
struct https_fetch_ctx *n = c->fetches;
https_fetch_ctx_cleanup(c, n);
free(n);
}
for (i = 0; i < FD_SETSIZE; i++) {
if (c->fd[i].fd) {
ev_io_stop(c->loop, &c->fd[i]);
}
}
ev_timer_stop(c->loop, &c->timer);
curl_multi_cleanup(c->curlm);
}