Network: Added RawPacketHandler interface, query handler is now a component

This commit is contained in:
Dylan K. Taylor 2019-03-24 18:02:19 +00:00
parent a753c1342d
commit 6990d6239e
5 changed files with 108 additions and 28 deletions

View File

@ -62,7 +62,6 @@ use pocketmine\nbt\BigEndianNbtSerializer;
use pocketmine\nbt\NbtDataException;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\TreeRoot;
use pocketmine\network\AdvancedNetworkInterface;
use pocketmine\network\mcpe\CompressBatchPromise;
use pocketmine\network\mcpe\CompressBatchTask;
use pocketmine\network\mcpe\NetworkCipher;
@ -106,7 +105,6 @@ use function array_key_exists;
use function array_shift;
use function array_sum;
use function base64_encode;
use function bin2hex;
use function copy;
use function count;
use function define;
@ -280,9 +278,6 @@ class Server{
/** @var string[] */
private $uniquePlayers = [];
/** @var QueryHandler */
private $queryHandler;
/** @var QueryRegenerateEvent */
private $queryRegenerateTask = null;
@ -1201,7 +1196,7 @@ class Server{
$this->getLogger()->debug("Server unique id: " . $this->getServerUniqueId());
$this->getLogger()->debug("Machine unique id: " . Utils::getMachineUniqueId());
$this->network = new Network();
$this->network = new Network($this->logger);
$this->network->setName($this->getMotd());
@ -1327,7 +1322,7 @@ class Server{
$this->enablePlugins(PluginLoadOrder::POSTWORLD);
if($this->getConfigBool("enable-query", true)){
$this->queryHandler = new QueryHandler();
$this->network->registerRawPacketHandler(new QueryHandler());
}
foreach($this->getIPBans()->getEntries() as $entry){
@ -1953,22 +1948,6 @@ class Server{
Timings::$titleTickTimer->stopTiming();
}
/**
* @param AdvancedNetworkInterface $interface
* @param string $address
* @param int $port
* @param string $payload
*
* TODO: move this to Network
*/
public function handlePacket(AdvancedNetworkInterface $interface, string $address, int $port, string $payload) : void{
if($this->queryHandler === null or !$this->queryHandler->handle($interface, $address, $port, $payload)){
$this->logger->debug("Unhandled raw packet from $address $port: " . bin2hex($payload));
}
//TODO: add raw packet events
}
/**
* Tries to execute a server tick
*/

View File

@ -29,7 +29,9 @@ namespace pocketmine\network;
use pocketmine\event\server\NetworkInterfaceRegisterEvent;
use pocketmine\event\server\NetworkInterfaceUnregisterEvent;
use pocketmine\network\mcpe\protocol\PacketPool;
use function bin2hex;
use function get_class;
use function preg_match;
use function spl_object_id;
class Network{
@ -39,6 +41,9 @@ class Network{
/** @var AdvancedNetworkInterface[] */
private $advancedInterfaces = [];
/** @var RawPacketHandler[] */
private $rawPacketHandlers = [];
private $upload = 0;
private $download = 0;
@ -48,9 +53,13 @@ class Network{
/** @var NetworkSessionManager */
private $sessionManager;
public function __construct(){
/** @var \Logger */
private $logger;
public function __construct(\Logger $logger){
PacketPool::init();
$this->sessionManager = new NetworkSessionManager();
$this->logger = $logger;
}
public function addStatistics(float $upload, float $download) : void{
@ -181,9 +190,51 @@ class Network{
}
}
public function addRawPacketFilter(string $regex) : void{
/**
* Registers a raw packet handler on the network.
*
* @param RawPacketHandler $handler
*/
public function registerRawPacketHandler(RawPacketHandler $handler) : void{
$this->rawPacketHandlers[spl_object_id($handler)] = $handler;
$regex = $handler->getPattern();
foreach($this->advancedInterfaces as $interface){
$interface->addRawPacketFilter($regex);
}
}
/**
* Unregisters a previously-registered raw packet handler.
*
* @param RawPacketHandler $handler
*/
public function unregisterRawPacketHandler(RawPacketHandler $handler) : void{
unset($this->rawPacketHandlers[spl_object_id($handler)]);
}
/**
* @param AdvancedNetworkInterface $interface
* @param string $address
* @param int $port
* @param string $packet
*/
public function processRawPacket(AdvancedNetworkInterface $interface, string $address, int $port, string $packet) : void{
$handled = false;
foreach($this->rawPacketHandlers as $handler){
if(preg_match($handler->getPattern(), $packet) === 1){
$handled = true;
try{
$handler->handle($interface, $address, $port, $packet);
}catch(BadPacketException $e){
$this->logger->error("Bad raw packet from /$address:$port: " . $e->getMessage());
$this->blockAddress($address, 600);
break;
}
}
}
if(!$handled){
$this->logger->debug("Unhandled raw packet from /$address:$port: " . bin2hex($packet));
}
}
}

View File

@ -0,0 +1,46 @@
<?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;
interface RawPacketHandler{
/**
* Returns a preg_match() compatible regex pattern used to filter packets on this handler. Only packets matching
* this pattern will be delivered to the handler.
*
* @return string
*/
public function getPattern() : string;
/**
* @param AdvancedNetworkInterface $interface
* @param string $address
* @param int $port
* @param string $packet
*
* @return bool
* @throws BadPacketException
*/
public function handle(AdvancedNetworkInterface $interface, string $address, int $port, string $packet) : bool;
}

View File

@ -187,7 +187,7 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{
}
public function handleRaw(string $address, int $port, string $payload) : void{
$this->server->handlePacket($this, $address, $port, $payload);
$this->network->processRawPacket($this, $address, $port, $payload);
}
public function sendRawPacket(string $address, int $port, string $payload) : void{

View File

@ -28,6 +28,7 @@ declare(strict_types=1);
namespace pocketmine\network\query;
use pocketmine\network\AdvancedNetworkInterface;
use pocketmine\network\RawPacketHandler;
use pocketmine\Server;
use pocketmine\utils\Binary;
use pocketmine\utils\BinaryDataException;
@ -38,7 +39,7 @@ use function random_bytes;
use function strlen;
use function substr;
class QueryHandler{
class QueryHandler implements RawPacketHandler{
/** @var Server */
private $server;
/** @var string */
@ -51,7 +52,6 @@ class QueryHandler{
public function __construct(){
$this->server = Server::getInstance();
$this->server->getNetwork()->addRawPacketFilter('/^\xfe\xfd.+$/s');
$this->server->getLogger()->info($this->server->getLanguage()->translateString("pocketmine.server.query.start"));
$addr = $this->server->getIp();
$port = $this->server->getPort();
@ -70,6 +70,10 @@ class QueryHandler{
$this->server->getLogger()->info($this->server->getLanguage()->translateString("pocketmine.server.query.running", [$addr, $port]));
}
public function getPattern() : string{
return '/^\xfe\xfd.+$/s';
}
private function debug(string $message) : void{
//TODO: replace this with a proper prefixed logger
$this->server->getLogger()->debug("[Query] $message");