Advertisement
Guest User

pmmp_proxy

a guest
Sep 18th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.83 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  *
  5.  *  ____            _        _   __  __ _                  __  __ ____
  6.  * |  _ \ ___   ___| | _____| |_|  \/  (_)_ __   ___      |  \/  |  _ \
  7.  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
  8.  * |  __/ (_) | (__|   <  __/ |_| |  | | | | | |  __/_____| |  | |  __/
  9.  * |_|   \___/ \___|_|\_\___|\__|_|  |_|_|_| |_|\___|     |_|  |_|_|
  10.  *
  11.  * This program is free software: you can redistribute it and/or modify
  12.  * it under the terms of the GNU Lesser General Public License as published by
  13.  * the Free Software Foundation, either version 3 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * @author PocketMine Team
  17.  * @link http://www.pocketmine.net/
  18.  *
  19.  *
  20. */
  21.  
  22. declare(strict_types=1);
  23.  
  24. $bindAddr = "0.0.0.0";
  25. $bindPort = 19132;
  26.  
  27.  
  28. echo "Enter server address: ";
  29. $serverAddress = gethostbyname(trim(fgets(STDIN)));
  30. echo "Enter server port: ";
  31. $serverPort = (int) trim(fgets(STDIN));
  32. if($serverPort !== 19132){
  33.     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;
  34. }
  35.  
  36. echo "Opening socket... ";
  37. $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  38. if(!socket_bind($sock, $bindAddr, $bindPort)){
  39.     echo "[!] Can't bind to $bindAddr on port $bindPort, is something already using that port?" . PHP_EOL;
  40.     exit(1);
  41. }
  42. socket_set_option($sock, SOL_SOCKET, SO_SNDBUF, 1024 * 1024 * 8);
  43. socket_set_option($sock, SOL_SOCKET, SO_RCVBUF, 1024 * 1024 * 8);
  44.  
  45. $clientAddr = $clientPort = null;
  46. while(true){
  47.     echo "Waiting for client ping..." . PHP_EOL;
  48.     $len = socket_recvfrom($sock, $buffer, 65535, 0, $recvAddr, $recvPort);
  49.     if($buffer{0} === "\x01"){ //ID_UNCONNECTED_PING
  50.         $pingAddr = $recvAddr;
  51.         $pingPort = $recvPort;
  52.  
  53.         echo "Got ping from $recvAddr on port $recvPort, pinging server" . PHP_EOL;
  54.         socket_sendto($sock, $buffer, strlen($buffer), 0, $serverAddress, $serverPort);
  55.  
  56.         socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, ["sec" => 5, "usec" => 0]);
  57.         $len = socket_recvfrom($sock, $buffer, 65535, 0, $recvAddr, $recvPort);
  58.         socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, ["sec" => 0, "usec" => 0]);
  59.  
  60.         if($buffer{0} === "\x1c" and $recvAddr === $serverAddress and $recvPort === $serverPort){ //ID_UNCONNECTED_PONG
  61.             echo "Got ping response from server, sending to client" . PHP_EOL;
  62.             socket_sendto($sock, $buffer, strlen($buffer), 0, $pingAddr, $pingPort);
  63.         }
  64.     }elseif($buffer{0} === "\x05"){ //OpenConnectionRequest1
  65.         $clientAddr = $recvAddr;
  66.         $clientPort = $recvPort;
  67.         echo "Got connection from $recvAddr on port $recvPort!" . PHP_EOL;
  68.         break;
  69.     }
  70. }
  71.  
  72. if($clientAddr === null or $clientPort === null){
  73.     die("WTF! no client address!!!!");
  74. }
  75.  
  76. echo "Packets from $clientAddr on port $clientPort address are now being relayed to $serverAddress on port $serverPort" . PHP_EOL;
  77. echo "Press CTRL+C to stop the proxy." . PHP_EOL;
  78.  
  79. socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, ["sec" => 10, "usec" => 0]);
  80.  
  81. while(true){
  82.     $status = @socket_recvfrom($sock, $buffer, 65535, 0, $source, $port);
  83.     if($status !== false){
  84.         //echo "Got packet from $source $port: " . bin2hex($buffer) . PHP_EOL;
  85.         if($source === $serverAddress and $port === $serverPort){
  86.             //echo "Got packet from server: " . bin2hex($buffer) . PHP_EOL;
  87.             socket_sendto($sock, $buffer, strlen($buffer), 0, $clientAddr, $clientPort);
  88.         }elseif($source === $clientAddr and $port === $clientPort){
  89.             //echo "Got packet from client: " . bin2hex($buffer) . PHP_EOL;
  90.             socket_sendto($sock, $buffer, strlen($buffer), 0, $serverAddress, $serverPort);
  91.         }else{
  92.             //echo "Ignored packet from $source $port" . PHP_EOL;
  93.             continue;
  94.         }
  95.     }elseif(socket_last_error($sock) === SOCKET_ETIMEDOUT){
  96.         echo "No communications received from server or client within 10 seconds. Exiting." . PHP_EOL;
  97.         break;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement