Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- /**
- * Created by Gachl
- * admin@bloodisgood.org
- * Please give credit where credit belongs.
- *
- * Resources used:
- * * http://php.net/pack
- * * https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
- * * My own Rust server
- *
- * License: http://creativecommons.org/licenses/by-nc-sa/4.0/
- */
- class RustRcon
- {
- private $debug = false; // activate this flag if you want to see verbose communication
- private $read_timeout = 2; // 2 seconds read timeout
- private $packet_id = 0;
- private $stream;
- public function __construct($server, $port, $rcon_password)
- {
- $this->connect($server, $port, $rcon_password);
- }
- private function connect($server, $port, $rcon_password)
- {
- $error = 0;
- $errorstring = "";
- $this->stream = @fsockopen("tcp://$server", intval($port), $error, $errorstring);
- if ($error !== 0 || $errorstring !== "")
- throw new Exception("Could not connect to rust server: Error($error) $errorstring.");
- stream_set_timeout($this->stream, 1); // don't take too long for requests
- $auth = $this->transmit($rcon_password, 3); // 3 = Auth flag
- }
- public function __destruct()
- {
- $this->Disconnect();
- }
- public function Disconnect()
- {
- fclose($this->stream);
- }
- public function Send($command)
- {
- return $this->transmit($command);
- }
- private function transmit($packet_body, $packet_type = 2)
- {
- $packet_id = ++$this->packet_id;
- $request = pack('VV', $packet_id, $packet_type) . $packet_body . "\x00\x00";
- $request = Pack('V', strlen($request)) . $request;
- if ($this->debug)
- {
- echo "> LEN " . strlen($request) . "\n" .
- "> ID $packet_id\n" .
- "> TYPE $packet_type\n" .
- "> BODY $packet_body\n\n\n";
- }
- fwrite($this->stream, $request); // Give no shits about how much data was sent
- return $packet_id;
- }
- public function Read()
- {
- if (feof($this->stream))
- return null;
- // Let's do this
- $size = fread($this->stream, 4);
- if (strlen($size) !== 4)
- return null; // Invalid!
- $size = unpack('V', $size);
- $id = unpack('V', fread($this->stream, 4));
- $type = unpack('V', fread($this->stream, 4));
- $body = fread($this->stream, $size[1] - 8);
- if ($this->debug)
- {
- echo "< LEN ${size[1]}\n" .
- "< ID ${id[1]}" . ($id[1] == 0xffffffff ? ' (-1)' : '') . "\n" .
- "< TYPE ${type[1]}\n" .
- "< BODY " . $body . "\n\n\n";
- }
- if ($id[1] == 0xffffffff)
- throw new Exception("Rcon password invalid.");
- return new RustRconResponse($id[1], $body);
- }
- }
- class RustRconResponse
- {
- private $id = -1;
- private $body = null;
- public function __construct($id, $body)
- {
- $this->id = intval($id);
- $this->body = trim($body);
- }
- public function ID()
- {
- return $this->id;
- }
- public function Response()
- {
- return $this->body;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement