From 11356b68ba514980db01b49a364a306a961becec Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Wed, 2 Apr 2014 19:33:49 +0200 Subject: [PATCH] Added RCON --- src/pocketmine/Server.php | 8 ++++++++ .../command/RemoteConsoleCommandSender.php | 18 +++++++++++++++++- .../command/defaults/PluginsCommand.php | 4 ++-- src/pocketmine/network/rcon/RCON.php | 18 ++++++++++++++---- src/pocketmine/utils/TextFormat.php | 16 ++++++++-------- 5 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index afd2439ec..7eba72bf4 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -60,6 +60,7 @@ use pocketmine\network\query\QueryHandler; use pocketmine\network\query\QueryPacket; use pocketmine\network\raknet\Info as RakNetInfo; use pocketmine\network\raknet\Packet as RakNetPacket; +use pocketmine\network\rcon\RCON; use pocketmine\network\ThreadedHandler; use pocketmine\network\upnp\UPnP; use pocketmine\permission\BanList; @@ -1360,10 +1361,17 @@ class Server{ * Starts the PocketMine-MP server and starts processing ticks and packets */ public function start(){ + + if($this->getConfigBoolean("enable-rcon", false) === true){ + $this->rcon = new RCON($this->getConfigString("rcon.password", ""), $this->getConfigInt("rcon.port", $this->getPort()), ($ip = $this->getIp()) != "" ? $ip : "0.0.0.0", $this->getConfigInt("rcon.threads", 1), $this->getConfigInt("rcon.clients-per-thread", 50)); + } + if($this->getConfigBoolean("enable-query", true) === true){ $this->queryHandler = new QueryHandler(); } + + if($this->getConfigBoolean("upnp-forwarding", false) == true){ console("[INFO] [UPnP] Trying to port forward..."); UPnP::PortForward($this->getPort()); diff --git a/src/pocketmine/command/RemoteConsoleCommandSender.php b/src/pocketmine/command/RemoteConsoleCommandSender.php index 8d537a0b7..3651be843 100644 --- a/src/pocketmine/command/RemoteConsoleCommandSender.php +++ b/src/pocketmine/command/RemoteConsoleCommandSender.php @@ -22,6 +22,22 @@ namespace pocketmine\command; -interface RemoteConsoleCommandSender extends CommandSender{ +class RemoteConsoleCommandSender extends ConsoleCommandSender{ + + /** @var string */ + private $messages = ""; + + public function sendMessage($message){ + $this->messages .= trim($message, "\r\n")."\n"; + } + + public function getMessage(){ + return $this->messages; + } + + public function getName(){ + return "Rcon"; + } + } \ No newline at end of file diff --git a/src/pocketmine/command/defaults/PluginsCommand.php b/src/pocketmine/command/defaults/PluginsCommand.php index 6b97c7fe4..df6bdfe9a 100644 --- a/src/pocketmine/command/defaults/PluginsCommand.php +++ b/src/pocketmine/command/defaults/PluginsCommand.php @@ -53,8 +53,8 @@ class PluginsCommand extends VanillaCommand{ if(strlen($list) > 0){ $list .= TextFormat::WHITE . ", "; } - $list .= $plugin->isEnabled() ? TextFormat::DARK_GREEN : TextFormat::RED; - $list .= $plugin->getDescription()->getName(); + $list .= $plugin->isEnabled() ? TextFormat::GREEN : TextFormat::RED; + $list .= $plugin->getDescription()->getFullName(); } return "(" . count($plugins) . "): $list"; diff --git a/src/pocketmine/network/rcon/RCON.php b/src/pocketmine/network/rcon/RCON.php index fbc143e82..307825da5 100644 --- a/src/pocketmine/network/rcon/RCON.php +++ b/src/pocketmine/network/rcon/RCON.php @@ -25,11 +25,20 @@ */ namespace pocketmine\network\rcon; +use pocketmine\command\RemoteConsoleCommandSender; +use pocketmine\scheduler\CallbackTask; use pocketmine\Server; +use pocketmine\utils\TextFormat; class RCON{ - private $socket, $password, $workers, $threads, $clientsPerThread; + private $socket; + private $password; + /** @var RCONInstance[] */ + private $workers; + private $threads; + private $clientsPerThread; + private $rconSender; public function __construct($password, $port = 19132, $interface = "0.0.0.0", $threads = 1, $clientsPerThread = 50){ $this->workers = array(); @@ -49,18 +58,18 @@ class RCON{ return; } @socket_set_block($this->socket); + for($n = 0; $n < $this->threads; ++$n){ $this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread); } @socket_getsockname($this->socket, $addr, $port); console("[INFO] RCON running on $addr:$port"); - Server::getInstance()->schedule(2, array($this, "check"), array(), true); + Server::getInstance()->getScheduler()->scheduleRepeatingTask(new CallbackTask(array($this, "check")), 3); } public function stop(){ for($n = 0; $n < $this->threads; ++$n){ $this->workers[$n]->close(); - $this->workers[$n]->join(); usleep(50000); $this->workers[$n]->kill(); } @@ -77,7 +86,8 @@ class RCON{ console($this->workers[$n]->response); $this->workers[$n]->notify(); }else{ - $this->workers[$n]->response = Server::getInstance()->api->console->run($this->workers[$n]->cmd, "rcon"); + Server::getInstance()->dispatchCommand($response = new RemoteConsoleCommandSender(), $this->workers[$n]->cmd); + $this->workers[$n]->response = TextFormat::clean($response->getMessage()); $this->workers[$n]->notify(); } } diff --git a/src/pocketmine/utils/TextFormat.php b/src/pocketmine/utils/TextFormat.php index 16dd1cba0..d10393f34 100644 --- a/src/pocketmine/utils/TextFormat.php +++ b/src/pocketmine/utils/TextFormat.php @@ -222,28 +222,28 @@ class TextFormat{ //Colors case TextFormat::BLACK: - $newString .= "\x1b[30m"; + $newString .= "\x1b[30;0m"; break; case TextFormat::DARK_BLUE: - $newString .= "\x1b[34m"; + $newString .= "\x1b[34;0m"; break; case TextFormat::DARK_GREEN: - $newString .= "\x1b[32m"; + $newString .= "\x1b[32;0m"; break; case TextFormat::DARK_AQUA: - $newString .= "\x1b[36m"; + $newString .= "\x1b[36;0m"; break; case TextFormat::DARK_RED: - $newString .= "\x1b[31m"; + $newString .= "\x1b[31;0m"; break; case TextFormat::DARK_PURPLE: - $newString .= "\x1b[35m"; + $newString .= "\x1b[35;0m"; break; case TextFormat::GOLD: - $newString .= "\x1b[33m"; + $newString .= "\x1b[33;0m"; break; case TextFormat::GRAY: - $newString .= "\x1b[37m"; + $newString .= "\x1b[37;0m"; break; case TextFormat::DARK_GRAY: $newString .= "\x1b[30;1m";