From e428b4cfc470fd0fd79b5766faa977a2d7e4de4a Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Mon, 22 Apr 2013 23:33:46 +0200 Subject: [PATCH] Limited RCON threads to one and added a auth timeout --- src/API/BanAPI.php | 2 +- src/API/ChatAPI.php | 22 ++++++++++++++++++---- src/API/ConsoleAPI.php | 11 +---------- src/{utils => network}/RCON.php | 12 +++++++++--- 4 files changed, 29 insertions(+), 18 deletions(-) rename src/{utils => network}/RCON.php (94%) diff --git a/src/API/BanAPI.php b/src/API/BanAPI.php index 9a766c8ca..1af44149b 100644 --- a/src/API/BanAPI.php +++ b/src/API/BanAPI.php @@ -99,7 +99,7 @@ class BanAPI{ if($this->server->api->handle("console.check", $data) === true or $this->isOp($data["issuer"]->iusername)){ return true; } - }elseif($data["issuer"] === "console"){ + }elseif($data["issuer"] === "console" or $data["issuer"] === "rcon"){ return true; } return false; diff --git a/src/API/ChatAPI.php b/src/API/ChatAPI.php index 46ec432ed..7d1b20653 100644 --- a/src/API/ChatAPI.php +++ b/src/API/ChatAPI.php @@ -34,6 +34,7 @@ class ChatAPI{ public function init(){ $this->server->api->console->register("tell", " ", array($this, "commandHandler")); $this->server->api->console->register("me", "", array($this, "commandHandler")); + $this->server->api->console->register("say", "", array($this, "commandHandler")); $this->server->api->ban->cmdWhitelist("tell"); $this->server->api->ban->cmdWhitelist("me"); } @@ -41,9 +42,22 @@ class ChatAPI{ public function commandHandler($cmd, $params, $issuer, $alias){ $output = ""; switch($cmd){ + case "say": + $s = implode(" ", $params); + if(trim($s) == ""){ + $output .= "Usage: /say \n"; + break; + } + $sender = ($issuer instanceof Player) ? "Server":ucfirst($issuer); + $this->server->api->chat->broadcast("[$sender] ".$s); + break; case "me": if(!($issuer instanceof Player)){ - $sender = "Console"; + if($issuer === "rcon"){ + $sender = "Rcon"; + }else{ + $sender = "Console"; + } }else{ $sender = $issuer->username; } @@ -55,7 +69,7 @@ class ChatAPI{ break; } if(!($issuer instanceof Player)){ - $sender = "Console"; + $sender = ucfirst($issuer); }else{ $sender = $issuer->username; } @@ -65,7 +79,7 @@ class ChatAPI{ $target = $target->username; }else{ $target = strtolower($n); - if($target === "server" or $target === "console"){ + if($target === "server" or $target === "console" or $target === "rcon"){ $target = "Console"; }else{ $output .= "Usage: /$cmd \n"; @@ -74,7 +88,7 @@ class ChatAPI{ } $mes = implode(" ", $params); $output .= "[me -> ".$target."] ".$mes."\n"; - if($target !== "Console"){ + if($target !== "Console" and $target !== "Rcon"){ $this->sendTo(false, "[".$sender." -> me] ".$mes, $target); } console("[INFO] [".$sender." -> ".$target."] ".$mes); diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 1124fee84..98b4ea662 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -42,7 +42,6 @@ class ConsoleAPI{ $this->register("status", "", array($this, "defaultCommands")); $this->register("difficulty", "<0|1>", array($this, "defaultCommands")); $this->register("invisible", "", array($this, "defaultCommands")); - $this->register("say", "", array($this, "defaultCommands")); $this->register("save-all", "", array($this, "defaultCommands")); $this->register("stop", "", array($this, "defaultCommands")); $this->register("defaultgamemode", "", array($this, "defaultCommands")); @@ -128,20 +127,12 @@ class ConsoleAPI{ $this->server->api->setProperty("difficulty", (int) $s); $output .= "Difficulty changed to ".$this->server->difficulty."\n"; break; - case "say": - $s = implode(" ", $params); - if(trim($s) == ""){ - $output .= "Usage: /say \n"; - break; - } - $this->server->api->chat->broadcast("[Server] ".$s); - break; case "save-all": $this->server->save(); break; case "?": - if($issuer !== "console"){ + if($issuer !== "console" and $issuer !== "rcon"){ break; } case "help": diff --git a/src/utils/RCON.php b/src/network/RCON.php similarity index 94% rename from src/utils/RCON.php rename to src/network/RCON.php index c64df2e68..023f72f98 100644 --- a/src/utils/RCON.php +++ b/src/network/RCON.php @@ -30,7 +30,7 @@ the Free Software Foundation, either version 3 of the License, or class RCON{ private $socket, $password, $workers, $threads; - public function __construct($password, $port = 19132, $interface = "0.0.0.0", $threads = 4, $clientsPerThread = 25){ + public function __construct($password, $port = 19132, $interface = "0.0.0.0", $threads = 1, $clientsPerThread = 50){ $this->workers = array(); $this->password = (string) $password; console("[INFO] Starting remote control listener"); @@ -72,7 +72,7 @@ class RCON{ console($this->workers[$n]->response); $this->workers[$n]->notify(); }else{ - $this->workers[$n]->response = ServerAPI::request()->api->console->run($this->workers[$n]->cmd, "console"); + $this->workers[$n]->response = ServerAPI::request()->api->console->run($this->workers[$n]->cmd, "rcon"); $this->workers[$n]->notify(); } } @@ -90,7 +90,7 @@ class RCONInstance extends Thread{ private $status; private $maxClients; - public function __construct($socket, $password, $maxClients = 5){ + public function __construct($socket, $password, $maxClients = 50){ $this->stop = false; $this->cmd = ""; $this->response = ""; @@ -100,6 +100,7 @@ class RCONInstance extends Thread{ for($n = 0; $n < $this->maxClients; ++$n){ $this->{"client".$n} = null; $this->{"status".$n} = 0; + $this->{"timeout".$n} = 0; } $this->status = array(); $this->start(); @@ -145,6 +146,7 @@ class RCONInstance extends Thread{ if($this->{"client".$n} === null){ $this->{"client".$n} = $client; $this->{"status".$n} = 0; + $this->{"timeout".$n} = microtime(true) + 5; $done = true; break; } @@ -158,6 +160,10 @@ class RCONInstance extends Thread{ $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; + continue; + } $p = $this->readPacket($client, $size, $requestID, $packetType, $payload); if($p === false){ $this->{"status".$n} = -1;