Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
674 lines (588 loc) · 13.5 KB
/
Copy pathmain.c
File metadata and controls
674 lines (588 loc) · 13.5 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
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
#define _GNU_SOURCE
#include <sys/types.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <netdb.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <pthread.h>
#define BUFSIZE 65536
#define IPSIZE 4
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#define ARRAY_INIT {0}
unsigned short int port = 1080;
int daemon_mode = 0;
int auth_type;
char *arg_username;
char *arg_password;
FILE *log_file;
pthread_mutex_t lock;
enum socks {
RESERVED = 0x00,
VERSION4 = 0x04,
VERSION5 = 0x05
};
enum socks_auth_methods {
NOAUTH = 0x00,
USERPASS = 0x02,
NOMETHOD = 0xff
};
enum socks_auth_userpass {
AUTH_OK = 0x00,
AUTH_VERSION = 0x01,
AUTH_FAIL = 0xff
};
enum socks_command {
CONNECT = 0x01
};
enum socks_command_type {
IP = 0x01,
DOMAIN = 0x03
};
enum socks_status {
OK = 0x00,
FAILED = 0x05
};
void log_message(const char *message, ...)
{
if (daemon_mode) {
return;
}
char vbuffer[255];
va_list args;
va_start(args, message);
vsnprintf(vbuffer, ARRAY_SIZE(vbuffer), message, args);
va_end(args);
time_t now;
time(&now);
char *date = ctime(&now);
date[strlen(date) - 1] = '\0';
pthread_t self = pthread_self();
if (errno != 0) {
pthread_mutex_lock(&lock);
fprintf(log_file, "[%s][%lu] Critical: %s - %s\n", date, self,
vbuffer, strerror(errno));
errno = 0;
pthread_mutex_unlock(&lock);
} else {
fprintf(log_file, "[%s][%lu] Info: %s\n", date, self, vbuffer);
}
fflush(log_file);
}
int readn(int fd, void *buf, int n)
{
int nread, left = n;
while (left > 0) {
if ((nread = read(fd, buf, left)) == -1) {
if (errno == EINTR || errno == EAGAIN) {
continue;
}
} else {
if (nread == 0) {
return 0;
} else {
left -= nread;
buf += nread;
}
}
}
return n;
}
int writen(int fd, void *buf, int n)
{
int nwrite, left = n;
while (left > 0) {
if ((nwrite = write(fd, buf, left)) == -1) {
if (errno == EINTR || errno == EAGAIN) {
continue;
}
} else {
if (nwrite == n) {
return 0;
} else {
left -= nwrite;
buf += nwrite;
}
}
}
return n;
}
void app_thread_exit(int ret, int fd)
{
close(fd);
pthread_exit((void *)&ret);
}
int app_connect(int type, void *buf, unsigned short int portnum)
{
int fd;
struct sockaddr_in remote;
char address[16];
memset(address, 0, ARRAY_SIZE(address));
if (type == IP) {
char *ip = (char *)buf;
snprintf(address, ARRAY_SIZE(address), "%hhu.%hhu.%hhu.%hhu",
ip[0], ip[1], ip[2], ip[3]);
memset(&remote, 0, sizeof(remote));
remote.sin_family = AF_INET;
remote.sin_addr.s_addr = inet_addr(address);
remote.sin_port = htons(portnum);
fd = socket(AF_INET, SOCK_STREAM, 0);
if (connect(fd, (struct sockaddr *)&remote, sizeof(remote)) < 0) {
log_message("connect() in app_connect");
close(fd);
return -1;
}
return fd;
} else if (type == DOMAIN) {
char portaddr[6];
struct addrinfo *res;
snprintf(portaddr, ARRAY_SIZE(portaddr), "%d", portnum);
log_message("getaddrinfo: %s %s", (char *)buf, portaddr);
int ret = getaddrinfo((char *)buf, portaddr, NULL, &res);
if (ret == EAI_NODATA) {
return -1;
} else if (ret == 0) {
struct addrinfo *r;
for (r = res; r != NULL; r = r->ai_next) {
fd = socket(r->ai_family, r->ai_socktype,
r->ai_protocol);
if (fd == -1) {
continue;
}
ret = connect(fd, r->ai_addr, r->ai_addrlen);
if (ret == 0) {
freeaddrinfo(res);
return fd;
} else {
close(fd);
}
}
}
freeaddrinfo(res);
return -1;
}
return -1;
}
int socks_invitation(int fd, int *version)
{
char init[2];
int nread = readn(fd, (void *)init, ARRAY_SIZE(init));
if (nread == 2 && init[0] != VERSION5 && init[0] != VERSION4) {
log_message("They send us %hhX %hhX", init[0], init[1]);
log_message("Incompatible version!");
app_thread_exit(0, fd);
}
log_message("Initial %hhX %hhX", init[0], init[1]);
*version = init[0];
return init[1];
}
char *socks5_auth_get_user(int fd)
{
unsigned char size;
readn(fd, (void *)&size, sizeof(size));
char *user = (char *)malloc(sizeof(char) * size + 1);
readn(fd, (void *)user, (int)size);
user[size] = 0;
return user;
}
char *socks5_auth_get_pass(int fd)
{
unsigned char size;
readn(fd, (void *)&size, sizeof(size));
char *pass = (char *)malloc(sizeof(char) * size + 1);
readn(fd, (void *)pass, (int)size);
pass[size] = 0;
return pass;
}
int socks5_auth_userpass(int fd)
{
char answer[2] = { VERSION5, USERPASS };
writen(fd, (void *)answer, ARRAY_SIZE(answer));
char resp;
readn(fd, (void *)&resp, sizeof(resp));
log_message("auth %hhX", resp);
char *username = socks5_auth_get_user(fd);
char *password = socks5_auth_get_pass(fd);
log_message("l: %s p: %s", username, password);
if (strcmp(arg_username, username) == 0
&& strcmp(arg_password, password) == 0) {
char answer[2] = { AUTH_VERSION, AUTH_OK };
writen(fd, (void *)answer, ARRAY_SIZE(answer));
free(username);
free(password);
return 0;
} else {
char answer[2] = { AUTH_VERSION, AUTH_FAIL };
writen(fd, (void *)answer, ARRAY_SIZE(answer));
free(username);
free(password);
return 1;
}
}
int socks5_auth_noauth(int fd)
{
char answer[2] = { VERSION5, NOAUTH };
writen(fd, (void *)answer, ARRAY_SIZE(answer));
return 0;
}
void socks5_auth_notsupported(int fd)
{
char answer[2] = { VERSION5, NOMETHOD };
writen(fd, (void *)answer, ARRAY_SIZE(answer));
}
void socks5_auth(int fd, int methods_count)
{
int supported = 0;
int num = methods_count;
for (int i = 0; i < num; i++) {
char type;
readn(fd, (void *)&type, 1);
log_message("Method AUTH %hhX", type);
if (type == auth_type) {
supported = 1;
}
}
if (supported == 0) {
socks5_auth_notsupported(fd);
app_thread_exit(1, fd);
}
int ret = 0;
switch (auth_type) {
case NOAUTH:
ret = socks5_auth_noauth(fd);
break;
case USERPASS:
ret = socks5_auth_userpass(fd);
break;
}
if (ret == 0) {
return;
} else {
app_thread_exit(1, fd);
}
}
int socks5_command(int fd)
{
char command[4];
readn(fd, (void *)command, ARRAY_SIZE(command));
log_message("Command %hhX %hhX %hhX %hhX", command[0], command[1],
command[2], command[3]);
return command[3];
}
unsigned short int socks_read_port(int fd)
{
unsigned short int p;
readn(fd, (void *)&p, sizeof(p));
log_message("Port %hu", ntohs(p));
return p;
}
char *socks_ip_read(int fd)
{
char *ip = (char *)malloc(sizeof(char) * IPSIZE);
readn(fd, (void *)ip, IPSIZE);
log_message("IP %hhu.%hhu.%hhu.%hhu", ip[0], ip[1], ip[2], ip[3]);
return ip;
}
void socks5_ip_send_response(int fd, char *ip, unsigned short int port)
{
char response[4] = { VERSION5, OK, RESERVED, IP };
writen(fd, (void *)response, ARRAY_SIZE(response));
writen(fd, (void *)ip, IPSIZE);
writen(fd, (void *)&port, sizeof(port));
}
char *socks5_domain_read(int fd, unsigned char *size)
{
unsigned char s;
readn(fd, (void *)&s, sizeof(s));
char *address = (char *)malloc((sizeof(char) * s) + 1);
readn(fd, (void *)address, (int)s);
address[s] = 0;
log_message("Address %s", address);
*size = s;
return address;
}
void socks5_domain_send_response(int fd, char *domain, unsigned char size,
unsigned short int port)
{
char response[4] = { VERSION5, OK, RESERVED, DOMAIN };
writen(fd, (void *)response, ARRAY_SIZE(response));
writen(fd, (void *)&size, sizeof(size));
writen(fd, (void *)domain, size * sizeof(char));
writen(fd, (void *)&port, sizeof(port));
}
int socks4_is_4a(char *ip)
{
return (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] != 0);
}
int socks4_read_nstring(int fd, char *buf, int size)
{
char sym = 0;
int nread = 0;
int i = 0;
while (i < size) {
nread = recv(fd, &sym, sizeof(char), 0);
if (nread <= 0) {
break;
} else {
buf[i] = sym;
i++;
}
if (sym == 0) {
break;
}
}
return i;
}
void socks4_send_response(int fd, int status)
{
char resp[8] = {0x00, (char)status, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
writen(fd, (void *)resp, ARRAY_SIZE(resp));
}
void app_socket_pipe(int fd0, int fd1)
{
int maxfd, ret;
fd_set rd_set;
size_t nread;
char buffer_r[BUFSIZE];
log_message("Connecting two sockets");
maxfd = (fd0 > fd1) ? fd0 : fd1;
while (1) {
FD_ZERO(&rd_set);
FD_SET(fd0, &rd_set);
FD_SET(fd1, &rd_set);
ret = select(maxfd + 1, &rd_set, NULL, NULL, NULL);
if (ret < 0 && errno == EINTR) {
continue;
}
if (FD_ISSET(fd0, &rd_set)) {
nread = recv(fd0, buffer_r, BUFSIZE, 0);
if (nread <= 0)
break;
send(fd1, (const void *)buffer_r, nread, 0);
}
if (FD_ISSET(fd1, &rd_set)) {
nread = recv(fd1, buffer_r, BUFSIZE, 0);
if (nread <= 0)
break;
send(fd0, (const void *)buffer_r, nread, 0);
}
}
}
void *app_thread_process(void *fd)
{
int net_fd = *(int *)fd;
int version = 0;
int inet_fd = -1;
char methods = socks_invitation(net_fd, &version);
switch (version) {
case VERSION5: {
socks5_auth(net_fd, methods);
int command = socks5_command(net_fd);
if (command == IP) {
char *ip = socks_ip_read(net_fd);
unsigned short int p = socks_read_port(net_fd);
inet_fd = app_connect(IP, (void *)ip, ntohs(p));
if (inet_fd == -1) {
app_thread_exit(1, net_fd);
}
socks5_ip_send_response(net_fd, ip, p);
free(ip);
break;
} else if (command == DOMAIN) {
unsigned char size;
char *address = socks5_domain_read(net_fd, &size);
unsigned short int p = socks_read_port(net_fd);
inet_fd = app_connect(DOMAIN, (void *)address, ntohs(p));
if (inet_fd == -1) {
app_thread_exit(1, net_fd);
}
socks5_domain_send_response(net_fd, address, size, p);
free(address);
break;
} else {
app_thread_exit(1, net_fd);
}
}
case VERSION4: {
if (methods == 1) {
char ident[255];
unsigned short int p = socks_read_port(net_fd);
char *ip = socks_ip_read(net_fd);
socks4_read_nstring(net_fd, ident, sizeof(ident));
if (socks4_is_4a(ip)) {
char domain[255];
socks4_read_nstring(net_fd, domain, sizeof(domain));
log_message("Socks4A: ident:%s; domain:%s;", ident, domain);
inet_fd = app_connect(DOMAIN, (void *)domain, ntohs(p));
} else {
log_message("Socks4: connect by ip & port");
inet_fd = app_connect(IP, (void *)ip, ntohs(p));
}
if (inet_fd != -1) {
socks4_send_response(net_fd, 0x5a);
} else {
socks4_send_response(net_fd, 0x5b);
free(ip);
app_thread_exit(1, net_fd);
}
free(ip);
} else {
log_message("Unsupported mode");
}
break;
}
}
app_socket_pipe(inet_fd, net_fd);
close(inet_fd);
app_thread_exit(0, net_fd);
return NULL;
}
int app_loop()
{
int sock_fd, net_fd;
int optval = 1;
struct sockaddr_in local, remote;
socklen_t remotelen;
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
log_message("socket()");
exit(1);
}
if (setsockopt
(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&optval,
sizeof(optval)) < 0) {
log_message("setsockopt()");
exit(1);
}
memset(&local, 0, sizeof(local));
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_port = htons(port);
if (bind(sock_fd, (struct sockaddr *)&local, sizeof(local)) < 0) {
log_message("bind()");
exit(1);
}
if (listen(sock_fd, 25) < 0) {
log_message("listen()");
exit(1);
}
remotelen = sizeof(remote);
memset(&remote, 0, sizeof(remote));
log_message("Listening port %d...", port);
pthread_t worker;
while (1) {
if ((net_fd =
accept(sock_fd, (struct sockaddr *)&remote,
&remotelen)) < 0) {
log_message("accept()");
exit(1);
}
int one = 1;
setsockopt(sock_fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
if (pthread_create
(&worker, NULL, &app_thread_process,
(void *)&net_fd) == 0) {
pthread_detach(worker);
} else {
log_message("pthread_create()");
}
}
}
void daemonize()
{
pid_t pid;
int x;
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
if (setsid() < 0) {
exit(EXIT_FAILURE);
}
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
chdir("/");
for (x = sysconf(_SC_OPEN_MAX); x >= 0; x--) {
close(x);
}
}
void usage(char *app)
{
printf
("USAGE: %s [-h][-n PORT][-a AUTHTYPE][-u USERNAME][-p PASSWORD][-l LOGFILE]\n",
app);
printf("AUTHTYPE: 0 for NOAUTH, 2 for USERPASS\n");
printf
("By default: port is 1080, authtype is no auth, logfile is stdout\n");
exit(1);
}
int main(int argc, char *argv[])
{
int ret;
log_file = stdout;
auth_type = NOAUTH;
arg_username = "user";
arg_password = "pass";
pthread_mutex_init(&lock, NULL);
signal(SIGPIPE, SIG_IGN);
while ((ret = getopt(argc, argv, "n:u:p:l:a:hd")) != -1) {
switch (ret) {
case 'd':{
daemon_mode = 1;
daemonize();
break;
}
case 'n':{
port = atoi(optarg) & 0xffff;
break;
}
case 'u':{
arg_username = strdup(optarg);
break;
}
case 'p':{
arg_password = strdup(optarg);
break;
}
case 'l':{
freopen(optarg, "wa", log_file);
break;
}
case 'a':{
auth_type = atoi(optarg);
break;
}
case 'h':
default:
usage(argv[0]);
}
}
log_message("Starting with authtype %X", auth_type);
if (auth_type != NOAUTH) {
log_message("Username is %s, password is %s", arg_username,
arg_password);
}
app_loop();
return 0;
}