Advertisement
lonksys

FortressCraft Evolved Modified RCON PHP Class

Jul 2nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.44 KB | None | 0 0
  1. <?php
  2. /**
  3.  * See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for
  4.  * more information about Source RCON Packets
  5.  *
  6.  * PHP Version 7
  7.  *
  8.  * @copyright 2013-2017 Chris Churchwell
  9.  * @author thedudeguy
  10.  * @link https://github.com/thedudeguy/PHP-Minecraft-Rcon
  11.  */
  12.  
  13. class Rcon
  14. {
  15.     private $host;
  16.     private $port;
  17.     private $password;
  18.     private $timeout;
  19.  
  20.     private $socket;
  21.  
  22.     private $authorized = false;
  23.     private $lastResponse = '';
  24.  
  25.     const PACKET_AUTHORIZE = 1;
  26.     const PACKET_COMMAND = 1;
  27.  
  28.     const SERVERDATA_RESPONSE_VALUE = 0;
  29.     const SERVERDATA_AUTH_RESPONSE  = 2;
  30.     const SERVERDATA_EXECCOMMAND    = 2;
  31.     const SERVERDATA_AUTH           = 3;
  32.    
  33.    
  34.    
  35.    
  36.  
  37.     /**
  38.      * Create a new instance of the Rcon class.
  39.      *
  40.      * @param string $host
  41.      * @param integer $port
  42.      * @param string $password
  43.      * @param integer $timeout
  44.      */
  45.     public function __construct($host, $port, $password, $timeout)
  46.     {
  47.         $this->host = $host;
  48.         $this->port = $port;
  49.         $this->password = $password;
  50.         $this->timeout = $timeout;
  51.     }
  52.  
  53.     /**
  54.      * Get the latest response from the server.
  55.      *
  56.      * @return string
  57.      */
  58.     public function getResponse()
  59.     {
  60.         return $this->lastResponse;
  61.     }
  62.  
  63.     /**
  64.      * Connect to a server.
  65.      *
  66.      * @return boolean
  67.      */
  68.     public function connect()
  69.     {
  70.         $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
  71.  
  72.         if (!$this->socket) {
  73.             $this->lastResponse = $errstr;
  74.             return false;
  75.         }
  76.  
  77.         //set timeout
  78.         stream_set_timeout($this->socket, 3, 0);
  79.  
  80.         // check authorization
  81.         return $this->authorize();
  82.     }
  83.  
  84.     /**
  85.      * Disconnect from server.
  86.      *
  87.      * @return void
  88.      */
  89.     public function disconnect()
  90.     {
  91.         if ($this->socket) {
  92.             fclose($this->socket);
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * True if socket is connected and authorized.
  98.      *
  99.      * @return boolean
  100.      */
  101.     public function isConnected()
  102.     {
  103.         return $this->authorized;
  104.     }
  105.  
  106.     /**
  107.      * Send a command to the connected server.
  108.      *
  109.      * @param string $command
  110.      *
  111.      * @return boolean|mixed
  112.      */
  113.     public function sendCommand($command)
  114.     {
  115.         if (!$this->isConnected()) {
  116.                     return false;
  117.         }
  118.  
  119.         // send command packet
  120.         $this->writePacket(self::PACKET_COMMAND, self::SERVERDATA_EXECCOMMAND, $command);
  121.        
  122.         // get response
  123.         //echo __FUNCTION__."\n";
  124.         $response_packet = $this->readPacket();
  125.         if(is_string($response_packet))
  126.         {
  127.             if(preg_match("/Welcome, user ID ([0-9]{1,})/",$response_packet))
  128.             {
  129.                 $this->authorized = true;
  130.                  return true;
  131.             }
  132.             else
  133.             {
  134.                 $this->disconnect();
  135.                 return false;
  136.             }
  137.         }
  138.         else
  139.         {
  140.             $this->disconnect();
  141.             return false;  
  142.         }
  143.     }
  144.  
  145.     /**
  146.      * Log into the server with the given credentials.
  147.      *
  148.      * @return boolean
  149.      */
  150.     private function authorize()
  151.     {
  152.        
  153.         $this->writePacket(self::PACKET_AUTHORIZE, self::SERVERDATA_AUTH, $this->password);
  154.         //echo __FUNCTION__."\n";
  155.         $response_packet = $this->readPacket();
  156.         if(is_string($response_packet))
  157.         {
  158.             if(preg_match("/Welcome, user ID ([0-9]{1,})/",$response_packet))
  159.             {
  160.                 $this->authorized = true;
  161.                  return $response_packet;
  162.             }
  163.             else
  164.             {
  165.                 $this->disconnect();
  166.                 return false;
  167.             }
  168.         }
  169.         else
  170.         {
  171.             $this->disconnect();
  172.             return false;  
  173.         }
  174.     }
  175.  
  176.     /**
  177.      * Writes a packet to the socket stream.
  178.      *
  179.      * @param $packetId
  180.      * @param $packetType
  181.      * @param string $packetBody
  182.      *
  183.      * @return void
  184.      */
  185.     private function writePacket($packetId, $packetType, $packetBody)
  186.     {
  187.         /*
  188.         Size            32-bit little-endian Signed Integer     Varies, see below.
  189.         ID              32-bit little-endian Signed Integer     Varies, see below.
  190.         Type            32-bit little-endian Signed Integer     Varies, see below.
  191.         Body            Null-terminated ASCII String            Varies, see below.
  192.         Empty String    Null-terminated ASCII String            0x00
  193.         */
  194.        
  195.         //create packet
  196.         $packet = pack('VV', $packetId, $packetType);
  197.         $packet = $packet . $packetBody;
  198.         $packet = $packet . "\x00";
  199.        
  200.         // get packet size.
  201.         $packet_size = strlen($packet);
  202.  
  203.         // attach size to packet.
  204.         $packet = pack('V', $packet_size) . $packet;
  205.  
  206.         // write packet.
  207.         fwrite($this->socket, $packet, strlen($packet));
  208.     }
  209.  
  210.     /**
  211.      * Read a packet from the socket stream.
  212.      *
  213.      * @return array
  214.      */
  215.     private function readPacket()
  216.     {
  217.         //get packet size.
  218.         $size_data = fread($this->socket, 4096);
  219.         $size_pack = unpack('VVVa*', $size_data);
  220.         $size = $size_pack['VVa*'];
  221.        
  222.         if($size == 10)
  223.         {
  224.             $packet_data = fread($this->socket, $size);
  225.            
  226.             $packet_pack = unpack('VVa*', $packet_data);
  227.             if($packet_pack)
  228.             {
  229.                 $packet_data = fread($this->socket, $packet_pack['Va*']);
  230.             }
  231.            
  232.             return $packet_data;
  233.         }
  234.         else
  235.         {
  236.             $packet_data = fread($this->socket, $size);
  237.             return $packet_data;
  238.         }
  239.     }
  240. }
  241.  
  242.  
  243. $Rcon = new Rcon('10.31.173.123',27015,'password',10);
  244. echo "<pre>";
  245. $Rcon->connect();
  246. var_dump($Rcon->isConnected());
  247. var_dump($Rcon->sendCommand("help"));
  248. var_dump($Rcon->getResponse());
  249. echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement