forked from semigodking/redsocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redsocks.c
1131 lines (961 loc) · 36.6 KB
/
redsocks.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* redsocks2 - transparent TCP-to-proxy redirector
* Copyright (C) 2013-2015 Zhuofei Wang <[email protected]>
*
* This code is based on redsocks project developed by Leonid Evdokimov.
* Licensed under the Apache License, Version 2.0 (the "License").
*
*
* 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 <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <assert.h>
#include <event.h>
#include "list.h"
#include "parser.h"
#include "log.h"
#include "main.h"
#include "base.h"
#include "redsocks.h"
#include "utils.h"
#define REDSOCKS_RELAY_HALFBUFF 1024*32
#define REDSOCKS_AUDIT_INTERVAL 60*2
static void redsocks_relay_relayreadcb(struct bufferevent *from, void *_client);
static void redsocks_relay_relaywritecb(struct bufferevent *from, void *_client);
void redsocks_event_error(struct bufferevent *buffev, short what, void *_arg);
extern relay_subsys direct_connect_subsys;
extern relay_subsys http_connect_subsys;
extern relay_subsys http_relay_subsys;
extern relay_subsys socks4_subsys;
extern relay_subsys socks5_subsys;
extern relay_subsys shadowsocks_subsys;
static relay_subsys *relay_subsystems[] =
{
&direct_connect_subsys,
&http_connect_subsys,
&http_relay_subsys,
&socks4_subsys,
&socks5_subsys,
&shadowsocks_subsys,
};
extern relay_subsys autoproxy_subsys;
static list_head instances = LIST_HEAD_INIT(instances);
static parser_entry redsocks_entries[] =
{
{ .key = "local_ip", .type = pt_in_addr },
{ .key = "local_port", .type = pt_uint16 },
{ .key = "interface", .type = pt_pchar },
{ .key = "ip", .type = pt_in_addr },
{ .key = "port", .type = pt_uint16 },
{ .key = "type", .type = pt_pchar },
{ .key = "login", .type = pt_pchar },
{ .key = "password", .type = pt_pchar },
{ .key = "listenq", .type = pt_uint16 },
{ .key = "min_accept_backoff", .type = pt_uint16 },
{ .key = "max_accept_backoff", .type = pt_uint16 },
{ .key = "autoproxy", .type = pt_uint16 },
{ .key = "timeout", .type = pt_uint16 },
{ }
};
/* There is no way to get `EVLIST_INSERTED` event flag outside of libevent, so
* here are tracking functions. */
static void tracked_event_set(
struct tracked_event *tev, evutil_socket_t fd, short events,
void (*callback)(evutil_socket_t, short, void *), void *arg)
{
event_assign(&tev->ev, get_event_base(), fd, events, callback, arg);
timerclear(&tev->inserted);
}
static int tracked_event_add(struct tracked_event *tev, const struct timeval *tv)
{
int ret = event_add(&tev->ev, tv);
if (ret == 0)
gettimeofday(&tev->inserted, NULL);
return ret;
}
static int tracked_event_del(struct tracked_event *tev)
{
int ret = event_del(&tev->ev);
if (ret == 0)
timerclear(&tev->inserted);
return ret;
}
static int redsocks_onenter(parser_section *section)
{
// FIXME: find proper way to calulate instance_payload_len
int instance_payload_len = 0;
relay_subsys **ss;
FOREACH(ss, relay_subsystems)
if (instance_payload_len < (*ss)->instance_payload_len)
instance_payload_len = (*ss)->instance_payload_len;
redsocks_instance *instance = calloc(1, sizeof(*instance) + instance_payload_len);
if (!instance) {
parser_error(section->context, "Not enough memory");
return -1;
}
INIT_LIST_HEAD(&instance->list);
INIT_LIST_HEAD(&instance->clients);
instance->config.bindaddr.sin_family = AF_INET;
instance->config.bindaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
instance->config.relayaddr.sin_family = AF_INET;
instance->config.relayaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
/* Default value can be checked in run-time, but I doubt anyone needs that.
* Linux: sysctl net.core.somaxconn
* FreeBSD: sysctl kern.ipc.somaxconn */
instance->config.listenq = SOMAXCONN;
instance->config.min_backoff_ms = 100;
instance->config.max_backoff_ms = 60000;
instance->config.autoproxy = 0;
instance->config.timeout = 0;
for (parser_entry *entry = §ion->entries[0]; entry->key; entry++)
entry->addr =
(strcmp(entry->key, "local_ip") == 0) ? (void*)&instance->config.bindaddr.sin_addr :
(strcmp(entry->key, "local_port") == 0) ? (void*)&instance->config.bindaddr.sin_port :
(strcmp(entry->key, "interface") == 0) ? (void*)&instance->config.interface :
(strcmp(entry->key, "ip") == 0) ? (void*)&instance->config.relayaddr.sin_addr :
(strcmp(entry->key, "port") == 0) ? (void*)&instance->config.relayaddr.sin_port :
(strcmp(entry->key, "type") == 0) ? (void*)&instance->config.type :
(strcmp(entry->key, "login") == 0) ? (void*)&instance->config.login :
(strcmp(entry->key, "password") == 0) ? (void*)&instance->config.password :
(strcmp(entry->key, "listenq") == 0) ? (void*)&instance->config.listenq :
(strcmp(entry->key, "min_accept_backoff") == 0) ? (void*)&instance->config.min_backoff_ms :
(strcmp(entry->key, "max_accept_backoff") == 0) ? (void*)&instance->config.max_backoff_ms :
(strcmp(entry->key, "autoproxy") == 0) ? (void*)&instance->config.autoproxy :
(strcmp(entry->key, "timeout") == 0) ? (void*)&instance->config.timeout:
NULL;
section->data = instance;
return 0;
}
static int redsocks_onexit(parser_section *section)
{
/* FIXME: Rewrite in bullet-proof style. There are memory leaks if config
* file is not correct, so correct on-the-fly config reloading is
* currently impossible.
*/
const char *err = NULL;
redsocks_instance *instance = section->data;
section->data = NULL;
for (parser_entry *entry = §ion->entries[0]; entry->key; entry++)
entry->addr = NULL;
instance->config.bindaddr.sin_port = htons(instance->config.bindaddr.sin_port);
instance->config.relayaddr.sin_port = htons(instance->config.relayaddr.sin_port);
if (instance->config.type) {
relay_subsys **ss;
FOREACH(ss, relay_subsystems) {
if (!strcmp((*ss)->name, instance->config.type)) {
instance->relay_ss = *ss;
list_add(&instance->list, &instances);
break;
}
}
if (!instance->relay_ss)
err = "invalid `type` for redsocks";
}
else {
err = "no `type` for redsocks";
}
if (!err && !instance->config.min_backoff_ms) {
err = "`min_accept_backoff` must be positive, 0 ms is too low";
}
if (!err && !instance->config.max_backoff_ms) {
err = "`max_accept_backoff` must be positive, 0 ms is too low";
}
if (!err && !(instance->config.min_backoff_ms < instance->config.max_backoff_ms)) {
err = "`min_accept_backoff` must be less than `max_accept_backoff`";
}
if (err)
parser_error(section->context, err);
return err ? -1 : 0;
}
static parser_section redsocks_conf_section =
{
.name = "redsocks",
.entries = redsocks_entries,
.onenter = redsocks_onenter,
.onexit = redsocks_onexit
};
void redsocks_log_write_plain(
const char *file, int line, const char *func, int do_errno,
const struct sockaddr_in *clientaddr, const struct sockaddr_in *destaddr,
int priority, const char *orig_fmt, ...
) {
int saved_errno = errno;
struct evbuffer *fmt = evbuffer_new();
va_list ap;
char clientaddr_str[RED_INET_ADDRSTRLEN], destaddr_str[RED_INET_ADDRSTRLEN];
if (!fmt) {
log_errno(LOG_ERR, "evbuffer_new()");
// no return, as I have to call va_start/va_end
}
if (fmt) {
evbuffer_add_printf(fmt, "[%s->%s]: %s",
red_inet_ntop(clientaddr, clientaddr_str, sizeof(clientaddr_str)),
red_inet_ntop(destaddr, destaddr_str, sizeof(destaddr_str)),
orig_fmt);
}
va_start(ap, orig_fmt);
if (fmt) {
errno = saved_errno;
_log_vwrite(file, line, func, do_errno, priority, (const char*)EVBUFFER_DATA(fmt), ap);
evbuffer_free(fmt);
}
va_end(ap);
}
void redsocks_touch_client(redsocks_client *client)
{
redsocks_time(&client->last_event);
}
static void redsocks_relay_readcb(redsocks_client *client, struct bufferevent *from, struct bufferevent *to)
{
redsocks_log_error(client, LOG_DEBUG, "RCB %s, in: %u", from == client->client?"client":"relay",
evbuffer_get_length(bufferevent_get_input(from)));
if (evbuffer_get_length(to->output) < to->wm_write.high) {
if (bufferevent_write_buffer(to, bufferevent_get_input(from)) == -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");
}
}
int process_shutdown_on_write_(redsocks_client *client, struct bufferevent *from, struct bufferevent *to)
{
assert(from == client->client || from == client->relay);
unsigned short from_evshut = from == client->client ? client->client_evshut : client->relay_evshut;
unsigned short to_evshut = to == client->client ? client->client_evshut : client->relay_evshut;
redsocks_log_error(client, LOG_DEBUG, "WCB %s, fs: %u, ts: %u, fin: %u, fout: %u, tin: %u",
to == client->client?"client":"relay",
from_evshut,
to_evshut,
evbuffer_get_length(bufferevent_get_input(from)),
evbuffer_get_length(bufferevent_get_output(from)),
evbuffer_get_length(bufferevent_get_input(to)));
if (evbuffer_get_length(bufferevent_get_input(from)) == 0
&& (from_evshut & EV_READ)
&& !(to_evshut & EV_WRITE)) {
redsocks_shutdown(client, to, SHUT_WR);
return 1;
}
return 0;
}
static void redsocks_relay_writecb(redsocks_client *client, struct bufferevent *from, struct bufferevent *to)
{
unsigned short from_evshut = from == client->client ? client->client_evshut : client->relay_evshut;
assert(from == client->client || from == client->relay);
if (process_shutdown_on_write_(client, from, to))
return;
if (evbuffer_get_length(to->output) < to->wm_write.high) {
if (bufferevent_write_buffer(to, bufferevent_get_input(from)) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_write_buffer");
if (!(from_evshut & EV_READ) && bufferevent_enable(from, EV_READ) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_enable");
}
}
static void redsocks_relay_relayreadcb(struct bufferevent *from, void *_client)
{
redsocks_client *client = _client;
redsocks_touch_client(client);
redsocks_relay_readcb(client, client->relay, client->client);
}
static void redsocks_relay_relaywritecb(struct bufferevent *to, void *_client)
{
redsocks_client *client = _client;
redsocks_touch_client(client);
redsocks_relay_writecb(client, client->client, client->relay);
}
static void redsocks_relay_clientreadcb(struct bufferevent *from, void *_client)
{
redsocks_client *client = _client;
redsocks_touch_client(client);
redsocks_relay_readcb(client, client->client, client->relay);
}
static void redsocks_relay_clientwritecb(struct bufferevent *to, void *_client)
{
redsocks_client *client = _client;
redsocks_touch_client(client);
redsocks_relay_writecb(client, client->relay, client->client);
}
int redsocks_start_relay(redsocks_client *client)
{
int error;
bufferevent_event_cb event_cb;
bufferevent_setwatermark(client->client, EV_READ|EV_WRITE, 0, REDSOCKS_RELAY_HALFBUFF);
bufferevent_setwatermark(client->relay, EV_READ|EV_WRITE, 0, REDSOCKS_RELAY_HALFBUFF);
#ifdef bufferevent_getcb
bufferevent_getcb(client->client, NULL, NULL, &event_cb, NULL);
#else
event_cb = client->client->errorcb;
#endif
bufferevent_setcb(client->client, redsocks_relay_clientreadcb,
redsocks_relay_clientwritecb,
event_cb,
client);
#ifdef bufferevent_getcb
bufferevent_getcb(client->relay, NULL, NULL, &event_cb, NULL);
#else
event_cb = client->relay->errorcb;
#endif
bufferevent_setcb(client->relay, redsocks_relay_relayreadcb,
redsocks_relay_relaywritecb,
event_cb,
client);
error = bufferevent_enable(client->client,
client->client_evshut == EV_READ ? EV_WRITE :
client->client_evshut == EV_WRITE ? EV_READ :
client->client_evshut == (EV_READ|EV_WRITE) ? 0 : EV_READ | EV_WRITE);
if (!error)
error = bufferevent_enable(client->relay, EV_READ | EV_WRITE);
if (!error) {
redsocks_log_error(client, LOG_DEBUG, "data relaying started");
}
else {
redsocks_log_errno(client, LOG_ERR, "bufferevent_enable");
redsocks_drop_client(client);
}
return error;
}
void redsocks_drop_client(redsocks_client *client)
{
int fd;
redsocks_log_error(client, LOG_DEBUG, "dropping client");
if (client->instance->config.autoproxy && autoproxy_subsys.fini)
autoproxy_subsys.fini(client);
if (client->instance->relay_ss->fini)
client->instance->relay_ss->fini(client);
if (client->client) {
fd = bufferevent_getfd(client->client);
bufferevent_free(client->client);
if (!(client->client_evshut & EV_WRITE))
shutdown(fd, SHUT_WR);
redsocks_close(fd);
}
if (client->relay) {
fd = bufferevent_getfd(client->relay);
bufferevent_free(client->relay);
if (!(client->relay_evshut & EV_WRITE))
shutdown(fd, SHUT_WR);
redsocks_close(fd);
}
list_del(&client->list);
free(client);
}
void redsocks_shutdown(redsocks_client *client, struct bufferevent *buffev, int how)
{
short evhow = 0;
char *strev, *strhow = NULL, *strevhow = NULL;
unsigned short *pevshut;
assert(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR);
assert(buffev == client->client || buffev == client->relay);
if (how == SHUT_RD) {
strhow = "SHUT_RD";
evhow = EV_READ;
strevhow = "EV_READ";
}
else if (how == SHUT_WR) {
strhow = "SHUT_WR";
evhow = EV_WRITE;
strevhow = "EV_WRITE";
}
else if (how == SHUT_RDWR) {
strhow = "SHUT_RDWR";
evhow = EV_READ|EV_WRITE;
strevhow = "EV_READ|EV_WRITE";
}
assert(strhow && strevhow);
strev = buffev == client->client ? "client" : "relay";
pevshut = buffev == client->client ? &client->client_evshut : &client->relay_evshut;
if (bufferevent_disable(buffev, evhow) != 0)
redsocks_log_errno(client, LOG_ERR, "bufferevent_disable(%s, %s)", strev, strevhow);
// if EV_WRITE is already shut and we're going to shutdown read then
// we're either going to abort data flow (bad behaviour) or confirm EOF
// and in this case socket is already SHUT_RD'ed
if ( !(how == SHUT_RD && (*pevshut & EV_WRITE)) )
if (shutdown(bufferevent_getfd(buffev), how) != 0)
redsocks_log_errno(client, LOG_ERR, "shutdown(%s, %s)", strev, strhow);
*pevshut |= evhow;
if (client->relay_evshut == (EV_READ|EV_WRITE) && client->client_evshut == (EV_READ|EV_WRITE)) {
redsocks_log_error(client, LOG_DEBUG, "both client and server disconnected");
redsocks_drop_client(client);
}
}
// I assume that -1 is invalid errno value
static int redsocks_socket_geterrno(redsocks_client *client, struct bufferevent *buffev)
{
int pseudo_errno = red_socket_geterrno(buffev);
if (pseudo_errno == -1) {
redsocks_log_errno(client, LOG_ERR, "red_socket_geterrno");
return -1;
}
return pseudo_errno;
}
void redsocks_event_error(struct bufferevent *buffev, short what, void *_arg)
{
redsocks_client *client = _arg;
assert(buffev == client->relay || buffev == client->client);
redsocks_touch_client(client);
if (!(what & BEV_EVENT_ERROR))
errno = redsocks_socket_geterrno(client, buffev);
redsocks_log_errno(client, LOG_DEBUG, "%s, what: " event_fmt_str,
buffev == client->client?"client":"relay",
event_fmt(what));
if (what == (BEV_EVENT_READING|BEV_EVENT_EOF)) {
redsocks_shutdown(client, buffev, SHUT_RD);
// Ensure the other party could send remaining data and SHUT_WR also
if (buffev == client->client)
{
if (!(client->relay_evshut & EV_WRITE) && client->relay_connected)
bufferevent_enable(client->relay, EV_WRITE);
}
else
{
if (!(client->client_evshut & EV_WRITE))
bufferevent_enable(client->client, EV_WRITE);
}
}
else {
redsocks_drop_client(client);
}
}
int sizes_equal(size_t a, size_t b)
{
return a == b;
}
int sizes_greater_equal(size_t a, size_t b)
{
return a >= b;
}
int redsocks_read_expected(redsocks_client *client, struct evbuffer *input, void *data, size_comparator comparator, size_t expected)
{
size_t len = evbuffer_get_length(input);
if (comparator(len, expected)) {
int read = evbuffer_remove(input, data, expected);
UNUSED(read);
assert(read == expected);
return 0;
}
else {
redsocks_log_error(client, LOG_NOTICE, "Can't get expected amount of data");
redsocks_drop_client(client);
return -1;
}
}
struct evbuffer *mkevbuffer(void *data, size_t len)
{
struct evbuffer *buff = NULL, *retval = NULL;
buff = evbuffer_new();
if (!buff) {
log_errno(LOG_ERR, "evbuffer_new");
goto fail;
}
if (evbuffer_add(buff, data, len) < 0) {
log_errno(LOG_ERR, "evbuffer_add");
goto fail;
}
retval = buff;
buff = NULL;
fail:
if (buff)
evbuffer_free(buff);
return retval;
}
int redsocks_write_helper_ex(
struct bufferevent *buffev, redsocks_client *client,
redsocks_message_maker mkmessage, int state, size_t wm_low, size_t wm_high)
{
assert(client);
return redsocks_write_helper_ex_plain(buffev, client, (redsocks_message_maker_plain)mkmessage,
client, state, wm_low, wm_high);
}
int redsocks_write_helper_ex_plain(
struct bufferevent *buffev, redsocks_client *client,
redsocks_message_maker_plain mkmessage, void *p, int state, size_t wm_low, size_t wm_high)
{
int len;
struct evbuffer *buff = NULL;
int drop = 1;
if (mkmessage) {
buff = mkmessage(p);
if (!buff)
goto fail;
assert(!client || buffev == client->relay);
len = bufferevent_write_buffer(buffev, buff);
if (len < 0) {
if (client)
redsocks_log_errno(client, LOG_ERR, "bufferevent_write_buffer");
else
log_errno(LOG_ERR, "bufferevent_write_buffer");
goto fail;
}
}
if (client)
client->state = state;
buffev->wm_read.low = wm_low;
buffev->wm_read.high = wm_high;
bufferevent_enable(buffev, EV_READ);
drop = 0;
fail:
if (buff)
evbuffer_free(buff);
if (drop && client)
redsocks_drop_client(client);
return drop ? -1 : 0;
}
int redsocks_write_helper(
struct bufferevent *buffev, redsocks_client *client,
redsocks_message_maker mkmessage, int state, size_t wm_only)
{
assert(client);
return redsocks_write_helper_ex(buffev, client, mkmessage, state, wm_only, wm_only);
}
void redsocks_relay_connected(struct bufferevent *buffev, void *_arg)
{
redsocks_client *client = _arg;
assert(buffev == client->relay);
redsocks_touch_client(client);
if (!red_is_socket_connected_ok(buffev)) {
redsocks_log_errno(client, LOG_NOTICE, "red_is_socket_connected_ok");
goto fail;
}
client->relay_connected = 1;
/* We do not need to detect timeouts any more.
The two peers will handle it. */
bufferevent_set_timeouts(client->relay, NULL, NULL);
bufferevent_setcb(client->relay, client->instance->relay_ss->readcb,
client->instance->relay_ss->writecb,
redsocks_event_error,
client);
client->instance->relay_ss->writecb(client->relay, client);
return;
fail:
redsocks_drop_client(client);
}
int redsocks_connect_relay(redsocks_client *client)
{
struct timeval tv;
tv.tv_sec = client->instance->config.timeout;
tv.tv_usec = 0;
if (tv.tv_sec == 0)
tv.tv_sec = DEFAULT_CONNECT_TIMEOUT;
client->relay = red_connect_relay2(&client->instance->config.relayaddr,
NULL,
redsocks_relay_connected,
redsocks_event_error, client, &tv);
if (!client->relay) {
redsocks_log_errno(client, LOG_ERR, "red_connect_relay failed!!!");
redsocks_drop_client(client);
return -1;
}
return 0;
}
static void redsocks_accept_backoff(int fd, short what, void *_arg)
{
redsocks_instance *self = _arg;
/* Isn't it already deleted? EV_PERSIST has nothing common with timeouts in
* old libevent... On the other hand libevent does not return any error. */
if (tracked_event_del(&self->accept_backoff) != 0)
log_errno(LOG_ERR, "event_del");
if (tracked_event_add(&self->listener, NULL) != 0)
log_errno(LOG_ERR, "event_add");
}
void redsocks_close_internal(int fd, const char* file, int line, const char *func)
{
if (close(fd) == 0) {
redsocks_instance *instance = NULL;
struct timeval now;
gettimeofday(&now, NULL);
list_for_each_entry(instance, &instances, list) {
if (timerisset(&instance->accept_backoff.inserted)) {
struct timeval min_accept_backoff = {
instance->config.min_backoff_ms / 1000,
(instance->config.min_backoff_ms % 1000) * 1000};
struct timeval time_passed;
timersub(&now, &instance->accept_backoff.inserted, &time_passed);
if (timercmp(&min_accept_backoff, &time_passed, <)) {
redsocks_accept_backoff(-1, 0, instance);
break;
}
}
}
}
else {
const int do_errno = 1;
_log_write(file, line, func, do_errno, LOG_WARNING, "close");
}
}
static void redsocks_accept_client(int fd, short what, void *_arg)
{
redsocks_instance *self = _arg;
redsocks_client *client = NULL;
struct sockaddr_in clientaddr;
struct sockaddr_in myaddr;
struct sockaddr_in destaddr;
socklen_t addrlen = sizeof(clientaddr);
int on = 1;
int client_fd = -1;
int error;
// working with client_fd
client_fd = accept(fd, (struct sockaddr*)&clientaddr, &addrlen);
if (client_fd == -1) {
/* Different systems use different `errno` value to signal different
* `lack of file descriptors` conditions. Here are most of them. */
if (errno == ENFILE || errno == EMFILE || errno == ENOBUFS || errno == ENOMEM) {
self->accept_backoff_ms = (self->accept_backoff_ms << 1) + 1;
clamp_value(self->accept_backoff_ms, self->config.min_backoff_ms, self->config.max_backoff_ms);
int delay = (random() % self->accept_backoff_ms) + 1;
log_errno(LOG_WARNING, "accept: out of file descriptors, backing off for %u ms", delay);
struct timeval tvdelay = { delay / 1000, (delay % 1000) * 1000 };
if (tracked_event_del(&self->listener) != 0)
log_errno(LOG_ERR, "event_del");
if (tracked_event_add(&self->accept_backoff, &tvdelay) != 0)
log_errno(LOG_ERR, "event_add");
}
else {
log_errno(LOG_WARNING, "accept");
}
goto fail;
}
self->accept_backoff_ms = 0;
// socket is really bound now (it could be bound to 0.0.0.0)
addrlen = sizeof(myaddr);
error = getsockname(client_fd, (struct sockaddr*)&myaddr, &addrlen);
if (error) {
log_errno(LOG_WARNING, "getsockname");
goto fail;
}
error = getdestaddr(client_fd, &clientaddr, &myaddr, &destaddr);
if (error) {
goto fail;
}
error = setsockopt(client_fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
if (error) {
log_errno(LOG_WARNING, "setsockopt");
goto fail;
}
// everything seems to be ok, let's allocate some memory
if (self->config.autoproxy)
client = calloc(1, sizeof(redsocks_client) +
self->relay_ss->payload_len + autoproxy_subsys.payload_len
);
else
client = calloc(1, sizeof(redsocks_client) + self->relay_ss->payload_len);
if (!client) {
log_errno(LOG_ERR, "calloc");
goto fail;
}
client->instance = self;
memcpy(&client->clientaddr, &clientaddr, sizeof(clientaddr));
memcpy(&client->destaddr, &destaddr, sizeof(destaddr));
INIT_LIST_HEAD(&client->list);
self->relay_ss->init(client);
if (self->config.autoproxy)
autoproxy_subsys.init(client);
if (redsocks_time(&client->first_event) == ((time_t)-1))
goto fail;
redsocks_touch_client(client);
client->client = bufferevent_socket_new(get_event_base(), client_fd, 0);
if (!client->client) {
log_errno(LOG_ERR, "bufferevent_socket_new");
goto fail;
}
bufferevent_setcb(client->client, NULL, NULL, redsocks_event_error, client);
client_fd = -1;
// enable reading to handle EOF from client
if (bufferevent_enable(client->client, EV_READ) != 0) {
redsocks_log_errno(client, LOG_ERR, "bufferevent_enable");
goto fail;
}
list_add(&client->list, &self->clients);
redsocks_log_error(client, LOG_DEBUG, "accepted");
if (self->config.autoproxy && autoproxy_subsys.connect_relay)
autoproxy_subsys.connect_relay(client);
else if (self->relay_ss->connect_relay)
self->relay_ss->connect_relay(client);
else
redsocks_connect_relay(client);
return;
fail:
if (client) {
redsocks_drop_client(client);
}
if (client_fd != -1)
redsocks_close(client_fd);
}
static const char *redsocks_evshut_str(unsigned short evshut)
{
return
evshut == EV_READ ? "SHUT_RD" :
evshut == EV_WRITE ? "SHUT_WR" :
evshut == (EV_READ|EV_WRITE) ? "SHUT_RDWR" :
evshut == 0 ? "" :
"???";
}
static const char *redsocks_event_str(unsigned short what)
{
return
what == EV_READ ? "R/-" :
what == EV_WRITE ? "-/W" :
what == (EV_READ|EV_WRITE) ? "R/W" :
what == 0 ? "-/-" :
"???";
}
void redsocks_dump_client(redsocks_client * client, int loglevel)
{
time_t now = redsocks_time(NULL);
const char *s_client_evshut = redsocks_evshut_str(client->client_evshut);
const char *s_relay_evshut = redsocks_evshut_str(client->relay_evshut);
redsocks_log_error(client, loglevel, "client(%i): (%s)%s%s input %u output %u, relay(%i): (%s)%s%s input %u output %u, age: %li sec, idle: %li sec.",
bufferevent_getfd(client->client),
redsocks_event_str(bufferevent_get_enabled(client->client)),
s_client_evshut[0] ? " " : "", s_client_evshut,
evbuffer_get_length(bufferevent_get_input(client->client)),
evbuffer_get_length(bufferevent_get_output(client->client)),
bufferevent_getfd(client->relay),
redsocks_event_str(bufferevent_get_enabled(client->relay)),
s_relay_evshut[0] ? " " : "", s_relay_evshut,
evbuffer_get_length(bufferevent_get_input(client->relay)),
evbuffer_get_length(bufferevent_get_output(client->relay)),
now - client->first_event,
now - client->last_event);
}
static void redsocks_dump_instance(redsocks_instance *instance)
{
redsocks_client *client = NULL;
log_error(LOG_INFO, "Dumping client list for instance %p(%s):", instance,
instance->relay_ss->name);
list_for_each_entry(client, &instance->clients, list)
redsocks_dump_client(client, LOG_INFO);
log_error(LOG_INFO, "End of client list.");
}
static void redsocks_debug_dump()
{
redsocks_instance *instance = NULL;
list_for_each_entry(instance, &instances, list)
redsocks_dump_instance(instance);
}
/* Audit is required to clean up hung connections.
* Not all connections are closed gracefully by both ends. In any case that
* either far end of client or far end of relay does not close connection
* gracefully, we got hung connections.
*/
static void redsocks_audit_instance(redsocks_instance *instance)
{
redsocks_client *tmp, *client = NULL;
time_t now = redsocks_time(NULL);
int drop_it = 0;
log_error(LOG_DEBUG, "Audit client list for instance %p(%s):", instance,
instance->relay_ss->name);
list_for_each_entry_safe(client, tmp, &instance->clients, list) {
drop_it = 0;
if (now - client->last_event >= REDSOCKS_AUDIT_INTERVAL){
/* Only take actions if no touch of the client for at least an audit cycle.*/
/* drop this client if either end disconnected */
if ((client->client_evshut == EV_WRITE && client->relay_evshut == EV_READ)
|| (client->client_evshut == EV_READ && client->relay_evshut == EV_WRITE)
|| (client->client_evshut == (EV_READ|EV_WRITE) && client->relay_evshut == EV_WRITE))
drop_it = 1;
}
/* close long connections without activities */
if (now - client->last_event >= 3600 * 2)
drop_it = 1;
if (drop_it){
redsocks_dump_client(client, LOG_DEBUG);
redsocks_drop_client(client);
}
}
log_error(LOG_DEBUG, "End of auditing client list.");
}
static void redsocks_heartbeat(int sig, short what, void *_arg)
{
time_t now = redsocks_time(NULL);
FILE * tmp = NULL;
char tmp_fname[128];
snprintf(tmp_fname, sizeof(tmp_fname), "/tmp/redtime-%d", getppid());
tmp = fopen(tmp_fname, "w");
if (tmp)
{
fprintf(tmp, "%ld ", (long int)now);
fclose(tmp);
}
}
static void redsocks_audit(int sig, short what, void *_arg)
{
redsocks_instance * tmp, *instance = NULL;
list_for_each_entry_safe(instance, tmp, &instances, list)
redsocks_audit_instance(instance);
}
static void redsocks_fini_instance(redsocks_instance *instance);
static int redsocks_init_instance(redsocks_instance *instance)
{
/* FIXME: redsocks_fini_instance is called in case of failure, this
* function will remove instance from instances list - result
* looks ugly.
*/
int error;
evutil_socket_t fd = -1;
if (instance->relay_ss->instance_init
&& instance->relay_ss->instance_init(instance)) {
log_errno(LOG_ERR, "Failed to init relay subsystem.");
goto fail;
}
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
log_errno(LOG_ERR, "socket");
goto fail;
}
error = evutil_make_listen_socket_reuseable(fd);
if (error) {
log_errno(LOG_ERR, "evutil_make_listen_socket_reuseable");
goto fail;
}
error = bind(fd, (struct sockaddr*)&instance->config.bindaddr, sizeof(instance->config.bindaddr));
if (error) {
log_errno(LOG_ERR, "bind");
goto fail;
}
error = evutil_make_socket_nonblocking(fd);
if (error) {
log_errno(LOG_ERR, "evutil_make_socket_nonblocking");
goto fail;
}
error = listen(fd, instance->config.listenq);
if (error) {
log_errno(LOG_ERR, "listen");
goto fail;
}
tracked_event_set(&instance->listener, fd, EV_READ | EV_PERSIST, redsocks_accept_client, instance);
fd = -1;
tracked_event_set(&instance->accept_backoff, -1, 0, redsocks_accept_backoff, instance);
error = tracked_event_add(&instance->listener, NULL);
if (error) {
log_errno(LOG_ERR, "event_add");
goto fail;
}
return 0;
fail:
redsocks_fini_instance(instance);
if (fd != -1) {
redsocks_close(fd);