mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 20:19:44 +00:00
rcon update from https://github.com/pmmp/RconServer
This commit is contained in:
parent
27f0231f2c
commit
e3fa53613d
1 changed files with 92 additions and 86 deletions
|
|
@ -6,8 +6,12 @@ Source: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class RCON{
|
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){
|
public function __construct($password, $port = 19132, $interface = "0.0.0.0", $threads = 1, $clientsPerThread = 50){
|
||||||
$this->workers = [];
|
$this->workers = [];
|
||||||
|
|
@ -45,18 +49,16 @@ class RCON{
|
||||||
}
|
}
|
||||||
|
|
||||||
public function check(){
|
public function check(){
|
||||||
for($n = 0; $n < $this->threads; ++$n){
|
foreach($this->workers as $w){
|
||||||
if($this->workers[$n]->isTerminated()){
|
$w->synchronized(function($rcon, $worker){
|
||||||
$this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread);
|
if($worker->isTerminated()){
|
||||||
}elseif($this->workers[$n]->isWaiting()){
|
$worker = new RCONInstance($rcon->socket, $rcon->password, $rcon->clientsPerThread);
|
||||||
if($this->workers[$n]->response !== ""){
|
}//elseif($worker->isWaiting()){
|
||||||
console($this->workers[$n]->response);
|
$worker->response = ServerAPI::request()->api->console->run($worker->cmd, "rcon");
|
||||||
$this->workers[$n]->notify();
|
$worker->cmd = "";
|
||||||
}else{
|
$worker->notify();
|
||||||
$this->workers[$n]->response = ServerAPI::request()->api->console->run($this->workers[$n]->cmd, "rcon");
|
//}
|
||||||
$this->workers[$n]->notify();
|
}, $this, $w);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -70,7 +72,6 @@ class RCONInstance extends Thread{
|
||||||
private $socket;
|
private $socket;
|
||||||
private $password;
|
private $password;
|
||||||
private $maxClients;
|
private $maxClients;
|
||||||
|
|
||||||
public function __construct($socket, $password, $maxClients = 50){
|
public function __construct($socket, $password, $maxClients = 50){
|
||||||
$this->stop = false;
|
$this->stop = false;
|
||||||
$this->cmd = "";
|
$this->cmd = "";
|
||||||
|
|
@ -78,113 +79,119 @@ class RCONInstance extends Thread{
|
||||||
$this->socket = $socket;
|
$this->socket = $socket;
|
||||||
$this->password = $password;
|
$this->password = $password;
|
||||||
$this->maxClients = (int) $maxClients;
|
$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();
|
$this->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function close(){
|
public function close(){
|
||||||
$this->stop = true;
|
$this->stop = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run(){
|
public function run(){
|
||||||
|
$clients = [];
|
||||||
|
$authenticated = [];
|
||||||
|
$timeouts = [];
|
||||||
|
$nextClientId = 0;
|
||||||
while($this->stop !== true){
|
while($this->stop !== true){
|
||||||
usleep(2000);
|
$r = $clients;
|
||||||
$r = [$socket = $this->socket];
|
$r["main"] = $this->socket; //this is ugly, but we need to be able to mass-select()
|
||||||
$w = null;
|
$w = null;
|
||||||
$e = null;
|
$e = null;
|
||||||
if(socket_select($r, $w, $e, 0) === 1){
|
$disconnect = [];
|
||||||
if(($client = socket_accept($this->socket)) !== false){
|
if(socket_select($r, $w, $e, 5, 0) > 0){
|
||||||
socket_set_block($client);
|
foreach($r as $id => $sock){
|
||||||
socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1);
|
if($sock === $this->socket){
|
||||||
$done = false;
|
if(($client = socket_accept($this->socket)) !== false){
|
||||||
for($n = 0; $n < $this->maxClients; ++$n){
|
if(count($clients) >= $this->maxClients){
|
||||||
if($this->{"client" . $n} === null){
|
@socket_close($client);
|
||||||
$this->{"client" . $n} = $client;
|
}else{
|
||||||
$this->{"status" . $n} = 0;
|
socket_set_nonblock($client);
|
||||||
$this->{"timeout" . $n} = microtime(true) + 5;
|
socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1);
|
||||||
$done = true;
|
|
||||||
break;
|
$id = $nextClientId++;
|
||||||
|
$clients[$id] = $client;
|
||||||
|
$authenticated[$id] = false;
|
||||||
|
$timeouts[$id] = microtime(true) + 5;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}else{
|
||||||
if($done === false){
|
$p = $this->readPacket($sock, $size, $requestID, $packetType, $payload);
|
||||||
@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);
|
|
||||||
if($p === false){
|
if($p === false){
|
||||||
$this->{"status" . $n} = -1;
|
$disconnect[$id] = $sock;
|
||||||
break;
|
continue;
|
||||||
}elseif($p === null){
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch($packetType){
|
switch($packetType){
|
||||||
case 3: //Login
|
case 3: //Login
|
||||||
if($this->{"status" . $n} !== 0){
|
if($authenticated[$id]){
|
||||||
$this->{"status" . $n} = -1;
|
$disconnect[$id] = $sock;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
socket_getpeername($sock, $addr, $port);
|
||||||
if($payload === $this->password){
|
if($payload === $this->password){
|
||||||
@socket_getpeername($client, $addr, $port);
|
console("Successful Rcon connection from: /$addr:$port");
|
||||||
$this->response = "[INFO] Successful Rcon connection from: /$addr:$port";
|
$this->writePacket($sock, $requestID, 2, "");
|
||||||
$this->wait();
|
$authenticated[$id] = true;
|
||||||
$this->response = "";
|
|
||||||
$this->writePacket($client, $requestID, 2, "");
|
|
||||||
$this->{"status" . $n} = 1;
|
|
||||||
}else{
|
}else{
|
||||||
$this->{"status" . $n} = -1;
|
$disconnect[$id] = $sock;
|
||||||
$this->writePacket($client, -1, 2, "");
|
$this->writePacket($sock, -1, 2, "");
|
||||||
break;
|
$this->logger->info("Unsuccessful connection from: /$addr:$port (wrong password)");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 2: //Command
|
case 2: //Command
|
||||||
if($this->{"status" . $n} !== 1){
|
if(!$authenticated[$id]){
|
||||||
$this->{"status" . $n} = -1;
|
$disconnect[$id] = $sock;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(strlen($payload) > 0){
|
if($payload !== ""){
|
||||||
$this->cmd = ltrim($payload);
|
$this->synchronized(function($payload){
|
||||||
$this->wait();
|
$this->cmd = ltrim($payload);
|
||||||
$this->writePacket($client, $requestID, 0, str_replace("\n", "\r\n", trim($this->response)));
|
$this->wait();
|
||||||
|
}, $payload);
|
||||||
|
if($this->writePacket($sock, $requestID, 0, str_replace("\n", "\r\n", trim($this->response))) === false){
|
||||||
|
$disconnect[$id] = $sock;
|
||||||
|
}
|
||||||
$this->response = "";
|
$this->response = "";
|
||||||
$this->cmd = "";
|
$this->cmd = "";
|
||||||
}
|
}
|
||||||
break;
|
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);
|
unset($this->socket, $this->cmd, $this->response, $this->stop);
|
||||||
exit(0);
|
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){
|
private function readPacket($client, &$size, &$requestID, &$packetType, &$payload){
|
||||||
@socket_set_nonblock($client);
|
@socket_set_nonblock($client);
|
||||||
$d = socket_read($client, 4);
|
$d = @socket_read($client, 4);
|
||||||
if($this->stop){
|
if($this->stop){
|
||||||
return false;
|
return false;
|
||||||
}elseif($d === false){
|
}elseif($d === false){
|
||||||
|
|
@ -192,7 +199,6 @@ class RCONInstance extends Thread{
|
||||||
}elseif($d === "" or strlen($d) < 4){
|
}elseif($d === "" or strlen($d) < 4){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@socket_set_block($client);
|
|
||||||
$size = Utils::readLInt($d);
|
$size = Utils::readLInt($d);
|
||||||
if($size < 0 or $size > 65535){
|
if($size < 0 or $size > 65535){
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -208,6 +214,6 @@ class RCONInstance extends Thread{
|
||||||
. Utils::writeLInt((int) $packetType)
|
. Utils::writeLInt((int) $packetType)
|
||||||
. $payload
|
. $payload
|
||||||
. "\x00\x00"; //Terminate payload and packet
|
. "\x00\x00"; //Terminate payload and packet
|
||||||
return socket_write($client, Utils::writeLInt(strlen($pk)) . $pk);
|
return @socket_write($client, Utils::writeLInt(strlen($pk)) . $pk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue