Updated to allow multiple protocol versions

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-06 17:05:05 +01:00
parent 8b1c010b4d
commit 7732d6b6f7
9 changed files with 124 additions and 49 deletions

View File

@ -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{

View File

@ -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;

View File

@ -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,

View File

@ -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;
}
}
}

View File

@ -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");

41
pstruct/4.php Normal file
View File

@ -0,0 +1,41 @@
<?php
/*
-
/ \
/ \
/ POCKET \
/ MINECRAFT PHP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
//Protocol Version: 4
define("MC_KEEP_ALIVE", 0x00);
define("MC_CLIENT_HANDSHAKE", 0x09);
define("MC_SERVER_HANDSHAKE", 0x10);
define("MC_CLIENT_CONNECT", 0x13);
define("MC_CLIENT_DISCONNECT", 0x15);
define("MC_LOGIN", 0x86);
define("MC_LOGIN_STATUS", 0x87);
define("MC_READY", 0x88);
define("MC_CHAT", 0x89);
define("MC_SET_TIME", 0x8a);
define("MC_START_GAME", 0x8b);

41
pstruct/5.php Normal file
View File

@ -0,0 +1,41 @@
<?php
/*
-
/ \
/ \
/ POCKET \
/ MINECRAFT PHP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
//Protocol Version: 5
define("MC_KEEP_ALIVE", 0x00);
define("MC_CLIENT_HANDSHAKE", 0x09);
define("MC_SERVER_HANDSHAKE", 0x10);
define("MC_CLIENT_CONNECT", 0x13);
define("MC_CLIENT_DISCONNECT", 0x15);
define("MC_LOGIN", 0x82);
define("MC_LOGIN_STATUS", 0x83);
define("MC_READY", 0x84);
define("MC_CHAT", 0x85);
define("MC_SET_TIME", 0x86);
define("MC_START_GAME", 0x87);

View File

@ -26,23 +26,23 @@ the Free Software Foundation, either version 3 of the License, or
*/
$dataName = array(
0x00 => "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"
);

View File

@ -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();