116 lines
3.2 KiB
PHP
116 lines
3.2 KiB
PHP
|
<?php
|
|||
|
|
|||
|
use Workerman\Worker;
|
|||
|
use Workerman\Connection\TcpConnection;
|
|||
|
use Workerman\Connection\AsyncTcpConnection;
|
|||
|
use Workerman\Protocols\Http\Request;
|
|||
|
|
|||
|
require_once __DIR__ . '/vendor/autoload.php';
|
|||
|
|
|||
|
$worker = new Worker('http://0.0.0.0:8686');
|
|||
|
|
|||
|
|
|||
|
function replaceHost($data,$host)
|
|||
|
{
|
|||
|
return preg_replace('/Host: (.*)/i','Host: '.$host,$data);
|
|||
|
}
|
|||
|
function HTMLview($filePath, $data) {
|
|||
|
ob_start();
|
|||
|
extract($data);
|
|||
|
include $filePath;
|
|||
|
$str = ob_get_contents();
|
|||
|
ob_end_clean();
|
|||
|
|
|||
|
return $str;
|
|||
|
}
|
|||
|
$worker->onMessage = function(TcpConnection $connection, Request $data)
|
|||
|
{
|
|||
|
$token = $data->cookie('token', 'null');
|
|||
|
if($token=='null'){
|
|||
|
$host = $data->host();
|
|||
|
|
|||
|
|
|||
|
$public=true;
|
|||
|
|
|||
|
$connection->send(HTMLview('./view/login.php', array('public'=>$public)));
|
|||
|
$connection->close();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// Parse http header.
|
|||
|
list($method, $addr, $http_version) = explode(' ', $data);
|
|||
|
$_headers=explode("\r\n",$data);
|
|||
|
$headers=[];
|
|||
|
foreach ($_headers as $_header){
|
|||
|
$_point=strpos($_header,':');
|
|||
|
if ($_point===false) continue;
|
|||
|
$headers[substr($_header,0,$_point)]=trim(trim(substr($_header,$_point),':'));
|
|||
|
}
|
|||
|
|
|||
|
//源站IP地址
|
|||
|
$source_ip='106.54.203.91';
|
|||
|
$source_host='laysense.cn';
|
|||
|
$ssl=false;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
if($ssl==true){
|
|||
|
$port=443;
|
|||
|
$source_ip=$source_host;
|
|||
|
}else{
|
|||
|
$port=80;
|
|||
|
}
|
|||
|
print_r('用户IP:'.$connection->getRemoteAddress().'| 访问域名:'.$headers['Host'] .' | 源站:'.gethostbyname($source_ip).'| 回源域名'.$source_host.'|SSL:'.$ssl);
|
|||
|
|
|||
|
// Async TCP connection.
|
|||
|
$remote_connection = new AsyncTcpConnection('tcp://'.$source_ip.':'.$port);
|
|||
|
// CONNECT.
|
|||
|
if($ssl==true){
|
|||
|
$remote_connection->transport = 'ssl';
|
|||
|
}
|
|||
|
|
|||
|
if ($method !== 'CONNECT') {
|
|||
|
//替换request 中的回源host
|
|||
|
$remote_connection->send(replaceHost($data,$source_host));
|
|||
|
// POST GET PUT DELETE etc.
|
|||
|
} else {
|
|||
|
$connection->send("HTTP/1.1 200 Connection Established\r\n\r\n");
|
|||
|
}
|
|||
|
|
|||
|
$remote_connection->onMessage=function (TcpConnection $SourceConn, $SourceData)use ($connection){
|
|||
|
$connection->send($SourceData);
|
|||
|
};
|
|||
|
|
|||
|
$remote_connection->onClose = function ($source) use ($connection) {
|
|||
|
$connection->close();
|
|||
|
};
|
|||
|
|
|||
|
$remote_connection->onBufferFull = function ($SourceConn) use ($connection) {
|
|||
|
$connection->pauseRecv();
|
|||
|
};
|
|||
|
|
|||
|
$remote_connection->onBufferDrain = function ($SourceConn) use ($connection) {
|
|||
|
$connection->resumeRecv();
|
|||
|
};
|
|||
|
|
|||
|
$connection->onMessage=function (TcpConnection $ClientConn, $ClientData)use ($remote_connection,$source_host){
|
|||
|
$remote_connection->send(replaceHost($ClientData,$source_host));
|
|||
|
};
|
|||
|
|
|||
|
$connection->onClose = function (TcpConnection $ClientConn) use ($remote_connection) {
|
|||
|
$remote_connection->close();
|
|||
|
};
|
|||
|
|
|||
|
$connection->onBufferFull = function (TcpConnection $ClientConn) use ($remote_connection) {
|
|||
|
$remote_connection->pauseRecv();
|
|||
|
};
|
|||
|
|
|||
|
$connection->onBufferDrain = function (TcpConnection $ClientConn) use ($remote_connection) {
|
|||
|
$remote_connection->resumeRecv();
|
|||
|
};
|
|||
|
|
|||
|
$remote_connection->connect();
|
|||
|
};
|
|||
|
|
|||
|
// 运行worker
|
|||
|
Worker::runAll();
|