Simple php rcon class

Hello guys

I looked on the forum but I have not seen php code to allow a controlled rust remote server, maybe I just have not seen.
Here is a small code that will give you the ability to send full control distance.
translated by Google transtlate, sorry

Rcon class (rcon.php):



<?php

class RustRcon {

    private $password;
    private $_sock = null;
    private $_id = 0;
    private $isfsock = true;

    const SERVERDATA_EXECCOMMAND = 02;
    const SERVERDATA_AUTH = 03;
    const SERVERDATA_RESPONSE_VALUE = 00;
    const SERVERDATA_AUTH_RESPONSE = 02;

    function RustRcon($address, $port, $password) {
        $this->password = $password;

        try {
            if (defined('BIND_IP') && function_exists('socket_create') && function_exists('socket_bind')) {
                $this->isfsock = false;
                $this->_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

                socket_set_option($this->_sock, SOL_SOCKET, SO_REUSEADDR, 1);
                socket_bind($this->_sock, BIND_IP);

                socket_connect($this->_sock, $address, $port);

                socket_set_option($this->_sock, SOL_SOCKET, SO_SNDTIMEO, array("sec" => 2, "usec" => 0));
                socket_set_option($this->_sock, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 2, "usec" => 0));
            } else {
                $this->_sock = @fsockopen($address, $port, $errno, $errstr, 2);
                stream_set_timeout($this->_sock, 2);
            }
        } catch (Exception $err) {
          
        }
    }

    public function Auth() {
        $PackID = $this->_Write(RustRcon::SERVERDATA_AUTH, $this->password);
        $ret = $this->_PacketRead();

        return (isset($ret[1]['ID']) && $ret[1]['ID'] == -1) ? 0 : 1;
    }

    private function _Write($cmd, $s1 = '', $s2 = '') {
        $id = ++$this->_id;
        $data = pack("VV", $id, $cmd) . $s1 . chr(0) . $s2 . chr(0);
        $data = pack("V", strlen($data)) . $data;

        if ($this->isfsock)
            fwrite($this->_sock, $data, strlen($data));
        else
            socket_write($this->_sock, $data, strlen($data));

        return $id;
    }

    private function _sock_read($size) {
        if ($this->isfsock)
            return @fread($this->_sock, $size);
        else
            return socket_read($this->_sock, $size);
    }

    private function _PacketRead() {
        $retarray = array();

        while ($size = $this->_sock_read(4)) {
            $size = unpack('V1Size', $size);

            if ($size["Size"] > 4096)
                $packet = "\x00\x00\x00\x00\x00\x00\x00\x00" . $this->_sock_read(4096);
            else
                $packet = $this->_sock_read($size["Size"]);

            array_push($retarray, unpack("V1ID/V1Reponse/a*S1/a*S2", $packet));
        }

        return $retarray;
    }

    public function Read() {
        $Packets = $this->_PacketRead();

        foreach ($Packets as $pack) {
            if (isset($ret[$pack['ID']])) {
                $ret[$pack['ID']]['S1'] .= $pack['S1'];
                $ret[$pack['ID']]['S2'] .= $pack['S1'];
            } else {
                $ret[$pack['ID']] = array('Reponse' => $pack['Reponse'],
                    'S1' => $pack['S1'],
                    'S2' => $pack['S2'],);
            }
        }

        return $ret;
    }

    public function sendCommand($command) {
        $this->_Write(RustRcon::SERVERDATA_EXECCOMMAND, $command, '');
    }

    public function rconCommand($command) {
        $this->sendCommand($command);
        $ret = $this->Read();
        return $ret[2]['S1'];
    }

}

?>


Simple example (example.php):



<?php 
include "rcon.php";

$server1 = new RustRcon('188.222.xx.xxx', '28200', 'password1');
$server2 = new RustRcon('188.222.xx.xxx', '28245', 'password2');


if ($server1->Auth()) 
{
    $return = $server1->rconCommand("status");
    echo "<pre>$return</pre>";
}

if ($server2->Auth()) 
{
    $return = $server2->rconCommand("status");
    echo "<pre>$return</pre>";
}

if ($server1->Auth()) 
{
    $server1->rconCommand("say Hello welcome to serveur 1, this message was send from my website");
}

if ($server2->Auth()) 
{
    $server2->rconCommand("say Hello welcome to serveur 2, this message was send from my website");
}

?>


if you need help, or you have web project and need help you can contact me at:
email: majid81200@orange.fr
steam: usmba_22

Thank’s