mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 16:51:42 +00:00
Strip out RCON, closes #2686
Users should move to the new RconServer plugin instead.
This commit is contained in:
parent
61e04d5284
commit
738e310798
@ -53,7 +53,6 @@ use function ob_start;
|
||||
use function php_uname;
|
||||
use function phpinfo;
|
||||
use function phpversion;
|
||||
use function preg_replace;
|
||||
use function str_split;
|
||||
use function strpos;
|
||||
use function substr;
|
||||
@ -173,7 +172,6 @@ class CrashDump{
|
||||
if($this->server->getProperty("auto-report.send-settings", true) !== false){
|
||||
$this->data["parameters"] = (array) $argv;
|
||||
$this->data["server.properties"] = @file_get_contents($this->server->getDataPath() . "server.properties");
|
||||
$this->data["server.properties"] = preg_replace("#^rcon\\.password=(.*)$#m", "rcon.password=******", $this->data["server.properties"]);
|
||||
$this->data["pocketmine.yml"] = @file_get_contents($this->server->getDataPath() . "pocketmine.yml");
|
||||
}else{
|
||||
$this->data["pocketmine.yml"] = "";
|
||||
|
@ -74,7 +74,6 @@ use pocketmine\network\mcpe\protocol\types\PlayerListEntry;
|
||||
use pocketmine\network\mcpe\RakLibInterface;
|
||||
use pocketmine\network\Network;
|
||||
use pocketmine\network\query\QueryHandler;
|
||||
use pocketmine\network\rcon\RCON;
|
||||
use pocketmine\network\upnp\UPnP;
|
||||
use pocketmine\permission\BanList;
|
||||
use pocketmine\permission\DefaultPermissions;
|
||||
@ -143,7 +142,6 @@ use function stripos;
|
||||
use function strlen;
|
||||
use function strrpos;
|
||||
use function strtolower;
|
||||
use function substr;
|
||||
use function time;
|
||||
use function touch;
|
||||
use function trim;
|
||||
@ -245,9 +243,6 @@ class Server{
|
||||
/** @var bool */
|
||||
private $onlineMode = true;
|
||||
|
||||
/** @var RCON */
|
||||
private $rcon;
|
||||
|
||||
/** @var EntityMetadataStore */
|
||||
private $entityMetadata;
|
||||
|
||||
@ -1087,8 +1082,6 @@ class Server{
|
||||
"level-seed" => "",
|
||||
"level-type" => "DEFAULT",
|
||||
"enable-query" => true,
|
||||
"enable-rcon" => false,
|
||||
"rcon.password" => substr(base64_encode(random_bytes(20)), 3, 10),
|
||||
"auto-save" => true,
|
||||
"view-distance" => 8,
|
||||
"xbox-auth" => true,
|
||||
@ -1191,20 +1184,6 @@ class Server{
|
||||
});
|
||||
$this->console->start(PTHREADS_INHERIT_NONE);
|
||||
|
||||
if($this->getConfigBool("enable-rcon", false)){
|
||||
try{
|
||||
$this->rcon = new RCON(
|
||||
$this,
|
||||
$this->getConfigString("rcon.password", ""),
|
||||
$this->getConfigInt("rcon.port", $this->getPort()),
|
||||
$this->getIp(),
|
||||
$this->getConfigInt("rcon.max-clients", 50)
|
||||
);
|
||||
}catch(\RuntimeException $e){
|
||||
$this->getLogger()->critical("RCON can't be started: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$this->entityMetadata = new EntityMetadataStore();
|
||||
$this->playerMetadata = new PlayerMetadataStore();
|
||||
$this->levelMetadata = new LevelMetadataStore();
|
||||
@ -1692,9 +1671,6 @@ class Server{
|
||||
$this->hasStopped = true;
|
||||
|
||||
$this->shutdown();
|
||||
if($this->rcon instanceof RCON){
|
||||
$this->rcon->stop();
|
||||
}
|
||||
|
||||
if($this->getProperty("network.upnp-forwarding", false)){
|
||||
$this->logger->info("[UPnP] Removing port forward...");
|
||||
|
@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\command;
|
||||
|
||||
use pocketmine\lang\TextContainer;
|
||||
use function trim;
|
||||
|
||||
class RemoteConsoleCommandSender extends ConsoleCommandSender{
|
||||
|
||||
/** @var string */
|
||||
private $messages = "";
|
||||
|
||||
public function sendMessage($message){
|
||||
if($message instanceof TextContainer){
|
||||
$message = $this->getServer()->getLanguage()->translate($message);
|
||||
}else{
|
||||
$message = $this->getServer()->getLanguage()->translateString($message);
|
||||
}
|
||||
|
||||
$this->messages .= trim($message, "\r\n") . "\n";
|
||||
}
|
||||
|
||||
public function getMessage(){
|
||||
return $this->messages;
|
||||
}
|
||||
|
||||
public function getName() : string{
|
||||
return "Rcon";
|
||||
}
|
||||
}
|
@ -1,126 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Implementation of the Source RCON Protocol to allow remote console commands
|
||||
* Source: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
|
||||
*/
|
||||
namespace pocketmine\network\rcon;
|
||||
|
||||
use pocketmine\command\RemoteConsoleCommandSender;
|
||||
use pocketmine\Server;
|
||||
use pocketmine\snooze\SleeperNotifier;
|
||||
use pocketmine\utils\TextFormat;
|
||||
use function max;
|
||||
use function socket_bind;
|
||||
use function socket_close;
|
||||
use function socket_create;
|
||||
use function socket_create_pair;
|
||||
use function socket_getsockname;
|
||||
use function socket_last_error;
|
||||
use function socket_listen;
|
||||
use function socket_set_block;
|
||||
use function socket_strerror;
|
||||
use function socket_write;
|
||||
use function trim;
|
||||
use const AF_INET;
|
||||
use const AF_UNIX;
|
||||
use const SOCK_STREAM;
|
||||
use const SOCKET_ENOPROTOOPT;
|
||||
use const SOCKET_EPROTONOSUPPORT;
|
||||
use const SOL_TCP;
|
||||
|
||||
class RCON{
|
||||
/** @var Server */
|
||||
private $server;
|
||||
/** @var resource */
|
||||
private $socket;
|
||||
|
||||
/** @var RCONInstance */
|
||||
private $instance;
|
||||
|
||||
/** @var resource */
|
||||
private $ipcMainSocket;
|
||||
/** @var resource */
|
||||
private $ipcThreadSocket;
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param string $password
|
||||
* @param int $port
|
||||
* @param string $interface
|
||||
* @param int $maxClients
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function __construct(Server $server, string $password, int $port = 19132, string $interface = "0.0.0.0", int $maxClients = 50){
|
||||
$this->server = $server;
|
||||
$this->server->getLogger()->info("Starting remote control listener");
|
||||
if($password === ""){
|
||||
throw new \RuntimeException("Empty password");
|
||||
}
|
||||
|
||||
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
||||
|
||||
if($this->socket === false or !@socket_bind($this->socket, $interface, $port) or !@socket_listen($this->socket, 5)){
|
||||
throw new \RuntimeException(trim(socket_strerror(socket_last_error())));
|
||||
}
|
||||
|
||||
socket_set_block($this->socket);
|
||||
|
||||
$ret = @socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $ipc);
|
||||
if(!$ret){
|
||||
$err = socket_last_error();
|
||||
if(($err !== SOCKET_EPROTONOSUPPORT and $err !== SOCKET_ENOPROTOOPT) or !@socket_create_pair(AF_INET, SOCK_STREAM, 0, $ipc)){
|
||||
throw new \RuntimeException(trim(socket_strerror(socket_last_error())));
|
||||
}
|
||||
}
|
||||
|
||||
[$this->ipcMainSocket, $this->ipcThreadSocket] = $ipc;
|
||||
|
||||
$notifier = new SleeperNotifier();
|
||||
$this->server->getTickSleeper()->addNotifier($notifier, function() : void{
|
||||
$response = new RemoteConsoleCommandSender();
|
||||
$this->server->dispatchCommand($response, $this->instance->cmd);
|
||||
|
||||
$this->instance->response = TextFormat::clean($response->getMessage());
|
||||
$this->instance->synchronized(function(RCONInstance $thread){
|
||||
$thread->notify();
|
||||
}, $this->instance);
|
||||
});
|
||||
$this->instance = new RCONInstance($this->socket, $password, (int) max(1, $maxClients), $this->server->getLogger(), $this->ipcThreadSocket, $notifier);
|
||||
|
||||
socket_getsockname($this->socket, $addr, $port);
|
||||
$this->server->getLogger()->info("RCON running on $addr:$port");
|
||||
}
|
||||
|
||||
public function stop() : void{
|
||||
$this->instance->close();
|
||||
socket_write($this->ipcMainSocket, "\x00"); //make select() return
|
||||
$this->instance->quit();
|
||||
|
||||
@socket_close($this->socket);
|
||||
@socket_close($this->ipcMainSocket);
|
||||
@socket_close($this->ipcThreadSocket);
|
||||
}
|
||||
}
|
@ -1,267 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\network\rcon;
|
||||
|
||||
use pocketmine\snooze\SleeperNotifier;
|
||||
use pocketmine\Thread;
|
||||
use pocketmine\utils\Binary;
|
||||
use function count;
|
||||
use function ltrim;
|
||||
use function microtime;
|
||||
use function socket_accept;
|
||||
use function socket_close;
|
||||
use function socket_getpeername;
|
||||
use function socket_last_error;
|
||||
use function socket_read;
|
||||
use function socket_select;
|
||||
use function socket_set_block;
|
||||
use function socket_set_nonblock;
|
||||
use function socket_set_option;
|
||||
use function socket_shutdown;
|
||||
use function socket_strerror;
|
||||
use function socket_write;
|
||||
use function str_replace;
|
||||
use function strlen;
|
||||
use function substr;
|
||||
use function trim;
|
||||
use const PTHREADS_INHERIT_NONE;
|
||||
use const SO_KEEPALIVE;
|
||||
use const SO_LINGER;
|
||||
use const SOCKET_ECONNRESET;
|
||||
use const SOL_SOCKET;
|
||||
|
||||
class RCONInstance extends Thread{
|
||||
|
||||
/** @var string */
|
||||
public $cmd;
|
||||
/** @var string */
|
||||
public $response;
|
||||
|
||||
/** @var bool */
|
||||
private $stop;
|
||||
/** @var resource */
|
||||
private $socket;
|
||||
/** @var string */
|
||||
private $password;
|
||||
/** @var int */
|
||||
private $maxClients;
|
||||
/** @var \ThreadedLogger */
|
||||
private $logger;
|
||||
/** @var resource */
|
||||
private $ipcSocket;
|
||||
/** @var SleeperNotifier|null */
|
||||
private $notifier;
|
||||
|
||||
/**
|
||||
* @param resource $socket
|
||||
* @param string $password
|
||||
* @param int $maxClients
|
||||
* @param \ThreadedLogger $logger
|
||||
* @param resource $ipcSocket
|
||||
* @param null|SleeperNotifier $notifier
|
||||
*/
|
||||
public function __construct($socket, string $password, int $maxClients = 50, \ThreadedLogger $logger, $ipcSocket, ?SleeperNotifier $notifier){
|
||||
$this->stop = false;
|
||||
$this->cmd = "";
|
||||
$this->response = "";
|
||||
$this->socket = $socket;
|
||||
$this->password = $password;
|
||||
$this->maxClients = $maxClients;
|
||||
$this->logger = $logger;
|
||||
$this->ipcSocket = $ipcSocket;
|
||||
$this->notifier = $notifier;
|
||||
|
||||
$this->start(PTHREADS_INHERIT_NONE);
|
||||
}
|
||||
|
||||
private function writePacket($client, int $requestID, int $packetType, string $payload){
|
||||
$pk = Binary::writeLInt($requestID)
|
||||
. Binary::writeLInt($packetType)
|
||||
. $payload
|
||||
. "\x00\x00"; //Terminate payload and packet
|
||||
return socket_write($client, Binary::writeLInt(strlen($pk)) . $pk);
|
||||
}
|
||||
|
||||
private function readPacket($client, ?int &$requestID, ?int &$packetType, ?string &$payload){
|
||||
$d = @socket_read($client, 4);
|
||||
|
||||
socket_getpeername($client, $ip, $port);
|
||||
if($d === false){
|
||||
$err = socket_last_error($client);
|
||||
if($err !== SOCKET_ECONNRESET){
|
||||
$this->logger->debug("Connection error with $ip $port: " . trim(socket_strerror($err)));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if(strlen($d) !== 4){
|
||||
if($d !== ""){ //empty data is returned on disconnection
|
||||
$this->logger->debug("Truncated packet from $ip $port (want 4 bytes, have " . strlen($d) . "), disconnecting");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
$size = Binary::readLInt($d);
|
||||
if($size < 0 or $size > 65535){
|
||||
$this->logger->debug("Packet with too-large length header $size from $ip $port, disconnecting");
|
||||
return false;
|
||||
}
|
||||
$buf = @socket_read($client, $size);
|
||||
if($buf === false){
|
||||
$err = socket_last_error($client);
|
||||
if($err !== SOCKET_ECONNRESET){
|
||||
$this->logger->debug("Connection error with $ip $port: " . trim(socket_strerror($err)));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if(strlen($buf) !== $size){
|
||||
$this->logger->debug("Truncated packet from $ip $port (want $size bytes, have " . strlen($buf) . "), disconnecting");
|
||||
return false;
|
||||
}
|
||||
$requestID = Binary::readLInt(substr($buf, 0, 4));
|
||||
$packetType = Binary::readLInt(substr($buf, 4, 4));
|
||||
$payload = substr($buf, 8, -2); //Strip two null bytes
|
||||
return true;
|
||||
}
|
||||
|
||||
public function close() : void{
|
||||
$this->stop = true;
|
||||
}
|
||||
|
||||
public function run() : void{
|
||||
$this->registerClassLoader();
|
||||
|
||||
/** @var resource[] $clients */
|
||||
$clients = [];
|
||||
/** @var int[] $authenticated */
|
||||
$authenticated = [];
|
||||
/** @var float[] $timeouts */
|
||||
$timeouts = [];
|
||||
|
||||
/** @var int $nextClientId */
|
||||
$nextClientId = 0;
|
||||
|
||||
while(!$this->stop){
|
||||
$r = $clients;
|
||||
$r["main"] = $this->socket; //this is ugly, but we need to be able to mass-select()
|
||||
$r["ipc"] = $this->ipcSocket;
|
||||
$w = null;
|
||||
$e = null;
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
}elseif($sock === $this->ipcSocket){
|
||||
//read dummy data
|
||||
socket_read($sock, 65535);
|
||||
}else{
|
||||
$p = $this->readPacket($sock, $requestID, $packetType, $payload);
|
||||
if($p === false){
|
||||
$disconnect[$id] = $sock;
|
||||
continue;
|
||||
}elseif($p === null){
|
||||
continue;
|
||||
}
|
||||
|
||||
switch($packetType){
|
||||
case 3: //Login
|
||||
if($authenticated[$id]){
|
||||
$disconnect[$id] = $sock;
|
||||
break;
|
||||
}
|
||||
if($payload === $this->password){
|
||||
socket_getpeername($sock, $addr, $port);
|
||||
$this->logger->info("Successful Rcon connection from: /$addr:$port");
|
||||
$this->writePacket($sock, $requestID, 2, "");
|
||||
$authenticated[$id] = true;
|
||||
}else{
|
||||
$disconnect[$id] = $sock;
|
||||
$this->writePacket($sock, -1, 2, "");
|
||||
}
|
||||
break;
|
||||
case 2: //Command
|
||||
if(!$authenticated[$id]){
|
||||
$disconnect[$id] = $sock;
|
||||
break;
|
||||
}
|
||||
if($payload !== ""){
|
||||
$this->cmd = ltrim($payload);
|
||||
$this->synchronized(function(){
|
||||
$this->notifier->wakeupSleeper();
|
||||
$this->wait();
|
||||
});
|
||||
$this->writePacket($sock, $requestID, 0, str_replace("\n", "\r\n", trim($this->response)));
|
||||
$this->response = "";
|
||||
$this->cmd = "";
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private function disconnectClient($client) : void{
|
||||
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);
|
||||
$this->logger->info("Disconnected client: /$ip:$port");
|
||||
}
|
||||
|
||||
public function getThreadName() : string{
|
||||
return "RCON";
|
||||
}
|
||||
}
|
@ -31,13 +31,10 @@ use pocketmine\lang\Language;
|
||||
use pocketmine\lang\LanguageNotFoundException;
|
||||
use pocketmine\utils\Config;
|
||||
use pocketmine\utils\Internet;
|
||||
use function base64_encode;
|
||||
use function fgets;
|
||||
use function gethostbyname;
|
||||
use function random_bytes;
|
||||
use function sleep;
|
||||
use function strtolower;
|
||||
use function substr;
|
||||
use function trim;
|
||||
use const PHP_EOL;
|
||||
use const STDIN;
|
||||
@ -206,16 +203,6 @@ LICENSE;
|
||||
$config->set("enable-query", true);
|
||||
}
|
||||
|
||||
$this->message($this->lang->get("rcon_info"));
|
||||
if(strtolower($this->getInput($this->lang->get("rcon_enable"), "n", "y/N")) === "y"){
|
||||
$config->set("enable-rcon", true);
|
||||
$password = substr(base64_encode(random_bytes(20)), 3, 10);
|
||||
$config->set("rcon.password", $password);
|
||||
$this->message($this->lang->get("rcon_password") . ": " . $password);
|
||||
}else{
|
||||
$config->set("enable-rcon", false);
|
||||
}
|
||||
|
||||
$config->save();
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user