' url:' . $js['Payload']['Url']);//注册成功
}
}
//远程代理连接
if ($sockinfo['type'] == 2) {
//未连接本地
if ($sockinfo['linkstate'] == 1) {
if ($js['Type'] == 'StartProxy') {
$loacladdr = getloacladdr($Tunnels, $js['Payload']['Url']);
$ip = dnsopen($loacladdr['lhost'], $loacladdr['lport']);//解析dns, port
if (!$ip) {//本地地址无效转向指定html页面
$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 class="copyright">Powered By ittun.com</div></div></div></body></html>';
$html = sprintf($body, $js['Payload']['Url'], $loacladdr['lhost'] .':' . $loacladdr['lport']);
$header = "HTTP/1.0 502 Bad Gateway"."\r\n";
$header .= "Server: ngrok-php"."\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);
} else {
$newsock = connectlocal($ip, $loacladdr['lport']);
if ($newsock) {
$socklist[] = array('sock' => $newsock, 'linkstate' => 0, 'type' => 3, 'tosock' => $sock);
}
//把本地连接覆盖上去
$sockinfo['tosock'] = $newsock;
$sockinfo['linkstate'] = 2;
}
}
}
}
//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(), 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($seraddr, $port) {
$ip = gethostbyname($seraddr);//解析dns
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
return false;
}
$fsock = @fsockopen($ip, $port, $errno, $errstr, 3);//检测端口
if (!$fsock) {
return false;
}
return $ip;
}
/* 连接到远程 */
function connectremote($seraddr, $port) {
global $is_verify_peer;
global $errno;
global $errstr;
$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_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
stream_set_blocking($socket, 0); //设置为非阻塞模式
return $socket;
}
/* 连接到本地 */
function connectlocal($localaddr, $localport) {
global $errno;
global $errstr;
$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) {
$protocol = substr($url, 0, strpos($url, ':'));
$hostname = substr($url, strpos($url, '//') + 2);
$subdomain = trim(substr($hostname, 0, strpos($hostname, '.')));
$rport = substr($url, strrpos($url, ':') + 1);
// echo 'protocol:'.$protocol."\r\n";
// echo '$subdomain:'.
Back to home |
File page
Subscribe |
Register |
Login
| N