From 4e805e0b43c86ba77d6e1978aa41afc89cd7c3d1 Mon Sep 17 00:00:00 2001 From: walkor Date: Tue, 20 Sep 2016 21:29:42 +0800 Subject: [PATCH] dir change --- Applications/PassWall/start.php | 119 --------------------------- start.php | 141 ++++++++++++++++++++++++++------ 2 files changed, 114 insertions(+), 146 deletions(-) delete mode 100644 Applications/PassWall/start.php diff --git a/Applications/PassWall/start.php b/Applications/PassWall/start.php deleted file mode 100644 index b15d754..0000000 --- a/Applications/PassWall/start.php +++ /dev/null @@ -1,119 +0,0 @@ -onConnect = function($connection) -{ - $connection->stage = STAGE_INIT; -}; -$worker->onMessage = function($connection, $buffer) -{ - switch($connection->stage) - { - case STAGE_INIT: - $connection->send("\x05\x00"); - $connection->stage = STAGE_ADDR; - return; - case STAGE_ADDR: - $cmd = ord($buffer[1]); - if($cmd != CMD_CONNECT) - { - echo "bad cmd $cmd\n"; - $connection->close(); - return; - } - $header_data = parse_socket5_header($buffer); - if(!$header_data) - { - $connection->close(); - return; - } - $connection->stage = STAGE_CONNECTING; - $remote_connection = new AsyncTcpConnection('tcp://'.$header_data[1].':'.$header_data[2]); - $remote_connection->onConnect = function($remote_connection)use($connection) - { - $connection->state = STAGE_STREAM; - $connection->send("\x05\x00\x00\x01\x00\x00\x00\x00\x10\x10"); - $connection->pipe($remote_connection); - $remote_connection->pipe($connection); - }; - $remote_connection->connect(); - } -}; - - -function parse_socket5_header($buffer) -{ - $addr_type = ord($buffer[3]); - switch($addr_type) - { - case ADDRTYPE_IPV4: - if(strlen($buffer) < 10) - { - echo bin2hex($buffer)."\n"; - echo "buffer too short\n"; - return false; - } - $dest_addr = ord($buffer[4]).'.'.ord($buffer[5]).'.'.ord($buffer[6]).'.'.ord($buffer[7]); - $port_data = unpack('n', substr($buffer, -2)); - $dest_port = $port_data[1]; - $header_length = 10; - break; - case ADDRTYPE_HOST: - $addrlen = ord($buffer[4]); - if(strlen($buffer) < $addrlen + 5) - { - echo $buffer."\n"; - echo bin2hex($buffer)."\n"; - echo "buffer too short\n"; - return false; - } - $dest_addr = substr($buffer, 5, $addrlen); - $port_data = unpack('n', substr($buffer, -2)); - $dest_port = $port_data[1]; - $header_length = $addrlen + 7; - break; - case ADDRTYPE_IPV6: - if(strlen($buffer) < 22) - { - echo "buffer too short\n"; - return false; - } - echo "todo ipv6\n"; - return false; - default: - echo "unsupported addrtype $addr_type\n"; - return false; - } - return array($addr_type, $dest_addr, $dest_port, $header_length); -} - -// 如果不是在根目录启动,则运行runAll方法 -if(!defined('GLOBAL_START')) -{ - Worker::runAll(); -} diff --git a/start.php b/start.php index af92f44..7ddfb98 100644 --- a/start.php +++ b/start.php @@ -1,32 +1,119 @@ -onConnect = function($connection) { - require_once $start_file; + $connection->stage = STAGE_INIT; +}; +$worker->onMessage = function($connection, $buffer) +{ + switch($connection->stage) + { + case STAGE_INIT: + $connection->send("\x05\x00"); + $connection->stage = STAGE_ADDR; + return; + case STAGE_ADDR: + $cmd = ord($buffer[1]); + if($cmd != CMD_CONNECT) + { + echo "bad cmd $cmd\n"; + $connection->close(); + return; + } + $header_data = parse_socket5_header($buffer); + if(!$header_data) + { + $connection->close(); + return; + } + $connection->stage = STAGE_CONNECTING; + $remote_connection = new AsyncTcpConnection('tcp://'.$header_data[1].':'.$header_data[2]); + $remote_connection->onConnect = function($remote_connection)use($connection) + { + $connection->state = STAGE_STREAM; + $connection->send("\x05\x00\x00\x01\x00\x00\x00\x00\x10\x10"); + $connection->pipe($remote_connection); + $remote_connection->pipe($connection); + }; + $remote_connection->connect(); + } +}; + + +function parse_socket5_header($buffer) +{ + $addr_type = ord($buffer[3]); + switch($addr_type) + { + case ADDRTYPE_IPV4: + if(strlen($buffer) < 10) + { + echo bin2hex($buffer)."\n"; + echo "buffer too short\n"; + return false; + } + $dest_addr = ord($buffer[4]).'.'.ord($buffer[5]).'.'.ord($buffer[6]).'.'.ord($buffer[7]); + $port_data = unpack('n', substr($buffer, -2)); + $dest_port = $port_data[1]; + $header_length = 10; + break; + case ADDRTYPE_HOST: + $addrlen = ord($buffer[4]); + if(strlen($buffer) < $addrlen + 5) + { + echo $buffer."\n"; + echo bin2hex($buffer)."\n"; + echo "buffer too short\n"; + return false; + } + $dest_addr = substr($buffer, 5, $addrlen); + $port_data = unpack('n', substr($buffer, -2)); + $dest_port = $port_data[1]; + $header_length = $addrlen + 7; + break; + case ADDRTYPE_IPV6: + if(strlen($buffer) < 22) + { + echo "buffer too short\n"; + return false; + } + echo "todo ipv6\n"; + return false; + default: + echo "unsupported addrtype $addr_type\n"; + return false; + } + return array($addr_type, $dest_addr, $dest_port, $header_length); +} + +// 如果不是在根目录启动,则运行runAll方法 +if(!defined('GLOBAL_START')) +{ + Worker::runAll(); } -// 运行所有服务 -Worker::runAll(); \ No newline at end of file