From 7166d589165a8a317119f37d9436beb3072ed6f3 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 27 Oct 2026 12:16:29 +0300 Subject: [PATCH 1/6] added ack packet limit --- src/Player.php | 6 ++++- src/network/MinecraftInterface.php | 35 ++++++++++++++++++++++++++++- src/network/UDPSocket.php | 5 ++++- src/network/raknet/RakNetParser.php | 19 +++++++++++++--- 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/Player.php b/src/Player.php index 86bf3317..29e398a4 100755 --- a/src/Player.php +++ b/src/Player.php @@ -223,7 +223,7 @@ class Player{ $this->chatMessagesQueue->data = []; //$this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); - + $this->server->interface->maxACKsAllowed["{$this->ip}:{$this->port}"] = 0; $this->evid[] = $this->server->event("server.close", [$this, "close"]); console("[DEBUG] New Session started with $ip:$port. MTU {$this->MTU}, Client ID {$this->clientID}", true, true, 2); } @@ -2021,6 +2021,10 @@ class Player{ //$this->entity->close(); } } + + public function __destruct(){ //pwease dont leak memory >_< + unset($this->server->interface->maxACKsAllowed["{$this->ip}:{$this->port}"]); + } public function save(){ if($this->entity instanceof Entity){ diff --git a/src/network/MinecraftInterface.php b/src/network/MinecraftInterface.php index 725d99ef..7f4b84f0 100644 --- a/src/network/MinecraftInterface.php +++ b/src/network/MinecraftInterface.php @@ -37,18 +37,43 @@ class MinecraftInterface{ return $this->parsePacket($buf, $source, $port); } + /** + * Stores max ACK packets that the server is allowed to handle. + * + * @var array + */ + public $maxACKsAllowed = []; + private function parsePacket($buffer, $source, $port){ $pid = ord($buffer[0]); if(RakNetInfo::isValid($pid)){ - $packet = RakNetParser::parse($buffer); + $id = "$source:$port"; + $ackCnt = 0; + $acks = $this->maxACKsAllowed[$id] ?? 0; + + $packet = RakNetParser::parse($buffer, $acks, $ackCnt); if($packet !== false){ + if($ackCnt != 0 && isset($this->maxACKsAllowed[$id])){ + $this->maxACKsAllowed[$id] = $acks - $ackCnt; + } $packet->ip = $source; $packet->port = $port; if(EventHandler::callEvent(new PacketReceiveEvent($packet)) === BaseEvent::DENY){ return false; } return $packet; + }else if($ackCnt > 0){ + $CID = PocketMinecraftServer::clientID($source, $port); + $server = ServerAPI::request(); + $c = $server->clients[$CID] ?? false; + if($c !== false){ + $username = $c->iusername == "" ? $id : $c->iusername; + ConsoleAPI::warn("Player '$username' sent too many ACK packets (allowed: {$acks}, attempted {$ackCnt} or more)!"); + $c->close("too many ack packets"); + }else{ + //ConsoleAPI::warn("$id sent too many ACK packets (allowed: {$acks}, attempted {$ackCnt} or more)!"); + } } return false; }elseif($pid === 0xfe and $buffer[1] === "\xfd" and ServerAPI::request()->api->query instanceof QueryHandler){ @@ -75,6 +100,14 @@ class MinecraftInterface{ return 0; }elseif($packet instanceof RakNetPacket){ RakNetCodec::encode($packet); + if($packet->pid() >= RakNetInfo::DATA_PACKET_0 && $packet->pid() <= RakNetInfo::DATA_PACKET_F){ + $id = "{$packet->ip}:{$packet->port}"; + if(isset($this->maxACKsAllowed[$id])){ + ++$this->maxACKsAllowed[$id]; + }else{ + $this->maxACKsAllowed[$id] = 1; + } + } } $write = $this->socket->write($packet->buffer, $packet->ip, $packet->port); $this->bandwidth[1] += $write; diff --git a/src/network/UDPSocket.php b/src/network/UDPSocket.php index 260a1d4b..f53fc9a3 100644 --- a/src/network/UDPSocket.php +++ b/src/network/UDPSocket.php @@ -2,7 +2,10 @@ class UDPSocket{ - public $connected, $sock, $server, $port; + public $connected; + public $sock; + public $server; + public $port; function __construct($server, $port, $listen = false, $serverip = "0.0.0.0"){ $this->server = $server; $this->port = $port; diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 74bc3708..27ec561e 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -1,7 +1,7 @@ packets = []; + $acks = 0; for($i = 0; $i < $count && isset($buffer[$offset]); ++$i){ ++$offset; if(ord(substr($buffer, $offset - 1, 1)) === 0){ @@ -67,17 +68,29 @@ class RakNetParser{ $start = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); $offset += 3; $end = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); - if(($end - $start) > 4096){ - $end = $start + 4096; + $acks += ($end - $start)+1; + if($maxACKAllowed >= 0 && ($maxACKAllowed - $acks) < 0){ + $packet = false; + break; } + /*if(($end - $start) > 4096){ //not really needed? + $end = $start + 4096; + }*/ for($c = $start; $c <= $end; ++$c){ $packet->packets[] = $c; } }else{ $offset += 3; + $acks += 1; + if($maxACKAllowed >= 0 && ($maxACKAllowed - $acks) < 0){ + $packet = false; + break; + } $packet->packets[] = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); } } + + $ackCnt = $acks; break; default: $packet = false; From 55efde1f5c31ef1fbe1c7e2e1a72ce728881bf4b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 27 Oct 2026 14:02:12 +0300 Subject: [PATCH 2/6] fixed server crash using invalid nbt, fixed playerinputpacket and setentitylinkpacket being handled if the player did not spawn --- src/Player.php | 12 ++++--- src/utils/NBT.php | 67 ++++++++++++++++++++++++++++++++++++-- src/utils/NBTException.php | 8 +++++ 3 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 src/utils/NBTException.php diff --git a/src/Player.php b/src/Player.php index 29e398a4..a5932981 100755 --- a/src/Player.php +++ b/src/Player.php @@ -3275,23 +3275,27 @@ class Player{ $t->spawn($this); }else{ $nbt = new NBT(); - $nbt->load($packet->namedtag); - $d = array_shift($nbt->tree); - if($d["id"] !== TILE_SIGN){ + $d = $nbt->loadSignNBT($packet->namedtag); + if($d === false){ $t->spawn($this); }else{ - $t->setText($d["Text1"], $d["Text2"], $d["Text3"], $d["Text4"]); } } } break; case ProtocolInfo::SET_ENTITY_LINK_PACKET: + if($this->spawned === false){ + break; + } if($this->entity->riding != 0){ $this->entity->setRiding(null); } break; case ProtocolInfo::PLAYER_INPUT_PACKET: + if($this->spawned === false){ + break; + } $this->isJumping = $packet->isJumping; $this->isSneaking = $packet->isSneaking; $this->entity->moveForward = $packet->moveForward; diff --git a/src/utils/NBT.php b/src/utils/NBT.php index 6d1084ac..bbf92e5e 100644 --- a/src/utils/NBT.php +++ b/src/utils/NBT.php @@ -22,6 +22,62 @@ class NBT{ $this->binary .= $bin; } + /** + * A function to safely load sign nbt + * @param string $str raw nbt data + * @return false|array array with sign data, false if failed + */ + public function loadSignNBT($str){ + $this->offset = 0; + $this->binary = (string) $str; + //sign nbt in vanilla is + /* + * [ + * "id": "Sign", + * "x": , + * "y": , + * "z": , + * "Text1": , //max 15 chars + * "Text2": , //^ + * "Text3": , //^ + * "Text4": //^ + * ] + */ + $tag = ord($this->read(1)); + if($tag == self::TAG_COMPOUND){ + $name = $this->readTAG_STRING_maxlen(2); + if($name != "") return false; + $value = []; + for($n = 0; $n < 8; ++$n){ + $tag = ord($this->read(1)); + if($tag == self::TAG_END){ + break; + }else if($tag == self::TAG_INT){ + $name = $this->readTAG_STRING_maxlen(1); + if($name != "x" && $name != "y" && $name != "z") return false; + $value[$name] = $this->readTAG_INT(); + }else if($tag == self::TAG_STRING){ + $name = $this->readTAG_STRING_maxlen(5); + if($name == "id"){ + $s = $this->readTAG_STRING_maxlen(4); + if($s != "Sign") return false; + $value[$name] = $s; + }else if($name == "Text1" || $name == "Text2" || $name == "Text3" || $name == "Text4"){ + $value[$name] = $this->readTAG_STRING_maxlen(15*4); //mcpe 0.8.1 supports unicode, so the max char size 4 bytes + }else{ + return false; + } + }else{ + return false; + } + } + if(count($value) != 8) return false; + return $value; + }else{ + return false; + } + } + public function load($str){ $this->offset = 0; $this->binary = (string) $str; @@ -66,7 +122,7 @@ class NBT{ break; default: echo bin2hex(substr($this->binary, $this->offset - 1)) . PHP_EOL . PHP_EOL; - die("Invalid NBT Tag $tag"); + throw new NBTException("Invalid NBT Tag $tag"); } $node[$name] = $value; } @@ -83,7 +139,14 @@ class NBT{ private function feof(){ return !isset($this->binary[$this->offset]); } - + + //i guess i could have modified old method? + //*but it will make it slower read(2), false); + return $this->read($len <= $ml ? $len : $ml); + } public function readTAG_STRING(){ return $this->read(Utils::readLShort($this->read(2), false)); } diff --git a/src/utils/NBTException.php b/src/utils/NBTException.php new file mode 100644 index 00000000..9af46728 --- /dev/null +++ b/src/utils/NBTException.php @@ -0,0 +1,8 @@ + Date: Tue, 27 Oct 2026 14:05:53 +0300 Subject: [PATCH 3/6] replaced old github repo link with the new one --- CONTRIBUTING.md | 4 ++-- start.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 83af8a8d..fac7d7b7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -5,14 +5,14 @@ Before contributing to NostalgiaCore, please read this. ## I want to create an issue -* First, use the [Issue Search](https://github.com/kotyaralih/NostalgiaCore/search?ref=cmdform&type=Issues) to check if anyone has reported it. +* First, use the [Issue Search](https://github.com/oldminecraftcommunity/NostalgiaCore/search?ref=cmdform&type=Issues) to check if anyone has reported it. * Is your issue related to a Plugin? If so, please contact their original author instead of reporting it here. * And no, we won't update a Plugin because you need it. * When reporting, give as much info as you can, and if the Issue is a crash, give the Crash Dump. * Issues should be written in English. ## I want to contribute code -* Use the [Pull Request](https://github.com/kotyaralih/NostalgiaCore/pull/new) system, your request will be checked and discussed. +* Use the [Pull Request](https://github.com/oldminecraftcommunity/NostalgiaCore/pull/new) system, your request will be checked and discussed. * __Create a single branch for that pull request__ * If you want to be part of NostalgiaCore, we will ask you to. * Code using the syntax as in NostalgiaCore. See below for an example. diff --git a/start.sh b/start.sh index e06f3f06..0b032804 100644 --- a/start.sh +++ b/start.sh @@ -36,7 +36,7 @@ if [ "$POCKETMINE_FILE" == "" ]; then POCKETMINE_FILE="./PocketMine-MP.php" else echo​ ​"​PocketMine-MP.php not found​" - ​ echo​ ​"​Downloads can be found at https://github.com/kotyaralih/NostalgiaCore" + ​ echo​ ​"​Downloads can be found at https://github.com/oldminecraftcommunity/NostalgiaCore" exit 1 fi fi From 339febd589a6473878e1634fc4ab5a2dd63c94c6 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 27 Oct 2026 14:27:28 +0300 Subject: [PATCH 4/6] fixed pocketminecraftserver::custom never getting cleared --- src/PocketMinecraftServer.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 150e3e90..71f52a35 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -5,7 +5,8 @@ class PocketMinecraftServer{ const TICK_F20TPS = 1; const TICK_NETWAIT = 2; public $tCnt, $ticks; - public $extraprops, $serverID, $interface, $database, $version, $invisible, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $eidCnt, $custom, $description, $motd, $port, $saveEnabled; + public $extraprops, $serverID, $interface, $database, $version, $invisible, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $eidCnt, $description, $motd, $port, $saveEnabled; + public $custom, $customLastTime = []; /** * @var ServerAPI */ @@ -627,7 +628,8 @@ class PocketMinecraftServer{ $pk->ip = $packet->ip; $pk->port = $packet->port; $this->send($pk); - $this->custom["times_" . $CID] = ($this->custom["times_" . $CID] + 1) % strlen($this->description); + $this->custom["times_$CID"] = ($this->custom["times_$CID"] + 1) % strlen($this->description); + $this->customLastTime[$CID] = microtime(true); break; case RakNetInfo::OPEN_CONNECTION_REQUEST_1: if($packet->structure !== RakNetInfo::STRUCTURE){ @@ -677,6 +679,14 @@ class PocketMinecraftServer{ public function tick(){ $time = microtime(true); if($this->lastTick <= ($time - 0.05)){ + foreach($this->customLastTime as $CID => $lastTime){ + //server is removed from the list every 3 seconds(will also add a tiny 1 second delay so it will def not be updated incorrectly <3) + if(($time - $lastTime) > 4){ + unset($this->custom["times_$CID"]); + unset($this->customLastTime[$CID]); + } + } + $this->tickMeasure[] = $this->lastTick = $time; unset($this->tickMeasure[key($this->tickMeasure)]); ++$this->ticks; From 3978b95843b28c4c5b231c1946e2c1638cc8c73e Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 27 Oct 2026 14:48:31 +0300 Subject: [PATCH 5/6] fixed received cnt queue not getting cleared --- src/Player.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Player.php b/src/Player.php index a5932981..0b34a8c8 100755 --- a/src/Player.php +++ b/src/Player.php @@ -222,7 +222,7 @@ class Player{ $this->chatMessagesQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->chatMessagesQueue->data = []; - //$this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); + $this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); $this->server->interface->maxACKsAllowed["{$this->ip}:{$this->port}"] = 0; $this->evid[] = $this->server->event("server.close", [$this, "close"]); console("[DEBUG] New Session started with $ip:$port. MTU {$this->MTU}, Client ID {$this->clientID}", true, true, 2); From 81cfb6b7054e658834b8e87418c3e7dc9d1668b3 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 27 Oct 2026 17:46:18 +0300 Subject: [PATCH 6/6] better multiack handling, fixed ack negative ranges --- src/Player.php | 4 +++- src/network/MinecraftInterface.php | 4 ++-- src/network/raknet/RakNetPacket.php | 1 + src/network/raknet/RakNetParser.php | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Player.php b/src/Player.php index 0b34a8c8..626c7160 100755 --- a/src/Player.php +++ b/src/Player.php @@ -3574,8 +3574,10 @@ class Player{ foreach($packet->packets as $count){ if(isset($this->packetAlwaysRecoverQueue[$count])){ $this->resendQueue[$count] = $this->packetAlwaysRecoverQueue[$count]; + $this->resendQueue[$count]->incrementAllowedAck = false; }else if(isset($this->recoveryQueue[$count])){ $this->resendQueue[$count] =& $this->recoveryQueue[$count]; + $this->resendQueue[$count]->incrementAllowedAck = false; $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; unset($this->recoveryQueue[$count]); }else{ //lost and wont be recovered @@ -3595,8 +3597,8 @@ class Player{ }else if(isset($this->recoveryQueue[$count])){ $this->lag[] = $time - $this->recoveryQueue[$count]->sendtime; unset($this->recoveryQueue[$count]); - unset($this->resendQueue[$count]); } + unset($this->resendQueue[$count]); ++$this->packetStats[0]; } break; diff --git a/src/network/MinecraftInterface.php b/src/network/MinecraftInterface.php index 7f4b84f0..087a0630 100644 --- a/src/network/MinecraftInterface.php +++ b/src/network/MinecraftInterface.php @@ -54,7 +54,7 @@ class MinecraftInterface{ $packet = RakNetParser::parse($buffer, $acks, $ackCnt); if($packet !== false){ - if($ackCnt != 0 && isset($this->maxACKsAllowed[$id])){ + if($ackCnt != 0 && isset($this->maxACKsAllowed[$id]) && $packet->pid() != RakNetInfo::NACK){ $this->maxACKsAllowed[$id] = $acks - $ackCnt; } $packet->ip = $source; @@ -100,7 +100,7 @@ class MinecraftInterface{ return 0; }elseif($packet instanceof RakNetPacket){ RakNetCodec::encode($packet); - if($packet->pid() >= RakNetInfo::DATA_PACKET_0 && $packet->pid() <= RakNetInfo::DATA_PACKET_F){ + if($packet->pid() >= RakNetInfo::DATA_PACKET_0 && $packet->pid() <= RakNetInfo::DATA_PACKET_F && $packet->incrementAllowedAck){ $id = "{$packet->ip}:{$packet->port}"; if(isset($this->maxACKsAllowed[$id])){ ++$this->maxACKsAllowed[$id]; diff --git a/src/network/raknet/RakNetPacket.php b/src/network/raknet/RakNetPacket.php index 870a2733..81ab86d2 100644 --- a/src/network/raknet/RakNetPacket.php +++ b/src/network/raknet/RakNetPacket.php @@ -5,6 +5,7 @@ class RakNetPacket extends Packet{ public $packetID; public $seqNumber; public $sendtime; + public $incrementAllowedAck = true; public function __construct($packetID){ $this->packetID = (int) $packetID; diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php index 27ec561e..8ee4d362 100644 --- a/src/network/raknet/RakNetParser.php +++ b/src/network/raknet/RakNetParser.php @@ -69,7 +69,7 @@ class RakNetParser{ $offset += 3; $end = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3))); $acks += ($end - $start)+1; - if($maxACKAllowed >= 0 && ($maxACKAllowed - $acks) < 0){ + if($acks <= 0 || $maxACKAllowed >= 0 && ($maxACKAllowed - $acks) < 0){ $packet = false; break; }