-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathextend.c
630 lines (539 loc) · 15.2 KB
/
extend.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#include "extend.h"
#ifdef _WIN32
#include <ws2tcpip.h>
#ifndef TCP_MAXRT
#define TCP_MAXRT 5
#endif
#else
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/un.h>
#include <sys/time.h>
#endif
#include <string.h>
#include <assert.h>
#include "proxy.h"
#include "error.h"
#include "params.h"
#include "desync.h"
#include "packets.h"
#define KEY_SIZE sizeof(union sockaddr_u)
static int set_timeout(int fd, unsigned int s)
{
#ifdef __linux__
if (setsockopt(fd, IPPROTO_TCP,
TCP_USER_TIMEOUT, (char *)&s, sizeof(s))) {
uniperror("setsockopt TCP_USER_TIMEOUT");
return -1;
}
#else
#ifdef _WIN32
if (setsockopt(fd, IPPROTO_TCP,
TCP_MAXRT, (char *)&s, sizeof(s))) {
uniperror("setsockopt TCP_MAXRT");
return -1;
}
#endif
#endif
return 0;
}
static ssize_t serialize_addr(const union sockaddr_u *dst,
uint8_t *const out, const size_t out_len)
{
#define serialize(raw, field, len, counter){ \
const size_t size = sizeof(field); \
if ((counter + size) <= len) { \
memcpy(raw + counter, &(field), size); \
counter += size; \
} else return 0; \
}
size_t c = 0;
serialize(out, dst->in.sin_port, out_len, c);
serialize(out, dst->sa.sa_family, out_len, c);
if (dst->sa.sa_family == AF_INET) {
serialize(out, dst->in.sin_addr, out_len, c);
} else {
serialize(out, dst->in6.sin6_addr, out_len, c);
}
#undef serialize
return c;
}
static int cache_get(const union sockaddr_u *dst)
{
uint8_t key[KEY_SIZE] = { 0 };
int len = serialize_addr(dst, key, sizeof(key));
struct elem_i *val = (struct elem_i *)mem_get(params.mempool, (char *)key, len);
if (!val) {
return -1;
}
time_t t = time(0);
if (t > val->time + params.cache_ttl) {
LOG(LOG_S, "time=%jd, now=%jd, ignore\n", (intmax_t)val->time, (intmax_t)t);
return 0;
}
return val->m;
}
static int cache_add(const union sockaddr_u *dst, int m)
{
assert(m >= 0 && m < params.dp_count);
uint8_t key[KEY_SIZE] = { 0 };
int len = serialize_addr(dst, key, sizeof(key));
INIT_ADDR_STR((*dst));
if (m == 0) {
LOG(LOG_S, "delete ip: %s\n", ADDR_STR);
mem_delete(params.mempool, (char *)key, len);
return 0;
}
LOG(LOG_S, "save ip: %s, m=%d\n", ADDR_STR, m);
time_t t = time(0);
char *key_d = malloc(len);
if (!key_d) {
return -1;
}
memcpy(key_d, key, len);
struct elem_i *val = (struct elem_i *)mem_add(params.mempool, key_d, len, sizeof(struct elem_i));
if (!val) {
uniperror("mem_add");
free(key_d);
return -1;
}
val->m = m;
val->time = t;
return 0;
}
int connect_hook(struct poolhd *pool, struct eval *val,
const union sockaddr_u *dst, int next)
{
int m = cache_get(dst);
val->cache = (m == 0);
val->attempt = m < 0 ? 0 : m;
return create_conn(pool, val, dst, next);
}
int socket_mod(int fd)
{
if (params.custom_ttl) {
if (setttl(fd, params.def_ttl) < 0) {
return -1;
}
}
if (params.protect_path) {
return protect(fd, params.protect_path);
}
return 0;
}
static int reconnect(struct poolhd *pool, struct eval *val, int m)
{
assert(val->flag == FLAG_CONN);
struct eval *client = val->pair;
if (create_conn(pool, client, &val->addr, EV_FIRST_TUNNEL)) {
return -1;
}
val->pair = 0;
del_event(pool, val);
//client->type = EV_IGNORE;
client->attempt = m;
client->cache = 1;
client->buff.offset = 0;
client->round_sent = 0;
return 0;
}
static bool check_host(
struct mphdr *hosts, const char *buffer, ssize_t n)
{
char *host = 0;
int len;
if (!(len = parse_tls(buffer, n, &host))) {
len = parse_http(buffer, n, &host, 0);
}
assert(len == 0 || host != 0);
if (len <= 0) {
return 0;
}
struct elem *v = mem_get(hosts, host, len);
return v && v->len <= len;
}
static bool check_ip(
struct mphdr *ipset, const union sockaddr_u *dst)
{
int len = sizeof(dst->in.sin_addr);
char *data = (char *)&dst->in.sin_addr;
if (dst->sa.sa_family == AF_INET6) {
len = sizeof(dst->in6.sin6_addr);
data = (char *)&dst->in6.sin6_addr;
}
if (mem_get(ipset, data, len * 8)) {
return 1;
}
return 0;
}
static bool check_proto_tcp(int proto, const char *buffer, ssize_t n)
{
if (!(proto & ~IS_IPV4)) {
return 1;
}
else if ((proto & IS_HTTP) &&
is_http(buffer, n)) {
return 1;
}
else if ((proto & IS_HTTPS) &&
is_tls_chello(buffer, n)) {
return 1;
}
return 0;
}
static bool check_l34(struct desync_params *dp, int st, const union sockaddr_u *dst)
{
if ((dp->proto & IS_UDP) && (st != SOCK_DGRAM)) {
return 0;
}
if (dp->proto & IS_IPV4) {
static const char *pat = "\0\0\0\0\0\0\0\0\0\0\xff\xff";
if (dst->sa.sa_family != AF_INET
&& memcmp(&dst->in6.sin6_addr, pat, 12)) {
return 0;
}
}
if (dp->pf[0] &&
(dst->in.sin_port < dp->pf[0] || dst->in.sin_port > dp->pf[1])) {
return 0;
}
if (dp->ipset && !check_ip(dp->ipset, dst)) {
return 0;
}
return 1;
}
static bool check_round(const int *nr, int r)
{
return (!nr[1] && r <= 1) || (r >= nr[0] && r <= nr[1]);
}
static int on_trigger(int type, struct poolhd *pool, struct eval *val)
{
int m = val->pair->attempt + 1;
bool can_reconn = (
val->pair->buff.data && !val->recv_count
&& params.auto_level > AUTO_NOBUFF
);
if (!can_reconn && params.auto_level <= AUTO_NOSAVE) {
return -1;
}
for (; m < params.dp_count; m++) {
struct desync_params *dp = ¶ms.dp[m];
if (!dp->detect) {
break;
}
if (!(dp->detect & type)) {
continue;
}
if (can_reconn) {
return reconnect(pool, val, m);
}
cache_add(&val->addr, m);
break;
}
if (m >= params.dp_count && m > 1) {
cache_add(&val->addr, 0);
}
return -1;
}
static int on_torst(struct poolhd *pool, struct eval *val)
{
if (on_trigger(DETECT_TORST, pool, val) == 0) {
return 0;
}
struct linger l = { .l_onoff = 1 };
if (setsockopt(val->pair->fd, SOL_SOCKET,
SO_LINGER, (char *)&l, sizeof(l)) < 0) {
uniperror("setsockopt SO_LINGER");
}
return -1;
}
static int on_fin(struct poolhd *pool, struct eval *val)
{
if (!(val->pair->mark && val->round_count <= 1)) {
return -1;
}
if (on_trigger(DETECT_TLS_ERR, pool, val) == 0) {
return 0;
}
return -1;
}
static int on_response(struct poolhd *pool, struct eval *val,
const char *resp, ssize_t sn)
{
int m = val->pair->attempt + 1;
char *req = val->pair->buff.data;
ssize_t qn = val->pair->buff.size;
for (; m < params.dp_count; m++) {
struct desync_params *dp = ¶ms.dp[m];
if (!dp->detect) {
return -1;
}
if ((dp->detect & DETECT_HTTP_LOCAT)
&& is_http_redirect(req, qn, resp, sn)) {
break;
}
else if ((dp->detect & DETECT_TLS_ERR)
&& ((is_tls_chello(req, qn) && !is_tls_shello(resp, sn))
|| neq_tls_sid(req, qn, resp, sn))) {
break;
}
}
if (m < params.dp_count) {
return reconnect(pool, val, m);
}
return -1;
}
static inline void free_first_req(struct eval *client)
{
client->type = EV_TUNNEL;
client->pair->type = EV_TUNNEL;
free(client->buff.data);
memset(&client->buff, 0, sizeof(client->buff));
}
static int setup_conn(struct eval *client, const char *buffer, ssize_t n)
{
int m = client->attempt;
if (!m) for (; m < params.dp_count; m++) {
struct desync_params *dp = ¶ms.dp[m];
if (!dp->detect
&& check_l34(dp, SOCK_STREAM, &client->pair->addr)
&& check_proto_tcp(dp->proto, buffer, n)
&& (!dp->hosts || check_host(dp->hosts, buffer, n))) {
break;
}
}
if (m >= params.dp_count) {
LOG(LOG_E, "drop connection (m=%d)\n", m);
return -1;
}
if (params.auto_level > AUTO_NOBUFF && params.dp_count > 1) {
client->mark = is_tls_chello(buffer, n);
}
client->attempt = m;
if (params.timeout
&& set_timeout(client->pair->fd, params.timeout)) {
return -1;
}
return 0;
}
static int cancel_setup(struct eval *remote)
{
if (params.timeout && params.auto_level <= AUTO_NOSAVE &&
set_timeout(remote->fd, 0)) {
return -1;
}
if (post_desync(remote->fd, remote->pair->attempt)) {
return -1;
}
return 0;
}
static int send_saved_req(struct poolhd *pool,
struct eval *client, char *buffer, ssize_t bfsize)
{
ssize_t offset = client->buff.offset;
ssize_t n = client->buff.size - offset;
assert(bfsize >= n);
memcpy(buffer, client->buff.data + offset, n);
ssize_t sn = tcp_send_hook(client->pair, buffer, bfsize, n);
if (sn < 0) {
return -1;
}
client->buff.offset += sn;
if (sn < n) {
if (mod_etype(pool, client->pair, POLLOUT) ||
mod_etype(pool, client, 0)) {
uniperror("mod_etype");
return -1;
}
}
return 0;
}
int on_first_tunnel(struct poolhd *pool,
struct eval *val, char *buffer, size_t bfsize, int etype)
{
if ((etype & POLLOUT) && val->flag == FLAG_CONN) {
if (mod_etype(pool, val, POLLIN) ||
mod_etype(pool, val->pair, POLLIN)) {
uniperror("mod_etype");
return -1;
}
val->pair->type = EV_FIRST_TUNNEL;
return send_saved_req(pool, val->pair, buffer, bfsize);
}
ssize_t n = tcp_recv_hook(pool, val, buffer, bfsize);
if (n < 1) {
return n;
}
if (val->flag != FLAG_CONN) {
val->buff.size += n;
if (val->buff.size >= bfsize) {
free_first_req(val);
}
else {
val->buff.data = realloc(val->buff.data, val->buff.size);
if (val->buff.data == 0) {
uniperror("realloc");
return -1;
}
memcpy(val->buff.data + val->buff.size - n, buffer, n);
return send_saved_req(pool, val, buffer, bfsize);
}
}
else {
if (on_response(pool, val, buffer, n) == 0) {
return 0;
}
free_first_req(val->pair);
int m = val->pair->attempt;
if (val->pair->cache && cache_add(&val->addr, m) < 0) {
return -1;
}
}
if (tcp_send_hook(val->pair, buffer, bfsize, n) < n) {
return -1;
}
return 0;
}
ssize_t tcp_send_hook(struct eval *remote,
char *buffer, size_t bfsize, ssize_t n)
{
ssize_t sn = -1;
int skip = remote->flag != FLAG_CONN;
if (!skip) {
struct eval *client = remote->pair;
if (client->recv_count == n
&& setup_conn(client, buffer, n) < 0) {
return -1;
}
int m = client->attempt, r = client->round_count;
if (!check_round(params.dp[m].rounds, r)) {
skip = 1;
}
else {
LOG(LOG_S, "desync TCP: group=%d, round=%d, fd=%d\n", m, r, remote->fd);
ssize_t offset = remote->pair->round_sent;
if (!offset && remote->round_count) offset = -1;
sn = desync(remote->fd,
buffer, bfsize, n, offset, m);
}
}
if (skip) {
sn = send(remote->fd, buffer, n, 0);
if (sn < 0 && get_e() == EAGAIN) {
return 0;
}
}
remote->pair->round_sent += sn;
return sn;
}
ssize_t tcp_recv_hook(struct poolhd *pool,
struct eval *val, char *buffer, size_t bfsize)
{
ssize_t n = recv(val->fd, buffer, bfsize, 0);
if (n < 1) {
if (!n) {
if (val->flag != FLAG_CONN) {
val = val->pair;
}
return on_fin(pool, val);
}
if (get_e() == EAGAIN) {
return 0;
}
uniperror("recv");
switch (get_e()) {
case ECONNRESET:
case ECONNREFUSED:
case ETIMEDOUT:
if (val->flag == FLAG_CONN)
return on_torst(pool, val);
else
return on_fin(pool, val->pair);
}
return -1;
}
val->recv_count += n;
if (val->round_sent == 0) {
val->round_count++;
val->pair->round_sent = 0;
}
if (val->flag == FLAG_CONN && !val->round_sent) {
int *nr = params.dp[val->pair->attempt].rounds;
if (check_round(nr, val->round_count)
&& !check_round(nr, val->round_count + 1)
&& cancel_setup(val)) {
return -1;
}
}
return n;
}
ssize_t udp_hook(struct eval *val,
char *buffer, ssize_t n, const union sockaddr_u *dst)
{
struct eval *pair = val->pair->pair;
int m = pair->attempt, r = pair->round_count;
if (!m) {
for (; m < params.dp_count; m++) {
struct desync_params *dp = ¶ms.dp[m];
if (!dp->detect
&& check_l34(dp, SOCK_DGRAM, dst)) {
break;
}
}
if (m >= params.dp_count) {
return -1;
}
pair->attempt = m;
}
if (!check_round(params.dp[m].rounds, r)) {
return send(val->fd, buffer, n, 0);
}
LOG(LOG_S, "desync UDP: group=%d, round=%d, fd=%d\n", m, r, val->fd);
return desync_udp(val->fd, buffer, n, &dst->sa, m);
}
#ifdef __linux__
static int protect(int conn_fd, const char *path)
{
struct sockaddr_un sa;
sa.sun_family = AF_UNIX;
strcpy(sa.sun_path, path);
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
uniperror("socket");
return -1;
}
struct timeval tv = { .tv_sec = 1 };
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
int err = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
if (err) {
uniperror("connect");
close(fd);
return -1;
}
char buf[CMSG_SPACE(sizeof(fd))] = { 0 };
struct iovec io = { .iov_base = "1", .iov_len = 1 };
struct msghdr msg = { .msg_iov = &io };
msg.msg_iovlen = 1;
msg.msg_control = buf;
msg.msg_controllen = sizeof(buf);
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(conn_fd));
*((int *)CMSG_DATA(cmsg)) = conn_fd;
msg.msg_controllen = CMSG_SPACE(sizeof(conn_fd));
if (sendmsg(fd, &msg, 0) < 0) {
uniperror("sendmsg");
close(fd);
return -1;
}
if (recv(fd, buf, 1, 0) < 1) {
uniperror("recv");
close(fd);
return -1;
}
close(fd);
return 0;
}
#endif