forked from shiguol/Surf
-
Notifications
You must be signed in to change notification settings - Fork 48
/
local.m
662 lines (604 loc) · 19.7 KB
/
local.m
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
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include "local.h"
#include "socks5.h"
#include <TargetConditionals.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/errno.h>
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#define ADDR_STR_LEN 512
#define SAVED_STR_LEN 512
char _server[SAVED_STR_LEN];
char _remote_port[SAVED_STR_LEN];
char _method[SAVED_STR_LEN];
char _password[SAVED_STR_LEN];
int setnonblocking(int fd) {
int flags;
if (-1 ==(flags = fcntl(fd, F_GETFL, 0)))
flags = 0;
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
int create_and_bind2(const char *port)
{
int socket_handle;
int error = 0;
socket_handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(socket_handle < 0)
//error = errno;
printf("%d",error);
if(error == 0)
{
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(1080);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
int opt = 1;
setsockopt(socket_handle, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
setsockopt(socket_handle, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
setsockopt(socket_handle, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
setsockopt(socket_handle, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
error = bind(socket_handle, (struct sockaddr const *)&addr, sizeof(addr));
if (error == 0) {
/* We managed to bind successfully! */
} else {
perror("bind");
}
close(socket_handle);
}
return socket_handle;
}
int create_and_bind(const char *port) {
struct addrinfo hints;
struct addrinfo *result, *rp;
int s, listen_sock = 0;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
//result = &hints;
//.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
#if TARGET_IPHONE_SIMULATOR
// iOS Simulator
s = getaddrinfo("127.0.0.1", port, &hints, &result);
#elif TARGET_OS_IPHONE
s = getaddrinfo("127.0.0.1", port, &hints, &result);
#elif TARGET_OS_MAC
// Other kinds of Mac OS
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
hints.ai_protocol = IPPROTO_TCP;
s = getaddrinfo(nil, port, &hints, &result);
#endif
if (s != 0) {
NSLog(@"getaddrinfo: %s", gai_strerror(s));
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
int opt = 1;
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
setsockopt(listen_sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
setsockopt(listen_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (listen_sock == -1)
continue;
s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen);
if (s == 0) {
/* We managed to bind successfully! */
break;
} else {
perror("bind");
}
close(listen_sock);
}
if (rp == NULL) {
NSLog(@"Could not bind");
return -1;
}
freeaddrinfo(result);
return listen_sock;
}
static void server_recv_cb (EV_P_ ev_io *w, int revents) {
struct server_ctx *server_recv_ctx = (struct server_ctx *)w;
struct server *server = server_recv_ctx->server;
struct remote *remote = server->remote;
// NSLog(@"server_recv_cb %d", server?server->stage:-1);
if (remote == NULL) {
close_and_free_server(EV_A_ server);
return;
}
char *buf = remote->buf;
size_t *buf_len = &remote->buf_len;
if (server->stage != 5) {
buf = server->buf;
buf_len = &server->buf_len;
}
ssize_t r = recv(server->fd, buf, BUF_SIZE, 0);
if (r == 0) {
// connection closed
*buf_len = 0;
close_and_free_server(EV_A_ server);
if (remote != NULL) {
ev_io_start(EV_A_ &remote->send_ctx->io);
}
return;
} else if(r < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// no data
// continue to wait for recv
return;
} else {
perror("server recv");
close_and_free_server(EV_A_ server);
close_and_free_remote(EV_A_ remote);
return;
}
}
// local socks5 server
if (server->stage == 5) {
encrypt_buf(&(remote->send_encryption_ctx), (unsigned char *)remote->buf, (size_t *)&r);
ssize_t w = send(remote->fd, remote->buf, (size_t)r, 0);
if(w == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
remote->buf_len = (size_t)r;
// no data, wait for send
ev_io_stop(EV_A_ &server_recv_ctx->io);
ev_io_start(EV_A_ &remote->send_ctx->io);
return;
} else {
perror("send");
close_and_free_server(EV_A_ server);
close_and_free_remote(EV_A_ remote);
return;
}
} else if(w < r) {
char *pt = remote->buf;
char *et = pt + r;
while (pt + w < et) {
*pt = *(pt + w);
pt++;
}
remote->buf_len = (size_t)r - w;
ev_io_stop(EV_A_ &server_recv_ctx->io);
ev_io_start(EV_A_ &remote->send_ctx->io);
return;
}
} else if (server->stage == 0) {
struct method_select_response response;
response.ver = SOCKS_VERSION;
response.method = 0;
char *send_buf = (char *)&response;
send(server->fd, send_buf, sizeof(response), 0);
server->stage = 1;
return;
} else if (server->stage == 1) {
struct socks5_request *request = (struct socks5_request *)server->buf;
if (request->cmd != SOCKS_CMD_CONNECT) {
NSLog(@"unsupported cmd: %d\n", request->cmd);
struct socks5_response response;
response.ver = SOCKS_VERSION;
response.rep = SOCKS_CMD_NOT_SUPPORTED;
response.rsv = 0;
response.atyp = SOCKS_IPV4;
char *send_buf = (char *)&response;
send(server->fd, send_buf, 4, 0);
close_and_free_server(EV_A_ server);
close_and_free_remote(EV_A_ remote);
return;
}
char addr_to_send[ADDR_STR_LEN];
size_t addr_len = 0;
addr_to_send[addr_len++] = request->atyp;
char addr_str[ADDR_STR_LEN];
// get remote addr and port
if (request->atyp == SOCKS_IPV4) {
// IP V4
size_t in_addr_len = sizeof(struct in_addr);
memcpy(addr_to_send + addr_len, server->buf + 4, in_addr_len + 2);
addr_len += in_addr_len + 2;
// addr_to_send[addr_len] = 0;
// now get it back and print it
inet_ntop(AF_INET, server->buf + 4, addr_str, ADDR_STR_LEN);
//#if !TARGET_OS_IPHONE
NSLog(@"Connecting an IPv4 address, please configure your browser to use hostname instead: https://github.com/clowwindy/shadowsocks/wiki/Troubleshooting");
//#endif
} else if (request->atyp == SOCKS_DOMAIN) {
// Domain name
unsigned char name_len = *(unsigned char *)(server->buf + 4);
addr_to_send[addr_len++] = name_len;
memcpy(addr_to_send + addr_len, server->buf + 4 + 1, name_len);
memcpy(addr_str, server->buf + 4 + 1, name_len);
addr_str[name_len] = '\0';
addr_len += name_len;
// get port
addr_to_send[addr_len++] = *(unsigned char *)(server->buf + 4 + 1 + name_len);
addr_to_send[addr_len++] = *(unsigned char *)(server->buf + 4 + 1 + name_len + 1);
// addr_to_send[addr_len] = 0;
//#if !TARGET_OS_IPHONE
char temp[256];
memcpy(temp, server->buf + 4 + 1, name_len);
temp[name_len] = '\0';
NSLog(@"Connecting %@", [NSString stringWithCString:addr_str encoding:NSUTF8StringEncoding]);
//#endif
} else {
NSLog(@"unsupported addrtype: %d\n", request->atyp);
close_and_free_server(EV_A_ server);
close_and_free_remote(EV_A_ remote);
return;
}
int n = send_encrypt(&(remote->send_encryption_ctx), remote->fd, (unsigned char *)addr_to_send, &addr_len, 0);
if (n != addr_len) {
NSLog(@"header not completely sent: n != addr_len: n==%d, addr_len==%d", n, (int)addr_len);
close_and_free_remote(EV_A_ remote);
close_and_free_server(EV_A_ server);
return;
}
// Fake reply
struct socks5_response response;
response.ver = SOCKS_VERSION;
response.rep = 0;
response.rsv = 0;
response.atyp = SOCKS_IPV4;
struct in_addr sin_addr;
inet_aton("0.0.0.0", &sin_addr);
memcpy(server->buf, &response, 4);
memcpy(server->buf + 4, &sin_addr, sizeof(struct in_addr));
*((unsigned short *)(server->buf + 4 + sizeof(struct in_addr)))
= (unsigned short) htons(atoi(_remote_port));
size_t reply_size = 4 + sizeof(struct in_addr) + sizeof(unsigned short);
ssize_t r = send(server->fd, server->buf, reply_size, 0);
if (r < reply_size) {
NSLog(@"header not complete sent\n");
close_and_free_remote(EV_A_ remote);
close_and_free_server(EV_A_ server);
return;
}
ev_io_start(EV_A_ &remote->recv_ctx->io);
server->stage = 5;
}
}
static void server_send_cb (EV_P_ ev_io *w, int revents) {
struct server_ctx *server_send_ctx = (struct server_ctx *)w;
struct server *server = server_send_ctx->server;
struct remote *remote = server->remote;
if (server->buf_len == 0) {
// close and free
close_and_free_server(EV_A_ server);
close_and_free_remote(EV_A_ remote);
return;
} else {
// has data to send
ssize_t r = send(server->fd, server->buf,
server->buf_len, 0);
if (r < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
perror("send");
close_and_free_server(EV_A_ server);
close_and_free_remote(EV_A_ remote);
return;
}
return;
}
if (r < server->buf_len) {
// partly sent, move memory, wait for the next time to send
char *pt = server->buf;
char *et = pt + server->buf_len;
while (pt + r < et) {
*pt = *(pt + r);
pt++;
}
server->buf_len -= r;
return;
} else {
// all sent out, wait for reading
server->buf_len = 0;
ev_io_stop(EV_A_ &server_send_ctx->io);
if (remote != NULL) {
ev_io_start(EV_A_ &remote->recv_ctx->io);
} else {
close_and_free_server(EV_A_ server);
close_and_free_remote(EV_A_ remote);
return;
}
}
}
}
static void remote_recv_cb (EV_P_ ev_io *w, int revents) {
struct remote_ctx *remote_recv_ctx = (struct remote_ctx *)w;
struct remote *remote = remote_recv_ctx->remote;
struct server *server = remote->server;
if (server == NULL) {
close_and_free_remote(EV_A_ remote);
return;
}
ssize_t r = recv(remote->fd, server->buf, BUF_SIZE, 0);
if (r == 0) {
// connection closed
server->buf_len = 0;
close_and_free_remote(EV_A_ remote);
if (server != NULL) {
ev_io_start(EV_A_ &server->send_ctx->io);
}
return;
} else if(r < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// no data
// continue to wait for recv
return;
} else {
perror("remote recv");
close_and_free_server(EV_A_ server);
close_and_free_remote(EV_A_ remote);
return;
}
}
decrypt_buf(&(remote->recv_encryption_ctx), (unsigned char *)server->buf, (size_t*)&r);
ssize_t s = send(server->fd, server->buf, (size_t)r, 0);
if(s == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
server->buf_len = (size_t)r;
// no data, wait for send
ev_io_stop(EV_A_ &remote_recv_ctx->io);
ev_io_start(EV_A_ &server->send_ctx->io);
return;
} else {
perror("send");
close_and_free_server(EV_A_ server);
close_and_free_remote(EV_A_ remote);
return;
}
} else if(s < r) {
char *pt = server->buf;
char *et = pt + r;
while (pt + s < et) {
*pt = *(pt + s);
pt++;
}
server->buf_len = (size_t)r - s;
ev_io_stop(EV_A_ &remote_recv_ctx->io);
ev_io_start(EV_A_ &server->send_ctx->io);
return;
}
}
static void remote_send_cb (EV_P_ ev_io *w, int revents) {
struct remote_ctx *remote_send_ctx = (struct remote_ctx *)w;
struct remote *remote = remote_send_ctx->remote;
struct server *server = remote->server;
if (!remote_send_ctx->connected) {
socklen_t len;
struct sockaddr_storage addr;
len = sizeof addr;
int r = getpeername(remote->fd, (struct sockaddr*)&addr, &len);
if (r == 0) {
remote_send_ctx->connected = 1;
ev_io_stop(EV_A_ &remote_send_ctx->io);
ev_io_start(EV_A_ &server->recv_ctx->io);
} else {
perror("getpeername");
// not connected
close_and_free_remote(EV_A_ remote);
close_and_free_server(EV_A_ server);
return;
}
} else {
if (remote->buf_len == 0) {
// close and free
close_and_free_remote(EV_A_ remote);
close_and_free_server(EV_A_ server);
return;
} else {
// has data to send
ssize_t r = send(remote->fd, remote->buf,
remote->buf_len, 0);
if (r < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
perror("send");
// close and free
close_and_free_remote(EV_A_ remote);
close_and_free_server(EV_A_ server);
return;
}
return;
}
if (r < remote->buf_len) {
// partly sent, move memory, wait for the next time to send
char *pt = remote->buf;
char *et = pt + remote->buf_len;
while (pt + r < et) {
*pt = *(pt + r);
pt++;
}
remote->buf_len -= r;
return;
} else {
// all sent out, wait for reading
ev_io_stop(EV_A_ &remote_send_ctx->io);
if (server != NULL) {
ev_io_start(EV_A_ &server->recv_ctx->io);
} else {
close_and_free_remote(EV_A_ remote);
close_and_free_server(EV_A_ server);
return;
}
}
}
}
}
struct remote* new_remote(int fd) {
struct remote *remote;
remote = malloc(sizeof(struct remote));
remote->fd = fd;
remote->buf_len = 0;
remote->recv_ctx = malloc(sizeof(struct remote_ctx));
remote->send_ctx = malloc(sizeof(struct remote_ctx));
ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ);
ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE);
remote->recv_ctx->remote = remote;
remote->recv_ctx->connected = 0;
remote->send_ctx->remote = remote;
remote->send_ctx->connected = 0;
remote->server = NULL;
init_encryption(&(remote->recv_encryption_ctx));
init_encryption(&(remote->send_encryption_ctx));
return remote;
}
void free_remote(struct remote *remote) {
if (remote != NULL) {
if (remote->server != NULL) {
remote->server->remote = NULL;
}
free(remote->recv_ctx);
free(remote->send_ctx);
cleanup_encryption(&(remote->recv_encryption_ctx));
cleanup_encryption(&(remote->send_encryption_ctx));
free(remote);
}
}
void close_and_free_remote(EV_P_ struct remote *remote) {
if (remote != NULL) {
ev_io_stop(EV_A_ &remote->send_ctx->io);
ev_io_stop(EV_A_ &remote->recv_ctx->io);
close(remote->fd);
free_remote(remote);
}
}
struct server* new_server(int fd) {
struct server *server;
server = malloc(sizeof(struct server));
server->fd = fd;
server->buf_len = 0;
server->recv_ctx = malloc(sizeof(struct server_ctx));
server->send_ctx = malloc(sizeof(struct server_ctx));
ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ);
ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE);
server->recv_ctx->server = server;
server->recv_ctx->connected = 0;
server->send_ctx->server = server;
server->send_ctx->connected = 0;
server->stage = 0;
server->remote = NULL;
return server;
}
void free_server(struct server *server) {
if (server != NULL) {
if (server->remote != NULL) {
server->remote->server = NULL;
}
free(server->recv_ctx);
free(server->send_ctx);
free(server);
}
}
void close_and_free_server(EV_P_ struct server *server) {
if (server != NULL) {
ev_io_stop(EV_A_ &server->send_ctx->io);
ev_io_stop(EV_A_ &server->recv_ctx->io);
close(server->fd);
free_server(server);
}
}
static void accept_cb (EV_P_ ev_io *w, int revents)
{
struct listen_ctx *listener = (struct listen_ctx *)w;
int serverfd;
while (1) {
serverfd = accept(listener->fd, NULL, NULL);
if (serverfd == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
perror("accept");
}
break;
}
setnonblocking(serverfd);
int opt = 1;
setsockopt(serverfd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
setsockopt(serverfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
struct server *server = new_server(serverfd);
struct addrinfo hints, *res;
int sockfd;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int r = getaddrinfo(_server, _remote_port, &hints, &res);
if (r) {
fprintf(stderr, "getaddrinfo: %s", gai_strerror(r));
free_server(server);
continue;
}
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
if (sockfd < 0) {
perror("socket");
close(sockfd);
free_server(server);
continue;
}
setnonblocking(sockfd);
struct remote *remote = new_remote(sockfd);
server->remote = remote;
remote->server = server;
connect(sockfd, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
// listen to remote connected event
ev_io_start(EV_A_ &remote->send_ctx->io);
break;
}
}
void set_config(const char *server, const char *remote_port, const char* password, const char* method) {
assert(strlen(server) < SAVED_STR_LEN);
assert(strlen(remote_port) < SAVED_STR_LEN);
assert(strlen(password) < SAVED_STR_LEN);
assert(strlen(method) < SAVED_STR_LEN);
strcpy(_server, server);
strcpy(_remote_port, remote_port);
strcpy(_password, password);
strcpy(_method, method);
#ifdef DEBUG
NSLog(@"calculating ciphers");
#endif
// TODO move to encrypt.m
config_encryption(password, method);
}
int listenfd;
struct listen_ctx listen_ctx;
struct ev_loop *loop;
int local_main ()
{
listenfd = create_and_bind("10800");
if (listenfd < 0) {
#ifdef DEBUG
NSLog(@"bind() error..");
#endif
return 1;
}
int result = listen(listenfd, SOMAXCONN);
if (result == -1) {
NSLog(@"listen() error.");
perror("listen");
return 1;
}
#ifdef DEBUG
NSLog(@"server listening at port %s\n", "1080");
#endif
setnonblocking(listenfd);
listen_ctx.fd = listenfd;
loop = EV_DEFAULT;
ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ);
ev_io_start (loop, &listen_ctx.io);
ev_run (loop, 0);
return 0;
}
int stopSocks()
{
ev_io_stop(loop, &listen_ctx.io);
close(listenfd);
NSLog(@"close proxy server");
return 0;
}