New packets, general Improvement

This commit is contained in:
Shoghi Cervantes Pueyo 2012-10-21 23:58:16 +02:00
parent 9c4abf49f5
commit 5425dfa5dc
7 changed files with 28 additions and 72 deletions

View File

@ -52,7 +52,7 @@ class MinecraftInterface{
protected function writeDump($pid, $raw, $data, $origin = "client", $ip = "", $port = 0){
if(LOG === true and DEBUG >= 2){
$p = "[".microtime(true)."] [".((($origin === "client" and $this->client === true) or ($origin === "server" and $this->client === false)) ? "CLIENT->SERVER":"SERVER->CLIENT")." ".$ip.":".$port."]: ".$this->name[$pid]." (0x".Utils::strTohex(chr($pid)).") [lenght ".strlen($raw)."]".PHP_EOL;
$p .= hexdump($raw, false, false, true);
$p .= Utils::hexdump($raw);
if(is_array($data)){
foreach($data as $i => $d){
$p .= $i ." => ".(!is_array($d) ? $this->pstruct[$pid][$i]."(".(($this->pstruct[$pid][$i] === "magic" or substr($this->pstruct[$pid][$i], 0, 7) === "special" or is_int($this->pstruct[$pid][$i])) ? Utils::strToHex($d):$d).")":$this->pstruct[$pid][$i]."(***)").PHP_EOL;
@ -80,7 +80,7 @@ class MinecraftInterface{
$struct = $this->getStruct($pid);
if($struct === false){
$p = "[".microtime(true)."] [SERVER->CLIENT]: Error, bad packet id 0x".Utils::strToHex(chr($pid)).PHP_EOL;
$p .= hexdump($data[0], false, false, true);
$p .= Utils::hexdump($data[0]);
$p .= PHP_EOL;
logg($p, "packets", true, 2);

View File

@ -36,7 +36,7 @@ class Packet{
$this->offset = 1;
$this->raw = $data;
$this->data = array();
if($pid !== false){
if($data === ""){
$this->addRaw(chr($pid));
}
$this->struct = $struct;
@ -61,6 +61,7 @@ class Packet{
switch($this->pid){
case 0x05:
case 0x84:
case 0x8c:
$this->addRaw($this->data[$field]);
break;
}
@ -140,6 +141,7 @@ class Packet{
switch($this->pid){
case 0x05:
case 0x84:
case 0x8c:
$this->data[] = $this->get(true);
break;
}

View File

@ -26,10 +26,11 @@ the Free Software Foundation, either version 3 of the License, or
*/
class PocketMinecraftClient{
protected $interface, $protocol, $entities, $player, $cnt, $events, $username, $version, $clientID, $connected, $serverID;
protected $interface, $protocol, $entities, $player, $cnt, $events, $username, $version, $clientID, $connected, $serverID, $start;
var $serverList = array();
function __construct($username, $protocol = CURRENT_PROTOCOL, $version = CURRENT_VERSION){
//$this->player = new Player($username);
$this->start = microtime(true);
$this->version = (int) $version;
$this->username = $username;
$this->connected = false;
@ -77,7 +78,7 @@ class PocketMinecraftClient{
}
public function getServerList(){
$this->action(1000000, '$this->send(0x02, array((microtime(true) * 1000)));');
$this->action(1000000, '$this->send(0x02, array(((microtime(true) - $this->start) * 1000)));');
$this->action(5000000, '$this->actions = array();$this->stop = true;');
$this->process();
$list = array();
@ -111,7 +112,8 @@ class PocketMinecraftClient{
case 0x08:
$serverID = $data[1];
$this->send(0x84, array(
"\x00\x00\x00\x40\x00\x90\x00\x00\x00\x09".$this->serverID.Utils::writeDouble(microtime(true) * 1000).chr(0x00),
0,
"\x00\x00\x40\x00\x90\x00\x00\x00\x09".$this->serverID.Utils::writeDouble((microtime(true) - $this->start) * 1000).chr(0x00),
/*"\x00\x00\x00\x40\x00\x90\x00\x00\x00\x09",
$this->serverID,
(microtime(true) * 1000),

View File

@ -131,6 +131,17 @@ class Utils{
return ($negative === true ? "-":"").str_replace("x", "", $hash);
}
public static function hexdump($bin){
$output = "";
$bin = str_split($bin, 16);
foreach($bin as $counter => $line){
$hex = chunk_split(chunk_split(str_pad(bin2hex($line), 32, " ", STR_PAD_RIGHT), 2, " "), 24, " ");
$ascii = preg_replace('#([^\x20-\x7E])#', '.', $line);
$output .= str_pad(dechex($counter << 4), 4, "0", STR_PAD_LEFT). " " . $hex . " " . $ascii . PHP_EOL;
}
return $output;
}
public static function microtime(){
return microtime(true);

View File

@ -111,70 +111,4 @@ function logg($message, $name, $EOL = true, $level = 2, $close = false){
unset($fpointers[$name]);
}
}
}
function hexdump($data, $htmloutput = true, $uppercase = false, $return = false)
{
// Init
$hexi = '';
$ascii = '';
$dump = ($htmloutput === true) ? '<pre>' : '';
$offset = 0;
$len = strlen($data);
// Upper or lower case hexadecimal
$x = ($uppercase === false) ? 'x' : 'X';
// Iterate string
for ($i = $j = 0; $i < $len; $i++)
{
// Convert to hexidecimal
$hexi .= Utils::strToHex($data[$i]);
// Replace non-viewable bytes with '.'
if (ord($data[$i]) >= 0x20 and ord($data[$i]) < 0x80) {
$ascii .= ($htmloutput === true) ?
htmlentities($data[$i]) :
$data[$i];
} else {
$ascii .= '.';
}
// Add extra column spacing
if ($j === 7) {
$hexi .= ' ';
$ascii .= ' ';
}
// Add row
if (++$j === 16 || $i === $len - 1) {
// Join the hexi / ascii output
$dump .= sprintf("%04$x %-49s %s", $offset, $hexi, $ascii);
// Reset vars
$hexi = $ascii = '';
$offset += 16;
$j = 0;
// Add newline
if ($i !== $len - 1) {
$dump .= "\n";
}
}
}
// Finish dump
$dump .= $htmloutput === true ?
'</pre>' :
'';
$dump .= "\n";
$dump = preg_replace("/[^[:print:]\\r\\n]/", ".", $dump);
// Output method
if ($return === false) {
echo $dump;
} else {
return $dump;
}
}

View File

@ -81,6 +81,7 @@ $pstruct = array(
),
0x84 => array(
"ubyte",
"special1",
/*10,
8,
@ -88,6 +89,11 @@ $pstruct = array(
"byte", */
),
0x8c => array(
"ubyte",
"special1",
),
0xc0 => array(
6,
),

View File

@ -35,5 +35,6 @@ $packetName = array(
0x1c => "ID_UNCONNECTED_PONG", //RakNet
0x1d => "ID_ADVERTISE_SYSTEM", //RakNet
0x84 => "ID_RESERVED_7", //Minecraft Implementation
0x8c => "Unknown", //Minecraft Implementation
0xc0 => "Unknown", //Minecraft Implementation
);