boleknowak

Untitled

Dec 30th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 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. * @copyright 2013 Chris Churchwell
  7. */
  8. class Rcon {
  9.  
  10. private $host;
  11. private $port;
  12. private $password;
  13. private $timeout;
  14.  
  15. private $socket;
  16.  
  17. private $authorized;
  18. private $last_response;
  19.  
  20. const PACKET_AUTHORIZE = 5;
  21. const PACKET_COMMAND = 6;
  22.  
  23. const SERVERDATA_AUTH = 3;
  24. const SERVERDATA_AUTH_RESPONSE = 2;
  25. const SERVERDATA_EXECCOMMAND = 2;
  26. const SERVERDATA_RESPONSE_VALUE = 0;
  27.  
  28. public function __construct($host, $port, $password, $timeout)
  29. {
  30. $this->host = $host;
  31. $this->port = $port;
  32. $this->password = $password;
  33. $this->timeout = $timeout;
  34.  
  35. }
  36.  
  37. public function get_response() {
  38. return $this->last_response;
  39. }
  40.  
  41. public function connect() {
  42.  
  43. $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
  44.  
  45. if (!$this->socket)
  46. {
  47. $this->last_response = $errstr;
  48. return false;
  49. }
  50.  
  51. //set timeout
  52. stream_set_timeout($this->socket, 3, 0);
  53.  
  54. //authorize
  55. $auth = $this->authorize();
  56.  
  57. if ($auth) {
  58. return true;
  59. }
  60.  
  61. return false;
  62. }
  63.  
  64. public function disconnect()
  65. {
  66. if ($this->socket)
  67. {
  68. fclose($this->socket);
  69. }
  70. }
  71.  
  72. public function is_connected() {
  73. return $this->authorized;
  74. }
  75.  
  76. public function send_command($command)
  77. {
  78. if (!$this->is_connected()) return false;
  79.  
  80. // send command packet.
  81. $this->write_packet(Rcon::PACKET_COMMAND, Rcon::SERVERDATA_EXECCOMMAND, $command);
  82.  
  83. // get response.
  84. $response_packet = $this->read_packet();
  85. if ($response_packet['id'] == Rcon::PACKET_COMMAND)
  86. {
  87. if ($response_packet['type'] == Rcon::SERVERDATA_RESPONSE_VALUE)
  88. {
  89. $this->last_response = $response_packet['body'];
  90. return $response_packet['body'];
  91. }
  92. }
  93.  
  94. return false;
  95. }
  96.  
  97. private function authorize() {
  98. $this->write_packet(Rcon::PACKET_AUTHORIZE, Rcon::SERVERDATA_AUTH, $this->password);
  99. $response_packet = $this->read_packet();
  100.  
  101. if ($response_packet['type'] == Rcon::SERVERDATA_AUTH_RESPONSE)
  102. {
  103. if ($response_packet['id'] == Rcon::PACKET_AUTHORIZE)
  104. {
  105. $this->authorized = true;
  106. return true;
  107. }
  108. }
  109.  
  110. $this->disconnect();
  111. return false;
  112. }
  113.  
  114. /**
  115. * Writes a packet to the socket stream..
  116. */
  117. private function write_packet($packet_id, $packet_type, $packet_body)
  118. {
  119. /*
  120. Size 32-bit little-endian Signed Integer Varies, see below.
  121. ID 32-bit little-endian Signed Integer Varies, see below.
  122. Type 32-bit little-endian Signed Integer Varies, see below.
  123. Body Null-terminated ASCII String Varies, see below.
  124. Empty String Null-terminated ASCII String 0x00
  125. */
  126.  
  127. //create packet
  128. $packet = pack("VV", $packet_id, $packet_type);
  129. $packet = $packet . $packet_body . "\x00";
  130. $packet = $packet . "\x00";
  131.  
  132. // get packet size.
  133. $packet_size = strlen($packet);
  134.  
  135. // attach size to packet.
  136. $packet = pack("V", $packet_size) . $packet;
  137.  
  138. // write packet.
  139. fwrite($this->socket, $packet, strlen($packet));
  140.  
  141. }
  142.  
  143. private function read_packet()
  144. {
  145. //get packet size.
  146. $size_data = fread($this->socket, 4);
  147. $size_pack = unpack("V1size", $size_data);
  148. $size = $size_pack['size'];
  149.  
  150. // if size is > 4096, the response will be in multiple packets.
  151. // this needs to be address. get more info about multi-packet responses
  152. // from the RCON protocol specification at
  153. // https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
  154. // currently, this script does not support multi-packet responses.
  155.  
  156. $packet_data = fread($this->socket, $size);
  157. $packet_pack = unpack("V1id/V1type/a*body", $packet_data);
  158.  
  159. return $packet_pack;
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment