Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for
- * more information about Source RCON Packets
- *
- * PHP Version 7
- *
- * @copyright 2013-2017 Chris Churchwell
- * @author thedudeguy
- * @link https://github.com/thedudeguy/PHP-Minecraft-Rcon
- */
- class Rcon
- {
- private $host;
- private $port;
- private $password;
- private $timeout;
- private $socket;
- private $authorized = false;
- private $lastResponse = '';
- const PACKET_AUTHORIZE = 1;
- const PACKET_COMMAND = 1;
- const SERVERDATA_RESPONSE_VALUE = 0;
- const SERVERDATA_AUTH_RESPONSE = 2;
- const SERVERDATA_EXECCOMMAND = 2;
- const SERVERDATA_AUTH = 3;
- /**
- * Create a new instance of the Rcon class.
- *
- * @param string $host
- * @param integer $port
- * @param string $password
- * @param integer $timeout
- */
- public function __construct($host, $port, $password, $timeout)
- {
- $this->host = $host;
- $this->port = $port;
- $this->password = $password;
- $this->timeout = $timeout;
- }
- /**
- * Get the latest response from the server.
- *
- * @return string
- */
- public function getResponse()
- {
- return $this->lastResponse;
- }
- /**
- * Connect to a server.
- *
- * @return boolean
- */
- public function connect()
- {
- $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
- if (!$this->socket) {
- $this->lastResponse = $errstr;
- return false;
- }
- //set timeout
- stream_set_timeout($this->socket, 3, 0);
- // check authorization
- return $this->authorize();
- }
- /**
- * Disconnect from server.
- *
- * @return void
- */
- public function disconnect()
- {
- if ($this->socket) {
- fclose($this->socket);
- }
- }
- /**
- * True if socket is connected and authorized.
- *
- * @return boolean
- */
- public function isConnected()
- {
- return $this->authorized;
- }
- /**
- * Send a command to the connected server.
- *
- * @param string $command
- *
- * @return boolean|mixed
- */
- public function sendCommand($command)
- {
- if (!$this->isConnected()) {
- return false;
- }
- // send command packet
- $this->writePacket(self::PACKET_COMMAND, self::SERVERDATA_EXECCOMMAND, $command);
- // get response
- //echo __FUNCTION__."\n";
- $response_packet = $this->readPacket();
- if(is_string($response_packet))
- {
- if(preg_match("/Welcome, user ID ([0-9]{1,})/",$response_packet))
- {
- $this->authorized = true;
- return true;
- }
- else
- {
- $this->disconnect();
- return false;
- }
- }
- else
- {
- $this->disconnect();
- return false;
- }
- }
- /**
- * Log into the server with the given credentials.
- *
- * @return boolean
- */
- private function authorize()
- {
- $this->writePacket(self::PACKET_AUTHORIZE, self::SERVERDATA_AUTH, $this->password);
- //echo __FUNCTION__."\n";
- $response_packet = $this->readPacket();
- if(is_string($response_packet))
- {
- if(preg_match("/Welcome, user ID ([0-9]{1,})/",$response_packet))
- {
- $this->authorized = true;
- return $response_packet;
- }
- else
- {
- $this->disconnect();
- return false;
- }
- }
- else
- {
- $this->disconnect();
- return false;
- }
- }
- /**
- * Writes a packet to the socket stream.
- *
- * @param $packetId
- * @param $packetType
- * @param string $packetBody
- *
- * @return void
- */
- private function writePacket($packetId, $packetType, $packetBody)
- {
- /*
- Size 32-bit little-endian Signed Integer Varies, see below.
- ID 32-bit little-endian Signed Integer Varies, see below.
- Type 32-bit little-endian Signed Integer Varies, see below.
- Body Null-terminated ASCII String Varies, see below.
- Empty String Null-terminated ASCII String 0x00
- */
- //create packet
- $packet = pack('VV', $packetId, $packetType);
- $packet = $packet . $packetBody;
- $packet = $packet . "\x00";
- // get packet size.
- $packet_size = strlen($packet);
- // attach size to packet.
- $packet = pack('V', $packet_size) . $packet;
- // write packet.
- fwrite($this->socket, $packet, strlen($packet));
- }
- /**
- * Read a packet from the socket stream.
- *
- * @return array
- */
- private function readPacket()
- {
- //get packet size.
- $size_data = fread($this->socket, 4096);
- $size_pack = unpack('VVVa*', $size_data);
- $size = $size_pack['VVa*'];
- if($size == 10)
- {
- $packet_data = fread($this->socket, $size);
- $packet_pack = unpack('VVa*', $packet_data);
- if($packet_pack)
- {
- $packet_data = fread($this->socket, $packet_pack['Va*']);
- }
- return $packet_data;
- }
- else
- {
- $packet_data = fread($this->socket, $size);
- return $packet_data;
- }
- }
- }
- $Rcon = new Rcon('10.31.173.123',27015,'password',10);
- echo "<pre>";
- $Rcon->connect();
- var_dump($Rcon->isConnected());
- var_dump($Rcon->sendCommand("help"));
- var_dump($Rcon->getResponse());
- echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement