Solved PHP web rcon

Discussion in 'Rust Development' started by Nathan 3, Jan 8, 2017.

  1. Hello,

    Has anyone manage to get a PHP script to work with the web RCON?

    I have found the following -

    Code:
    <?php
    class RustRcon {
            protected $socket;
            protected $packetId = 0;
            public static $timeout = 1;
            public static $blockingMode = 0;
            const PACKET_TYPE_CMD = 2;
            const PACKET_TYPE_AUTH = 3;
            const PACKET_TYPE_CMD_RESPONSE = 0;
            const PACKET_TYPE_MESSAGE = 4;
            public function __construct($server, $port, $password)
            {
                    $this->connect($server, $port, $password);
            }
            protected function connect($server, $port, $password)
            {
                    $this->socket = @fsockopen("tcp://" . $server, $port, $error, $errorstring);
                    if ($error !== 0 || !empty($errorstring)) {
                            throw new Exception("Connection failed (" . $error . "): " . $errorstring);
                    }
                    stream_set_timeout($this->socket, static::$timeout);
                    stream_set_blocking($this->socket, static::$blockingMode ? 1 : 0);
                    $this->send($password, self::PACKET_TYPE_AUTH);
            }
            public function disconnect()
            {
                    fclose($this->socket);
            }
            public function send($message, $type = self::PACKET_TYPE_CMD)
            {
                    $id = ++$this->packetId;
                    $request = pack('VV', $id, $type) . $message . "\x00\x00";
                    $request = pack('V', strlen($request)) . $request;
                    fwrite($this->socket, $request);
                    return $id;
            }
            /**
             * @return null|string Null if no or empty response received, a string if a response was received.
             * @throws Exception when the socket terminates or something goes wrong.
             */
            public function read()
            {
                    if (feof($this->socket)) {
                            throw new Exception('Reached unexpected end of socket.');
                    }
                    // Let's do this
                    $size = fread($this->socket, 4);
                    if (strlen($size) !== 4) {
                            return null;
                    }
                    $size = unpack('V', $size);
                    $id = unpack('V', fread($this->socket, 4))[1];
                    $type = unpack('V', fread($this->socket, 4))[1];
                    $remaining = $size[1] - 8;
                    $body = '';
                    while ($remaining > 0) {
                            $response = fread($this->socket, $remaining);
                            $size = strlen($response);
                            if ($size === 0) {
                                    throw new Exception('Received 0 byte response, expected: ' . $remaining);
                            }
                            $body .= $response;
                            $remaining = $remaining - strlen($body);
                    }
                    $body = trim($body);
                    if (empty($body)) {
                            return null;
                    }
                    if ($type === self::PACKET_TYPE_CMD_RESPONSE) {
                            // We don't want responses as we will receive these anyway through the normal stream.
                            return null;
                    }
                    return $body;
            }
    }
    $ip = 'xxx.xxx.xxx.xxx';
    $port = 28020;
    $password = 'xxxxxxxx';
    respond('Connecting to ' . $ip . ':' . $port . '...');
    $rcon = new RustRcon($ip, $port, $password);
    respond('Connected!');$rcon->disconnect();
    function respond($str)
    {
            echo date('Y-m-d H:i:s') . substr((string)microtime(), 1, 4) . ': ' . $str . PHP_EOL;
    }
    We are trying to figure out how to send basic console commands but getting the following error...
     

    Attached Files:

  2. this is to use rcon.web 0
    not rcon.web 1
     
  3. Oh... derp, Is there a PHP method available for web.rcon because I read standard RCON will be removed soon. I have the web rcon js client but I need it to automate purchases.
     
  4. dunno, search for websocket for php, but it is way easier with js indeed
     
  5. I use this lib:
    GitHub - Textalk/websocket-php: WebSocket client and server in PHP
    My code:
    Code:
    <?php
    header('Content-Type: text/plain; charset=utf-8');require_once __DIR__ . '/lib/Base.php';
    require_once __DIR__ . '/lib/Exception.php';
    require_once __DIR__ . '/lib/BadOpcodeException.php';
    require_once __DIR__ . '/lib/BadUriException.php';
    require_once __DIR__ . '/lib/ConnectionException.php';
    require_once __DIR__ . '/lib/Server.php';
    require_once __DIR__ . '/lib/Client.php';use WebSocket\Client;$ip = "1.2.4.5"; // server IP-address
    $rcon_port = "28016"; // server RCON-port
    $rcon_pass = "123456"; // server RCON-password$client = new Client("ws://{$ip}:{$rcon_port}/{$rcon_pass}");
    $data = array(
        'Identifier' => 0,
        'Message' => 'status', // command
        'Stacktrace' => '',
        'Type' => 3
    );$client->send(json_encode($data));
    $result = json_decode($client->receive());echo $result->Message;
    That if enable rcon.web
     
  6. Awesome I will try and hack it together <3
     
  7. does this method still work?
     
  8. I had an idea of making a PHP handler that piggy backs off of this. (trying to make it work with my web store and remote ban system again - SFPortal)
     
  9. I am working on the one from facepunch GitHub - maestroi/Simpel-rcon: Webrcon to make it with more features, i could make it with server site and proxy all commands to make it a bit more secure.
     
  10. We could really use a serverside version of this for security when using web applications else you can parse the rcon password on command execution :( - if used in a management system.
     
  11. @Russo-turisto Oof I've been looking so long for that project, thanks!