Player packet handling rewrite, fix #350

This commit is contained in:
Shoghi Cervantes 2013-06-09 05:56:48 +02:00
parent 715b92b681
commit 9b88a4a73f
9 changed files with 823 additions and 861 deletions

View file

@ -57,26 +57,6 @@ class MinecraftInterface{
return false;
}
protected function writeDump($pid, $raw, $data, $origin = "client", $ip = "", $port = 0){
if(LOG === true and DEBUG >= 3){
$p = "[".(microtime(true) - $this->start)."] [".((($origin === "client" and $this->client === true) or ($origin === "server" and $this->client === false)) ? "CLIENT->SERVER":"SERVER->CLIENT")." ".$ip.":".$port."]: ".(isset($data["id"]) ? "MC Packet ".Protocol::$dataName[$pid]:Protocol::$packetName[$pid])." (0x".Utils::strTohex(chr($pid)).") [length ".strlen($raw)."]".PHP_EOL;
$p .= Utils::hexdump($raw);
if(is_array($data)){
foreach($data as $i => $d){
if(!isset(Protocol::$raknet[$pid][$i])){
$ty = "special";
}else{
$ty = Protocol::$raknet[$pid][$i];
}
$p .= $i ." => ".(!is_array($d) ? $ty."(".(($ty === "magic" or substr($ty, 0, 7) === "special" or is_int($ty)) ? Utils::strToHex($d):Utils::printable((string) $d)).")":$ty."(\"".serialize(array_map("Utils::printable", $d))."\")").PHP_EOL;
}
}
$p .= PHP_EOL;
logg($p, "packets", false);
}
}
public function readPacket(){
$pk = $this->popPacket();
if($this->socket->connected === false){
@ -104,13 +84,7 @@ class MinecraftInterface{
"ip" => $source,
"port" => $port
)) !== true){
if(LOG === true and DEBUG >= 3){
console("[ERROR] Unknown Packet ID 0x".Utils::strToHex(chr($pid)), true, true, 2);
$p = "[".(microtime(true) - $this->start)."] [CLIENT->SERVER ".$source.":".$port."]: Error, bad packet id 0x".Utils::strToHex(chr($pid))." [length ".strlen($buf)."]".PHP_EOL;
$p .= Utils::hexdump($buf);
$p .= PHP_EOL;
logg($p, "packets", true, 2);
}
console("[ERROR] Unknown Packet ID 0x".Utils::strToHex(chr($pid)), true, true, 2);
}
return false;
}
@ -251,15 +225,6 @@ class MinecraftInterface{
$p = each($this->data);
unset($this->data[$p[0]]);
$p = $p[1];
if(isset($p[1]["packets"]) and is_array($p[1]["packets"])){
foreach($p[1]["packets"] as $d){
$this->data[] = array($p[0], $d[1], $d[2], $p[3], $p[4]);
}
}
$c = (isset($p[1]["id"]) ? true:false);
$p[2] = $c ? chr($p[1]["id"]).$p[2]:$p[2];
$this->writeDump(($c ? $p[1]["id"]:$p[0]), $p[2], $p[1], "server", $p[3], $p[4]);
return array("pid" => $p[0], "data" => $p[1], "raw" => $p[2], "ip" => $p[3], "port" => $p[4]);
}
return false;
@ -279,7 +244,6 @@ class MinecraftInterface{
$write = strlen($packet->raw);
}else{
$write = $this->socket->write($packet->raw, $dest, $port);
$this->writeDump($pid, $packet->raw, $data, "client", $dest, $port);
}
}else{
if($force === false and $this->isChunked($CID)){
@ -290,7 +254,6 @@ class MinecraftInterface{
$write = strlen($data);
}else{
$write = $this->socket->write($data, $dest, $port);
$this->writeDump($pid, $data, false, "client", $dest, $port);
}
}
return $write;

View file

@ -51,10 +51,6 @@ class Packet{
$this->addRaw($this->data[$field]);
continue;
}
if(is_int($type)){
$this->addRaw($this->data[$field]);
continue;
}
switch($type){
case "special1":
switch($this->pid){
@ -191,13 +187,12 @@ class Packet{
public function parse(){
foreach($this->struct as $field => $type){
if(is_int($type)){
$this->data[] = $this->get($type);
continue;
}
switch($type){
case "special1":
switch($this->pid){
case 0x07:
$this->data[] = $this->get(5);
break;
case 0x99:
if($this->data[0] >= 2){ //
$messageID = Utils::readShort($this->get(2), false);
@ -238,7 +233,7 @@ class Packet{
$this->data["packets"] = array();
while($len > $offset){
$pid = ord($raw{$offset});
++$offset;
++$offset;
$reliability = ($pid & 0b11100000) >> 5;
$hasSplit = ($pid & 0b00010000) >> 4;
$length = Utils::readShort(substr($raw, $offset, 2), false);

View file

@ -208,7 +208,7 @@ class Protocol{
0x07 => array(
"magic",
5, //Security Cookie (idk why it's sent here)
"special1", //Security Cookie
"short", //Server UDP Port
"short", //MTU Size
"long", //Client GUID

View file

@ -67,7 +67,7 @@ class UDPSocket{
if($this->connected === false){
return false;
}
return @socket_recvfrom($this->sock, $buf, 1500, 0, $source, $port);
return @socket_recvfrom($this->sock, $buf, 65535, 0, $source, $port);
}
public function write($data, $dest = false, $port = false){