-
Notifications
You must be signed in to change notification settings - Fork 203
/
ngrok.cpp
654 lines (608 loc) · 19.2 KB
/
ngrok.cpp
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
#include "config.h"
#include <string>
#if WIN32
#include <windows.h>
#include <string>
#else
#include <strings.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
typedef long long __int64;
#endif
#include "bytestool.h"
#include "ngrok.h"
#include "sslbio.h"
#include <stdlib.h>
using namespace std;
/*控制udp*/
int ControlUdp(int port)
{
int sockfd;
struct sockaddr_in my_addr;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
return -2;
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(port);
my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(my_addr.sin_zero, 0, 8);
int re_flag = 1;
int re_len = sizeof(int);
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&re_flag, re_len);
if (::bind(sockfd, (const struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
return -3;
}
setnonblocking(sockfd, 1);
return sockfd;
}
/**/
int UdpCmd(int udpsocket)
{
struct sockaddr udpaddr;
#if WIN32
int udplen = sizeof(udpaddr);
#else
socklen_t udplen = sizeof(udpaddr);
#endif
char buf[1024] = {0};
int udpbuflen = recvfrom(udpsocket, buf, 1024, MSG_PEEK, &udpaddr, &udplen);
if (udpbuflen > 0)
{
cJSON *json = cJSON_Parse(buf);
cJSON *cmd = cJSON_GetObjectItem(json, "cmd");
//退出
if (strcmp(cmd->valuestring, "exit") == 0)
{
cJSON_Delete(json);
exit(0);
}
if (strcmp(cmd->valuestring, "ping") == 0)
{
//回复
char sendbuf[255] = "{\"cmd\":\"pong\"}";
sendto(udpsocket, sendbuf, strlen(sendbuf), 0, (struct sockaddr *)&udpaddr, sizeof(udpaddr));
cJSON_Delete(json);
}
cJSON_Delete(json);
}
return 0;
}
int ReqProxy(struct sockaddr_in server_addr)
{
int proxy_fd = socket(AF_INET, SOCK_STREAM, 0);
int flag = 1;
setsockopt(proxy_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag));
SetBufSize(proxy_fd);
setnonblocking(proxy_fd, 1);
SetKeepAlive(proxy_fd);
connect(proxy_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
Sockinfo *sinfo = (Sockinfo *)malloc(sizeof(Sockinfo));
memset(sinfo, 0, sizeof(Sockinfo));
sinfo->istype = 1;
sinfo->isconnect = 0;
sinfo->packbuflen = 0;
sinfo->linktime = getUnixTime();
sinfo->sslinfo = NULL;
sinfo->localSslinfo=NULL;
sinfo->isconnectlocal = 0;
sinfo->sock = proxy_fd;
G_SockList.insert(map<int, Sockinfo *>::value_type(proxy_fd, sinfo));
return 0;
}
int InitTunnelList()
{
list<TunnelInfo *>::iterator iter;
for (iter = G_TunnelList.begin(); iter != G_TunnelList.end(); iter++)
{
TunnelInfo *tunnelinfo = (TunnelInfo *)*iter;
if (strcasecmp(tunnelinfo->protocol, "udp") != 0)
{
tunnelinfo->regtime = 0;
tunnelinfo->regstate = 0;
}
}
//释放所有通道信息
map<string, TunnelReq *>::iterator it3;
for (it3 = mainInfo.G_TunnelAddr.begin(); it3 != mainInfo.G_TunnelAddr.end();)
{
free(it3->second);
it3++;
}
mainInfo.G_TunnelAddr.clear();
return 0;
}
int SetLocalAddrInfo(char *url, char *ReqId, int regstate)
{
list<TunnelInfo *>::iterator iter;
char protocol[32] = {0};
char host[256] = {0};
char portstr[8] = {0};
char subdomain[128] = {0};
int port = 0;
sscanf(url, "%[^:]://%[^:]:%[0-9]", protocol, host, portstr);
port = atoi(portstr);
sscanf(host, "%[^.].", subdomain);
//进行迭代遍历
for (iter = G_TunnelList.begin(); iter != G_TunnelList.end(); iter++)
{
TunnelInfo *tunnelinfo = (TunnelInfo *)*iter;
if (strcasecmp(ReqId, tunnelinfo->ReqId) == 0)
{
//memcpy(tunnelinfo->hostname,host,strlen(host));
if (strncasecmp(protocol, "tcp", 3) == 0)
{
tunnelinfo->remoteport = port;
}
#if UDPTUNNEL
if (strncasecmp(protocol, "udp", 3) == 0)
{
tunnelinfo->remoteport = port;
}
#endif // UDPTUNNEL
if (strncasecmp(protocol, "http", 4) == 0)
{
memset(tunnelinfo->subdomain, 0, 255);
memcpy(tunnelinfo->subdomain, subdomain, strlen(subdomain));
}
tunnelinfo->regstate = regstate;
TunnelReq *tunnelreq = (TunnelReq *)malloc(sizeof(TunnelReq));
memset(tunnelreq, 0, sizeof(TunnelReq));
memcpy(tunnelreq->url, url, strlen(url));
memcpy(tunnelreq->localhost, tunnelinfo->localhost, strlen(tunnelinfo->localhost));
tunnelreq->localport = tunnelinfo->localport;
tunnelreq->localtls = tunnelinfo->localtls;
memcpy(tunnelreq->hostheader, tunnelinfo->hostheader, strlen(tunnelinfo->hostheader));
#if UDPTUNNEL
if (strncasecmp(protocol, "udp", 3) == 0)
{
udpInfo.G_TunnelAddr.insert(map<string, TunnelReq *>::value_type(string(url), tunnelreq));
}
#endif // UDPTUNNEL
if (strncasecmp(protocol, "udp", 3) != 0)
{
mainInfo.G_TunnelAddr.insert(map<string, TunnelReq *>::value_type(string(url), tunnelreq));
}
}
}
return 0;
}
int NewTunnel(cJSON *json)
{
cJSON *Payload = cJSON_GetObjectItem(json, "Payload");
char *error = cJSON_GetObjectItem(Payload, "Error")->valuestring;
if (strcmp(error, "") == 0)
{
char *url = cJSON_GetObjectItem(Payload, "Url")->valuestring;
char *ReqId = cJSON_GetObjectItem(Payload, "ReqId")->valuestring;
char *protocol = cJSON_GetObjectItem(Payload, "Protocol")->valuestring;
SetLocalAddrInfo(url, ReqId, 1);
echo("Add tunnel ok,type:%s url:%s\r\n", protocol, url);
}
else
{
echo("Add tunnel failed,%s\r\n", error);
return -1;
}
return 0;
}
int RemoteSslInit(Sockinfo *tempinfo)
{
ssl_info *sslinfo = (ssl_info *)malloc(sizeof(ssl_info));
tempinfo->sslinfo = sslinfo;
if (ssl_init_info(&tempinfo->sock, sslinfo) != -1)
{
#if OPENSSL
setnonblocking(tempinfo->sock, 1);
SendRegProxy(tempinfo->sock, sslinfo->ssl);
#else
SendRegProxy(tempinfo->sock, &sslinfo->ssl);
#endif
}
else
{
#if OPENSSL
setnonblocking(tempinfo->sock, 1);
#endif
/* ssl 初始化失败,移除连接 */
clearsock(tempinfo);
return -1;
}
return 0;
}
int LocalSslInit(Sockinfo *tempinfo)
{
ssl_info *sslinfo = (ssl_info *)malloc(sizeof(ssl_info));
tempinfo->localSslinfo = sslinfo;
if (ssl_init_info(&tempinfo->sock, sslinfo) != -1)
{
#if OPENSSL
setnonblocking(tempinfo->sock, 1);
#endif
}
else
{
#if OPENSSL
setnonblocking(tempinfo->sock, 1);
#endif
/*ssl初始化失败,移除连接 */
clearsock(tempinfo);
return -1;
}
return 0;
}
int LocalToRemote(Sockinfo *tempinfo)
{
ssl_info *sslinfo = tempinfo->sslinfo;
ssl_info *localSslinfo = tempinfo->localSslinfo;
int readlen;
int bufsize = 1024 * 15; //15K //oolarssl SSL_MAX_CONTENT_LEN 16384
//oolarssl 最大发送长度不能超过16K。。还是改成15吧
char buf[bufsize + 1];
memset(buf, 0, bufsize + 1);
#if WIN32
if (tempinfo->tunnelreq->localtls == 1)
{
#if OPENSSL
readlen = SslRecv(localSslinfo->ssl, (unsigned char *)buf, bufsize);
#else
readlen = SslRecv(&localSslinfo->ssl, (unsigned char *)buf, bufsize);
#endif
}
else
{
readlen = recv(tempinfo->sock, (char *)buf, bufsize, 0);
}
#else
if (tempinfo->tunnelreq->localtls == 1)
{
#if OPENSSL
readlen = SslRecv(localSslinfo->ssl, (unsigned char *)buf, bufsize);
#else
readlen = SslRecv(&localSslinfo->ssl, (unsigned char *)buf, bufsize);
#endif
}
else{
readlen = recv(tempinfo->sock, buf, bufsize, 0);
}
#endif
if (readlen > 0 && sslinfo != NULL){
//发送到远程
#if OPENSSL
sendTls(tempinfo->tosock, sslinfo->ssl, buf, readlen, 1);
#else
sendTls(tempinfo->tosock, &sslinfo->ssl, buf, readlen, 1);
#endif
}else{
shutdown(tempinfo->tosock, 2);
clearsock(tempinfo);
return -1;
}
return 0;
}
int RemoteToLocal(Sockinfo *tempinfo)
{
ssl_info *sslinfo = tempinfo->sslinfo;
int readlen, sendlen;
int bufsize = 1024 * 15; //15K //oolarssl SSL_MAX_CONTENT_LEN 16384
//oolarssl 最大发送长度不能超过16K。。还是改成15吧
char buf[bufsize + 1];
memset(buf, 0, bufsize + 1);
#if OPENSSL
readlen = SslRecv(sslinfo->ssl, (unsigned char *)buf, bufsize);
#else
readlen = SslRecv(&sslinfo->ssl, (unsigned char *)buf, bufsize);
#endif
if (readlen == 0 || readlen == -2)
{
/* close to sock */
int tosock = tempinfo->tosock;
shutdown(tosock, 2);
clearsock(tempinfo);
//这行绝对不能删除,用标记ssl已经销毁,删除会导致崩溃。
if (G_SockList.count(tosock) == 1)
{
G_SockList[tosock]->sslinfo = NULL;
}
return -1;
}
else if (readlen > 0)
{
TunnelReq *tunnelreq = tempinfo->tunnelreq;
char protocol[32] = {0};
char remotehost[256] = {0};
char httpline[3] = "\r\n";
sscanf(tunnelreq->url, "%[^:]://%[^\n]", protocol, remotehost);
//不是tcp才需要转发
if (strncmp(protocol, "tcp", 3) != 0)
{
//需要host头转发
if (strlen(tunnelreq->hostheader) > 0)
{
//拼接\r\n用于识别http头rfc协议描述的
memcpy(remotehost + strlen(remotehost), httpline, 2);
char *p = strstr(buf, remotehost);
char srchost[256] = {0};
memcpy(srchost, tunnelreq->hostheader, strlen(tunnelreq->hostheader));
memcpy(srchost + strlen(srchost), httpline, 2);
//查到了。。
if (p != NULL)
{
//替换http请求头
str_replace(p, strlen(remotehost), srchost);
}
}
}
if (tempinfo->tunnelreq->localtls == 1)
{
ssl_info *localSslInfo = tempinfo->localSslinfo;
//发送到本地
#if OPENSSL
sendlen = sendTls(tempinfo->tosock, localSslInfo->ssl, buf, readlen, 1);
#else
sendlen = sendTls(tempinfo->tosock, &localSslInfo->ssl, buf, readlen, 1);
#endif
}
else
{
sendlen = sendlocal(tempinfo->tosock, buf, readlen, 1);
}
if (sendlen < 1)
{
shutdown(tempinfo->sock, 2);
shutdown(tempinfo->tosock, 2);
}
}
return 0;
}
int ConnectLocal(Sockinfo *tempinfo)
{
ssl_info *sslinfo = tempinfo->sslinfo;
//避免指针为空崩溃
if (sslinfo == NULL)
{
clearsock(tempinfo);
return -1;
}
int readlen;
#if WIN32
unsigned __int64 packlen;
#else
unsigned long long packlen;
#endif
char tempjson[MAXBUF + 1];
char buf[MAXBUF + 1] = {0};
#if OPENSSL
readlen = SslRecv(sslinfo->ssl, (unsigned char *)buf, MAXBUF - 1);
#else
readlen = SslRecv(&sslinfo->ssl, (unsigned char *)buf, MAXBUF - 1);
#endif
if (readlen == 0 || readlen == -2)
{
clearsock(tempinfo);
return -1;
}
if (readlen == -1)
{
return 0; //这里不能用-1不然会导致,map迭代器不自增
}
//有时候readlen变成-76导致崩溃
if (readlen < 1)
{
clearsock(tempinfo);
return -1;
}
/* copy到临时缓存区 */
if (tempinfo->packbuflen == 0)
{
tempinfo->packbuf = (unsigned char *)malloc(MAXBUF);
}
memcpy(tempinfo->packbuf + tempinfo->packbuflen, buf, readlen);
tempinfo->packbuflen = tempinfo->packbuflen + readlen;
if (tempinfo->packbuflen > 8)
{
memcpy(&packlen, tempinfo->packbuf, 8);
if (BigEndianTest() == BigEndian)
{
packlen = Swap64(packlen);
}
if (tempinfo->packbuflen == packlen + 8)
{
memset(tempjson, 0, MAXBUF + 1);
memcpy(tempjson, tempinfo->packbuf + 8, packlen);
free(tempinfo->packbuf);
echo("tcp:%s\r\n", tempjson);
tempinfo->packbuf = NULL;
tempinfo->packbuflen = 0;
cJSON *json = cJSON_Parse(tempjson);
cJSON *Type = cJSON_GetObjectItem(json, "Type");
if (strcmp(Type->valuestring, "StartProxy") == 0)
{
cJSON *Payload = cJSON_GetObjectItem(json, "Payload");
char *Url = cJSON_GetObjectItem(Payload, "Url")->valuestring;
if (mainInfo.G_TunnelAddr.count(string(Url)) != 0)
{
TunnelReq *tunnelreq = mainInfo.G_TunnelAddr[string(Url)];
struct sockaddr_in local_addr = {0};
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(tunnelreq->localport);
local_addr.sin_addr.s_addr = inet_addr(tunnelreq->localhost);
int tcp = socket(AF_INET, SOCK_STREAM, 0);
int flag = 1;
setsockopt(tcp, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag));
SetBufSize(tcp);
// SOL_SOCKET
setnonblocking(tcp, 1);
connect(tcp, (struct sockaddr *)&local_addr, sizeof(local_addr));
Sockinfo *sinfo = (Sockinfo *)malloc(sizeof(Sockinfo));
sinfo->istype = 2;
sinfo->isconnect = 0;
sinfo->sslinfo = sslinfo;
sinfo->linktime = getUnixTime();
sinfo->tosock = tempinfo->sock;
sinfo->sock = tcp;
sinfo->packbuflen = 0;
sinfo->packbuf = NULL;
sinfo->localSslinfo=NULL;
sinfo->tunnelreq = tunnelreq;
G_SockList.insert(map<int, Sockinfo *>::value_type(tcp, sinfo));
/* 远程的带上本地链接 */
tempinfo->tosock = tcp;
tempinfo->tunnelreq = tunnelreq;
tempinfo->isconnectlocal = 1;
cJSON_Delete(json);
}
else
{
clearsock(tempinfo);
cJSON_Delete(json);
return -1;
}
}
}
}
return 0;
}
int CmdSock(int *mainsock, Sockinfo *tempinfo, struct sockaddr_in server_addr)
{
//检测是否断开
if (check_sock(*mainsock) != 0)
{
return -1;
}
ssl_info *sslinfo = tempinfo->sslinfo;
int readlen;
char buf[MAXBUF + 1] = {0};
#if WIN32
unsigned __int64 packlen;
#else
unsigned long long packlen;
#endif
char tempjson[MAXBUF + 1];
#if OPENSSL
readlen = SslRecv(sslinfo->ssl, (unsigned char *)buf, MAXBUF);
#else
readlen = SslRecv(&sslinfo->ssl, (unsigned char *)buf, MAXBUF);
#endif
if (readlen == 0 || readlen == -2)
{
return -1;
}
if (readlen == -1 || readlen < 1)
{
return 0;
}
/* copy到临时缓存区 */
if (tempinfo->packbuflen == 0)
{
tempinfo->packbuf = (unsigned char *)malloc(MAXBUF);
}
memcpy(tempinfo->packbuf + tempinfo->packbuflen, buf, readlen);
tempinfo->packbuflen = tempinfo->packbuflen + readlen;
if (tempinfo->packbuflen > 8)
{
memcpy(&packlen, tempinfo->packbuf, 8);
if (BigEndianTest() == BigEndian)
{
packlen = Swap64(packlen);
}
if (tempinfo->packbuflen == packlen + 8)
{
memset(tempjson, 0, MAXBUF + 1);
memcpy(tempjson, tempinfo->packbuf + 8, packlen);
free(tempinfo->packbuf);
tempinfo->packbuf = NULL;
tempinfo->packbuflen = 0;
echo("tcp:%s\r\n", tempjson);
cJSON *json = cJSON_Parse(tempjson);
cJSON *Type = cJSON_GetObjectItem(json, "Type");
if (strcmp(Type->valuestring, "ReqProxy") == 0)
{
ReqProxy(server_addr);
}
else if (strcmp(Type->valuestring, "AuthResp") == 0)
{
cJSON *Payload = cJSON_GetObjectItem(json, "Payload");
char *error = cJSON_GetObjectItem(Payload, "Error")->valuestring;
if (strcmp(error, "") == 0)
{
char *cid = cJSON_GetObjectItem(Payload, "ClientId")->valuestring;
mainInfo.ClientId = string(cid);
#if OPENSSL
SendPing(*mainsock, sslinfo->ssl);
#else
SendPing(*mainsock, &sslinfo->ssl);
#endif
tempinfo->isauth = 1;
}
else
{
cJSON_Delete(json);
echo("Auth failed ,Please check authtoken. ");
return -1;
}
}
else if (strcmp(Type->valuestring, "Ping") == 0)
{
#if OPENSSL
SendPong(*mainsock, sslinfo->ssl);
#else
SendPing(*mainsock, &sslinfo->ssl);
#endif
}
else if (strcmp(Type->valuestring, "Pong") == 0)
{
}
else if (strcmp(Type->valuestring, "NewTunnel") == 0)
{
NewTunnel(json);
}
cJSON_Delete(json);
}
}
return 0;
}
int ConnectMain(int *mainsock, struct sockaddr_in server_addr, ssl_info **mainsslinfo)
{
*mainsock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
SetBufSize(*mainsock);
if (connect(*mainsock, (struct sockaddr *)&server_addr, sizeof(server_addr)) != 0)
{
echo("connect failed...!\r\n");
#if WIN32
closesocket(*mainsock);
#else
close(*mainsock);
#endif
return -1;
}
*mainsslinfo = (ssl_info *)malloc(sizeof(ssl_info));
if (ssl_init_info(mainsock, *mainsslinfo) == -1)
{
echo("ssl init failed!\r\n");
#if WIN32
closesocket(*mainsock);
#else
close(*mainsock);
#endif
ssl_free_info(*mainsslinfo);
free(*mainsslinfo);
return -1;
}
#if OPENSSL
SendAuth(*mainsock, (*mainsslinfo)->ssl);
#else
SendAuth(*mainsock, &(*mainsslinfo)->ssl);
#endif
setnonblocking(*mainsock, 1);
Sockinfo *sinfo = (Sockinfo *)malloc(sizeof(Sockinfo));
sinfo->istype = 3;
sinfo->isconnect = 1;
sinfo->packbuflen = 0;
sinfo->sock = *mainsock;
sinfo->sslinfo = *mainsslinfo;
G_SockList.insert(map<int, Sockinfo *>::value_type(*mainsock, sinfo));
return 0;
}