1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved PHP web rcon

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

  1. Nathan 3

    Nathan 3 Shack Builder

    Hello,

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

    I have found the following -

    Code (Text):
    <?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...
     
  2. Reneb

    Reneb Plugin Developer

    this is to use rcon.web 0
    not rcon.web 1
     
  3. Nathan 3

    Nathan 3 Shack Builder

    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. Reneb

    Reneb Plugin Developer

    dunno, search for websocket for php, but it is way easier with js indeed
     
  5. Russo-turisto

    Russo-turisto Scavenger

    I use this lib:
    GitHub - Textalk/websocket-php: WebSocket client and server in PHP
    My code:
    Code (Text):
    <?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
     
    Nathan 3 likes this.
  6. Nathan 3

    Nathan 3 Shack Builder

    Awesome I will try and hack it together <3
     
  7. Bl4d3z

    Bl4d3z Naked Wanderer

    does this method still work?
     
  8. Kappasaurus

    Kappasaurus Plugin Developer

  9. Bl4d3z

    Bl4d3z Naked Wanderer

    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)
     
  10. Maestro

    Maestro Naked Wanderer

    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.
     
  11. InsomniaSimplyFrag

    InsomniaSimplyFrag Naked Wanderer

    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.