forked from semigodking/redsocks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoproxy.c
585 lines (506 loc) · 17.5 KB
/
autoproxy.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
/* Copyright (C) 2013 Zhuofei Wang <[email protected]>
*
*
* redsocks - transparent TCP-to-proxy redirector
* Copyright (C) 2007-2011 Leonid Evdokimov <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "utils.h"
#include "log.h"
#include "redsocks.h"
typedef enum autoproxy_state_t {
/* Introduce subsystem */
AUTOPROXY_NEW=10000,
AUTOPROXY_CONNECTED,
AUTOPROXY_CONFIRMED,
} autoproxy_state;
typedef struct autoproxy_client_t {
autoproxy_state state;
time_t time_connect_relay; // timestamp when start to connect relay
size_t data_recv;
size_t data_sent;
} autoproxy_client;
void redsocks_shutdown(redsocks_client *client, struct bufferevent *buffev, int how);
static int auto_retry_or_drop(redsocks_client * client);
static void auto_connect_relay(redsocks_client *client);
static void direct_relay_clientreadcb(struct bufferevent *from, void *_client);
#define CIRCUIT_RESET_SECONDS 1
#define DEFAULT_CONNECT_TIMEOUT_SECONDS 10
#define QUICK_CONNECT_TIMEOUT_SECONDS 2
#define CACHE_ITEM_STALE_SECONDS 60*15
#define ADDR_CACHE_BLOCKS 128
#define ADDR_CACHE_BLOCK_SIZE 16
#define block_from_sockaddr_in(addr) (addr->sin_addr.s_addr & 0xFF) / (256/ADDR_CACHE_BLOCKS)
typedef struct cache_data_t {
struct sockaddr_in addr;
time_t access_time;
} cache_data;
static cache_data addr_cache[ADDR_CACHE_BLOCKS][ADDR_CACHE_BLOCK_SIZE];
static int addr_cache_counters[ADDR_CACHE_BLOCKS];
static int addr_cache_pointers[ADDR_CACHE_BLOCKS];
static int cache_init = 0;
static void init_addr_cache()
{
if (!cache_init)
{
memset((void *)addr_cache, 0, sizeof(addr_cache));
memset((void *)addr_cache_counters, 0, sizeof(addr_cache_counters));
memset((void *)addr_cache_pointers, 0, sizeof(addr_cache_pointers));
cache_init = 1;
}
}
static int is_addr_in_cache(const struct sockaddr_in * addr)
{
/* get block index */
int block = block_from_sockaddr_in(addr);
int count = addr_cache_counters[block];
int first = addr_cache_pointers[block];
int i = 0;
/* do reverse search for efficency */
for ( i = count - 1; i >= 0; i -- )
if (addr->sin_addr.s_addr == addr_cache[block][(first+i)%ADDR_CACHE_BLOCK_SIZE].addr.sin_addr.s_addr
&& addr->sin_family == addr_cache[block][(first+i)%ADDR_CACHE_BLOCK_SIZE].addr.sin_family
)
return 1;
return 0;
}
static time_t * get_addr_time_in_cache(const struct sockaddr_in * addr)
{
/* get block index */
int block = block_from_sockaddr_in(addr);
int count = addr_cache_counters[block];
int first = addr_cache_pointers[block];
int i = 0;
/* do reverse search for efficency */
for ( i = count - 1; i >= 0; i -- )
if (addr->sin_addr.s_addr == addr_cache[block][(first+i)%ADDR_CACHE_BLOCK_SIZE].addr.sin_addr.s_addr
&& addr->sin_family == addr_cache[block][(first+i)%ADDR_CACHE_BLOCK_SIZE].addr.sin_family
)
return &addr_cache[block][(first+i)%ADDR_CACHE_BLOCK_SIZE].access_time;
return NULL;
}
static void add_addr_to_cache(const struct sockaddr_in * addr)
{
int block = block_from_sockaddr_in(addr);
int count = addr_cache_counters[block];
/* use 'first' to index item in cache block when count is equal or greater than block size */
int first = addr_cache_pointers[block];
if (count < ADDR_CACHE_BLOCK_SIZE)
{
memcpy((void *)&addr_cache[block][count].addr, (void *) addr, sizeof(struct sockaddr_in));
addr_cache[block][count].access_time = redsocks_time(NULL);
addr_cache_counters[block]++;
}
else
{
memcpy((void *)&addr_cache[block][first].addr, (void *) addr, sizeof(struct sockaddr_in));
addr_cache[block][first].access_time = redsocks_time(NULL);
addr_cache_pointers[block]++;
addr_cache_pointers[block]%=ADDR_CACHE_BLOCK_SIZE;
}
}
static void del_addr_from_cache(const struct sockaddr_in * addr)
{ /* get block index */
int block = block_from_sockaddr_in(addr);
int count = addr_cache_counters[block];
int first = addr_cache_pointers[block];
int i = 0;
/* do reverse search for efficency */
for ( i = count - 1; i >= 0; i -- )
if (addr->sin_addr.s_addr == addr_cache[block][(first+i)%ADDR_CACHE_BLOCK_SIZE].addr.sin_addr.s_addr
&& addr->sin_family == addr_cache[block][(first+i)%ADDR_CACHE_BLOCK_SIZE].addr.sin_family
)
/* found. zero this item */
{
memset((void *)&addr_cache[block][(first+i)%ADDR_CACHE_BLOCK_SIZE], 0, sizeof(cache_data));
break;
}
}
void auto_client_init(redsocks_client *client)
{
autoproxy_client * aclient = (void*)(client + 1) + client->instance->relay_ss->payload_len;
aclient->state = AUTOPROXY_NEW;
aclient->data_recv = 0;
aclient->data_sent = 0;
init_addr_cache();
}
static void on_connection_confirmed(redsocks_client *client)
{
redsocks_log_error(client, LOG_DEBUG, "IP Confirmed");
}
static void on_connection_blocked(redsocks_client *client)
{
redsocks_log_error(client, LOG_DEBUG, "IP Blocked");
}
static void direct_relay_readcb_helper(redsocks_client *client, struct bufferevent *from, struct bufferevent *to)
{
if (EVBUFFER_LENGTH(to->output) < to->wm_write.high) {
if (bufferevent_write_buffer(to, from->input) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_write_buffer");
if (bufferevent_enable(from, EV_READ) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_enable");
}
else {
if (bufferevent_disable(from, EV_READ) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_disable");
}
}
static void direct_relay_clientreadcb(struct bufferevent *from, void *_client)
{
redsocks_client *client = _client;
autoproxy_client * aclient = (void*)(client + 1) + client->instance->relay_ss->payload_len;
redsocks_touch_client(client);
if (aclient->state == AUTOPROXY_CONNECTED)
{
if (aclient->data_sent && aclient->data_recv)
{
/* No CONNECTION RESET error occur after sending data, good. */
aclient->state = AUTOPROXY_CONFIRMED;
on_connection_confirmed(client);
if (evbuffer_get_length(from->input))
{
evbuffer_drain(from->input, aclient->data_sent);
aclient->data_sent = 0;
}
}
}
direct_relay_readcb_helper(client, client->client, client->relay);
}
static void direct_relay_relayreadcb(struct bufferevent *from, void *_client)
{
redsocks_client *client = _client;
autoproxy_client * aclient = (void*)(client + 1) + client->instance->relay_ss->payload_len;
redsocks_touch_client(client);
if (!aclient->data_recv)
aclient->data_recv = EVBUFFER_LENGTH(from->input);
direct_relay_readcb_helper(client, client->relay, client->client);
}
static void direct_relay_clientwritecb(struct bufferevent *to, void *_client)
{
redsocks_client *client = _client;
autoproxy_client * aclient = (void*)(client + 1) + client->instance->relay_ss->payload_len;
struct bufferevent * from = client->relay;
redsocks_touch_client(client);
if (EVBUFFER_LENGTH(from->input) == 0 && (client->relay_evshut & EV_READ)) {
redsocks_shutdown(client, to, SHUT_WR);
return;
}
if (aclient->state == AUTOPROXY_CONNECTED)
{
if (!aclient->data_recv)
aclient->data_recv = EVBUFFER_LENGTH(from->input);
}
if (EVBUFFER_LENGTH(to->output) < to->wm_write.high) {
if (bufferevent_write_buffer(to, from->input) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_write_buffer");
if (bufferevent_enable(from, EV_READ) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_enable");
}
}
static void direct_relay_relaywritecb(struct bufferevent *to, void *_client)
{
redsocks_client *client = _client;
autoproxy_client * aclient = (void*)(client + 1) + client->instance->relay_ss->payload_len;
struct bufferevent * from = client->client;
redsocks_touch_client(client);
if (EVBUFFER_LENGTH(from->input) == 0 && (client->client_evshut & EV_READ)) {
redsocks_shutdown(client, to, SHUT_WR);
return;
}
else if (aclient->state == AUTOPROXY_CONNECTED )
{
/* Not send or receive data. */
if (!aclient->data_sent && !aclient->data_recv)
{
/* Ensure we have data to send */
if (EVBUFFER_LENGTH(from->input))
{
/* copy data from input to output of relay */
aclient->data_sent = copy_evbuffer (to, from, 0);
redsocks_log_error(client, LOG_DEBUG, "not sent, not got %d", EVBUFFER_LENGTH(from->input));
}
}
/*
* Relay reaceived data before writing to relay.
*/
else if (!aclient->data_sent && aclient->data_recv)
{
redsocks_log_error(client, LOG_DEBUG, "not sent, got");
aclient->data_sent = copy_evbuffer(to, from, 0);
}
/* aclient->state = AUTOPROXY_CONFIRMED; */
/* We have writen data to relay, but got nothing until we are requested to
* write to it again.
*/
else if (aclient->data_sent && !aclient->data_recv)
{
/* No response from relay and no CONNECTION RESET,
Send more data.
*/
redsocks_log_error(client, LOG_DEBUG, "sent, not got in:%d out:%d high:%d sent:%d",
evbuffer_get_length(from->input),
evbuffer_get_length(to->output),
to->wm_write.high, aclient->data_sent );
/* Write more data util input buffer is full */
if (EVBUFFER_LENGTH(from->input)- aclient->data_sent > 0) /* we have more data waiting to be sent */
{
aclient->data_sent += copy_evbuffer(to, from, aclient->data_sent);
}
else if (EVBUFFER_LENGTH(to->output) < aclient->data_sent /* data is sent out, more or less */
&& EVBUFFER_LENGTH(from->input) == from->wm_read.high /* do not confirm unless read buffer is full */
&& EVBUFFER_LENGTH(from->input) == aclient->data_sent /* all data in read buffer is sent */
)
{
evbuffer_drain(from->input, aclient->data_sent);
aclient->data_sent = 0;
aclient->state = AUTOPROXY_CONFIRMED;
on_connection_confirmed(client);
}
}
/* We sent data to and got data from relay. */
else if (aclient->data_sent && aclient->data_recv)
{
/* No CONNECTION RESET error occur after sending data, good. */
aclient->state = AUTOPROXY_CONFIRMED;
on_connection_confirmed(client);
redsocks_log_error(client, LOG_DEBUG, "sent, got %d ", aclient->data_recv);
if (evbuffer_get_length(from->input))
{
evbuffer_drain(from->input, aclient->data_sent);
aclient->data_sent = 0;
}
}
}
if (aclient->state == AUTOPROXY_CONFIRMED)
{
if (EVBUFFER_LENGTH(to->output) < to->wm_write.high) {
if (bufferevent_write_buffer(to, from->input) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_write_buffer");
if (bufferevent_enable(from, EV_READ) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_enable");
}
}
}
static void auto_drop_relay(redsocks_client *client)
{
if (client->relay) {
redsocks_log_error(client, LOG_DEBUG, "dropping relay only");
redsocks_close(EVENT_FD(&client->relay->ev_write));
bufferevent_free(client->relay);
client->relay = NULL;
}
}
static void auto_retry(redsocks_client * client, int updcache)
{
autoproxy_client * aclient = (void*)(client + 1) + client->instance->relay_ss->payload_len;
if (aclient->state == AUTOPROXY_CONNECTED)
bufferevent_disable(client->client, EV_READ| EV_WRITE);
/* drop relay and update state, then retry with specified relay */
if (updcache)
{
/* only add IP to cache when the IP is not in cache */
if (get_addr_time_in_cache(&client->destaddr) == NULL)
{
add_addr_to_cache(&client->destaddr);
redsocks_log_error(client, LOG_DEBUG, "ADD IP to cache: %s",
inet_ntoa(client->destaddr.sin_addr));
}
}
auto_drop_relay(client);
// restore callbacks for ordinary client.
bufferevent_setcb(client->client, NULL, NULL, client->client->errorcb, client);
// enable reading to handle EOF from client
bufferevent_enable(client->client, EV_READ);
/* connect to relay */
if (client->instance->relay_ss->connect_relay)
client->instance->relay_ss->connect_relay(client);
else
redsocks_connect_relay(client);
//
if (EVBUFFER_LENGTH(client->client->input) && client->client->readcb)
client->client->readcb(client->client, client);
}
/* return 1 for drop, 0 for retry. */
static int auto_retry_or_drop(redsocks_client * client)
{
time_t now = redsocks_time(NULL);
autoproxy_client * aclient = (void*)(client + 1) + client->instance->relay_ss->payload_len;
if (aclient->state == AUTOPROXY_NEW)
{
if (now - aclient->time_connect_relay <= CIRCUIT_RESET_SECONDS)
{
on_connection_blocked(client);
auto_retry(client, 0);
return 0;
}
}
else if ( aclient->state == AUTOPROXY_CONNECTED)
{
// if (now - aclient->time_connect_relay <= CIRCUIT_RESET_SECONDS)
{
on_connection_blocked(client);
auto_retry(client, 0);
return 0;
}
}
/* drop */
return 1;
}
static void auto_relay_connected(struct bufferevent *buffev, void *_arg)
{
redsocks_client *client = _arg;
autoproxy_client * aclient = (void*)(client + 1) + client->instance->relay_ss->payload_len;
assert(buffev == client->relay);
redsocks_touch_client(client);
if (!red_is_socket_connected_ok(buffev)) {
if (aclient->state == AUTOPROXY_NEW && !auto_retry_or_drop(client))
return;
redsocks_log_error(client, LOG_DEBUG, "failed to connect to proxy");
goto fail;
}
/* update client state */
aclient->state = AUTOPROXY_CONNECTED;
/* We do not need to detect timeouts any more.
The two peers will handle it. */
bufferevent_set_timeouts(client->relay, NULL, NULL);
if (!redsocks_start_relay(client))
{
/* overwrite theread callback to my function */
client->client->readcb = direct_relay_clientreadcb;
client->client->writecb = direct_relay_clientwritecb;
client->relay->readcb = direct_relay_relayreadcb;
client->relay->writecb = direct_relay_relaywritecb;
}
else
{
redsocks_log_error(client, LOG_DEBUG, "failed to start relay");
goto fail;
}
client->relay->writecb(buffev, _arg);
return;
fail:
redsocks_drop_client(client);
}
static void auto_event_error(struct bufferevent *buffev, short what, void *_arg)
{
redsocks_client *client = _arg;
autoproxy_client * aclient = (void*)(client + 1) + client->instance->relay_ss->payload_len;
int saved_errno = errno;
assert(buffev == client->relay || buffev == client->client);
redsocks_touch_client(client);
redsocks_log_errno(client, LOG_DEBUG, "%s errno(%d), State: %d, what: " event_fmt_str,
buffev == client->client?"client":"relay",
saved_errno, aclient->state, event_fmt(what));
if (buffev == client->relay)
{
if ( aclient->state == AUTOPROXY_NEW
&& what == (EVBUFFER_WRITE|EVBUFFER_TIMEOUT))
{
on_connection_blocked(client);
/* In case timeout occurs while connecting relay, we try to connect
to target via SOCKS5 proxy. It is possible that the connection to
target can be set up a bit longer than the timeout value we set.
However, it is still better to make connection via proxy. */
auto_retry(client, 1);
return;
}
if (aclient->state == AUTOPROXY_NEW && saved_errno == ECONNRESET)
if (!auto_retry_or_drop(client))
return;
if (aclient->state == AUTOPROXY_CONNECTED && what == (EVBUFFER_READ|EVBUFFER_ERROR)
&& saved_errno == ECONNRESET )
{
if (!auto_retry_or_drop(client))
return;
}
}
if (what == (EVBUFFER_READ|EVBUFFER_EOF)) {
struct bufferevent *antiev;
if (buffev == client->relay)
antiev = client->client;
else
antiev = client->relay;
redsocks_shutdown(client, buffev, SHUT_RD);
if (antiev != NULL && EVBUFFER_LENGTH(antiev->output) == 0)
redsocks_shutdown(client, antiev, SHUT_WR);
}
else {
redsocks_drop_client(client);
}
}
static void auto_connect_relay(redsocks_client *client)
{
autoproxy_client * aclient = (void*)(client + 1) + client->instance->relay_ss->payload_len;
struct timeval tv;
tv.tv_sec = client->instance->config.timeout;
tv.tv_usec = 0;
time_t * acc_time = NULL;
time_t now = redsocks_time(NULL);
/* use default timeout if timeout is not configured */
if (tv.tv_sec == 0)
tv.tv_sec = DEFAULT_CONNECT_TIMEOUT_SECONDS;
if (aclient->state == AUTOPROXY_NEW)
{
acc_time = get_addr_time_in_cache(&client->destaddr);
if (acc_time)
{
if (now - *acc_time < CACHE_ITEM_STALE_SECONDS )
{
redsocks_log_error(client, LOG_DEBUG, "Found dest IP in cache");
auto_retry(client, 0);
return ;
}
else
{
/* stale this address in cache */
del_addr_from_cache(&client->destaddr);
/* update timeout value for quick detection */
tv.tv_sec = QUICK_CONNECT_TIMEOUT_SECONDS;
}
}
/* connect to target directly without going through proxy */
client->relay = red_connect_relay2(&client->destaddr,
auto_relay_connected, auto_event_error, client,
&tv);
aclient->time_connect_relay = redsocks_time(NULL);
if (!client->relay) {
redsocks_log_errno(client, LOG_ERR, "auto_connect_relay");
redsocks_drop_client(client);
}
}
else
{
redsocks_log_errno(client, LOG_ERR, "invalid state: %d", aclient->state);
}
}
relay_subsys autoproxy_subsys =
{
.name = "autoproxy",
.payload_len = sizeof(autoproxy_client),
.instance_payload_len = 0,
/*
.readcb = auto_read_cb,
.writecb = auto_write_cb,
*/
.init = auto_client_init,
.connect_relay = auto_connect_relay,
};
/* vim:set tabstop=4 softtabstop=4 shiftwidth=4: */
/* vim:set foldmethod=marker foldlevel=32 foldmarker={,}: */