-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathnatapp.php
479 lines (426 loc) · 16.2 KB
/
natapp.php
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
<?php
/**
* natapp.cn 内网穿透服务 PHP 版
*
* 本程序仅适用于natapp.cn 使用前请先在 https://natapp.cn 注册账号.
* 机器只要装有php(无须web服务)即可运行本程序,可用于路由器等OpenWRT操作系统
* 命令行模式执行 php natapp.php --authtoken=xxxxxx 即可运行
*
* 感谢 dosgo 提供的 ngrok-php 原版程序
*
*/
ConsoleOut("欢迎使用内网穿透 natapp-php v1.38\r\nCtrl+C 退出");
set_time_limit(0); //设置执行时间
ignore_user_abort(true);
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
//检测大小端
define('BIG_ENDIAN', pack('L', 1) === pack('N', 1));
$options = getopt("",
array(
"clienttoken::",
"authtoken::",
)
);
if ($options['authtoken'] == '' and $options['clienttoken'] == '') {
ConsoleOut("\r\n使用说明\r\n在命令行模式运行\r\nphp natapp.php --authtoken=xxxxxx\r\n如果是复合隧道请把authtoken参数换成 --clienttoken= \r\n请登录 natapp.cn获取authtoken\r\n");
sleep(10);
die();
}
$serverArr = natapp_auth($options);
$seraddr = $serverArr[0]; //服务器地址
$port = $serverArr[1]; //端口
$is_verify_peer = false; //是否校验证书
$isDebug = false; //调试开关
//定义变量
$readfds = array();
$writefds = array();
$e = null;
$t = 1;
$Tunnels = array();
$socklist = array();
$ClientId = '';
$recvflag = true;
$starttime = time(); //启动时间
$pingtime = 0;
//建立隧道协议
$mainsocket = connectremote($seraddr, $port);
if($mainsocket) {
$socklist[] = array('sock' => $mainsocket, 'linkstate' => 0, 'type' => 1);
}
//注册退出执行函数
register_shutdown_function('shutdown');
while ($recvflag) {
//重排
array_filter($socklist);
sort($socklist);
//检测控制连接是否连接.
if ($mainsocket == false) {
$ip = dnsopen($seraddr); //解析dns
if (!$ip) {
ConsoleOut('连接natapp服务器失败.');
sleep(1);
continue;
}
$mainsocket = connectremote($ip, $port);
if(!$mainsocket) {
ConsoleOut('连接natapp服务器失败.');
sleep(10);
continue;
}
$socklist[] = array('sock' => $mainsocket, 'linkstate' => 0, 'type' => 1);
}
//如果非cli超过1小时自杀
if (is_cli() == false) {
if ($starttime + 3600 < time()) {
fclose($mainsocket);
$recvflag = false;
break;
}
}
//发送心跳
if ($pingtime + 25 < time() && $pingtime != 0) {
sendpack($mainsocket, Ping());
$pingtime = time();
}
//重新赋值
$readfds = array();
$writefds = array();
foreach ($socklist as $k => $z) {
if (is_resource($z['sock'])) {
$readfds[] = $z['sock'];
if ($z['linkstate'] == 0) {
$writefds[] = $z['sock'];
}
} else {
//close的时候不是资源。。移除
if ($z['type'] == 1) {
$mainsocket = false;
}
array_splice($socklist, $k, 1);
}
}
//查询
$res = stream_select($readfds, $writefds, $e, $t);
if ($res === false) {
ConsoleOut('sockerr', 'debug');
}
//有事件
if ($res > 0) {
foreach ($socklist as $k => $sockinfo) {
$sock = $sockinfo['sock'];
//可读
if (in_array($sock, $readfds)) {
$recvbut = fread($sock, 1024);
if ($recvbut == false || strlen($recvbut) == 0) {
//主连接关闭,关闭所有
if ($sockinfo['type'] == 1) {
$mainsocket = false;
}
if ($sockinfo['type'] == 3) {
fclose($sockinfo['tosock']);
}
unset($socklist[$k]);
continue;
}
if (strlen($recvbut) > 0) {
if (!isset($sockinfo['recvbuf'])) {
$sockinfo['recvbuf'] = $recvbut;
} else {
$sockinfo['recvbuf'] = $sockinfo['recvbuf'] . $recvbut;
}
$socklist[$k] = $sockinfo;
}
//控制连接,或者远程未连接本地连接
if ($sockinfo['type'] == 1 || ($sockinfo['type'] == 2 && $sockinfo['linkstate'] == 1)) {
$allrecvbut = $sockinfo['recvbuf'];
//处理
$lenbuf = substr($allrecvbut, 0, 8);
$len = tolen1($lenbuf);
if (strlen($allrecvbut) >= (8 + $len)) {
$json = substr($allrecvbut, 8, $len);
ConsoleOut($json, 'debug');
$js = json_decode($json, true);
//远程主连接
if ($sockinfo['type'] == 1) {
if ($js['Type'] == 'ReqProxy') {
$newsock = connectremote($seraddr, $port);
if ($newsock) {
$socklist[] = array('sock' => $newsock, 'linkstate' => 0, 'type' => 2);
}
}
if ($js['Type'] == 'AuthResp') {
if ($js['Payload']['Error'] != null) {
ConsoleOut($js['Payload']['Error']);
sleep(30);
exit();
}
$ClientId = $js['Payload']['ClientId'];
$pingtime = time();
sendpack($sock, Ping());
}
if ($js['Type'] == 'NewTunnel') {
$Tunnels[$js['Payload']['Url']] = $js['Payload'];
ConsoleOut('隧道建立成功:' . $js['Payload']['Url']);
}
}
//远程代理连接
if ($sockinfo['type'] == 2) {
//未连接本地
if ($sockinfo['linkstate'] == 1) {
if ($js['Type'] == 'StartProxy') {
$loacladdr = getloacladdr($Tunnels, $js['Payload']['Url']);
$newsock = connectlocal($loacladdr[0], $loacladdr[1]);
if ($newsock) {
$socklist[] = array('sock' => $newsock, 'linkstate' => 0, 'type' => 3, 'tosock' => $sock);
//把本地连接覆盖上去
$sockinfo['tosock'] = $newsock;
$sockinfo['linkstate'] = 2;
} else {
$body = '<!DOCTYPE html><html><head><meta charset="utf-8"><title>Web服务错误</title><meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><style>html,body{height:100%%}body{margin:0;padding:0;width:100%%;display:table;font-weight:100;font-family:"Microsoft YaHei",Arial,Helvetica,sans-serif}.container{text-align:center;display:table-cell;vertical-align:middle}.content{border:1px solid #ebccd1;text-align:center;display:inline-block;background-color:#f2dede;color:#a94442;padding:30px}.title{font-size:18px}.copyright{margin-top:30px;text-align:right;color:#000}</style></head><body><div class="container"><div class="content"><div class="title">隧道 %s 无效<br>无法连接到<strong>%s</strong>. 此端口尚未提供Web服务</div></div></div></body></html>';
$html = sprintf($body, $js['Payload']['Url'], $loacladdr[0] .':' . $loacladdr[1]);
$header = "HTTP/1.0 502 Bad Gateway"."\r\n";
$header .= "Content-Type: text/html"."\r\n";
$header .= "Content-Length: %d"."\r\n";
$header .= "\r\n"."%s";
$buf = sprintf($header, strlen($html), $html);
sendbuf($sock, $buf);
}
}
}
}
//edit buffer
if (strlen($allrecvbut) == (8 + $len)) {
$sockinfo['recvbuf'] = '';
} else {
$sockinfo['recvbuf'] = substr($allrecvbut, 8 + $len);
}
$socklist[$k] = $sockinfo;
}
}
//远程连接已连接本地跟本地连接,纯转发
if ($sockinfo['type'] == 3 || ($sockinfo['type'] == 2 && $sockinfo['linkstate'] == 2)) {
sendbuf($sockinfo['tosock'], $sockinfo['recvbuf']);
$sockinfo['recvbuf'] = '';
$socklist[$k] = $sockinfo;
}
}
//可写
if (in_array($sock, $writefds)) {
if ($sockinfo['linkstate'] == 0) {
if ($sockinfo['type'] == 1) {
sendpack($sock, NgrokAuth($options), false);
$sockinfo['linkstate'] = 1;
$socklist[$k] = $sockinfo;
}
if ($sockinfo['type'] == 2) {
sendpack($sock, RegProxy($ClientId), false);
$sockinfo['linkstate'] = 1;
$socklist[$k] = $sockinfo;
}
if ($sockinfo['type'] == 3) {
$sockinfo['linkstate'] = 1;
$socklist[$k] = $sockinfo;
}
}
}
}
}
}
/* 域名解析 */
function dnsopen($host) {
$ip = gethostbyname($host); //解析dns
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
return false;
}
return $ip;
}
/* 连接到远程 */
function connectremote($seraddr, $port) {
global $is_verify_peer;
$socket = stream_socket_client('tcp://' . $seraddr . ':' . $port, $errno, $errstr, 30);
if (!$socket) {
return false;
}
//设置加密连接,默认是ssl,如果需要tls连接,可以查看php手册stream_socket_enable_crypto函数的解释
if ($is_verify_peer == false) {
stream_context_set_option($socket, 'ssl', 'verify_host', false);
stream_context_set_option($socket, 'ssl', 'verify_peer_name', false);
stream_context_set_option($socket, 'ssl', 'verify_peer', false);
stream_context_set_option($socket, 'ssl', 'allow_self_signed', false);
}
stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT);
stream_set_blocking($socket, 0); //设置为非阻塞模式
return $socket;
}
/* 连接到本地 */
function connectlocal($localaddr, $localport) {
$socket = stream_socket_client('tcp://' . $localaddr . ':' . $localport, $errno, $errstr, 30);
if (!$socket) {
return false;
}
stream_set_blocking($socket, 0); //设置为非阻塞模式
return $socket;
}
function getloacladdr($Tunnels, $url) {
$proto = explode(':', $Tunnels[$url]['LocalAddr']);
return $proto;
}
function NgrokAuth($token) {
$Payload = array(
'ClientId' => '',
'OS' => 'php',
'Arch' => 'amd64',
'Version' => '4',
'MmVersion' => '2.1',
'User' => 'user',
'Password' => '',
'AuthToken' => $token['authtoken'],
'ClientToken' => $token['clienttoken'],
);
$json = array(
'Type' => 'Auth',
'Payload' => $Payload,
);
return json_encode($json);
}
function RegProxy($ClientId) {
$Payload = array('ClientId' => $ClientId);
$json = array(
'Type' => 'RegProxy',
'Payload' => $Payload,
);
return json_encode($json);
}
function Ping() {
$Payload = (object) array();
$json = array(
'Type' => 'Ping',
'Payload' => $Payload,
);
return json_encode($json);
}
/* 网络字节序 (只支持整型范围) */
function lentobyte($len) {
$xx = pack("N", $len);
$xx1 = pack("C4", 0, 0, 0, 0);
return $xx1 . $xx;
}
/* 机器字节序 (小端 只支持整型范围) */
function lentobyte1($len) {
$xx = pack("L", $len);
$xx1 = pack("C4", 0, 0, 0, 0);
return $xx . $xx1;
}
function sendpack($sock, $msg, $isblock = true) {
if ($isblock) {
stream_set_blocking($sock, 1); //设置为非阻塞模式
}
fwrite($sock, lentobyte1(strlen($msg)) . $msg);
if ($isblock) {
stream_set_blocking($sock, 0); //设置为非阻塞模式
}
}
function sendbuf($sock, $buf, $isblock = true) {
if ($isblock) {
stream_set_blocking($sock, 1); //设置为非阻塞模式
}
fwrite($sock, $buf);
if ($isblock) {
stream_set_blocking($sock, 0); //设置为非阻塞模式
}
}
/* 网络字节序 (只支持整型范围) */
function tolen($v) {
$array = unpack("N", $v);
return $array[1];
}
/* 机器字节序 (小端) 只支持整型范围 */
function tolen1($v) {
$array = unpack("L", $v);
return $array[1];
}
//输出日记到命令行
function ConsoleOut($log, $level = 'info') {
global $isDebug;
if ($level == 'debug' and $isDebug == false) {
return;
}
//cli
if (is_cli()) {
if (DIRECTORY_SEPARATOR == "\\") {
$log = iconv('UTF-8', 'GB2312', $log);
}
echo $log . "\r\n";
}
//web
else {
echo $log . "<br/>";
ob_flush();
flush();
// file_put_contents("ngrok.log", date("Y-m-d H:i:s:::") . $log . "\r\n", FILE_APPEND);
}
}
//判断是否命令行运行
function is_cli() {
return (php_sapi_name() === 'cli') ? true : false;
}
//natapp.cn 获取服务器设置
function natapp_auth($token) {
global $is_verify_peer;
$host = 'auth.natapp.cn';
$port = 443;
$fp = stream_socket_client('tcp://' . $host . ':' . $port, $errno, $errstr, 10);
if (!$fp) {
ConsoleOut('连接认证服务器: https://auth.natapp.cn 错误.');
sleep(10);
exit();
}
if ($is_verify_peer == false) {
stream_context_set_option($fp, 'ssl', 'verify_host', false);
stream_context_set_option($fp, 'ssl', 'verify_peer_name', false);
stream_context_set_option($fp, 'ssl', 'verify_peer', false);
stream_context_set_option($fp, 'ssl', 'allow_self_signed', false);
}
stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT);
$data = array(
'Authtoken' => $token['authtoken'],
'Clienttoken' => $token['clienttoken'],
'Token' => 'fffeephptokenkhd672',
);
$query = json_encode($data);
$header = "POST "."/auth"." HTTP/1.1"."\r\n";
$header .= "Content-Type: text/html"."\r\n";
$header .= "Host: %s"."\r\n";
$header .= "Content-Length: %d"."\r\n";
$header .= "\r\n"."%s";
$buf = sprintf($header, $host, strlen($query), $query);
$write = fputs($fp, $buf);
$body = null;
while (!feof($fp)) {
$line = fgets($fp, 1024); //去除请求包的头只显示页面的返回数据
if ($line == "\n" || $line == "\r\n") {
$chunk_size = (integer) hexdec(fgets($fp, 1024));
if ($chunk_size > 0) {
$body = fread($fp, $chunk_size);
break;
}
}
}
fclose($fp);
$authData = json_decode($body, true);
if ($authData['success'] == false) {
ConsoleOut('认证错误:' . $authData['msg'] . ' ErrorCode:' . $authData['errorCode']);
sleep(10);
exit();
}
ConsoleOut('认证成功,正在连接服务器...');
$proto = explode(':', $authData['data']['ServerAddr']);
return $proto;
}
//注册退出执行函数
function shutdown() {
global $mainsocket;
sendpack($mainsocket, 'close');
fclose($mainsocket);
}
?>