dont create RakNetCodec/RakNetParser instances to encode/decode packet

This commit is contained in:
GameHerobrine 2025-06-02 08:42:44 +03:00
parent e5562d81f4
commit 0eb5e0ea7d
3 changed files with 149 additions and 166 deletions

View file

@ -41,14 +41,14 @@ class MinecraftInterface{
$pid = ord($buffer[0]); $pid = ord($buffer[0]);
if(RakNetInfo::isValid($pid)){ if(RakNetInfo::isValid($pid)){
$parser = new RakNetParser($buffer); $packet = RakNetParser::parse($buffer);
if($parser->packet !== false){ if($packet !== false){
$parser->packet->ip = $source; $packet->ip = $source;
$parser->packet->port = $port; $packet->port = $port;
if(EventHandler::callEvent(new PacketReceiveEvent($parser->packet)) === BaseEvent::DENY){ if(EventHandler::callEvent(new PacketReceiveEvent($packet)) === BaseEvent::DENY){
return false; return false;
} }
return $parser->packet; return $packet;
} }
return false; return false;
}elseif($pid === 0xfe and $buffer[1] === "\xfd" and ServerAPI::request()->api->query instanceof QueryHandler){ }elseif($pid === 0xfe and $buffer[1] === "\xfd" and ServerAPI::request()->api->query instanceof QueryHandler){
@ -74,7 +74,7 @@ class MinecraftInterface{
if(EventHandler::callEvent(new PacketSendEvent($packet)) === BaseEvent::DENY){ if(EventHandler::callEvent(new PacketSendEvent($packet)) === BaseEvent::DENY){
return 0; return 0;
}elseif($packet instanceof RakNetPacket){ }elseif($packet instanceof RakNetPacket){
$codec = new RakNetCodec($packet); RakNetCodec::encode($packet);
} }
$write = $this->socket->write($packet->buffer, $packet->ip, $packet->port); $write = $this->socket->write($packet->buffer, $packet->ip, $packet->port);
$this->bandwidth[1] += $write; $this->bandwidth[1] += $write;

View file

@ -1,46 +1,39 @@
<?php <?php
class RakNetCodec{ class RakNetCodec{
public static function encode(RakNetPacket $packet){
public $packet; if($packet->buffer != null && strlen($packet->buffer) > 0){
public $buffer;
public function __construct(RakNetPacket $packet){
$this->packet = $packet;
$this->buffer = &$this->packet->buffer;
$this->encode();
}
private function encode(){
if($this->packet->buffer != null && strlen($this->packet->buffer) > 0){
return; return;
} }
$this->buffer .= chr($this->packet->pid()); $packet->buffer .= chr($packet->pid());
switch($this->packet->pid()){ switch($packet->pid()){
case RakNetInfo::OPEN_CONNECTION_REPLY_1: case RakNetInfo::OPEN_CONNECTION_REPLY_1:
$this->buffer .= RakNetInfo::MAGIC; $packet->buffer .= RakNetInfo::MAGIC;
$this->putLong($this->packet->serverID); $packet->buffer .= Utils::writeLong($packet->serverID);
$this->putByte(0); //server security $packet->buffer .= chr(0); //server security
$this->putShort($this->packet->mtuSize); $packet->buffer .= Utils::writeShort($packet->mtuSize);
break; break;
case RakNetInfo::OPEN_CONNECTION_REPLY_2: case RakNetInfo::OPEN_CONNECTION_REPLY_2:
$this->buffer .= RakNetInfo::MAGIC; $packet->buffer .= RakNetInfo::MAGIC;
$this->putLong($this->packet->serverID); $packet->buffer .= Utils::writeLong($packet->serverID);
$this->putShort($this->packet->port); $packet->buffer .= Utils::writeShort($packet->port);
$this->putShort($this->packet->mtuSize); $packet->buffer .= Utils::writeShort($packet->mtuSize);
$this->putByte(0); //Server security $packet->buffer .= chr(0); //Server security
break; break;
case RakNetInfo::INCOMPATIBLE_PROTOCOL_VERSION: case RakNetInfo::INCOMPATIBLE_PROTOCOL_VERSION:
$this->putByte(RakNetInfo::STRUCTURE); $packet->buffer .= chr(RakNetInfo::STRUCTURE);
$this->buffer .= RakNetInfo::MAGIC; $packet->buffer .= RakNetInfo::MAGIC;
$this->putLong($this->packet->serverID); $packet->buffer .= Utils::writeLong($packet->serverID);
break; break;
case RakNetInfo::UNCONNECTED_PONG: case RakNetInfo::UNCONNECTED_PONG:
case RakNetInfo::ADVERTISE_SYSTEM: case RakNetInfo::ADVERTISE_SYSTEM:
$this->putLong($this->packet->pingID); $packet->buffer .= Utils::writeLong($packet->pingID);
$this->putLong($this->packet->serverID); $packet->buffer .= Utils::writeLong($packet->serverID);
$this->buffer .= RakNetInfo::MAGIC; $packet->buffer .= RakNetInfo::MAGIC;
$this->putString($this->packet->serverType);
$packet->buffer .= Utils::writeShort(strlen($packet->serverType));
$packet->buffer .= $packet->serverType;
break; break;
case RakNetInfo::DATA_PACKET_0: case RakNetInfo::DATA_PACKET_0:
case RakNetInfo::DATA_PACKET_1: case RakNetInfo::DATA_PACKET_1:
@ -58,9 +51,9 @@ class RakNetCodec{
case RakNetInfo::DATA_PACKET_D: case RakNetInfo::DATA_PACKET_D:
case RakNetInfo::DATA_PACKET_E: case RakNetInfo::DATA_PACKET_E:
case RakNetInfo::DATA_PACKET_F: case RakNetInfo::DATA_PACKET_F:
$this->putLTriad($this->packet->seqNumber); $packet->buffer .= Utils::writeLTriad($packet->seqNumber);
foreach($this->packet->data as $pk){ foreach($packet->data as $pk){
$this->encodeDataPacket($pk); static::encodeDataPacket($packet, $pk);
} }
break; break;
case RakNetInfo::NACK: case RakNetInfo::NACK:
@ -68,14 +61,14 @@ class RakNetCodec{
$payload = b""; $payload = b"";
$records = 0; $records = 0;
$pointer = 0; $pointer = 0;
sort($this->packet->packets, SORT_NUMERIC); sort($packet->packets, SORT_NUMERIC);
$max = count($this->packet->packets); $max = count($packet->packets);
while($pointer < $max){ while($pointer < $max){
$type = true; $type = true;
$curr = $start = $this->packet->packets[$pointer]; $curr = $start = $packet->packets[$pointer];
for($i = $start + 1; $i < $max; ++$i){ for($i = $start + 1; $i < $max; ++$i){
$n = $this->packet->packets[$i]; $n = $packet->packets[$i];
if(($n - $curr) === 1){ if(($n - $curr) === 1){
$curr = $end = $n; $curr = $end = $n;
$type = false; $type = false;
@ -95,73 +88,58 @@ class RakNetCodec{
} }
++$records; ++$records;
} }
$this->putShort($records); $packet->buffer .= Utils::writeShort($records);
$this->buffer .= $payload; $packet->buffer .= $payload;
break; break;
default: default:
} }
} }
protected function putLong($v){ public static function encodeDataPacket(RakNetPacket $packet, RakNetDataPacket $pk){
$this->buffer .= Utils::writeLong($v); $packet->buffer .= chr(($pk->reliability << 5) | ($pk->hasSplit > 0 ? 0b00010000 : 0));
} $packet->buffer .= Utils::writeShort(strlen($pk->buffer) << 3);
protected function putByte($v){
$this->buffer .= chr($v);
}
protected function putShort($v){
$this->buffer .= Utils::writeShort($v);
}
protected function putString($v){
$this->putShort(strlen($v));
$this->put($v);
}
protected function put($str){
$this->buffer .= $str;
}
protected function putLTriad($v){
$this->buffer .= Utils::writeLTriad($v);
}
private function encodeDataPacket(RakNetDataPacket $pk){
$this->putByte(($pk->reliability << 5) | ($pk->hasSplit > 0 ? 0b00010000 : 0));
$this->putShort(strlen($pk->buffer) << 3);
if($pk->reliability === 2 if($pk->reliability === 2
or $pk->reliability === 3 or $pk->reliability === 3
or $pk->reliability === 4 or $pk->reliability === 4
or $pk->reliability === 6 or $pk->reliability === 6
or $pk->reliability === 7){ or $pk->reliability === 7){
$this->putLTriad($pk->messageIndex); $packet->buffer .= Utils::writeLTriad($pk->messageIndex);
} }
if($pk->reliability === 1 if($pk->reliability === 1
or $pk->reliability === 3 or $pk->reliability === 3
or $pk->reliability === 4 or $pk->reliability === 4
or $pk->reliability === 7){ or $pk->reliability === 7){
$this->putLTriad($pk->orderIndex); $packet->buffer .= Utils::writeLTriad($pk->orderIndex);
$this->putByte($pk->orderChannel); $packet->buffer .= chr($pk->orderChannel);
} }
if($pk->hasSplit === true){ if($pk->hasSplit === true){
$this->putInt($pk->splitCount); $packet->buffer .= Utils::writeInt($pk->splitCount);
$this->putShort($pk->splitID); $packet->buffer .= Utils::writeShort($pk->splitID);
$this->putInt($pk->splitIndex); $packet->buffer .= Utils::writeInt($pk->splitIndex);
} }
$this->buffer .= $pk->buffer; $packet->buffer .= $pk->buffer;
} }
protected function putInt($v){ /**
$this->buffer .= Utils::writeInt($v); * @deprecated
} */
public $packet;
protected function putTriad($v){ /**
$this->buffer .= Utils::writeTriad($v); * @deprecated
*/
public $buffer;
/**
* @deprecated use RakNetCodec::encode
* @param RakNetPacket $packet
*/
public function __construct(RakNetPacket $packet){
$this->packet = $packet;
$this->buffer = &$packet->buffer;
static::encode($packet);
} }
} }

View file

@ -1,42 +1,35 @@
<?php <?php
class RakNetParser{ class RakNetParser{
public static function parse(&$buffer){
public $packet; $offset = 0;
private $buffer; ++$offset;
private $offset; $packet = new RakNetPacket(ord(substr($buffer, $offset - 1, 1)));
$packet->buffer = &$buffer;
public function __construct(&$buffer){ $packet->length = strlen($buffer);
$this->buffer =& $buffer; switch($packet->pid()){
$this->offset = 0;
if(strlen($this->buffer) > 0){
$this->parse();
}else{
$this->packet = false;
}
}
private function parse(){
$this->packet = new RakNetPacket(ord($this->get(1)));
$this->packet->buffer =& $this->buffer;
$this->packet->length = strlen($this->buffer);
switch($this->packet->pid()){
case RakNetInfo::UNCONNECTED_PING: case RakNetInfo::UNCONNECTED_PING:
case RakNetInfo::UNCONNECTED_PING_OPEN_CONNECTIONS: case RakNetInfo::UNCONNECTED_PING_OPEN_CONNECTIONS:
$this->packet->pingID = $this->getLong(); $offset += 8;
$this->offset += 16; //Magic $packet->pingID = Utils::readLong(substr($buffer, $offset - 8, 8), false);
$offset += 16; //Magic
break; break;
case RakNetInfo::OPEN_CONNECTION_REQUEST_1: case RakNetInfo::OPEN_CONNECTION_REQUEST_1:
$this->offset += 16; //Magic $offset += 16; //Magic
$this->packet->structure = $this->getByte(); ++$offset;
$this->packet->mtuSize = strlen($this->get(true)); $packet->structure = ord(substr($buffer, $offset - 1, 1));
$packet->mtuSize = strlen(substr($buffer, $offset));
break; break;
case RakNetInfo::OPEN_CONNECTION_REQUEST_2: case RakNetInfo::OPEN_CONNECTION_REQUEST_2:
$this->offset += 16; //Magic $offset += 16; //Magic
$this->packet->security = $this->get(5); $offset += 5;
$this->packet->port = $this->getShort(false); $packet->security = Utils::readLong(substr($buffer, $offset - 5, 5), false);
$this->packet->mtuSize = $this->getShort(false); $offset += 2;
$this->packet->clientID = $this->getLong(); $packet->port = Utils::readShort(substr($buffer, $offset - 2, 2), false);
$offset += 2;
$packet->mtuSize = Utils::readShort(substr($buffer, $offset - 2, 2), false);
$offset += 8;
$packet->clientID = Utils::readLong(substr($buffer, $offset - 8, 8), false);
break; break;
case RakNetInfo::DATA_PACKET_0: case RakNetInfo::DATA_PACKET_0:
case RakNetInfo::DATA_PACKET_1: case RakNetInfo::DATA_PACKET_1:
@ -54,36 +47,43 @@ class RakNetParser{
case RakNetInfo::DATA_PACKET_D: case RakNetInfo::DATA_PACKET_D:
case RakNetInfo::DATA_PACKET_E: case RakNetInfo::DATA_PACKET_E:
case RakNetInfo::DATA_PACKET_F: case RakNetInfo::DATA_PACKET_F:
$this->packet->seqNumber = $this->getLTriad(); $offset += 3;
$this->packet->data = []; $packet->seqNumber = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3)));
$packet->data = [];
while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ while(isset($buffer[$offset]) and ($pk = static::parseDataPacket($offset, $buffer)) instanceof RakNetDataPacket){
$this->packet->data[] = $pk; $packet->data[] = $pk;
} }
break; break;
case RakNetInfo::NACK: case RakNetInfo::NACK:
case RakNetInfo::ACK: case RakNetInfo::ACK:
$count = $this->getShort(); $offset += 2;
$this->packet->packets = []; $count = Utils::readShort(substr($buffer, $offset - 2, 2), false);
for($i = 0; $i < $count and !$this->feof(); ++$i){ $packet->packets = [];
if($this->getByte() === 0){ for($i = 0; $i < $count && isset($buffer[$offset]); ++$i){
$start = $this->getLTriad(); ++$offset;
$end = $this->getLTriad(); if(ord(substr($buffer, $offset - 1, 1)) === 0){
$offset += 3;
$start = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3)));
$offset += 3;
$end = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3)));
if(($end - $start) > 4096){ if(($end - $start) > 4096){
$end = $start + 4096; $end = $start + 4096;
} }
for($c = $start; $c <= $end; ++$c){ for($c = $start; $c <= $end; ++$c){
$this->packet->packets[] = $c; $packet->packets[] = $c;
} }
}else{ }else{
$this->packet->packets[] = $this->getLTriad(); $offset += 3;
$packet->packets[] = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3)));
} }
} }
break; break;
default: default:
$this->packet = false; $packet = false;
break; break;
} }
return $packet;
} }
private function get($len){ private function get($len){
@ -98,57 +98,50 @@ class RakNetParser{
return substr($this->buffer, $this->offset - $len, $len); return substr($this->buffer, $this->offset - $len, $len);
} }
private function getLong($unsigned = false){
return Utils::readLong($this->get(8), $unsigned);
}
private function getByte(){ private function getByte(){
return ord($this->get(1)); return ord($this->get(1));
} }
private function getShort($unsigned = false){ private function getShort($unsigned = false){
return Utils::readShort($this->get(2), $unsigned); return ;
} }
private function getLTriad(){ private function getLTriad(){
return Utils::readTriad(strrev($this->get(3))); return Utils::readTriad(strrev($this->get(3)));
} }
private function feof(){ private static function parseDataPacket(&$offset, &$buffer){
return !isset($this->buffer[$this->offset]); ++$offset;
} $packetFlags = ord(substr($buffer, $offset - 1, 1));
private function parseDataPacket(){
$packetFlags = $this->getByte();
$reliability = ($packetFlags & 0b11100000) >> 5; $reliability = ($packetFlags & 0b11100000) >> 5;
$hasSplit = ($packetFlags & 0b00010000) > 0; $hasSplit = ($packetFlags & 0b00010000) > 0;
$length = (int) ceil($this->getShort() / 8); $offset += 2;
$length = (int) ceil(Utils::readShort(substr($buffer, $offset - 2, 2), false) / 8);
if($reliability === 2 if($reliability == 2 ||$reliability == 3 || $reliability == 4 || $reliability == 6 || $reliability == 7){
or $reliability === 3 $offset += 3;
or $reliability === 4 $messageIndex = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3)));
or $reliability === 6
or $reliability === 7){
$messageIndex = $this->getLTriad();
}else{ }else{
$messageIndex = false; $messageIndex = false;
} }
if($reliability === 1 if($reliability == 1 ||$reliability == 3 || $reliability == 4 || $reliability == 7){
or $reliability === 3 $offset += 3;
or $reliability === 4 $orderIndex = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3)));
or $reliability === 7){ ++$offset;
$orderIndex = $this->getLTriad(); $orderChannel = ord(substr($buffer, $offset - 1, 1));
$orderChannel = $this->getByte();
}else{ }else{
$orderIndex = false; $orderIndex = false;
$orderChannel = false; $orderChannel = false;
} }
if($hasSplit){ if($hasSplit){
$splitCount = $this->getInt(); $offset += 4;
$splitID = $this->getShort(); $splitCount = Utils::readInt(substr($buffer, $offset - 4, 4), false);
$splitIndex = $this->getInt(); $offset += 2;
$splitID = Utils::readShort(substr($buffer, $offset - 2, 2), false);
$offset += 4;
$splitIndex = Utils::readInt(substr($buffer, $offset - 4, 4), false);
}else{ }else{
$splitCount = false; $splitCount = false;
$splitID = false; $splitID = false;
@ -160,9 +153,11 @@ class RakNetParser{
or ($hasSplit === true and $splitIndex >= $splitCount)){ or ($hasSplit === true and $splitIndex >= $splitCount)){
return false; return false;
}else{ }else{
$pid = $this->getByte(); ++$offset;
$buffer = $this->get($length - 1); $pid = ord(substr($buffer, $offset - 1, 1));
if(strlen($buffer) < ($length - 1)){ $offset += $length-1;
$buf = substr($buffer, $offset - ($length - 1), ($length - 1));
if(strlen($buf) < ($length - 1)){
return false; return false;
} }
@ -233,13 +228,23 @@ class RakNetParser{
$data->splitCount = $splitCount; $data->splitCount = $splitCount;
$data->splitID = $splitID; $data->splitID = $splitID;
$data->splitIndex = $splitIndex; $data->splitIndex = $splitIndex;
$data->setBuffer($buffer); $data->setBuffer($buf);
} }
return $data; return $data;
} }
private function getInt($unsigned = false){ /**
return Utils::readInt($this->get(4), $unsigned); * @deprecated
*/
public $packet;
/**
* @deprecated use RakNetParser::parse
*/
public function __construct(&$buffer){
if(strlen($buffer) > 0){
$this->packet = static::parse($buffer);
}else{
$this->packet = false;
}
} }
} }