From e3fa53613d01760a8d5ced565e301d4584f9fcb4 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 6 Mar 2023 08:13:16 +0300 Subject: [PATCH] rcon update from https://github.com/pmmp/RconServer --- src/network/RCON.php | 178 ++++++++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 86 deletions(-) diff --git a/src/network/RCON.php b/src/network/RCON.php index 15a08707..177c28b6 100644 --- a/src/network/RCON.php +++ b/src/network/RCON.php @@ -6,8 +6,12 @@ Source: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol */ class RCON{ - - private $socket, $password, $workers, $threads, $clientsPerThread; + /** + * @var Array[RCONInstance] + */ + private $workers; + + private $socket, $password, $threads, $clientsPerThread; public function __construct($password, $port = 19132, $interface = "0.0.0.0", $threads = 1, $clientsPerThread = 50){ $this->workers = []; @@ -45,18 +49,16 @@ class RCON{ } public function check(){ - for($n = 0; $n < $this->threads; ++$n){ - if($this->workers[$n]->isTerminated()){ - $this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread); - }elseif($this->workers[$n]->isWaiting()){ - if($this->workers[$n]->response !== ""){ - console($this->workers[$n]->response); - $this->workers[$n]->notify(); - }else{ - $this->workers[$n]->response = ServerAPI::request()->api->console->run($this->workers[$n]->cmd, "rcon"); - $this->workers[$n]->notify(); - } - } + foreach($this->workers as $w){ + $w->synchronized(function($rcon, $worker){ + if($worker->isTerminated()){ + $worker = new RCONInstance($rcon->socket, $rcon->password, $rcon->clientsPerThread); + }//elseif($worker->isWaiting()){ + $worker->response = ServerAPI::request()->api->console->run($worker->cmd, "rcon"); + $worker->cmd = ""; + $worker->notify(); + //} + }, $this, $w); } } @@ -70,7 +72,6 @@ class RCONInstance extends Thread{ private $socket; private $password; private $maxClients; - public function __construct($socket, $password, $maxClients = 50){ $this->stop = false; $this->cmd = ""; @@ -78,113 +79,119 @@ class RCONInstance extends Thread{ $this->socket = $socket; $this->password = $password; $this->maxClients = (int) $maxClients; - for($n = 0; $n < $this->maxClients; ++$n){ - $this->{"client" . $n} = null; - $this->{"status" . $n} = 0; - $this->{"timeout" . $n} = 0; - } $this->start(); } - + public function close(){ $this->stop = true; } public function run(){ + $clients = []; + $authenticated = []; + $timeouts = []; + $nextClientId = 0; while($this->stop !== true){ - usleep(2000); - $r = [$socket = $this->socket]; + $r = $clients; + $r["main"] = $this->socket; //this is ugly, but we need to be able to mass-select() $w = null; $e = null; - if(socket_select($r, $w, $e, 0) === 1){ - if(($client = socket_accept($this->socket)) !== false){ - socket_set_block($client); - socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1); - $done = false; - for($n = 0; $n < $this->maxClients; ++$n){ - if($this->{"client" . $n} === null){ - $this->{"client" . $n} = $client; - $this->{"status" . $n} = 0; - $this->{"timeout" . $n} = microtime(true) + 5; - $done = true; - break; + $disconnect = []; + if(socket_select($r, $w, $e, 5, 0) > 0){ + foreach($r as $id => $sock){ + if($sock === $this->socket){ + if(($client = socket_accept($this->socket)) !== false){ + if(count($clients) >= $this->maxClients){ + @socket_close($client); + }else{ + socket_set_nonblock($client); + socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1); + + $id = $nextClientId++; + $clients[$id] = $client; + $authenticated[$id] = false; + $timeouts[$id] = microtime(true) + 5; + } } - } - if($done === false){ - @socket_close($client); - } - } - } - - for($n = 0; $n < $this->maxClients; ++$n){ - $client = &$this->{"client" . $n}; - if($client !== null){ - if($this->{"status" . $n} !== -1 and $this->stop !== true){ - if($this->{"status" . $n} === 0 and $this->{"timeout" . $n} < microtime(true)){ //Timeout - $this->{"status" . $n} = -1; - break; - } - $p = $this->readPacket($client, $size, $requestID, $packetType, $payload); + }else{ + $p = $this->readPacket($sock, $size, $requestID, $packetType, $payload); if($p === false){ - $this->{"status" . $n} = -1; - break; - }elseif($p === null){ - break; + $disconnect[$id] = $sock; + continue; } - + switch($packetType){ case 3: //Login - if($this->{"status" . $n} !== 0){ - $this->{"status" . $n} = -1; + if($authenticated[$id]){ + $disconnect[$id] = $sock; break; } + socket_getpeername($sock, $addr, $port); if($payload === $this->password){ - @socket_getpeername($client, $addr, $port); - $this->response = "[INFO] Successful Rcon connection from: /$addr:$port"; - $this->wait(); - $this->response = ""; - $this->writePacket($client, $requestID, 2, ""); - $this->{"status" . $n} = 1; + console("Successful Rcon connection from: /$addr:$port"); + $this->writePacket($sock, $requestID, 2, ""); + $authenticated[$id] = true; }else{ - $this->{"status" . $n} = -1; - $this->writePacket($client, -1, 2, ""); - break; + $disconnect[$id] = $sock; + $this->writePacket($sock, -1, 2, ""); + $this->logger->info("Unsuccessful connection from: /$addr:$port (wrong password)"); } break; case 2: //Command - if($this->{"status" . $n} !== 1){ - $this->{"status" . $n} = -1; + if(!$authenticated[$id]){ + $disconnect[$id] = $sock; break; } - if(strlen($payload) > 0){ - $this->cmd = ltrim($payload); - $this->wait(); - $this->writePacket($client, $requestID, 0, str_replace("\n", "\r\n", trim($this->response))); + if($payload !== ""){ + $this->synchronized(function($payload){ + $this->cmd = ltrim($payload); + $this->wait(); + }, $payload); + if($this->writePacket($sock, $requestID, 0, str_replace("\n", "\r\n", trim($this->response))) === false){ + $disconnect[$id] = $sock; + } $this->response = ""; $this->cmd = ""; } break; } - usleep(1); - }else{ - @socket_set_option($client, SOL_SOCKET, SO_LINGER, ["l_onoff" => 1, "l_linger" => 1]); - @socket_shutdown($client, 2); - @socket_set_block($client); - @socket_read($client, 1); - @socket_close($client); - $this->{"status" . $n} = 0; - $this->{"client" . $n} = null; } } } + + foreach($authenticated as $id => $status){ + if(!isset($disconnect[$id]) and !$authenticated[$id] and $timeouts[$id] < microtime(true)){ //Timeout + $disconnect[$id] = $clients[$id]; + } + } + + foreach($disconnect as $id => $client){ + $this->disconnectClient($client); + unset($clients[$id], $authenticated[$id], $timeouts[$id]); + } + + + } + foreach($clients as $client){ + $this->disconnectClient($client); } unset($this->socket, $this->cmd, $this->response, $this->stop); exit(0); } - + + private function disconnectClient($client){ + socket_getpeername($client, $ip, $port); + @socket_set_option($client, SOL_SOCKET, SO_LINGER, ["l_onoff" => 1, "l_linger" => 1]); + @socket_shutdown($client, 2); + @socket_set_block($client); + @socket_read($client, 1); + @socket_close($client); + console("Disconnected client: /$ip:$port"); + } + private function readPacket($client, &$size, &$requestID, &$packetType, &$payload){ @socket_set_nonblock($client); - $d = socket_read($client, 4); + $d = @socket_read($client, 4); if($this->stop){ return false; }elseif($d === false){ @@ -192,7 +199,6 @@ class RCONInstance extends Thread{ }elseif($d === "" or strlen($d) < 4){ return false; } - @socket_set_block($client); $size = Utils::readLInt($d); if($size < 0 or $size > 65535){ return false; @@ -208,6 +214,6 @@ class RCONInstance extends Thread{ . Utils::writeLInt((int) $packetType) . $payload . "\x00\x00"; //Terminate payload and packet - return socket_write($client, Utils::writeLInt(strlen($pk)) . $pk); + return @socket_write($client, Utils::writeLInt(strlen($pk)) . $pk); } }