Gachl

Untitled

Apr 14th, 2014
1,422
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?PHP
  2. /**
  3.  * Created by Gachl
  4.  * admin@bloodisgood.org
  5.  * Please give credit where credit belongs.
  6.  *
  7.  * Resources used:
  8.  * * http://php.net/pack
  9.  * * https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
  10.  * * My own Rust server
  11.  *
  12.  * License: http://creativecommons.org/licenses/by-nc-sa/4.0/
  13.  */
  14. class RustRcon
  15. {
  16.     private $debug = false; // activate this flag if you want to see verbose communication
  17.     private $read_timeout = 2; // 2 seconds read timeout
  18.     private $packet_id = 0;
  19.     private $stream;
  20.  
  21.     public function __construct($server, $port, $rcon_password)
  22.     {
  23.         $this->connect($server, $port, $rcon_password);
  24.     }
  25.  
  26.     private function connect($server, $port, $rcon_password)
  27.     {
  28.         $error = 0;
  29.         $errorstring = "";
  30.  
  31.         $this->stream = @fsockopen("tcp://$server", intval($port), $error, $errorstring);
  32.  
  33.         if ($error !== 0 || $errorstring !== "")
  34.             throw new Exception("Could not connect to rust server: Error($error) $errorstring.");
  35.  
  36.         stream_set_timeout($this->stream, 1); // don't take too long for requests
  37.         $auth = $this->transmit($rcon_password, 3); // 3 = Auth flag
  38.     }
  39.  
  40.     public function __destruct()
  41.     {
  42.         $this->Disconnect();
  43.     }
  44.  
  45.     public function Disconnect()
  46.     {
  47.         fclose($this->stream);
  48.     }
  49.  
  50.     public function Send($command)
  51.     {
  52.         return $this->transmit($command);
  53.     }
  54.  
  55.     private function transmit($packet_body, $packet_type = 2)
  56.     {
  57.         $packet_id = ++$this->packet_id;
  58.         $request = pack('VV', $packet_id, $packet_type) . $packet_body . "\x00\x00";
  59.         $request = Pack('V', strlen($request)) . $request;
  60.  
  61.         if ($this->debug)
  62.         {
  63.             echo "> LEN " . strlen($request) . "\n" .
  64.                      "> ID $packet_id\n" .
  65.                      "> TYPE $packet_type\n" .
  66.                      "> BODY $packet_body\n\n\n";
  67.         }
  68.        
  69.         fwrite($this->stream, $request); // Give no shits about how much data was sent
  70.         return $packet_id;
  71.     }
  72.  
  73.     public function Read()
  74.     {
  75.         if (feof($this->stream))
  76.             return null;
  77.        
  78.         // Let's do this
  79.         $size = fread($this->stream, 4);
  80.         if (strlen($size) !== 4)
  81.             return null; // Invalid!
  82.  
  83.         $size = unpack('V', $size);
  84.         $id = unpack('V', fread($this->stream, 4));
  85.         $type = unpack('V', fread($this->stream, 4));
  86.         $body = fread($this->stream, $size[1] - 8);
  87.  
  88.         if ($this->debug)
  89.         {
  90.             echo "< LEN ${size[1]}\n" .
  91.                      "< ID ${id[1]}" . ($id[1] == 0xffffffff ? ' (-1)' : '') . "\n" .
  92.                      "< TYPE ${type[1]}\n" .
  93.                      "< BODY " . $body . "\n\n\n";
  94.         }
  95.  
  96.         if ($id[1] == 0xffffffff)
  97.             throw new Exception("Rcon password invalid.");
  98.  
  99.         return new RustRconResponse($id[1], $body);
  100.     }
  101. }
  102.  
  103. class RustRconResponse
  104. {
  105.     private $id = -1;
  106.     private $body = null;
  107.  
  108.     public function __construct($id, $body)
  109.     {
  110.         $this->id = intval($id);
  111.         $this->body = trim($body);
  112.     }
  113.  
  114.     public function ID()
  115.     {
  116.         return $this->id;
  117.     }
  118.  
  119.     public function Response()
  120.     {
  121.         return $this->body;
  122.     }
  123. }
RAW Paste Data