From ccbdb77618ff76078567c33bf61eb3ac2454ebeb Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 26 Sep 2017 09:49:35 +0100 Subject: [PATCH] Cleaned up LoginPacket handling, don't nuke the buffer --- .../network/mcpe/PlayerNetworkSessionAdapter.php | 2 +- .../network/mcpe/protocol/DataPacket.php | 8 ++++++++ .../network/mcpe/protocol/LoginPacket.php | 14 +++++++++----- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter.php b/src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter.php index ae753a5d5..fd6e7dd86 100644 --- a/src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter.php +++ b/src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter.php @@ -82,7 +82,7 @@ class PlayerNetworkSessionAdapter extends NetworkSession{ $timings->startTiming(); $packet->decode(); - if(!$packet->feof()){ + if(!$packet->feof() and !$packet->mayHaveUnreadBytes()){ $remains = substr($packet->buffer, $packet->offset); $this->server->getLogger()->debug("Still " . strlen($remains) . " bytes unread in " . $packet->getName() . ": 0x" . bin2hex($remains)); } diff --git a/src/pocketmine/network/mcpe/protocol/DataPacket.php b/src/pocketmine/network/mcpe/protocol/DataPacket.php index c68c2d447..4a16885b3 100644 --- a/src/pocketmine/network/mcpe/protocol/DataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/DataPacket.php @@ -62,6 +62,14 @@ abstract class DataPacket extends BinaryStream{ return false; } + /** + * Returns whether the packet may legally have unread bytes left in the buffer. + * @return bool + */ + public function mayHaveUnreadBytes() : bool{ + return false; + } + public function decode(){ $this->offset = 0; $this->decodeHeader(); diff --git a/src/pocketmine/network/mcpe/protocol/LoginPacket.php b/src/pocketmine/network/mcpe/protocol/LoginPacket.php index d6d33ce89..0e9eb77ec 100644 --- a/src/pocketmine/network/mcpe/protocol/LoginPacket.php +++ b/src/pocketmine/network/mcpe/protocol/LoginPacket.php @@ -27,6 +27,7 @@ namespace pocketmine\network\mcpe\protocol; use pocketmine\network\mcpe\NetworkSession; +use pocketmine\utils\BinaryStream; use pocketmine\utils\Utils; class LoginPacket extends DataPacket{ @@ -63,17 +64,20 @@ class LoginPacket extends DataPacket{ return true; } + public function mayHaveUnreadBytes() : bool{ + return $this->protocol !== null and $this->protocol !== ProtocolInfo::CURRENT_PROTOCOL; + } + protected function decodePayload(){ $this->protocol = $this->getInt(); if($this->protocol !== ProtocolInfo::CURRENT_PROTOCOL){ - $this->buffer = null; - return; //Do not attempt to decode for non-accepted protocols + return; //Do not attempt to continue decoding for non-accepted protocols } - $this->setBuffer($this->getString(), 0); + $buffer = new BinaryStream($this->getString()); - $this->chainData = json_decode($this->get($this->getLInt()), true); + $this->chainData = json_decode($buffer->get($buffer->getLInt()), true); foreach($this->chainData["chain"] as $chain){ $webtoken = Utils::decodeJWT($chain); if(isset($webtoken["extraData"])){ @@ -89,7 +93,7 @@ class LoginPacket extends DataPacket{ } } - $this->clientDataJwt = $this->get($this->getLInt()); + $this->clientDataJwt = $buffer->get($buffer->getLInt()); $this->clientData = Utils::decodeJWT($this->clientDataJwt); $this->clientId = $this->clientData["ClientRandomId"] ?? null;