Advertisement
Guest User

Untitled

a guest
May 6th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  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 Connect($server, $port, $rcon_password)
  22. {
  23. $error = 0;
  24. $errorstring = "";
  25. $this->stream = @fsockopen("tcp://$server", intval($port), $error, $errorstring);
  26. if ($error !== 0 || $errorstring !== "")
  27. throw new Exception("Could not connect to rust server: Error($error) $errorstring.");
  28. stream_set_timeout($this->stream, 1); // don't take too long for requests
  29. $auth = $this->internal_send(++$this->packet_id, 3, $rcon_password);
  30. $this->read(); $this->read(); // discard auth messages (throws error on rcon pass)
  31. }
  32.  
  33. public function Disconnect()
  34. {
  35. fclose($this->stream);
  36. }
  37.  
  38. public function Send($command)
  39. {
  40. $this->internal_send(++$this->packet_id, 2, $command);
  41. $response[] = $this->read();
  42. while ($response[count($response) - 1] != null) // read the shit out of the server
  43. $response[] = $this->read();
  44. return $response;
  45. }
  46.  
  47. private function internal_send($packet_id, $packet_type, $packet_body)
  48. {
  49. $request = pack('VV', $packet_id, $packet_type) . $packet_body . "\x00\x00";
  50. $request = Pack('V', strlen($request)) . $request;
  51.  
  52. if ($this->debug)
  53. {
  54. echo "> LEN " . strlen($request) . "\n" .
  55. "> ID $packet_id\n" .
  56. "> TYPE $packet_type\n" .
  57. "> BODY $packet_body\n\n\n";
  58. }
  59.  
  60. fwrite($this->stream, $request); // Give no shits about how much data was sent
  61. }
  62.  
  63. private function read()
  64. {
  65. // wait 100 cycles if feof (unlikely)
  66. $i = 0;
  67. while (feof($this->stream))
  68. {
  69. if (++$i > 100)
  70. return null; // nope.
  71. }
  72.  
  73. // try to read the packet size n times before aborting (n seconds)
  74. $szr = fread($this->stream, 4);
  75. $i = 0;
  76. while (strlen($szr) == 0)
  77. {
  78. if (++$i > $this->read_timeout)
  79. return null; // more nope.
  80. $szr = fread($this->stream, 4);
  81. }
  82.  
  83. // Let's do this
  84. $size = unpack('V', $szr);
  85. $id = unpack('V', fread($this->stream, 4));
  86. $type = unpack('V', fread($this->stream, 4));
  87. $body = fread($this->stream, $size[1] - 8);
  88.  
  89. if ($this->debug)
  90. {
  91. echo "< LEN ${size[1]}\n" .
  92. "< ID ${id[1]}" . ($id[1] == 0xffffffff ? ' (-1)' : '') . "\n" .
  93. "< TYPE ${type[1]}\n" .
  94. "< BODY " . $body . "\n\n\n";
  95. }
  96.  
  97. if ($id[1] == 0xffffffff)
  98. throw new Exception("Rcon password invalid.");
  99.  
  100. return $body;
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement