From 7732d6b6f72eed5497b9878630256a0d046fcdec Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Thu, 6 Dec 2012 17:05:05 +0100 Subject: [PATCH] Updated to allow multiple protocol versions --- classes/CustomPacketHandler.class.php | 18 +++++------ classes/MinecraftInterface.class.php | 1 + classes/PocketMinecraftServer.class.php | 5 +-- classes/Session.class.php | 34 ++++++++------------ common/config.php | 4 +-- pstruct/4.php | 41 +++++++++++++++++++++++++ pstruct/5.php | 41 +++++++++++++++++++++++++ pstruct/dataName.php | 26 ++++++++-------- server.php | 3 +- 9 files changed, 124 insertions(+), 49 deletions(-) create mode 100644 pstruct/4.php create mode 100644 pstruct/5.php diff --git a/classes/CustomPacketHandler.class.php b/classes/CustomPacketHandler.class.php index 70931bf8..6d95afcc 100644 --- a/classes/CustomPacketHandler.class.php +++ b/classes/CustomPacketHandler.class.php @@ -49,14 +49,14 @@ class CustomPacketHandler{ $this->offset = 0; $this->c = (bool) $create; switch($pid){ - case 0x00: + case MC_KEEP_ALIVE: if($this->c === false){ $this->data["payload"] = Utils::readLong($this->get(8)); }else{ $this->raw .= Utils::writeLong($this->data["payload"]); } break; - case 0x09: + case MC_CLIENT_HANDSHAKE: if($this->c === false){ $this->data["clientID"] = Utils::readLong($this->get(8)); $this->data["session"] = Utils::readLong($this->get(8)); @@ -67,7 +67,7 @@ class CustomPacketHandler{ $this->raw .= "\x00"; } break; - case 0x10: + case MC_SERVER_HANDSHAKE: if($this->c === false){ $this->data["cookie"] = $this->get(4); // 043f57fe $this->data["security"] = $this->get(1); @@ -98,7 +98,7 @@ class CustomPacketHandler{ $this->raw .= Utils::writeLong($this->data["session2"]); } break; - case 0x13: + case MC_CLIENT_CONNECT: if($this->c === false){ $this->data["cookie"] = $this->get(4); // 043f57fe $this->data["security"] = $this->get(1); @@ -121,13 +121,13 @@ class CustomPacketHandler{ $this->raw .= Utils::writeLong($this->data["session"]); } break; - case 0x15: + case MC_CLIENT_DISCONNECT: //null break; case 0x18: //null break; - case 0x82: + case MC_LOGIN: if($this->c === false){ $this->data["username"] = $this->get(Utils::readShort($this->get(2), false)); $this->data["unknown1"] = Utils::readInt($this->get(4)); @@ -137,21 +137,21 @@ class CustomPacketHandler{ $this->raw .= "\x00\x00\x00\x07\x00\x00\x00\x07"; } break; - case 0x83: + case MC_LOGIN_STATUS: if($this->c === false){ $this->data["status"] = Utils::readInt($this->get(4)); }else{ $this->raw .= Utils::writeInt($this->data["status"]); } break; - case 0x84: + case MC_READY: if($this->c === false){ $this->data["status"] = ord($this->get(1)); }else{ $this->raw .= chr($this->data["status"]); } break; - case 0x85: + case MC_CHAT: if($this->c === false){ $this->data["message"] = $this->get(Utils::readShort($this->get(2), false)); }else{ diff --git a/classes/MinecraftInterface.class.php b/classes/MinecraftInterface.class.php index a4345c86..ddaa1b05 100644 --- a/classes/MinecraftInterface.class.php +++ b/classes/MinecraftInterface.class.php @@ -33,6 +33,7 @@ class MinecraftInterface{ $this->protocol = (int) $protocol; require("pstruct/RakNet.php"); require("pstruct/packetName.php"); + require("pstruct/".$this->protocol.".php"); require("pstruct/dataName.php"); $this->pstruct = $pstruct; $this->name = $packetName; diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index 41b33208..407ac4b4 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -30,7 +30,7 @@ require_once("classes/Session.class.php"); class PocketMinecraftServer{ var $seed, $protocol, $gamemode, $name, $maxClients, $clients; protected $interface, $entities, $player, $cnt, $events, $version, $serverType; - function __construct($name, $gamemode = 1, $seed = false, $port = 19132, $protocol = CURRENT_PROTOCOL, $serverID = false, $version = CURRENT_VERSION){ + function __construct($name, $gamemode = 1, $seed = false, $protocol = CURRENT_PROTOCOL, $port = 19132, $serverID = false, $version = CURRENT_VERSION){ $this->gamemode = (int) $gamemode; $this->port = (int) $port; $this->version = (int) $version; @@ -55,6 +55,7 @@ class PocketMinecraftServer{ $this->action(1000000 * 5 * 60, '$this->chat(false, "Check it at http://bit.ly/RE7uaW");'); console("[INFO] Server Name: ".$this->name); console("[INFO] Server GUID: ".$this->serverID); + console("[INFO] Protocol Version: ".$this->protocol); console("[INFO] Seed: ".$this->seed); console("[INFO] Gamemode: ".($this->gamemode === 0 ? "survival":"creative")); console("[INFO] Max Clients: ".$this->maxClients); @@ -143,7 +144,7 @@ class PocketMinecraftServer{ case 0x05: $version = $data[1]; $size = strlen($data[2]); - if($version != 5){ + if($version !== $this->protocol){ $this->send(0x1a, array( 5, MAGIC, diff --git a/classes/Session.class.php b/classes/Session.class.php index 606f5108..85683fda 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -55,16 +55,6 @@ class Session{ foreach($this->evid as $ev){ $this->server->deleteEvent($ev[0], $ev[1]); } - if($this->reason === "server stop"){ - $this->send(0x84, array( - $this->counter[0], - 0x00, - array( - "id" => 0x15, - ), - )); - ++$this->counter[0]; - } $this->connected = false; $this->server->trigger("onChat", $this->username." left the game"); console("[DEBUG] Session with ".$this->ip.":".$this->port." closed due to ".$reason, true, true, 2); @@ -78,7 +68,7 @@ class Session{ $this->counter[0], 0x00, array( - "id" => 0x86, + "id" => MC_SET_TIME, "time" => $data, ), )); @@ -89,7 +79,7 @@ class Session{ $this->counter[0], 0x00, array( - "id" => 0x85, + "id" => MC_CHAT, "message" => $data, ), )); @@ -110,22 +100,25 @@ class Session{ $data[3], 0, )); - break; + break; + case 0x80: case 0x84: + case 0x88: + case 0x8c: if(isset($data[0])){ $this->counter[1] = $data[0]; $this->send(0xc0, array(1, true, $data[0])); } switch($data["id"]){ - case 0x15: + case MC_CLIENT_DISCONNECT: $this->close("client disconnect"); break; - case 0x09: + case MC_CLIENT_HANDSHAKE: $this->send(0x84, array( $this->counter[0], 0x00, array( - "id" => 0x10, + "id" => MC_SERVER_HANDSHAKE, "port" => $this->port, "session" => $data["session"], "session2" => Utils::readLong("\x00\x00\x00\x00\x04\x44\x0b\xa9"), @@ -134,7 +127,7 @@ class Session{ ++$this->counter[0]; break; - case 0x82: + case MC_LOGIN: $this->username = $data["username"]; console("[INFO] ".$this->username." connected from ".$this->ip.":".$this->port); $this->evid[] = array("onTimeChange", $this->server->event("onTimeChange", array($this, "eventHandler"))); @@ -143,7 +136,7 @@ class Session{ $this->counter[0], 0x00, array( - "id" => 0x83, + "id" => MC_LOGIN_STATUS, "status" => 0, ), )); @@ -152,7 +145,7 @@ class Session{ $this->counter[0], 0x00, array( - "id" => 0x87, + "id" => MC_START_GAME, "seed" => $this->server->seed, "x" => 128, "y" => 100, @@ -172,9 +165,6 @@ class Session{ } break; - case 0x8c: - $counter = $data[0]; - break; } } } diff --git a/common/config.php b/common/config.php index f764b882..8b28b06f 100644 --- a/common/config.php +++ b/common/config.php @@ -32,8 +32,8 @@ ini_set('default_charset', 'utf-8'); define("FILE_PATH", dirname(__FILE__)."/../"); set_include_path(get_include_path() . PATH_SEPARATOR . FILE_PATH . PATH_SEPARATOR . FILE_PATH . "/classes/"); ini_set("memory_limit", "512M"); -define("CURRENT_PROTOCOL", 1); -define("CURRENT_VERSION", 5); +define("CURRENT_PROTOCOL", 5); +define("CURRENT_VERSION", 1); define("DEBUG", 2); define("LOG", true); define("MAGIC", "\x00\xff\xff\x00\xfe\xfe\xfe\xfe\xfd\xfd\xfd\xfd\x12\x34\x56\x78"); \ No newline at end of file diff --git a/pstruct/4.php b/pstruct/4.php new file mode 100644 index 00000000..747ffb6c --- /dev/null +++ b/pstruct/4.php @@ -0,0 +1,41 @@ + "KeepAlive", + MC_KEEP_ALIVE => "KeepAlive", - 0x09 => "ClientHandshake", - 0x10 => "ServerHandshake", + MC_CLIENT_HANDSHAKE => "ClientHandshake", + MC_SERVER_HANDSHAKE => "ServerHandshake", - 0x13 => "ClientConnect", + MC_CLIENT_CONNECT => "ClientConnect", - 0x15 => "ClientDisconnect", + MC_CLIENT_DISCONNECT => "ClientDisconnect", - 0x18 => "ServerDisconnect", + 0x18 => "Unknown", - 0x82 => "Login", - 0x83 => "LoginStatus", - 0x84 => "Ready", - 0x85 => "Message", - 0x86 => "SetTime", - 0x87 => "StartGame", + MC_LOGIN => "Login", + MC_LOGIN_STATUS => "LoginStatus", + MC_READY => "Ready", + MC_CHAT => "Chat", + MC_SET_TIME => "SetTime", + MC_START_GAME => "StartGame", 0x93 => "MoveEntity_PosRot", 0x94 => "MovePlayer", @@ -59,4 +59,6 @@ $dataName = array( 0xa5 => "SetHealth", 0xa7 => "Animate", + + 0xb1 => "ClientMessage" ); \ No newline at end of file diff --git a/server.php b/server.php index 1f4ce11a..f92e6353 100644 --- a/server.php +++ b/server.php @@ -29,6 +29,5 @@ require_once("common/dependencies.php"); require_once("classes/PocketMinecraftServer.class.php"); file_put_contents("packets.log", ""); -$server = new PocketMinecraftServer("PHP Server"); -$server->setType("minecon"); +$server = new PocketMinecraftServer("PHP Server", 1, false, 5); $server->start(); \ No newline at end of file