added ack packet limit

This commit is contained in:
GameHerobrine 2026-10-27 12:16:29 +03:00
parent 5023bb2286
commit 7166d58916
4 changed files with 59 additions and 6 deletions

View file

@ -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){

View file

@ -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;

View file

@ -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;

View file

@ -1,7 +1,7 @@
<?php
class RakNetParser{
public static function parse(&$buffer){
public static function parse(&$buffer, $maxACKAllowed = -1, &$ackCnt = 0){
$offset = 0;
++$offset;
$packet = new RakNetPacket(ord(substr($buffer, $offset - 1, 1)));
@ -60,6 +60,7 @@ class RakNetParser{
$offset += 2;
$count = Utils::readShort(substr($buffer, $offset - 2, 2), false);
$packet->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;