From 60c80110d33e78371dc5258e6c14a195fe7d93e1 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Mon, 22 Oct 2012 01:49:10 +0200 Subject: [PATCH] Server handles Client Sessions --- classes/MinecraftInterface.class.php | 2 +- classes/PocketMinecraftServer.class.php | 34 ++++++++++------------- classes/Session.class.php | 36 +++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/classes/MinecraftInterface.class.php b/classes/MinecraftInterface.class.php index 09509cbd9..e7de67a14 100644 --- a/classes/MinecraftInterface.class.php +++ b/classes/MinecraftInterface.class.php @@ -106,7 +106,7 @@ class MinecraftInterface{ $packet->data = $data; $packet->create(); $write = $this->server->write($packet->raw, $dest, $port); - $this->writeDump($pid, $packet->raw, $data, "client"); + $this->writeDump($pid, $packet->raw, $data, "client", $dest, $port); }else{ $write = $this->server->write($data, $dest, $port); $this->writeDump($pid, $data, false, "client", $dest, $port); diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index 6a1335078..0fd034a63 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -25,8 +25,10 @@ the Free Software Foundation, either version 3 of the License, or */ +require_once("classes/Session.class.php"); + class PocketMinecraftServer{ - protected $interface, $protocol, $entities, $player, $cnt, $events, $username, $version; + protected $interface, $protocol, $entities, $player, $cnt, $events, $username, $version, $clients; function __construct($username, $protocol = CURRENT_PROTOCOL, $version = CURRENT_VERSION){ //$this->player = new Player($username); $this->version = (int) $version; @@ -35,6 +37,7 @@ class PocketMinecraftServer{ $this->serverID = substr(Utils::generateKey(), 0, 8); $this->events = array("disabled" => array()); $this->actions = array(); + $this->clients = array(); $this->protocol = (int) $protocol; $this->interface = new MinecraftInterface("255.255.255.255", $this->protocol, 19132, true, false); console("[INFO] Creating Minecraft Server"); @@ -67,8 +70,16 @@ class PocketMinecraftServer{ } } + public function clientID($ip, $port){ + return md5($pi . $port, true); + } + public function packetHandler($packet, $event){ $data =& $packet["data"]; + $CID = $this->clientID($packet["ip"], $packet["port"]); + if(isset($this->clients[$CID])){ + $this->clients[$CID]->handle($packet["pid"], $data); + } switch($packet["pid"]){ case 0x02: $this->send(0x1c, array( @@ -81,7 +92,6 @@ class PocketMinecraftServer{ case 0x05: $version = $data[1]; $size = strlen($data[2]); - console("[DEBUG] ".$packet["ip"].":".$packet["port"]." v".$version." MTU Sizing ".$size, true, true, 2); if($version != 5){ $this->send(0x1a, array( 5, @@ -101,24 +111,8 @@ class PocketMinecraftServer{ $port = $data[2]; $MTU = $data[3]; $clientID = $data[4]; - //console("[DEBUG] ".$packet["ip"].":".$packet["port"]." v".$version." response (".$size.")", true, true, 2); - //$sess2 = Utils::readInt(substr(Utils::generateKey(), 0, 4)); - $this->send(0x08, array( - MAGIC, - $this->serverID, - $packet["port"], - $MTU, - 0, - ), 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", - )); + $this->clients[$CID] = new Session($this, $clientID, $packet["ip"], $packet["port"]); + $this->clients[$CID]->handle(0x07, $data); break; } } diff --git a/classes/Session.class.php b/classes/Session.class.php index f0f25aad4..77607bdf9 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -27,7 +27,39 @@ the Free Software Foundation, either version 3 of the License, or class Session{ - - + protected $server, $serverID; + var $clientID, $ip, $port; + function __construct($server, $clientID, $ip, $port){ + $this->server = $server; + $this->clientID = $clientID; + $this->ip = $ip; + $this->port = $port; + $this->serverID =& $this->server->serverID; + console("[DEBUG] New Session started with ".$ip.":".$port, true, true, 2); + } + + public function handle($pid, &$data){ + switch($pid){ + case 0x07: + $this->send(0x08, array( + MAGIC, + $this->serverID, + $this->port, + $data[3], + 0, + )); + break; + case 0x84: + $counter = $data[0]; + $this->send(0xc0, array( + "\x00\x01\x01\x00\x00\x00", + )); + break; + } + } + + public function send($pid, $data = array(), $raw = false){ + $this->server->send($pid, $data, $raw, $this->ip, $this->port); + } } \ No newline at end of file