-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvsocks.c
858 lines (737 loc) · 19.5 KB
/
svsocks.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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <syslog.h>
#include <pthread.h>
#include <sys/resource.h>
#include <sys/epoll.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#define SYSLOG_IDENT "svsocks"
#define LISTEN_ADDRESS_IPV4 "127.0.0.1"
#define LISTEN_ADDRESS_IPV6 "::ffff:127.0.0.1"
#define LISTEN_PORT 1080
#define NTHREADS 1000
#define DOMAIN_IPV4 4
#define DOMAIN_IPV6 6
#define USERNAME "svsocks"
#define PASSWORD "1234"
#define BUFF_SIZE 4096
#define BACKLOG 10
struct server_t {
int serverfd;
int nthreads;
int domain;
int listen_port;
union {
char ipv6[INET6_ADDRSTRLEN];
char ipv4[INET_ADDRSTRLEN];
} listen_address;
char username[255];
char password[255];
pthread_mutex_t server_mtx;
};
struct server_t server;
uint8_t ipv4_connect(int *hostfd, char *buffer)
{
struct sockaddr_in host;
uint32_t dst_addr;
uint16_t dst_port;
/* | dst.addr (4 octets) | dst.port (2 octets) | */
memcpy(&dst_addr, &buffer[4], 4);
memcpy(&dst_port, &buffer[8], 2);
memset(&host, 0, sizeof(struct sockaddr_in));
host.sin_family = AF_INET;
host.sin_port = dst_port;
host.sin_addr.s_addr = dst_addr;
if ((*hostfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
/* general socks server failure */
return 0x01;
}
if (connect(*hostfd, (struct sockaddr *)&host, sizeof(struct sockaddr_in)) == -1) {
int err = errno;
switch (err) {
case ECONNREFUSED:
/* conncetion refused */
return 0x05;
case ENETUNREACH:
/* network unreachable */
return 0x03;
default:
/* general socks server failure */
return 0x01;
}
}
/* success */
return 0x00;
}
uint8_t domain_connect(int *hostfd, char *buffer)
{
struct addrinfo hint, *result, *ptr;
uint8_t domain_len, domain_name[255], reply;
uint16_t dst_port;
char port_addr[255];
/* | domain name length (1 octet) | domain name (variable) | dst.port (2 octets) | */
memset(domain_name, 0, 255);
domain_len = buffer[4];
memcpy(domain_name, &buffer[5], domain_len);
memcpy(&dst_port, &buffer[5 + domain_len], 2);
snprintf(port_addr, 255, "%d", ntohs(dst_port));
memset(&hint, 0, sizeof(struct addrinfo));
hint.ai_flags = AI_NUMERICSERV;
hint.ai_family = AF_UNSPEC;
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = 0;
if (getaddrinfo((const char *)domain_name, port_addr, &hint, &result) != 0) {
/* general socks server failure */
return 0x01;
}
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
if ((*hostfd = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol)) == -1) {
/* general socks server failure */
reply = 0x01;
continue;
}
if (connect(*hostfd, ptr->ai_addr, ptr->ai_addrlen) == -1) {
int err = errno;
close(*hostfd);
switch (err) {
case ECONNREFUSED:
/* connection refused */
reply = 0x05;
break;
case ENETUNREACH:
/* network unreachable */
reply = 0x03;
break;
default:
/* general socks server failure */
reply = 0x01;
break;
}
continue;
} else {
/* success */
reply = 0x00;
break;
}
}
freeaddrinfo(result);
return reply;
}
uint8_t ipv6_connect(int *hostfd, char *buffer)
{
struct sockaddr_in6 host;
uint8_t dst_addr[16];
uint16_t dst_port;
/* | dst.addr (16 octets) | dst.port (2 octets) | */
memcpy(&dst_addr, &buffer[4], 16);
memcpy(&dst_port, &buffer[20], 2);
memset(&host, 0, sizeof(struct sockaddr_in6));
host.sin6_family = AF_INET6;
host.sin6_port = dst_port;
memcpy(&host.sin6_addr.s6_addr, dst_addr, 16);
if ((*hostfd = socket(AF_INET6, SOCK_STREAM, 0)) == -1) {
/* general socks server failure */
return 0x01;
}
if (connect(*hostfd, (struct sockaddr *)&host, sizeof(struct sockaddr_in6)) == -1) {
int err = errno;
switch (err) {
case ECONNREFUSED:
/* connection refused */
return 0x05;
case ENETUNREACH:
/* network unreachable */
return 0x03;
default:
/* general socks server failure */
return 0x01;
}
}
/* success */
return 0x00;
}
int userpass_auth(int clientfd)
{
int total;
char buffer[BUFF_SIZE];
uint8_t version, ulen, uname[255], plen, passwd[255], status;
total = recv(clientfd, buffer, BUFF_SIZE, 0);
if (total <= 0) {
return -1;
}
/* | version | ulen | uname (1 to 255) | plen | passwd (1 to 255) | */
/* version should be 0x01 for subnegotiation */
version = buffer[0];
if (version != 0x01) {
return -1;
}
ulen = buffer[1];
memcpy(uname, &buffer[2], ulen);
plen = buffer[2 + ulen];
memcpy(passwd, &buffer[3 + ulen], plen);
status = 0xFF;
if (!strncmp(server.username, (const char *)uname, 255) &&
!strncmp(server.password, (const char *)passwd, 255)) {
/* success */
status = 0x00;
}
/* | version | status | */
memset(buffer, 0, BUFF_SIZE);
buffer[0] = 0x01;
buffer[1] = status;
total = send(clientfd, buffer, 2, 0);
if (total == -1 || total != 2) {
return -1;
}
/* if not succeeded */
if (status == 0xFF) {
return -1;
}
return 0;
}
int method_negotiation(int clientfd)
{
int total;
char buffer[BUFF_SIZE];
uint8_t version, nmethods, methods[255], i, choosen_method = 0xFF;
total = recv(clientfd, buffer, BUFF_SIZE, 0);
if (total <= 0 || total > 257) {
return -1;
}
/* | version | nmethods | methods (1 to 255) | */
/* socks version should be 0x05 for socks5 */
version = buffer[0];
if (version != 0x05) {
return -1;
}
nmethods = buffer[1];
memcpy(methods, &buffer[2], nmethods);
for (i = 0; i < nmethods; i++) {
if (methods[i] == 0x00) {
/* no authentication */
choosen_method = 0x00;
break;
} else if (methods[i] == 0x02) {
/* username/password authentication */
choosen_method = 0x02;
}
}
/* | version | method | */
memset(buffer, 0, BUFF_SIZE);
buffer[0] = 0x05;
buffer[1] = choosen_method;
total = send(clientfd, buffer, 2, 0);
if (total == -1 || total != 2) {
return -1;
}
/* no acceptable methods, close conncetion */
if (choosen_method == 0xFF) {
return -1;
} else if (choosen_method == 0x02) {
return userpass_auth(clientfd);
}
return 0;
}
uint8_t connect_command(int *hostfd, char *buffer, uint8_t atype)
{
uint8_t reply;
switch (atype) {
case 0x01:
/* IPv4 address */
reply = ipv4_connect(hostfd, buffer);
break;
case 0x03:
/* domain name address*/
reply = domain_connect(hostfd, buffer);
break;
case 0x04:
/* IPv6 address */
reply = ipv6_connect(hostfd, buffer);
break;
default:
/* address type not supported */
reply = 0x08;
break;
}
return reply;
}
int process_request(int clientfd)
{
int hostfd, total;
char buffer[BUFF_SIZE];
uint8_t version, command, atype, reply;
total = recv(clientfd, buffer, BUFF_SIZE, 0);
if (total <= 0 || total > 262) {
return -1;
}
/* | ver | cmd | rsv | atype | dst.addr | dst.port | */
/* socks version should be 0x05 for socks5 */
version = buffer[0];
if (version != 0x05) {
return -1;
}
command = buffer[1];
atype = buffer[3];
/* CONNECT command */
if (command == 0x01) {
reply = connect_command(&hostfd, buffer, atype);
} else {
/* command not supported */
reply = 0x07;
}
/* | ver | rep | rsv | atype | bnd.addr | bnd.port | */
buffer[1] = reply;
total = send(clientfd, buffer, total, 0);
if (total == -1) {
return -1;
}
/* if not succeeded */
if (reply != 0x00) {
return -1;
}
return hostfd;
}
int log_session(int clientfd, int hostfd)
{
struct sockaddr_storage client_addr;
struct sockaddr_storage host_addr;
socklen_t client_len = sizeof(struct sockaddr_storage);
socklen_t host_len = sizeof(struct sockaddr_storage);
char client_ip[INET6_ADDRSTRLEN], host_ip[INET6_ADDRSTRLEN];
int client_port, host_port;
if (getpeername(clientfd, (struct sockaddr *)&client_addr, &client_len) == -1) {
return -1;
}
if (getpeername(hostfd, (struct sockaddr *)&host_addr, &host_len) == -1) {
return -1;
}
if (host_addr.ss_family == AF_INET) {
struct sockaddr_in *host_ipv4 = (struct sockaddr_in *)&host_addr;
host_port = ntohs(host_ipv4->sin_port);
inet_ntop(AF_INET, &host_ipv4->sin_addr, host_ip, INET6_ADDRSTRLEN);
} else if (host_addr.ss_family == AF_INET6) {
struct sockaddr_in6 *host_ipv6 = (struct sockaddr_in6 *)&host_addr;
host_port = ntohs(host_ipv6->sin6_port);
inet_ntop(AF_INET6, &host_ipv6->sin6_addr, host_ip, INET6_ADDRSTRLEN);
}
if (server.domain == DOMAIN_IPV4) {
struct sockaddr_in *addr4 = (struct sockaddr_in *)&client_addr;
client_port = ntohs(addr4->sin_port);
inet_ntop(AF_INET, &addr4->sin_addr, client_ip, INET6_ADDRSTRLEN);
} else {
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&client_addr;
client_port = ntohs(addr6->sin6_port);
inet_ntop(AF_INET6, &addr6->sin6_addr, client_ip, INET6_ADDRSTRLEN);
}
syslog(LOG_INFO, "[%s]:%d connected to [%s]:%d", client_ip, client_port, host_ip, host_port);
return 0;
}
int set_nonblock(int sockfd)
{
int flags, yes = 1;
if ((flags = fcntl(sockfd, F_GETFL)) == -1) {
return -1;
}
flags |= O_NONBLOCK;
if (fcntl(sockfd, F_SETFL, flags) == -1) {
return -1;
}
if (setsockopt(sockfd, SOL_TCP, TCP_NODELAY, &yes, sizeof(int)) == -1) {
return -1;
}
return 0;
}
int add_epoll_event(int epollfd, int sockfd)
{
struct epoll_event ev;
int ret;
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = sockfd;
ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev);
return ret;
}
int init_session(int clientfd, int hostfd)
{
int epollfd;
if (set_nonblock(clientfd) == -1) {
return -1;
}
if (set_nonblock(hostfd) == -1) {
return -1;
}
if ((epollfd = epoll_create1(0)) == -1) {
return -1;
}
if (add_epoll_event(epollfd, clientfd) == -1) {
return -1;
}
if (add_epoll_event(epollfd, hostfd) == -1) {
return -1;
}
return epollfd;
}
int transmit_data(int recvfd, int sendfd)
{
char buffer[BUFF_SIZE];
int total;
for (;;) {
total = recv(recvfd, buffer, BUFF_SIZE, 0);
if (total == -1) {
int err = errno;
if (err == EAGAIN || err == EWOULDBLOCK) {
return 0;
} else {
return -1;
}
} else if (total == 0) {
return -1;
}
total = send(sendfd, buffer, total, 0);
if (total == -1) {
return -1;
}
}
return total;
}
int svsocks_session(int epollfd, int clientfd, int hostfd)
{
struct epoll_event events[2];
int nfds, n, ret;
for (;;) {
nfds = epoll_wait(epollfd, events, 2, -1);
if (nfds == -1) {
return -1;
}
for (n = 0; n < nfds; n++) {
if ((events[n].events & EPOLLERR) ||
(events[n].events & EPOLLHUP) ||
(!(events[n].events & EPOLLIN))) {
return -1;
} else if (events[n].data.fd == clientfd) {
ret = transmit_data(clientfd, hostfd);
} else if (events[n].data.fd == hostfd) {
ret = transmit_data(hostfd, clientfd);
}
}
if (ret == -1) {
return -1;
}
}
return 0;
}
void destroy_session(int epollfd, int clientfd, int hostfd)
{
close(epollfd);
close(clientfd);
close(hostfd);
}
int handle_client(int clientfd)
{
int epollfd, hostfd;
if (method_negotiation(clientfd) == -1) {
close(clientfd);
return -1;
}
if ((hostfd = process_request(clientfd)) == -1) {
close(clientfd);
return -1;
}
if (log_session(clientfd, hostfd) == -1) {
close(clientfd);
close(hostfd);
return -1;
}
if ((epollfd = init_session(clientfd, hostfd)) == -1) {
close(clientfd);
close(hostfd);
return -1;
}
svsocks_session(epollfd, clientfd, hostfd);
destroy_session(epollfd, clientfd, hostfd);
return 0;
}
void *svsocks_worker()
{
int clientfd;
socklen_t len = sizeof(struct sockaddr_in6);
struct sockaddr_storage addr;
char client_addr[INET6_ADDRSTRLEN];
int client_port;
for (;;) {
pthread_mutex_lock(&server.server_mtx);
if (server.domain == DOMAIN_IPV4) {
struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
memset(addr4, 0, sizeof(struct sockaddr_in));
clientfd = accept(server.serverfd, (struct sockaddr *)&addr, &len);
if (clientfd == -1) {
continue;
}
inet_ntop(AF_INET, &addr4->sin_addr, client_addr, INET6_ADDRSTRLEN);
client_port = ntohs(addr4->sin_port);
} else {
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
memset(addr6, 0, sizeof(struct sockaddr_in6));
clientfd = accept(server.serverfd, (struct sockaddr *)&addr, &len);
if (clientfd == -1) {
continue;
}
inet_ntop(AF_INET6, &addr6->sin6_addr, client_addr, INET6_ADDRSTRLEN);
client_port = ntohs(addr6->sin6_port);
}
pthread_mutex_unlock(&server.server_mtx);
syslog(LOG_INFO, "[%s]:%d connected", client_addr, client_port);
handle_client(clientfd);
syslog(LOG_INFO, "[%s]:%d disconnected", client_addr, client_port);
}
}
void init_threads()
{
int ret;
pthread_t tid;
ret = pthread_mutex_init(&server.server_mtx, NULL);
if (ret != 0) {
syslog(LOG_ERR, "pthread_mutex_init error [%d] : could not initalize mutex",
__LINE__);
exit(EXIT_FAILURE);
}
for (int i = 0; i < server.nthreads; i++) {
ret = pthread_create(&tid, NULL, svsocks_worker, NULL);
if (ret != 0) {
syslog(LOG_ERR, "pthread_create error [%d] : %s",
__LINE__, strerror(ret));
exit(EXIT_FAILURE);
}
}
}
void init_socket()
{
struct sockaddr_storage addr;
int domain, yes = 1, no = 0;
socklen_t addr_size;
if (server.domain == DOMAIN_IPV4) {
struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
memset(addr4, 0, sizeof(struct sockaddr_in));
domain = AF_INET;
addr_size = sizeof(struct sockaddr_in);
addr4->sin_family = AF_INET;
addr4->sin_port = htons(server.listen_port);
inet_pton(AF_INET, server.listen_address.ipv4, &addr4->sin_addr.s_addr);
} else {
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
memset(addr6, 0, sizeof(struct sockaddr_in6));
domain = AF_INET6;
addr_size = sizeof(struct sockaddr_in6);
addr6->sin6_family = AF_INET6;
addr6->sin6_port = htons(server.listen_port);
inet_pton(AF_INET6, server.listen_address.ipv6, &addr6->sin6_addr.s6_addr);
}
if ((server.serverfd = socket(domain, SOCK_STREAM, 0)) == -1) {
syslog(LOG_ERR, "socket error [%d] : %s", __LINE__, strerror(errno));
exit(EXIT_FAILURE);
}
if (setsockopt(server.serverfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
syslog(LOG_ERR, "setsockopt error [SO_REUSEADDR][%d] : %s",
__LINE__, strerror(errno));
close(server.serverfd);
exit(EXIT_FAILURE);
}
if (server.domain == DOMAIN_IPV6) {
if (setsockopt(server.serverfd, IPPROTO_IPV6, IPV6_V6ONLY, &no,
sizeof(int)) == -1) {
syslog(LOG_ERR, "setsockopt error [IPV6_V6ONLY][%d] : %s",
__LINE__, strerror(errno));
close(server.serverfd);
exit(EXIT_FAILURE);
}
}
if (bind(server.serverfd, (struct sockaddr *)&addr, addr_size) == -1) {
syslog(LOG_ERR, "bind error [%d] : %s", __LINE__, strerror(errno));
close(server.serverfd);
exit(EXIT_FAILURE);
}
if (listen(server.serverfd, BACKLOG) == -1) {
syslog(LOG_ERR, "listen error [%d] : %s", __LINE__, strerror(errno));
close(server.serverfd);
exit(EXIT_FAILURE);
}
}
void signal_exit(int signum)
{
struct rlimit rlim;
syslog(LOG_INFO, "closing svsocks, signal = %d", signum);
closelog();
/* get the resource limit for max number of file descriptors */
if (getrlimit(RLIMIT_NOFILE, &rlim) == -1) {
exit(EXIT_FAILURE);
}
/* close all open file descriptors */
for (unsigned long int i = 0; i < rlim.rlim_max; i++) {
close(i);
}
exit(EXIT_SUCCESS);
}
void init_signal()
{
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = &signal_exit;
if (sigaction(SIGINT, &act, NULL) == -1) {
syslog(LOG_ERR, "sigaction error [%d] : %s", __LINE__, strerror(errno));
exit(EXIT_FAILURE);
}
if (sigaction(SIGTERM, &act, NULL) == -1) {
syslog(LOG_ERR, "sigaction error [%d] : %s", __LINE__, strerror(errno));
exit(EXIT_FAILURE);
}
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = SIG_IGN;
if (sigaction(SIGPIPE, &act, NULL) == -1) {
syslog(LOG_ERR, "sigaction error [%d] : %s", __LINE__, strerror(errno));
exit(EXIT_FAILURE);
}
}
void init_syslog()
{
openlog(SYSLOG_IDENT, LOG_NDELAY | LOG_PID, LOG_USER);
}
void daemonize_server()
{
int fd;
struct sigaction act;
struct rlimit rlim;
/* get the resource limit for max number of file descriptors */
if (getrlimit(RLIMIT_NOFILE, &rlim) == -1) {
exit(EXIT_FAILURE);
}
/* close all file descriptor except stdin(0), stdout(1) and stderr(2)*/
for (unsigned long int i = 3; i < rlim.rlim_max; i++) {
close(i);
}
/* reset all signal handlers to their default */
act.sa_handler = SIG_DFL;
for (unsigned long int i = 0; i < _NSIG; i++) {
sigaction(i, &act, NULL);
}
switch (fork()) {
case -1:
exit(EXIT_FAILURE);
case 0:
break;
default:
exit(EXIT_SUCCESS);
}
if (setsid() == -1) {
exit(EXIT_FAILURE);
}
switch (fork()) {
case -1:
exit(EXIT_FAILURE);
case 0:
break;
default:
exit(EXIT_SUCCESS);
}
/* close stdin(0), stdout(1) and stderr(2)*/
for (int i = 0; i < 3; i++) {
close(i);
}
/* connect /dev/null to stdin(0), stdout(1) and stderr(2)*/
if ((fd = open("/dev/null", O_RDWR)) == -1) {
exit(EXIT_FAILURE);
}
if (dup2(fd, 1) != 1) {
exit(EXIT_FAILURE);
}
if (dup2(fd, 2) != 2) {
exit(EXIT_FAILURE);
}
umask(0);
if (chdir("/") == -1) {
exit(EXIT_FAILURE);
}
}
void svsocks_usage()
{
fprintf(stdout,
"\nusage: ./svsocks [options]\n\n"
"options:\n"
" -6 : use IPv6 protocol.\n"
" -a [listen address] : specify the address for incoming connections.\n"
" (use the appropriate address format.)\n"
" -p [listen port] : specify the port for incoming connections.\n"
" -n [number of threads] : specify the number of threads for the thread pool.\n"
" -u [username] : username for username/password authentication.\n"
" -P [password] : password for username/password authentication.\n\n"
);
}
void arg_parser(int argc, char *argv[])
{
int opt;
while ((opt = getopt(argc, argv, "6a:p:n:u:s:h")) != -1) {
switch (opt) {
case '6':
server.domain = DOMAIN_IPV6;
break;
case 'a':
if (server.domain == DOMAIN_IPV4) {
strncpy(server.listen_address.ipv4, optarg, INET_ADDRSTRLEN);
} else {
strncpy(server.listen_address.ipv6, optarg, INET6_ADDRSTRLEN);
}
break;
case 'p':
server.listen_port = atoi(optarg);
break;
case 'n':
server.nthreads = atoi(optarg);
break;
case 'u':
strncpy(server.username, optarg, 255);
break;
case 's':
strncpy(server.password, optarg, 255);
break;
case 'h':
svsocks_usage();
exit(EXIT_SUCCESS);
}
}
}
int main(int argc, char *argv[])
{
/* initialize variables with default values */
server.domain = DOMAIN_IPV4;
strncpy(server.listen_address.ipv4, LISTEN_ADDRESS_IPV4, INET_ADDRSTRLEN);
server.listen_port = LISTEN_PORT;
server.nthreads = NTHREADS;
strncpy(server.username, USERNAME, 255);
strncpy(server.password, PASSWORD, 255);
if (argc > 1) {
arg_parser(argc, argv);
}
daemonize_server();
init_syslog();
init_signal();
init_socket();
init_threads();
syslog(LOG_INFO, "svsocks started. listening on [%s]:%d, total threads : %d",
(server.domain == DOMAIN_IPV4 ? server.listen_address.ipv4 :
server.listen_address.ipv6), server.listen_port, server.nthreads);
for (;;) {
pause();
}
exit(EXIT_SUCCESS);
}