From 0dc53c2bc1ad2c9cf32f00e131d4c10c8108bbbc Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Fri, 19 Oct 2012 15:50:36 +0200 Subject: [PATCH] Client side, more packets --- classes/MinecraftInterface.class.php | 12 +- classes/Packet.class.php | 4 +- classes/PocketMinecraftClient.class.php | 161 +++++++++++++++++++++++- classes/PocketMinecraftServer.class.php | 33 ++++- client.php | 43 +++++++ pstruct/RakNet.php | 25 ++-- pstruct/packetName.php | 3 +- 7 files changed, 260 insertions(+), 21 deletions(-) create mode 100644 client.php diff --git a/classes/MinecraftInterface.class.php b/classes/MinecraftInterface.class.php index 6ee2f28cc..2ab13bda9 100644 --- a/classes/MinecraftInterface.class.php +++ b/classes/MinecraftInterface.class.php @@ -28,7 +28,7 @@ the Free Software Foundation, either version 3 of the License, or class MinecraftInterface{ var $pstruct, $name, $server, $protocol; - function __construct($server, $protocol = CURRENT_PROTOCOL, $port = 25565, $listen = false){ + function __construct($server, $protocol = CURRENT_PROTOCOL, $port = 25565, $listen = false, $client = true){ $this->server = new Socket($server, $port, (bool) $listen); $this->protocol = (int) $protocol; require("pstruct/RakNet.php"); @@ -50,7 +50,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" ? "CLIENT->SERVER":"SERVER->CLIENT")." ".$ip.":".$port."]: ".$this->name[$pid]." (0x".Utils::strTohex(chr($pid)).") [lenght ".strlen($raw)."]".PHP_EOL; + $p = "[".microtime(true)."] [".((($origin === "client" and $client === true) or ($origin === "server" and $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); if(is_array($data)){ foreach($data as $i => $d){ @@ -72,16 +72,16 @@ class MinecraftInterface{ return false; } $pid = ord($data[0]); - if($pid === 0x84){ + /*if($pid === 0x84){ $data[0] = substr($data[0], 10); $pid = ord($data[0]); - } + }*/ $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 .= PHP_EOL . "--------------- (1024 byte max extract) ----------" .PHP_EOL; - logg($p, "packets", true, 3); + $p .= PHP_EOL; + logg($p, "packets", true, 2); $this->buffer = ""; //$this->server->recieve("\xff".Utils::writeString('Bad packet id '.$pid.'')); diff --git a/classes/Packet.class.php b/classes/Packet.class.php index 197465a0b..6010d41c8 100644 --- a/classes/Packet.class.php +++ b/classes/Packet.class.php @@ -60,6 +60,7 @@ class Packet{ case "special1": switch($this->pid){ case 0x05: + case 0x84: $this->addRaw($this->data[$field]); break; } @@ -138,6 +139,7 @@ class Packet{ case "special1": switch($this->pid){ case 0x05: + case 0x84: $this->data[] = $this->get(true); break; } @@ -152,7 +154,7 @@ class Packet{ } break; case "string": - $this->data[] = $this->get(Utils::readShort($this->get(2)) << 1); + $this->data[] = $this->get(Utils::readShort($this->get(2))); break; case "long": $this->data[] = Utils::readLong($this->get(8)); diff --git a/classes/PocketMinecraftClient.class.php b/classes/PocketMinecraftClient.class.php index 0df1c85e6..e8ad4e8eb 100644 --- a/classes/PocketMinecraftClient.class.php +++ b/classes/PocketMinecraftClient.class.php @@ -26,8 +26,165 @@ 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; + var $serverList = array(); + function __construct($username, $protocol = CURRENT_PROTOCOL, $version = CURRENT_VERSION){ + //$this->player = new Player($username); + $this->version = (int) $version; + $this->username = $username; + $this->connected = false; + $this->cnt = 1; + $this->clientID = substr(Utils::generateKey(), 0, 8); + $this->events = array("disabled" => array()); + $this->actions = array(); + $this->interface = new MinecraftInterface("255.255.255.255", $protocol, 19132); + console("[INFO] Creating Minecraft Client"); + console("[INFO] Username: ".$this->username); + console("[INFO] Version: ".$this->version); + $this->event("onReceivedPacket", "packetHandler", true); + $this->stop = false; + declare(ticks=15); + register_tick_function(array($this, "tickerFunction")); + } + public function action($microseconds, $code){ + $this->actions[] = array($microseconds / 1000000, microtime(true), $code); + console("[INTERNAL] Attached to action ".$microseconds, true, true, 3); + } - - + public function tickerFunction(){ + //actions that repeat every x time will go here + $time = microtime(true); + foreach($this->actions as $id => $action){ + if($action[1] <= ($time - $action[0])){ + $this->actions[$id][1] = $time; + eval($action[2]); + } + } + } + + public function start($ip){ + $this->stop = false; + $this->action(50000, '$this->trigger("onTick", $time);'); + $this->interface = new MinecraftInterface($ip, $this->protocol, 19132); + console("[INFO] Connecting to Server ".$ip); + $this->send(0x05, array( + MAGIC, + $this->version, + str_repeat("\x00", 1447), + )); + $this->process(); + } + + public function getServerList(){ + $this->action(1000000, '$this->send(0x02, array((microtime(true) * 1000)));'); + $this->action(5000000, '$this->actions = array();$this->stop = true;'); + $this->process(); + $list = array(); + foreach($this->serverList as $ip => $info){ + $info["ip"] = $ip; + $list[] = $info; + } + return $list; + } + + public function packetHandler($packet, $event){ + $data =& $packet["data"]; + switch($packet["pid"]){ + case 0x1c: + $pingID = $data[0]; + $this->serverID = $data[1]; + $info = explode(";", $data[3]); + $this->serverList[$packet["ip"]] = array("serverID" => $serverID, "username" => array_pop($info)); + break; + case 0x06: + $serverID = $data[1]; + $lenght = $data[3]; + $this->send(0x07, array( + MAGIC, + "\x04\x3f\x57\xfe\xfd", + 19132, + $lenght, + $this->clientID, + )); + break; + 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), + /*"\x00\x00\x00\x40\x00\x90\x00\x00\x00\x09", + $this->serverID, + (microtime(true) * 1000), + 0,*/ + )); + break; + case 0xc0: + $this->send(0xc0, array( + $data[0], + )); + break; + } + } + + public function send($pid, $data = array(), $raw = false, $dest = false, $port = false){ + $this->trigger($pid, $data); + $this->trigger("onSentPacket", $data); + $this->interface->writePacket($pid, $data, $raw, $dest, $port); + } + + public function process(){ + while($this->stop === false){ + $packet = $this->interface->readPacket(); + if($packet !== false){ + $this->trigger("onReceivedPacket", $packet); + $this->trigger($packet["pid"], $packet); + }else{ + usleep(10000); + } + } + } + + public function trigger($event, $data = ""){ + console("[INTERNAL] Event ". $event, true, true, 3); + if(isset($this->events[$event]) and !isset($this->events["disabled"][$event])){ + foreach($this->events[$event] as $eid => $ev){ + if(isset($ev[1]) and ($ev[1] === true or is_object($ev[1]))){ + $this->responses[$eid] = call_user_func(array(($ev[1] === true ? $this:$ev[1]), $ev[0]), $data, $event, $this); + }else{ + $this->responses[$eid] = call_user_func($ev[0], $data, $event, $this); + } + } + } + } + public function toggleEvent($event){ + if(isset($this->events["disabled"][$event])){ + unset($this->events["disabled"][$event]); + console("[INTERNAL] Enabled event ".$event, true, true, 3); + }else{ + $this->events["disabled"][$event] = false; + console("[INTERNAL] Disabled event ".$event, true, true, 3); + } + } + + public function event($event, $func, $in = false){ + ++$this->cnt; + if(!isset($this->events[$event])){ + $this->events[$event] = array(); + } + $this->events[$event][$this->cnt] = array($func, $in); + console("[INTERNAL] Attached to event ".$event, true, true, 3); + return $this->cnt; + } + + public function deleteEvent($event, $id = -1){ + if($id === -1){ + unset($this->events[$event]); + }else{ + unset($this->events[$event][$id]); + if(isset($this->events[$event]) and count($this->events[$event]) === 0){ + unset($this->events[$event]); + } + } + } + } \ No newline at end of file diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index 6a368364e..ab0c83852 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -34,19 +34,39 @@ class PocketMinecraftServer{ $this->cnt = 1; $this->serverID = substr(Utils::generateKey(), 0, 8); $this->events = array("disabled" => array()); + $this->actions = array(); $this->protocol = (int) $protocol; - $this->interface = new MinecraftInterface("255.255.255.255", $this->protocol, 19132, true); + $this->interface = new MinecraftInterface("255.255.255.255", $this->protocol, 19132, true, false); console("[INFO] Creating Minecraft Server"); console("[INFO] Username: ".$this->username); console("[INFO] Protocol: ".$this->protocol); $this->stop = false; } + public function action($microseconds, $code){ + $this->actions[] = array($microseconds / 1000000, microtime(true), $code); + console("[INTERNAL] Attached to action ".$microseconds, true, true, 3); + } + public function start(){ + declare(ticks=15); + register_tick_function(array($this, "tickerFunction")); + $this->action(50000, '$this->trigger("onTick", $time);'); $this->event("onReceivedPacket", "packetHandler", true); $this->process(); } + public function tickerFunction(){ + //actions that repeat every x time will go here + $time = microtime(true); + foreach($this->actions as $id => $action){ + if($action[1] <= ($time - $action[0])){ + $this->actions[$id][1] = $time; + eval($action[2]); + } + } + } + public function packetHandler($packet, $event){ $data =& $packet["data"]; switch($packet["pid"]){ @@ -77,10 +97,19 @@ class PocketMinecraftServer{ $sess2 = Utils::readInt(substr(Utils::generateKey(), 0, 4)); $this->send(0x08, array( MAGIC, - $clientID, + $this->serverID, $data[1], ), false, $packet["ip"], $packet["port"]); break; + case 0x84: + $bytes = $data[0]; + $clientID = $data[1]; + $time = $data[2]; + $b = $data[3]; + $this->send(0xc0, array( + "\x00\x01\x01\x00\x00\x00", + )); + break; } } diff --git a/client.php b/client.php new file mode 100644 index 000000000..3039ebab0 --- /dev/null +++ b/client.php @@ -0,0 +1,43 @@ +getServerList(); +foreach($list as $i => $info){ + console("[Server] #".$i." ".$info["ip"]." ".$info["username"]); +} +console("[Select Server] #", false, false); +$i = (int) trim(fgets(STDIN)); +if(isset($list[$i])){ + $client->start($list[$i]["ip"]); +}else{ + console("[Error] Unknown ID"); +} \ No newline at end of file diff --git a/pstruct/RakNet.php b/pstruct/RakNet.php index 7ce9f2d96..923d05343 100644 --- a/pstruct/RakNet.php +++ b/pstruct/RakNet.php @@ -27,7 +27,7 @@ the Free Software Foundation, either version 3 of the License, or $pstruct = array( 0x02 => array( - 8, + "double", "magic", ), @@ -60,27 +60,34 @@ $pstruct = array( 0x09 => array( 8, - 8, + "double", "byte", ), - 0x10 => array( - - - ), - 0x1c => array( - 8, + "double", 8, "magic", "string", ), 0x1d => array( - 8, + "double", 8, "magic", "string", ), + + 0x84 => array( + "special1", + /*10, + 8, + "double", + "byte", */ + ), + + 0xc0 => array( + 6, + ), ); \ No newline at end of file diff --git a/pstruct/packetName.php b/pstruct/packetName.php index eb18dde05..80b96be96 100644 --- a/pstruct/packetName.php +++ b/pstruct/packetName.php @@ -32,7 +32,8 @@ $packetName = array( 0x07 => "ID_OPEN_CONNECTION_REQUEST_2", 0x08 => "ID_OPEN_CONNECTION_REPLY_2", 0x09 => "ID_CONNECTION_REQUEST", - 0x10 => "ID_CONNECTION_REQUEST_ACCEPTED", 0x1c => "ID_UNCONNECTED_PONG", 0x1d => "ID_ADVERTISE_SYSTEM", + 0x84 => "Unknown", + 0xc0 => "Unknown", ); \ No newline at end of file