From 3e2e59edd4a027278e1504190c2b65b63709f38e Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Sun, 28 Jul 2024 13:16:43 +0300 Subject: [PATCH 01/35] Cookie added --- src/protocol/OpenConnectionReply1.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/protocol/OpenConnectionReply1.php b/src/protocol/OpenConnectionReply1.php index eaa227c..1f1bff4 100644 --- a/src/protocol/OpenConnectionReply1.php +++ b/src/protocol/OpenConnectionReply1.php @@ -21,6 +21,7 @@ class OpenConnectionReply1 extends OfflineMessage{ public int $serverID; public bool $serverSecurity = false; + public int $cookie = 0; public int $mtuSize; public static function create(int $serverId, bool $serverSecurity, int $mtuSize) : self{ @@ -35,6 +36,9 @@ protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); $out->putLong($this->serverID); $out->putByte($this->serverSecurity ? 1 : 0); + if ($this->serverSecurity) { + $out->putInt($this->createCookie()); + } $out->putShort($this->mtuSize); } @@ -42,6 +46,16 @@ protected function decodePayload(PacketSerializer $in) : void{ $this->readMagic($in); $this->serverID = $in->getLong(); $this->serverSecurity = $in->getByte() !== 0; + if ($this->serverSecurity) { + $this->cookie = $in->getInt(); + } $this->mtuSize = $in->getShort(); } + + private function createCookie() : int { + $cookieSalt = mt_rand(0, 0xFFFFFFFF); + $cookie = Binary::writeLInt($cookieSalt) . Binary::writeLShort(Server::getInstance()->getPort()) . Server::getInstance()->getIp(); + return crc32($cookie); + } } + From 963c5339625684183e5301cbd957db3d02d3d88b Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Sun, 28 Jul 2024 13:21:46 +0300 Subject: [PATCH 02/35] Cookie added 2 --- src/protocol/OpenConnectionRequest2.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index bc22183..360ed2a 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -21,12 +21,18 @@ class OpenConnectionRequest2 extends OfflineMessage{ public static $ID = MessageIdentifiers::ID_OPEN_CONNECTION_REQUEST_2; + public bool $serverSecurity = false; + public int $cookie = 0; public int $clientID; public InternetAddress $serverAddress; public int $mtuSize; protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); + $out->putByte(($this->serverSecurity ? 1 : 0)); + if ($this->serverSecurity) { + $out->putInt($this->cookie); + } $out->putAddress($this->serverAddress); $out->putShort($this->mtuSize); $out->putLong($this->clientID); @@ -34,6 +40,10 @@ protected function encodePayload(PacketSerializer $out) : void{ protected function decodePayload(PacketSerializer $in) : void{ $this->readMagic($in); + $this->serverSecurity = $in->getByte(); + if ($this->serverSecurity) { + $this->cookie = $in->getInt(); + } $this->serverAddress = $in->getAddress(); $this->mtuSize = $in->getShort(); $this->clientID = $in->getLong(); From c22a7d116bff93f4c594150441535785822ac32c Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Sun, 28 Jul 2024 13:28:14 +0300 Subject: [PATCH 03/35] Update OpenConnectionRequest2.php --- src/protocol/OpenConnectionRequest2.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index 360ed2a..20dd4df 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -40,7 +40,7 @@ protected function encodePayload(PacketSerializer $out) : void{ protected function decodePayload(PacketSerializer $in) : void{ $this->readMagic($in); - $this->serverSecurity = $in->getByte(); + $this->serverSecurity = $in->getByte() !== 0; if ($this->serverSecurity) { $this->cookie = $in->getInt(); } From 7425ec5ba86a9cfedded9ed673f3b00301f52a77 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Sun, 28 Jul 2024 13:55:46 +0300 Subject: [PATCH 04/35] extra byte --- src/protocol/OpenConnectionRequest2.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index 20dd4df..05ab8c8 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -29,9 +29,9 @@ class OpenConnectionRequest2 extends OfflineMessage{ protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); - $out->putByte(($this->serverSecurity ? 1 : 0)); if ($this->serverSecurity) { $out->putInt($this->cookie); + $out->putByte(false); // WHY MOJANG? } $out->putAddress($this->serverAddress); $out->putShort($this->mtuSize); @@ -40,9 +40,9 @@ protected function encodePayload(PacketSerializer $out) : void{ protected function decodePayload(PacketSerializer $in) : void{ $this->readMagic($in); - $this->serverSecurity = $in->getByte() !== 0; if ($this->serverSecurity) { $this->cookie = $in->getInt(); + $in->getByte(); // JUST 5 BYTES AND THERE IS WEIRD EXTRA BYTE } $this->serverAddress = $in->getAddress(); $this->mtuSize = $in->getShort(); From 80e89f08a4954f2ba90c39252eb5eeddb37fc362 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Mon, 29 Jul 2024 11:02:15 +0300 Subject: [PATCH 05/35] some fixes --- src/protocol/OpenConnectionReply1.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/protocol/OpenConnectionReply1.php b/src/protocol/OpenConnectionReply1.php index 1f1bff4..32db708 100644 --- a/src/protocol/OpenConnectionReply1.php +++ b/src/protocol/OpenConnectionReply1.php @@ -20,14 +20,15 @@ class OpenConnectionReply1 extends OfflineMessage{ public static $ID = MessageIdentifiers::ID_OPEN_CONNECTION_REPLY_1; public int $serverID; - public bool $serverSecurity = false; - public int $cookie = 0; + public bool $serverSecurity; + public int $cookie; public int $mtuSize; - public static function create(int $serverId, bool $serverSecurity, int $mtuSize) : self{ + public static function create(int $serverId, bool $serverSecurity, int $cookie, int $mtuSize) : self{ $result = new self; $result->serverID = $serverId; $result->serverSecurity = $serverSecurity; + $result->cookie = $cookie; $result->mtuSize = $mtuSize; return $result; } @@ -37,7 +38,7 @@ protected function encodePayload(PacketSerializer $out) : void{ $out->putLong($this->serverID); $out->putByte($this->serverSecurity ? 1 : 0); if ($this->serverSecurity) { - $out->putInt($this->createCookie()); + $out->putInt($this->cookie); } $out->putShort($this->mtuSize); } @@ -51,11 +52,4 @@ protected function decodePayload(PacketSerializer $in) : void{ } $this->mtuSize = $in->getShort(); } - - private function createCookie() : int { - $cookieSalt = mt_rand(0, 0xFFFFFFFF); - $cookie = Binary::writeLInt($cookieSalt) . Binary::writeLShort(Server::getInstance()->getPort()) . Server::getInstance()->getIp(); - return crc32($cookie); - } } - From c58e2643b0c8245469c19d8df22dd61e1a418d4d Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Mon, 29 Jul 2024 08:13:19 +0000 Subject: [PATCH 06/35] updated unconnected message handler --- src/server/UnconnectedMessageHandler.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index 2ab7bad..1709515 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -81,8 +81,13 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->sendPacket(IncompatibleProtocolVersion::create($this->protocolAcceptor->getPrimaryVersion(), $this->server->getID()), $address); $this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)"); }else{ + $serverHasSecurity = false; // then relocate it ve make it available + $cookie = 0; + if ($serverHasSecurity) { + $cookie = crc32(Binary::writeLInt(mt_rand(0, 0xFFFFFFFF)) . Binary::writeLShort($address->getPort()) . $address->getIp()); + } //IP header size (20 bytes) + UDP header size (8 bytes) - $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), false, $packet->mtuSize + 28), $address); + $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), $serverHasSecurity, $cookie, $packet->mtuSize + 28), $address); } }elseif($packet instanceof OpenConnectionRequest2){ if($packet->serverAddress->getPort() === $this->server->getPort() or !$this->server->portChecking){ From 967555e7ecdfb7a3452b5a9dfef1fae58c2240e1 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Mon, 29 Jul 2024 11:17:31 +0300 Subject: [PATCH 07/35] oops --- src/server/UnconnectedMessageHandler.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index 1709515..9efed20 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -17,6 +17,7 @@ namespace raklib\server; use pocketmine\utils\BinaryDataException; +use pocketmine\utils\Binary; use raklib\generic\Session; use raklib\protocol\IncompatibleProtocolVersion; use raklib\protocol\MessageIdentifiers; From 78f5cf57f1ab8a9428b0529ee5d37f166b6eef02 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Mon, 29 Jul 2024 11:19:20 +0300 Subject: [PATCH 08/35] Update OpenConnectionRequest2.php --- src/protocol/OpenConnectionRequest2.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index 05ab8c8..9af9d67 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -31,7 +31,7 @@ protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); if ($this->serverSecurity) { $out->putInt($this->cookie); - $out->putByte(false); // WHY MOJANG? + $out->putByte(0); // WHY MOJANG? } $out->putAddress($this->serverAddress); $out->putShort($this->mtuSize); From 3cdf46e9645ce493041e00ad9152ac4ffaf9dae6 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Tue, 30 Jul 2024 21:57:30 +0000 Subject: [PATCH 09/35] Cookie Class --- src/protocol/OpenConnectionRequest2.php | 2 +- src/server/UnconnectedMessageHandler.php | 14 ++++-- src/utils/Cookie.php | 62 ++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 src/utils/Cookie.php diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index 9af9d67..05ab8c8 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -31,7 +31,7 @@ protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); if ($this->serverSecurity) { $out->putInt($this->cookie); - $out->putByte(0); // WHY MOJANG? + $out->putByte(false); // WHY MOJANG? } $out->putAddress($this->serverAddress); $out->putShort($this->mtuSize); diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index 9efed20..6838c71 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -17,7 +17,6 @@ namespace raklib\server; use pocketmine\utils\BinaryDataException; -use pocketmine\utils\Binary; use raklib\generic\Session; use raklib\protocol\IncompatibleProtocolVersion; use raklib\protocol\MessageIdentifiers; @@ -31,6 +30,7 @@ use raklib\protocol\UnconnectedPingOpenConnections; use raklib\protocol\UnconnectedPong; use raklib\utils\InternetAddress; +use raklib\utils\Cookie; use function get_class; use function min; use function ord; @@ -82,13 +82,12 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->sendPacket(IncompatibleProtocolVersion::create($this->protocolAcceptor->getPrimaryVersion(), $this->server->getID()), $address); $this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)"); }else{ - $serverHasSecurity = false; // then relocate it ve make it available - $cookie = 0; + $serverHasSecurity = false; // then relocate it and make it available if ($serverHasSecurity) { - $cookie = crc32(Binary::writeLInt(mt_rand(0, 0xFFFFFFFF)) . Binary::writeLShort($address->getPort()) . $address->getIp()); + Cookie::add($address); } //IP header size (20 bytes) + UDP header size (8 bytes) - $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), $serverHasSecurity, $cookie, $packet->mtuSize + 28), $address); + $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), $serverHasSecurity, Cookie::get($address), $packet->mtuSize + 28), $address); } }elseif($packet instanceof OpenConnectionRequest2){ if($packet->serverAddress->getPort() === $this->server->getPort() or !$this->server->portChecking){ @@ -103,6 +102,11 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->debug("Not creating session for $address due to session already opened"); return true; } + if (!Cookie::check($address, $packet->cookie)) { + // Disconnect if OpenConnectionReply1 and the cookie in the OpenCnnectionRequest2 packet do not match + $this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies"); + return true; + } $mtuSize = min($packet->mtuSize, $this->server->getMaxMtuSize()); //Max size, do not allow creating large buffers to fill server memory $this->server->sendPacket(OpenConnectionReply2::create($this->server->getID(), $address, $mtuSize, false), $address); $this->server->createSession($address, $packet->clientID, $mtuSize); diff --git a/src/utils/Cookie.php b/src/utils/Cookie.php new file mode 100644 index 0000000..a083406 --- /dev/null +++ b/src/utils/Cookie.php @@ -0,0 +1,62 @@ + + * + * RakLib is not affiliated with Jenkins Software LLC nor RakNet. + * + * RakLib is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + */ + + declare(strict_types=1); + + namespace raklib\utils; + + use raklib\utils\InternetAddress; + use pocketmine\utils\Binary; + use function mt_rand; + use function crc32; + + + final class Cookie{ + + private static array $cookies = []; + + public static function get(InternetAddress $address) : int{ + if (isset(self::$cookies[$address->toString()])) { + return self::$cookies[$address->toString()]; + } + return 0; + } + + public static function check(InternetAddress $address, int $cookie) : bool{ + $addressStr = $address->toString(); + + if (isset(self::$cookies[$addressStr])) { + if (self::$cookies[$addressStr] == $cookie) { + // If it checks the Cookie, it means that it is in the OpenConnectionRequest2 phase and we can delete it from memory + unset(self::$cookies[$addressStr]); + return true; + } + } else { + $e = new \Exception(); // can u fix that? + } + + unset(self::$cookies[$addressStr]); + return false; + } + + public static function add(InternetAddress $address) : void{ + if (!isset(self::$cookies[$address->toString()])) { + self::$cookies[$address->toString()] = self::generateCookie($address); + } + } + + private static function generate(InternetAddress $address) : int{ + return crc32(Binary::writeLInt(mt_rand(0, 0xFFFFFFFF)) . Binary::writeLShort($address->getPort()) . $address->getIp()); + } +} From 1c94a8c7dda6308f1a5bf6eef68289b4c464c081 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Wed, 31 Jul 2024 00:58:27 +0300 Subject: [PATCH 10/35] againagainagain --- src/protocol/OpenConnectionRequest2.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index 05ab8c8..9af9d67 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -31,7 +31,7 @@ protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); if ($this->serverSecurity) { $out->putInt($this->cookie); - $out->putByte(false); // WHY MOJANG? + $out->putByte(0); // WHY MOJANG? } $out->putAddress($this->serverAddress); $out->putShort($this->mtuSize); From 440196ccfd22c84d02a0c398bcdf42e8fb629c2a Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Wed, 31 Jul 2024 01:02:18 +0300 Subject: [PATCH 11/35] Update Cookie.php --- src/utils/Cookie.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/utils/Cookie.php b/src/utils/Cookie.php index a083406..141a9f3 100644 --- a/src/utils/Cookie.php +++ b/src/utils/Cookie.php @@ -23,7 +23,10 @@ final class Cookie{ - + + /** + * @var (string|int)[] $cookies + */ private static array $cookies = []; public static function get(InternetAddress $address) : int{ @@ -52,7 +55,7 @@ public static function check(InternetAddress $address, int $cookie) : bool{ public static function add(InternetAddress $address) : void{ if (!isset(self::$cookies[$address->toString()])) { - self::$cookies[$address->toString()] = self::generateCookie($address); + self::$cookies[$address->toString()] = self::generate($address); } } From 1287f4c03db938a5c0cdf2317a946fbcb2e9c88f Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Wed, 31 Jul 2024 01:04:30 +0300 Subject: [PATCH 12/35] Update Cookie.php aahhhh.... --- src/utils/Cookie.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/Cookie.php b/src/utils/Cookie.php index 141a9f3..abe2788 100644 --- a/src/utils/Cookie.php +++ b/src/utils/Cookie.php @@ -25,7 +25,7 @@ final class Cookie{ /** - * @var (string|int)[] $cookies + * @var array $cookies */ private static array $cookies = []; From 3764e4ba8903fcb2b50b67fa1579a2ca3d66853b Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Sat, 3 Aug 2024 10:49:44 +0000 Subject: [PATCH 13/35] for now --- src/server/UnconnectedMessageHandler.php | 13 +++++++------ src/utils/Cookie.php | 2 ++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index 6838c71..a8bb86b 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -82,8 +82,7 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->sendPacket(IncompatibleProtocolVersion::create($this->protocolAcceptor->getPrimaryVersion(), $this->server->getID()), $address); $this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)"); }else{ - $serverHasSecurity = false; // then relocate it and make it available - if ($serverHasSecurity) { + if (!Cookie::$disableCookies) { Cookie::add($address); } //IP header size (20 bytes) + UDP header size (8 bytes) @@ -102,10 +101,12 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->debug("Not creating session for $address due to session already opened"); return true; } - if (!Cookie::check($address, $packet->cookie)) { - // Disconnect if OpenConnectionReply1 and the cookie in the OpenCnnectionRequest2 packet do not match - $this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies"); - return true; + if (!Cookie::$disableCookies) { + if (!Cookie::check($address, $packet->cookie)) { + // Disconnect if OpenConnectionReply1 and the cookie in the OpenCnnectionRequest2 packet do not match + $this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies"); + return true; + } } $mtuSize = min($packet->mtuSize, $this->server->getMaxMtuSize()); //Max size, do not allow creating large buffers to fill server memory $this->server->sendPacket(OpenConnectionReply2::create($this->server->getID(), $address, $mtuSize, false), $address); diff --git a/src/utils/Cookie.php b/src/utils/Cookie.php index abe2788..7a4ad09 100644 --- a/src/utils/Cookie.php +++ b/src/utils/Cookie.php @@ -24,6 +24,8 @@ final class Cookie{ + public static bool $disableCookies = true; + /** * @var array $cookies */ From 312e11e516848821264b39e4ecacd1a178264088 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Sat, 3 Aug 2024 10:52:31 +0000 Subject: [PATCH 14/35] fix --- src/server/UnconnectedMessageHandler.php | 6 +++--- src/utils/Cookie.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index a8bb86b..741faa2 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -82,11 +82,11 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->sendPacket(IncompatibleProtocolVersion::create($this->protocolAcceptor->getPrimaryVersion(), $this->server->getID()), $address); $this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)"); }else{ - if (!Cookie::$disableCookies) { + if (Cookie::$serverHasSecurity) { Cookie::add($address); } //IP header size (20 bytes) + UDP header size (8 bytes) - $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), $serverHasSecurity, Cookie::get($address), $packet->mtuSize + 28), $address); + $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), Cookie::$serverHasSecurity, Cookie::get($address), $packet->mtuSize + 28), $address); } }elseif($packet instanceof OpenConnectionRequest2){ if($packet->serverAddress->getPort() === $this->server->getPort() or !$this->server->portChecking){ @@ -101,7 +101,7 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->debug("Not creating session for $address due to session already opened"); return true; } - if (!Cookie::$disableCookies) { + if (Cookie::$serverHasSecurity) { if (!Cookie::check($address, $packet->cookie)) { // Disconnect if OpenConnectionReply1 and the cookie in the OpenCnnectionRequest2 packet do not match $this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies"); diff --git a/src/utils/Cookie.php b/src/utils/Cookie.php index 7a4ad09..8f40b1f 100644 --- a/src/utils/Cookie.php +++ b/src/utils/Cookie.php @@ -24,7 +24,7 @@ final class Cookie{ - public static bool $disableCookies = true; + public static bool $serverHasSecurity = false; /** * @var array $cookies From 90788181364f1aa9223d05f7f5f487ecce9cc593 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Sat, 3 Aug 2024 15:38:47 +0000 Subject: [PATCH 15/35] some fixes --- src/protocol/OpenConnectionReply1.php | 2 +- src/protocol/OpenConnectionRequest2.php | 10 +++++----- src/server/UnconnectedMessageHandler.php | 5 ++++- src/utils/Cookie.php | 11 +++-------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/protocol/OpenConnectionReply1.php b/src/protocol/OpenConnectionReply1.php index 32db708..e1b3748 100644 --- a/src/protocol/OpenConnectionReply1.php +++ b/src/protocol/OpenConnectionReply1.php @@ -24,7 +24,7 @@ class OpenConnectionReply1 extends OfflineMessage{ public int $cookie; public int $mtuSize; - public static function create(int $serverId, bool $serverSecurity, int $cookie, int $mtuSize) : self{ + public static function create(int $serverId, bool $serverSecurity, int $cookie = null, int $mtuSize) : self{ $result = new self; $result->serverID = $serverId; $result->serverSecurity = $serverSecurity; diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index 9af9d67..8fea6f8 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -17,11 +17,11 @@ namespace raklib\protocol; use raklib\utils\InternetAddress; +use raklib\utils\Cookie; class OpenConnectionRequest2 extends OfflineMessage{ public static $ID = MessageIdentifiers::ID_OPEN_CONNECTION_REQUEST_2; - public bool $serverSecurity = false; public int $cookie = 0; public int $clientID; public InternetAddress $serverAddress; @@ -29,9 +29,9 @@ class OpenConnectionRequest2 extends OfflineMessage{ protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); - if ($this->serverSecurity) { + if (Cookie::$serverHasSecurity) { $out->putInt($this->cookie); - $out->putByte(0); // WHY MOJANG? + $out->putBool(0); // Client wrote challenge } $out->putAddress($this->serverAddress); $out->putShort($this->mtuSize); @@ -40,9 +40,9 @@ protected function encodePayload(PacketSerializer $out) : void{ protected function decodePayload(PacketSerializer $in) : void{ $this->readMagic($in); - if ($this->serverSecurity) { + if (Cookie::$serverHasSecurity) { $this->cookie = $in->getInt(); - $in->getByte(); // JUST 5 BYTES AND THERE IS WEIRD EXTRA BYTE + $in->getBool(); } $this->serverAddress = $in->getAddress(); $this->mtuSize = $in->getShort(); diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index 741faa2..d9df66d 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -82,13 +82,16 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->sendPacket(IncompatibleProtocolVersion::create($this->protocolAcceptor->getPrimaryVersion(), $this->server->getID()), $address); $this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)"); }else{ + $cookie = null; if (Cookie::$serverHasSecurity) { Cookie::add($address); + $cookie = Cookie::get($address); } //IP header size (20 bytes) + UDP header size (8 bytes) - $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), Cookie::$serverHasSecurity, Cookie::get($address), $packet->mtuSize + 28), $address); + $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), Cookie::$serverHasSecurity, $cookie, $packet->mtuSize + 28), $address); } }elseif($packet instanceof OpenConnectionRequest2){ + // The client may not send such data even though serverSecurity is enabled, and if we try to decode this, we may encounter an error if($packet->serverAddress->getPort() === $this->server->getPort() or !$this->server->portChecking){ if($packet->mtuSize < Session::MIN_MTU_SIZE){ $this->server->getLogger()->debug("Not creating session for $address due to bad MTU size $packet->mtuSize"); diff --git a/src/utils/Cookie.php b/src/utils/Cookie.php index 8f40b1f..5c5ca97 100644 --- a/src/utils/Cookie.php +++ b/src/utils/Cookie.php @@ -20,7 +20,6 @@ use pocketmine\utils\Binary; use function mt_rand; use function crc32; - final class Cookie{ @@ -42,16 +41,12 @@ public static function check(InternetAddress $address, int $cookie) : bool{ $addressStr = $address->toString(); if (isset(self::$cookies[$addressStr])) { + // If it checks the Cookie, it means that it is in the OpenConnectionRequest2 phase and we can delete it from memory + unset(self::$cookies[$addressStr]); if (self::$cookies[$addressStr] == $cookie) { - // If it checks the Cookie, it means that it is in the OpenConnectionRequest2 phase and we can delete it from memory - unset(self::$cookies[$addressStr]); return true; } - } else { - $e = new \Exception(); // can u fix that? - } - - unset(self::$cookies[$addressStr]); + } // Is there any chance that this is something else? return false; } From 9c11bb460df85b268d584383031d7e5bf1bee583 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Sat, 3 Aug 2024 15:41:33 +0000 Subject: [PATCH 16/35] okay --- src/protocol/OpenConnectionReply1.php | 2 +- src/protocol/OpenConnectionRequest2.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/protocol/OpenConnectionReply1.php b/src/protocol/OpenConnectionReply1.php index e1b3748..26a91f6 100644 --- a/src/protocol/OpenConnectionReply1.php +++ b/src/protocol/OpenConnectionReply1.php @@ -21,7 +21,7 @@ class OpenConnectionReply1 extends OfflineMessage{ public int $serverID; public bool $serverSecurity; - public int $cookie; + public int|null $cookie; public int $mtuSize; public static function create(int $serverId, bool $serverSecurity, int $cookie = null, int $mtuSize) : self{ diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index 8fea6f8..2152042 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -31,7 +31,7 @@ protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); if (Cookie::$serverHasSecurity) { $out->putInt($this->cookie); - $out->putBool(0); // Client wrote challenge + $out->putBool(false); // Client wrote challenge } $out->putAddress($this->serverAddress); $out->putShort($this->mtuSize); From ba4c754a7014241f2d5eaf4403c03680b74b0522 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Sat, 3 Aug 2024 15:46:25 +0000 Subject: [PATCH 17/35] is it right to add that lol --- src/protocol/OpenConnectionReply1.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/protocol/OpenConnectionReply1.php b/src/protocol/OpenConnectionReply1.php index 26a91f6..1024c16 100644 --- a/src/protocol/OpenConnectionReply1.php +++ b/src/protocol/OpenConnectionReply1.php @@ -16,6 +16,8 @@ namespace raklib\protocol; +use function is_int; + class OpenConnectionReply1 extends OfflineMessage{ public static $ID = MessageIdentifiers::ID_OPEN_CONNECTION_REPLY_1; @@ -37,7 +39,7 @@ protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); $out->putLong($this->serverID); $out->putByte($this->serverSecurity ? 1 : 0); - if ($this->serverSecurity) { + if ($this->serverSecurity && is_int($this->cookie)) { $out->putInt($this->cookie); } $out->putShort($this->mtuSize); From 9555243f164da94795b2179ad98555d0ec83b533 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:11:23 +0000 Subject: [PATCH 18/35] client supports security --- src/protocol/OpenConnectionRequest2.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index 2152042..8c783f5 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -24,6 +24,7 @@ class OpenConnectionRequest2 extends OfflineMessage{ public int $cookie = 0; public int $clientID; + public bool $clientSupportsSecurity = false; // Always false for the vanilla client. public InternetAddress $serverAddress; public int $mtuSize; @@ -31,7 +32,7 @@ protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); if (Cookie::$serverHasSecurity) { $out->putInt($this->cookie); - $out->putBool(false); // Client wrote challenge + $out->putBool($this->clientSupportsSecurity); } $out->putAddress($this->serverAddress); $out->putShort($this->mtuSize); @@ -42,7 +43,7 @@ protected function decodePayload(PacketSerializer $in) : void{ $this->readMagic($in); if (Cookie::$serverHasSecurity) { $this->cookie = $in->getInt(); - $in->getBool(); + $this->clientSupportsSecurity = $in->getBool(); } $this->serverAddress = $in->getAddress(); $this->mtuSize = $in->getShort(); From ec8532b7f1ccf6d6b75cba0885814044bf3dae48 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Thu, 8 Aug 2024 16:04:38 +0000 Subject: [PATCH 19/35] added static function --- src/protocol/OpenConnectionReply1.php | 4 ++-- src/protocol/OpenConnectionRequest2.php | 4 ++-- src/server/UnconnectedMessageHandler.php | 6 +++--- src/utils/Cookie.php | 4 ++++ 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/protocol/OpenConnectionReply1.php b/src/protocol/OpenConnectionReply1.php index 1024c16..4adf23f 100644 --- a/src/protocol/OpenConnectionReply1.php +++ b/src/protocol/OpenConnectionReply1.php @@ -23,10 +23,10 @@ class OpenConnectionReply1 extends OfflineMessage{ public int $serverID; public bool $serverSecurity; - public int|null $cookie; + public ?int $cookie; public int $mtuSize; - public static function create(int $serverId, bool $serverSecurity, int $cookie = null, int $mtuSize) : self{ + public static function create(int $serverId, bool $serverSecurity, ?int $cookie = null, int $mtuSize) : self{ $result = new self; $result->serverID = $serverId; $result->serverSecurity = $serverSecurity; diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index 8c783f5..bd7f7d8 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -30,7 +30,7 @@ class OpenConnectionRequest2 extends OfflineMessage{ protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); - if (Cookie::$serverHasSecurity) { + if (Cookie::hasServerSecurity()) { $out->putInt($this->cookie); $out->putBool($this->clientSupportsSecurity); } @@ -41,7 +41,7 @@ protected function encodePayload(PacketSerializer $out) : void{ protected function decodePayload(PacketSerializer $in) : void{ $this->readMagic($in); - if (Cookie::$serverHasSecurity) { + if (Cookie::hasServerSecurity()) { $this->cookie = $in->getInt(); $this->clientSupportsSecurity = $in->getBool(); } diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index d9df66d..5cee0f0 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -83,12 +83,12 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)"); }else{ $cookie = null; - if (Cookie::$serverHasSecurity) { + if (Cookie::hasServerSecurity()) { Cookie::add($address); $cookie = Cookie::get($address); } //IP header size (20 bytes) + UDP header size (8 bytes) - $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), Cookie::$serverHasSecurity, $cookie, $packet->mtuSize + 28), $address); + $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), Cookie::hasServerSecurity(), $cookie, $packet->mtuSize + 28), $address); } }elseif($packet instanceof OpenConnectionRequest2){ // The client may not send such data even though serverSecurity is enabled, and if we try to decode this, we may encounter an error @@ -104,7 +104,7 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->debug("Not creating session for $address due to session already opened"); return true; } - if (Cookie::$serverHasSecurity) { + if (Cookie::hasServerSecurity()) { if (!Cookie::check($address, $packet->cookie)) { // Disconnect if OpenConnectionReply1 and the cookie in the OpenCnnectionRequest2 packet do not match $this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies"); diff --git a/src/utils/Cookie.php b/src/utils/Cookie.php index 5c5ca97..56daac0 100644 --- a/src/utils/Cookie.php +++ b/src/utils/Cookie.php @@ -37,6 +37,10 @@ public static function get(InternetAddress $address) : int{ return 0; } + public static function hasServerSecurity () : bool { + return self::$serverHasSecurity; + } + public static function check(InternetAddress $address, int $cookie) : bool{ $addressStr = $address->toString(); From 884bbff5139b9635806a2966c4d38b47c357a3c2 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:38:01 +0000 Subject: [PATCH 20/35] non-static funcs --- src/protocol/OfflineMessage.php | 9 +-- src/protocol/OpenConnectionReply1.php | 2 +- src/protocol/OpenConnectionRequest2.php | 7 +- src/server/Server.php | 15 +++++ src/server/UnconnectedMessageHandler.php | 15 ++--- src/utils/Cookie.php | 83 ++++++++++++------------ 6 files changed, 71 insertions(+), 60 deletions(-) diff --git a/src/protocol/OfflineMessage.php b/src/protocol/OfflineMessage.php index d157d0f..754521b 100644 --- a/src/protocol/OfflineMessage.php +++ b/src/protocol/OfflineMessage.php @@ -18,6 +18,7 @@ use pocketmine\utils\BinaryDataException; use pocketmine\utils\BinaryStream; +use raklib\utils\Cookie; abstract class OfflineMessage extends Packet{ @@ -29,17 +30,13 @@ abstract class OfflineMessage extends Packet{ protected string $magic = self::MAGIC; /** - * @return void * @throws BinaryDataException */ - protected function readMagic(BinaryStream $in){ + protected function readMagic(BinaryStream $in) : void{ $this->magic = $in->get(16); } - /** - * @return void - */ - protected function writeMagic(BinaryStream $out){ + protected function writeMagic(BinaryStream $out) : void{ $out->put($this->magic); } diff --git a/src/protocol/OpenConnectionReply1.php b/src/protocol/OpenConnectionReply1.php index 4adf23f..2a3f62f 100644 --- a/src/protocol/OpenConnectionReply1.php +++ b/src/protocol/OpenConnectionReply1.php @@ -26,7 +26,7 @@ class OpenConnectionReply1 extends OfflineMessage{ public ?int $cookie; public int $mtuSize; - public static function create(int $serverId, bool $serverSecurity, ?int $cookie = null, int $mtuSize) : self{ + public static function create(int $serverId, bool $serverSecurity, ?int $cookie, int $mtuSize) : self{ $result = new self; $result->serverID = $serverId; $result->serverSecurity = $serverSecurity; diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index bd7f7d8..4ab1c22 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -17,20 +17,19 @@ namespace raklib\protocol; use raklib\utils\InternetAddress; -use raklib\utils\Cookie; class OpenConnectionRequest2 extends OfflineMessage{ public static $ID = MessageIdentifiers::ID_OPEN_CONNECTION_REQUEST_2; - public int $cookie = 0; public int $clientID; + public ?int $cookie; public bool $clientSupportsSecurity = false; // Always false for the vanilla client. public InternetAddress $serverAddress; public int $mtuSize; protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); - if (Cookie::hasServerSecurity()) { + if ($this->cookie !== null) { $out->putInt($this->cookie); $out->putBool($this->clientSupportsSecurity); } @@ -41,7 +40,7 @@ protected function encodePayload(PacketSerializer $out) : void{ protected function decodePayload(PacketSerializer $in) : void{ $this->readMagic($in); - if (Cookie::hasServerSecurity()) { + if ($this->cookie !== null) { $this->cookie = $in->getInt(); $this->clientSupportsSecurity = $in->getBool(); } diff --git a/src/server/Server.php b/src/server/Server.php index 359ca1d..ef1c0f0 100644 --- a/src/server/Server.php +++ b/src/server/Server.php @@ -27,6 +27,7 @@ use raklib\protocol\NACK; use raklib\protocol\Packet; use raklib\protocol\PacketSerializer; +use raklib\utils\Cookie; use raklib\utils\ExceptionTraceCleaner; use raklib\utils\InternetAddress; use function asort; @@ -77,6 +78,10 @@ class Server implements ServerInterface{ protected int $nextSessionId = 0; + public bool $hasServerSecurity = false; + + public ?Cookie $cookie; + /** * @phpstan-param positive-int $recvMaxSplitParts * @phpstan-param positive-int $recvMaxConcurrentSplits @@ -99,6 +104,8 @@ public function __construct( $this->socket->setBlocking(false); $this->unconnectedMessageHandler = new UnconnectedMessageHandler($this, $protocolAcceptor); + + $this->cookie = Cookie::setServerSecurity($this->hasServerSecurity()); } public function getPort() : int{ @@ -113,6 +120,14 @@ public function getLogger() : \Logger{ return $this->logger; } + public function hasServerSecurity() : bool { + return $this->hasServerSecurity; + } + + public function getCookie() : ?Cookie { + return $this->cookie; + } + public function tickProcessor() : void{ $start = microtime(true); diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index 5cee0f0..bd2fdf5 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -30,7 +30,6 @@ use raklib\protocol\UnconnectedPingOpenConnections; use raklib\protocol\UnconnectedPong; use raklib\utils\InternetAddress; -use raklib\utils\Cookie; use function get_class; use function min; use function ord; @@ -83,12 +82,12 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)"); }else{ $cookie = null; - if (Cookie::hasServerSecurity()) { - Cookie::add($address); - $cookie = Cookie::get($address); + if ($this->server->hasServerSecurity()) { + $this->server->getCookie()->add($address); + $cookie = $this->server->getCookie()->get($address); } //IP header size (20 bytes) + UDP header size (8 bytes) - $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), Cookie::hasServerSecurity(), $cookie, $packet->mtuSize + 28), $address); + $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), $this->server->hasServerSecurity(), $cookie, $packet->mtuSize + 28), $address); } }elseif($packet instanceof OpenConnectionRequest2){ // The client may not send such data even though serverSecurity is enabled, and if we try to decode this, we may encounter an error @@ -104,9 +103,9 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->debug("Not creating session for $address due to session already opened"); return true; } - if (Cookie::hasServerSecurity()) { - if (!Cookie::check($address, $packet->cookie)) { - // Disconnect if OpenConnectionReply1 and the cookie in the OpenCnnectionRequest2 packet do not match + if ($this->server->hasServerSecurity()) { // womp womp + if (!$this->server->getCookie()->check($address, $packet->cookie)) { + // Disconnect if OpenConnectionReply1 and the cookie in the OpenConnectionRequest2 packet do not match $this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies"); return true; } diff --git a/src/utils/Cookie.php b/src/utils/Cookie.php index 56daac0..526b27e 100644 --- a/src/utils/Cookie.php +++ b/src/utils/Cookie.php @@ -12,55 +12,56 @@ * (at your option) any later version. */ - declare(strict_types=1); +declare(strict_types=1); - namespace raklib\utils; +namespace raklib\utils; - use raklib\utils\InternetAddress; - use pocketmine\utils\Binary; - use function mt_rand; - use function crc32; +use raklib\utils\InternetAddress; +use pocketmine\utils\Binary; +use function mt_rand; +use function crc32; - final class Cookie{ +final class Cookie{ - public static bool $serverHasSecurity = false; + /** + * @var array $cookies + */ + private array $cookies = []; - /** - * @var array $cookies - */ - private static array $cookies = []; + public function get(InternetAddress $address) : int{ + if (isset($this->cookies[$address->toString()])) { + return $this->cookies[$address->toString()]; + } + return 0; + } - public static function get(InternetAddress $address) : int{ - if (isset(self::$cookies[$address->toString()])) { - return self::$cookies[$address->toString()]; - } - return 0; - } + public static function setServerSecurity(bool $security) : ?Cookie { + if ($security) { + return new Cookie(); + } + return null; + } - public static function hasServerSecurity () : bool { - return self::$serverHasSecurity; - } + public function check(InternetAddress $address, int $cookie) : bool{ + $addressStr = $address->toString(); - public static function check(InternetAddress $address, int $cookie) : bool{ - $addressStr = $address->toString(); + if (isset($this->cookies[$addressStr])) { + // If it checks the Cookie, it means that it is in the OpenConnectionRequest2 phase, and we can delete it from memory. + unset($this->cookies[$addressStr]); + if ($this->cookies[$addressStr] == $cookie) { + return true; + } + } // Is there any chance that this is something else? + return false; + } - if (isset(self::$cookies[$addressStr])) { - // If it checks the Cookie, it means that it is in the OpenConnectionRequest2 phase and we can delete it from memory - unset(self::$cookies[$addressStr]); - if (self::$cookies[$addressStr] == $cookie) { - return true; - } - } // Is there any chance that this is something else? - return false; - } + public function add(InternetAddress $address) : void{ + if (!isset($this->cookies[$address->toString()])) { + $this->cookies[$address->toString()] = $this->generate($address); + } + } - public static function add(InternetAddress $address) : void{ - if (!isset(self::$cookies[$address->toString()])) { - self::$cookies[$address->toString()] = self::generate($address); - } - } - - private static function generate(InternetAddress $address) : int{ - return crc32(Binary::writeLInt(mt_rand(0, 0xFFFFFFFF)) . Binary::writeLShort($address->getPort()) . $address->getIp()); - } + private function generate(InternetAddress $address) : int{ + return crc32(Binary::writeLInt(mt_rand(0, 0xFFFFFFFF)) . Binary::writeLShort($address->getPort()) . $address->getIp()); + } } From b6c85e7699973e1843d62befed9abd5db64472e2 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:42:39 +0000 Subject: [PATCH 21/35] fix nullable --- src/server/UnconnectedMessageHandler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index bd2fdf5..73625fa 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -82,7 +82,7 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)"); }else{ $cookie = null; - if ($this->server->hasServerSecurity()) { + if ($this->server->getCookie() instanceof Cookie) { $this->server->getCookie()->add($address); $cookie = $this->server->getCookie()->get($address); } @@ -103,7 +103,7 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->debug("Not creating session for $address due to session already opened"); return true; } - if ($this->server->hasServerSecurity()) { // womp womp + if ($this->server->getCookie() instanceof Cookie) { // womp womp if (!$this->server->getCookie()->check($address, $packet->cookie)) { // Disconnect if OpenConnectionReply1 and the cookie in the OpenConnectionRequest2 packet do not match $this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies"); From 15d1b13da8002d682d141d161af6ac0fb6752e57 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:43:51 +0000 Subject: [PATCH 22/35] use method --- src/server/UnconnectedMessageHandler.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index 73625fa..4219a57 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -30,6 +30,7 @@ use raklib\protocol\UnconnectedPingOpenConnections; use raklib\protocol\UnconnectedPong; use raklib\utils\InternetAddress; +use raklib\utils\Cookie; use function get_class; use function min; use function ord; From 75e85266008bcbe80669a41bc22bc19ee912deba Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:47:42 +0000 Subject: [PATCH 23/35] i forgot --- src/protocol/OfflineMessage.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/protocol/OfflineMessage.php b/src/protocol/OfflineMessage.php index 754521b..421fd09 100644 --- a/src/protocol/OfflineMessage.php +++ b/src/protocol/OfflineMessage.php @@ -18,7 +18,6 @@ use pocketmine\utils\BinaryDataException; use pocketmine\utils\BinaryStream; -use raklib\utils\Cookie; abstract class OfflineMessage extends Packet{ From ff2a37ea6373db82615ade233e883bb096c39be0 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Fri, 9 Aug 2024 15:44:54 +0000 Subject: [PATCH 24/35] fix but not finished --- src/protocol/OfflineMessage.php | 10 +++++++--- src/protocol/OpenConnectionReply1.php | 8 ++++++-- src/protocol/OpenConnectionRequest2.php | 1 + src/server/Server.php | 11 +++-------- src/server/UnconnectedMessageHandler.php | 4 +--- src/utils/Cookie.php | 17 +++++------------ 6 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/protocol/OfflineMessage.php b/src/protocol/OfflineMessage.php index 421fd09..ec4fc06 100644 --- a/src/protocol/OfflineMessage.php +++ b/src/protocol/OfflineMessage.php @@ -29,17 +29,21 @@ abstract class OfflineMessage extends Packet{ protected string $magic = self::MAGIC; /** + * @return void * @throws BinaryDataException */ - protected function readMagic(BinaryStream $in) : void{ + protected function readMagic(BinaryStream $in){ $this->magic = $in->get(16); } - protected function writeMagic(BinaryStream $out) : void{ + /** + * @return void + */ + protected function writeMagic(BinaryStream $out){ $out->put($this->magic); } public function isValid() : bool{ return $this->magic === self::MAGIC; } -} +} \ No newline at end of file diff --git a/src/protocol/OpenConnectionReply1.php b/src/protocol/OpenConnectionReply1.php index 2a3f62f..e37783c 100644 --- a/src/protocol/OpenConnectionReply1.php +++ b/src/protocol/OpenConnectionReply1.php @@ -26,10 +26,14 @@ class OpenConnectionReply1 extends OfflineMessage{ public ?int $cookie; public int $mtuSize; - public static function create(int $serverId, bool $serverSecurity, ?int $cookie, int $mtuSize) : self{ + public static function create(int $serverId, ?int $cookie, int $mtuSize) : self{ $result = new self; $result->serverID = $serverId; - $result->serverSecurity = $serverSecurity; + if ($cookie !== null) { + $result->serverSecurity = true; + } else { + $result->serverSecurity = false; + } $result->cookie = $cookie; $result->mtuSize = $mtuSize; return $result; diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index 4ab1c22..4cd1930 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -39,6 +39,7 @@ protected function encodePayload(PacketSerializer $out) : void{ } protected function decodePayload(PacketSerializer $in) : void{ + //$length = strlen($in->getRemaining()); // magic(16) + cookie(4) + clientSupportsSecurity(1) + serverAddress(??) + mtuSize(2) + clientID(8) $this->readMagic($in); if ($this->cookie !== null) { $this->cookie = $in->getInt(); diff --git a/src/server/Server.php b/src/server/Server.php index ef1c0f0..75e2b23 100644 --- a/src/server/Server.php +++ b/src/server/Server.php @@ -78,9 +78,7 @@ class Server implements ServerInterface{ protected int $nextSessionId = 0; - public bool $hasServerSecurity = false; - - public ?Cookie $cookie; + public ?Cookie $cookie = null; /** * @phpstan-param positive-int $recvMaxSplitParts @@ -105,7 +103,8 @@ public function __construct( $this->unconnectedMessageHandler = new UnconnectedMessageHandler($this, $protocolAcceptor); - $this->cookie = Cookie::setServerSecurity($this->hasServerSecurity()); + // If you don't want to use security on the server, just delete this line. + $this->cookie = new Cookie(); } public function getPort() : int{ @@ -120,10 +119,6 @@ public function getLogger() : \Logger{ return $this->logger; } - public function hasServerSecurity() : bool { - return $this->hasServerSecurity; - } - public function getCookie() : ?Cookie { return $this->cookie; } diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index 4219a57..5e0a48e 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -82,13 +82,11 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->sendPacket(IncompatibleProtocolVersion::create($this->protocolAcceptor->getPrimaryVersion(), $this->server->getID()), $address); $this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)"); }else{ - $cookie = null; if ($this->server->getCookie() instanceof Cookie) { $this->server->getCookie()->add($address); - $cookie = $this->server->getCookie()->get($address); } //IP header size (20 bytes) + UDP header size (8 bytes) - $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), $this->server->hasServerSecurity(), $cookie, $packet->mtuSize + 28), $address); + $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), $this->server->getCookie()->get($address), $packet->mtuSize + 28), $address); } }elseif($packet instanceof OpenConnectionRequest2){ // The client may not send such data even though serverSecurity is enabled, and if we try to decode this, we may encounter an error diff --git a/src/utils/Cookie.php b/src/utils/Cookie.php index 526b27e..2310cd5 100644 --- a/src/utils/Cookie.php +++ b/src/utils/Cookie.php @@ -18,7 +18,7 @@ use raklib\utils\InternetAddress; use pocketmine\utils\Binary; -use function mt_rand; +use function random_int; use function crc32; final class Cookie{ @@ -28,17 +28,10 @@ final class Cookie{ */ private array $cookies = []; - public function get(InternetAddress $address) : int{ + public function get(InternetAddress $address) : ?int{ if (isset($this->cookies[$address->toString()])) { return $this->cookies[$address->toString()]; } - return 0; - } - - public static function setServerSecurity(bool $security) : ?Cookie { - if ($security) { - return new Cookie(); - } return null; } @@ -47,8 +40,8 @@ public function check(InternetAddress $address, int $cookie) : bool{ if (isset($this->cookies[$addressStr])) { // If it checks the Cookie, it means that it is in the OpenConnectionRequest2 phase, and we can delete it from memory. - unset($this->cookies[$addressStr]); - if ($this->cookies[$addressStr] == $cookie) { + if ($this->cookies[$addressStr] === $cookie) { + unset($this->cookies[$addressStr]); return true; } } // Is there any chance that this is something else? @@ -62,6 +55,6 @@ public function add(InternetAddress $address) : void{ } private function generate(InternetAddress $address) : int{ - return crc32(Binary::writeLInt(mt_rand(0, 0xFFFFFFFF)) . Binary::writeLShort($address->getPort()) . $address->getIp()); + return crc32(Binary::writeLInt(random_int(0, 0xffffffff)) . Binary::writeLShort($address->getPort()) . $address->getIp()); } } From 8e6c6771b4996d82886636d2ee4c4001e613c741 Mon Sep 17 00:00:00 2001 From: ismail <74091824+ismaileke@users.noreply.github.com> Date: Fri, 9 Aug 2024 16:16:30 +0000 Subject: [PATCH 25/35] uhhh --- src/server/UnconnectedMessageHandler.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index 5e0a48e..d12e352 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -82,11 +82,13 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->sendPacket(IncompatibleProtocolVersion::create($this->protocolAcceptor->getPrimaryVersion(), $this->server->getID()), $address); $this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)"); }else{ + $cookie = null; if ($this->server->getCookie() instanceof Cookie) { $this->server->getCookie()->add($address); + $cookie = $this->server->getCookie()->get($address); } //IP header size (20 bytes) + UDP header size (8 bytes) - $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), $this->server->getCookie()->get($address), $packet->mtuSize + 28), $address); + $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), $cookie, $packet->mtuSize + 28), $address); } }elseif($packet instanceof OpenConnectionRequest2){ // The client may not send such data even though serverSecurity is enabled, and if we try to decode this, we may encounter an error From f8250af03ca0747133e76d91dbaa522b3bf3a281 Mon Sep 17 00:00:00 2001 From: ismail Date: Sat, 10 Aug 2024 10:44:31 +0300 Subject: [PATCH 26/35] short usage Co-authored-by: Jonathan Chan Kwan Yin --- src/protocol/OpenConnectionReply1.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/protocol/OpenConnectionReply1.php b/src/protocol/OpenConnectionReply1.php index e37783c..1bbf503 100644 --- a/src/protocol/OpenConnectionReply1.php +++ b/src/protocol/OpenConnectionReply1.php @@ -29,11 +29,7 @@ class OpenConnectionReply1 extends OfflineMessage{ public static function create(int $serverId, ?int $cookie, int $mtuSize) : self{ $result = new self; $result->serverID = $serverId; - if ($cookie !== null) { - $result->serverSecurity = true; - } else { - $result->serverSecurity = false; - } + $result->serverSecurity = $cookie !== null; $result->cookie = $cookie; $result->mtuSize = $mtuSize; return $result; From df75ce44f0e587c2f2fdf9206a0f58e836db8500 Mon Sep 17 00:00:00 2001 From: ismail Date: Sat, 10 Aug 2024 11:36:23 +0300 Subject: [PATCH 27/35] Update src/protocol/OpenConnectionReply1.php Co-authored-by: Jonathan Chan Kwan Yin --- src/protocol/OpenConnectionReply1.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/protocol/OpenConnectionReply1.php b/src/protocol/OpenConnectionReply1.php index 1bbf503..2241e0c 100644 --- a/src/protocol/OpenConnectionReply1.php +++ b/src/protocol/OpenConnectionReply1.php @@ -39,7 +39,7 @@ protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); $out->putLong($this->serverID); $out->putByte($this->serverSecurity ? 1 : 0); - if ($this->serverSecurity && is_int($this->cookie)) { + if ($this->serverSecurity && $this->cookie !== null) { $out->putInt($this->cookie); } $out->putShort($this->mtuSize); From 64cb14007adba820fbf3f9c78df96388515e013b Mon Sep 17 00:00:00 2001 From: ismail Date: Tue, 13 Aug 2024 11:40:39 +0000 Subject: [PATCH 28/35] some fixes are not yet complete --- src/protocol/OfflineMessage.php | 2 +- src/protocol/OpenConnectionReply1.php | 7 +++---- src/server/Server.php | 8 ++++---- src/server/UnconnectedMessageHandler.php | 9 ++++----- src/utils/Cookie.php | 19 ++++++------------- 5 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/protocol/OfflineMessage.php b/src/protocol/OfflineMessage.php index ec4fc06..d157d0f 100644 --- a/src/protocol/OfflineMessage.php +++ b/src/protocol/OfflineMessage.php @@ -46,4 +46,4 @@ protected function writeMagic(BinaryStream $out){ public function isValid() : bool{ return $this->magic === self::MAGIC; } -} \ No newline at end of file +} diff --git a/src/protocol/OpenConnectionReply1.php b/src/protocol/OpenConnectionReply1.php index 2241e0c..6dfaafa 100644 --- a/src/protocol/OpenConnectionReply1.php +++ b/src/protocol/OpenConnectionReply1.php @@ -23,13 +23,12 @@ class OpenConnectionReply1 extends OfflineMessage{ public int $serverID; public bool $serverSecurity; - public ?int $cookie; + public ?int $cookie = null; public int $mtuSize; public static function create(int $serverId, ?int $cookie, int $mtuSize) : self{ $result = new self; $result->serverID = $serverId; - $result->serverSecurity = $cookie !== null; $result->cookie = $cookie; $result->mtuSize = $mtuSize; return $result; @@ -38,8 +37,8 @@ public static function create(int $serverId, ?int $cookie, int $mtuSize) : self{ protected function encodePayload(PacketSerializer $out) : void{ $this->writeMagic($out); $out->putLong($this->serverID); - $out->putByte($this->serverSecurity ? 1 : 0); - if ($this->serverSecurity && $this->cookie !== null) { + $out->putByte($this->cookie !== null ? 1 : 0); + if ($this->cookie !== null) { $out->putInt($this->cookie); } $out->putShort($this->mtuSize); diff --git a/src/server/Server.php b/src/server/Server.php index 75e2b23..1a0c089 100644 --- a/src/server/Server.php +++ b/src/server/Server.php @@ -27,7 +27,7 @@ use raklib\protocol\NACK; use raklib\protocol\Packet; use raklib\protocol\PacketSerializer; -use raklib\utils\Cookie; +use raklib\utils\CookieCache; use raklib\utils\ExceptionTraceCleaner; use raklib\utils\InternetAddress; use function asort; @@ -78,7 +78,7 @@ class Server implements ServerInterface{ protected int $nextSessionId = 0; - public ?Cookie $cookie = null; + public ?CookieCache $cookie = null; /** * @phpstan-param positive-int $recvMaxSplitParts @@ -104,7 +104,7 @@ public function __construct( $this->unconnectedMessageHandler = new UnconnectedMessageHandler($this, $protocolAcceptor); // If you don't want to use security on the server, just delete this line. - $this->cookie = new Cookie(); + $this->cookie = new CookieCache(); } public function getPort() : int{ @@ -119,7 +119,7 @@ public function getLogger() : \Logger{ return $this->logger; } - public function getCookie() : ?Cookie { + public function getCookie() : ?CookieCache { return $this->cookie; } diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index d12e352..f4d84a3 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -30,7 +30,7 @@ use raklib\protocol\UnconnectedPingOpenConnections; use raklib\protocol\UnconnectedPong; use raklib\utils\InternetAddress; -use raklib\utils\Cookie; +use raklib\utils\CookieCache; use function get_class; use function min; use function ord; @@ -83,9 +83,8 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)"); }else{ $cookie = null; - if ($this->server->getCookie() instanceof Cookie) { - $this->server->getCookie()->add($address); - $cookie = $this->server->getCookie()->get($address); + if ($this->server->getCookie() instanceof CookieCache) { + $cookie = $this->server->getCookie()->add($address); } //IP header size (20 bytes) + UDP header size (8 bytes) $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), $cookie, $packet->mtuSize + 28), $address); @@ -104,7 +103,7 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->debug("Not creating session for $address due to session already opened"); return true; } - if ($this->server->getCookie() instanceof Cookie) { // womp womp + if ($this->server->getCookie() instanceof CookieCache) { // womp womp if (!$this->server->getCookie()->check($address, $packet->cookie)) { // Disconnect if OpenConnectionReply1 and the cookie in the OpenConnectionRequest2 packet do not match $this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies"); diff --git a/src/utils/Cookie.php b/src/utils/Cookie.php index 2310cd5..b33cdf1 100644 --- a/src/utils/Cookie.php +++ b/src/utils/Cookie.php @@ -21,20 +21,13 @@ use function random_int; use function crc32; -final class Cookie{ +final class CookieCache{ /** * @var array $cookies */ private array $cookies = []; - public function get(InternetAddress $address) : ?int{ - if (isset($this->cookies[$address->toString()])) { - return $this->cookies[$address->toString()]; - } - return null; - } - public function check(InternetAddress $address, int $cookie) : bool{ $addressStr = $address->toString(); @@ -48,13 +41,13 @@ public function check(InternetAddress $address, int $cookie) : bool{ return false; } - public function add(InternetAddress $address) : void{ - if (!isset($this->cookies[$address->toString()])) { - $this->cookies[$address->toString()] = $this->generate($address); - } + public function add(InternetAddress $address) : int{ + $cookie = $this->generate($address); + $this->cookies[$address->toString()] = $cookie; + return $cookie; } private function generate(InternetAddress $address) : int{ - return crc32(Binary::writeLInt(random_int(0, 0xffffffff)) . Binary::writeLShort($address->getPort()) . $address->getIp()); + return crc32(Binary::writeLInt(random_int(0, 0xffffffff))); } } From b9bb2690ab155f98e9a5b1fa99c2a744666c602e Mon Sep 17 00:00:00 2001 From: ismail Date: Tue, 13 Aug 2024 12:09:06 +0000 Subject: [PATCH 29/35] random_int --- src/utils/Cookie.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/Cookie.php b/src/utils/Cookie.php index b33cdf1..d9cf2fb 100644 --- a/src/utils/Cookie.php +++ b/src/utils/Cookie.php @@ -48,6 +48,6 @@ public function add(InternetAddress $address) : int{ } private function generate(InternetAddress $address) : int{ - return crc32(Binary::writeLInt(random_int(0, 0xffffffff))); + return random_int(0, 0xffffffff); } } From 65e2da836273cf321a357d67e23e43f953755ea5 Mon Sep 17 00:00:00 2001 From: ismail Date: Tue, 13 Aug 2024 12:59:29 +0000 Subject: [PATCH 30/35] Limits --- src/utils/Cookie.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/Cookie.php b/src/utils/Cookie.php index d9cf2fb..e62b4df 100644 --- a/src/utils/Cookie.php +++ b/src/utils/Cookie.php @@ -18,6 +18,7 @@ use raklib\utils\InternetAddress; use pocketmine\utils\Binary; +use pocketmine\utils\Limits; use function random_int; use function crc32; @@ -48,6 +49,6 @@ public function add(InternetAddress $address) : int{ } private function generate(InternetAddress $address) : int{ - return random_int(0, 0xffffffff); + return random_int(0, Limits::UINT32_MAX); } } From 0e8b73200de3a0a2f3deab99610301aad5c70a4f Mon Sep 17 00:00:00 2001 From: ismail Date: Tue, 13 Aug 2024 13:21:54 +0000 Subject: [PATCH 31/35] new name CookieCache --- src/utils/{Cookie.php => CookieCache.php} | 2 -- 1 file changed, 2 deletions(-) rename src/utils/{Cookie.php => CookieCache.php} (96%) diff --git a/src/utils/Cookie.php b/src/utils/CookieCache.php similarity index 96% rename from src/utils/Cookie.php rename to src/utils/CookieCache.php index e62b4df..6aad0e2 100644 --- a/src/utils/Cookie.php +++ b/src/utils/CookieCache.php @@ -17,10 +17,8 @@ namespace raklib\utils; use raklib\utils\InternetAddress; -use pocketmine\utils\Binary; use pocketmine\utils\Limits; use function random_int; -use function crc32; final class CookieCache{ From bd80d498f3f5d587f0ff5da5c06e00b90f444ac5 Mon Sep 17 00:00:00 2001 From: ismail Date: Fri, 20 Sep 2024 10:56:42 +0000 Subject: [PATCH 32/35] delete the cookie when player log out --- src/server/Server.php | 3 +++ src/utils/CookieCache.php | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/server/Server.php b/src/server/Server.php index 1a0c089..7e0a16b 100644 --- a/src/server/Server.php +++ b/src/server/Server.php @@ -179,6 +179,9 @@ private function tick() : void{ foreach($this->sessions as $session){ $session->update($time); if($session->isFullyDisconnected()){ + if ($this->getCookie() instanceof CookieCache) { + $this->getCookie()->remove($session->getAddress()); + } $this->removeSessionInternal($session); } } diff --git a/src/utils/CookieCache.php b/src/utils/CookieCache.php index 6aad0e2..fb955c9 100644 --- a/src/utils/CookieCache.php +++ b/src/utils/CookieCache.php @@ -46,6 +46,13 @@ public function add(InternetAddress $address) : int{ return $cookie; } + public function remove(InternetAddress $address) : void{ + $addressStr = $address->toString(); + if (isset($this->cookies[$addressStr])) { + unset($this->cookies[$addressStr]); + } + } + private function generate(InternetAddress $address) : int{ return random_int(0, Limits::UINT32_MAX); } From cb35ab3e5410c15474789d011ac488cb306166a0 Mon Sep 17 00:00:00 2001 From: ismail Date: Fri, 20 Sep 2024 11:08:09 +0000 Subject: [PATCH 33/35] CookieCache::check() fix? --- src/server/Server.php | 12 ++++++------ src/server/UnconnectedMessageHandler.php | 9 +++++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/server/Server.php b/src/server/Server.php index 7e0a16b..0d2fe5a 100644 --- a/src/server/Server.php +++ b/src/server/Server.php @@ -78,7 +78,7 @@ class Server implements ServerInterface{ protected int $nextSessionId = 0; - public ?CookieCache $cookie = null; + public ?CookieCache $cookieCache = null; /** * @phpstan-param positive-int $recvMaxSplitParts @@ -104,7 +104,7 @@ public function __construct( $this->unconnectedMessageHandler = new UnconnectedMessageHandler($this, $protocolAcceptor); // If you don't want to use security on the server, just delete this line. - $this->cookie = new CookieCache(); + $this->cookieCache = new CookieCache(); } public function getPort() : int{ @@ -119,8 +119,8 @@ public function getLogger() : \Logger{ return $this->logger; } - public function getCookie() : ?CookieCache { - return $this->cookie; + public function getCookieCache() : ?CookieCache { + return $this->cookieCache; } public function tickProcessor() : void{ @@ -179,8 +179,8 @@ private function tick() : void{ foreach($this->sessions as $session){ $session->update($time); if($session->isFullyDisconnected()){ - if ($this->getCookie() instanceof CookieCache) { - $this->getCookie()->remove($session->getAddress()); + if ($this->getCookieCache() instanceof CookieCache) { + $this->getCookieCache()->remove($session->getAddress()); } $this->removeSessionInternal($session); } diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index f4d84a3..fb5676f 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -83,8 +83,8 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)"); }else{ $cookie = null; - if ($this->server->getCookie() instanceof CookieCache) { - $cookie = $this->server->getCookie()->add($address); + if ($this->server->getCookieCache() instanceof CookieCache) { + $cookie = $this->server->getCookieCache()->add($address); } //IP header size (20 bytes) + UDP header size (8 bytes) $this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), $cookie, $packet->mtuSize + 28), $address); @@ -103,8 +103,9 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->debug("Not creating session for $address due to session already opened"); return true; } - if ($this->server->getCookie() instanceof CookieCache) { // womp womp - if (!$this->server->getCookie()->check($address, $packet->cookie)) { + $cookieCache = $this->server->getCookieCache(); + if ($cookie_cache instanceof CookieCache) { // womp womp + if (!$cookie_cache->check($address, $packet->cookie)) { // Disconnect if OpenConnectionReply1 and the cookie in the OpenConnectionRequest2 packet do not match $this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies"); return true; From 41ac282dfd7ea148a0757a735c4e36c19472dbc5 Mon Sep 17 00:00:00 2001 From: ismail Date: Fri, 20 Sep 2024 11:10:49 +0000 Subject: [PATCH 34/35] snake case :| --- src/server/UnconnectedMessageHandler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index fb5676f..ddb7f43 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -104,8 +104,8 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool return true; } $cookieCache = $this->server->getCookieCache(); - if ($cookie_cache instanceof CookieCache) { // womp womp - if (!$cookie_cache->check($address, $packet->cookie)) { + if ($cookieCache instanceof CookieCache) { // womp womp + if (!$cookieCache->check($address, $packet->cookie)) { // Disconnect if OpenConnectionReply1 and the cookie in the OpenConnectionRequest2 packet do not match $this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies"); return true; From 7d1586f798d2d8fb63c8989a653181b90680a7b7 Mon Sep 17 00:00:00 2001 From: ismail Date: Fri, 20 Sep 2024 11:14:44 +0000 Subject: [PATCH 35/35] pls gimme blue tick --- src/server/UnconnectedMessageHandler.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index ddb7f43..17b8f59 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -103,13 +103,13 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool $this->server->getLogger()->debug("Not creating session for $address due to session already opened"); return true; } - $cookieCache = $this->server->getCookieCache(); - if ($cookieCache instanceof CookieCache) { // womp womp - if (!$cookieCache->check($address, $packet->cookie)) { + if ($this->server->getCookieCache() instanceof CookieCache) { // womp womp + if ($packet->cookie === null || !$this->server->getCookieCache()->check($address, $packet->cookie)) { // Disconnect if OpenConnectionReply1 and the cookie in the OpenConnectionRequest2 packet do not match $this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies"); return true; } + } $mtuSize = min($packet->mtuSize, $this->server->getMaxMtuSize()); //Max size, do not allow creating large buffers to fill server memory $this->server->sendPacket(OpenConnectionReply2::create($this->server->getID(), $address, $mtuSize, false), $address);