mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-25 04:44:03 +00:00
127 lines
3.8 KiB
PHP
127 lines
3.8 KiB
PHP
<?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)){
|
|
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);
|
|
}
|
|
}
|