Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- *
- * ____ _ _ __ __ _ __ __ ____
- * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
- * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
- * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
- * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * @author PocketMine Team
- * @link http://www.pocketmine.net/
- *
- *
- */
- declare(strict_types=1);
- $bindAddr = "0.0.0.0";
- $bindPort = 19132;
- echo "Enter server address: ";
- $serverAddress = gethostbyname(trim(fgets(STDIN)));
- echo "Enter server port: ";
- $serverPort = (int) trim(fgets(STDIN));
- if($serverPort !== 19132){
- echo "Warning: You may experience problems connecting to PocketMine-MP servers on ports other than 19132 if the server has port checking enabled." . PHP_EOL;
- }
- echo "Opening socket... ";
- $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
- if(!socket_bind($sock, $bindAddr, $bindPort)){
- echo "[!] Can't bind to $bindAddr on port $bindPort, is something already using that port?" . PHP_EOL;
- exit(1);
- }
- socket_set_option($sock, SOL_SOCKET, SO_SNDBUF, 1024 * 1024 * 8);
- socket_set_option($sock, SOL_SOCKET, SO_RCVBUF, 1024 * 1024 * 8);
- echo "Sending ping to server on $serverAddress $serverPort... ";
- $ping = "\x01" . pack("NN", mt_rand(0, 0x7fffffff), mt_rand(0, 0x7fffffff)) . "\x00\xff\xff\x00\xfe\xfe\xfe\xfe\xfd\xfd\xfd\xfd\x12\x34\x56\x78";
- socket_sendto($sock, $ping, strlen($ping), 0, $serverAddress, $serverPort);
- $pong = "";
- echo "Waiting for communication... ";
- while(true){
- $len = socket_recvfrom($sock, $pong, 65535, 0, $addr, $port);
- if($addr === $serverAddress and $port === $serverPort and $pong{0} === "\x1c"){
- echo "Got ping response from server!" . PHP_EOL;
- break;
- }
- }
- echo "Waiting for client ping..." . PHP_EOL;
- $len = socket_recvfrom($sock, $buffer, 65535, 0, $clientAddr, $clientPort);
- echo "Got ping from $clientAddr on port $clientPort!" . PHP_EOL;
- socket_sendto($sock, $pong, strlen($pong), 0, $clientAddr, $clientPort);
- echo "Waiting for client connection..." . PHP_EOL;
- while(true){
- $len = socket_recvfrom($sock, $buffer, 65535, 0, $clientAddr, $clientPort);
- if($buffer{0} === "\x05"){ //OpenConnectionRequest1
- echo "Got connection from $clientAddr on port $clientPort!" . PHP_EOL;
- break;
- }
- // FIXME: We aren't relaying this connection request. Not really a problem since RakNet will resend it with the same MTU a few times, but shouldn't we relay this anyway?
- }
- echo "Packets from this address are now being relayed to $serverAddress on port $serverPort" . PHP_EOL;
- echo "Press CTRL+C to stop the proxy." . PHP_EOL;
- socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, ["sec" => 10, "usec" => 0]);
- while(true){
- $status = @socket_recvfrom($sock, $buffer, 65535, 0, $source, $port);
- if($status !== false){
- //echo "Got packet from $source $port: " . bin2hex($buffer) . PHP_EOL;
- if($source === $serverAddress and $port === $serverPort){
- //echo "Got packet from server: " . bin2hex($buffer) . PHP_EOL;
- socket_sendto($sock, $buffer, strlen($buffer), 0, $clientAddr, $clientPort);
- }elseif($source === $clientAddr and $port === $clientPort){
- //echo "Got packet from client: " . bin2hex($buffer) . PHP_EOL;
- socket_sendto($sock, $buffer, strlen($buffer), 0, $serverAddress, $serverPort);
- }else{
- //echo "Ignored packet from $source $port" . PHP_EOL;
- continue;
- }
- }elseif(socket_last_error($sock) === SOCKET_ETIMEDOUT){
- echo "No communications received from server or client within 10 seconds. Exiting." . PHP_EOL;
- break;
- }
- }