Updated RakLib version, Query works again

This commit is contained in:
Shoghi Cervantes 2014-07-06 01:18:28 +02:00
parent 1b6fcf7942
commit 892119f791
5 changed files with 71 additions and 76 deletions

View File

@ -60,6 +60,7 @@ use pocketmine\nbt\tag\String;
use pocketmine\network\protocol\DataPacket;
use pocketmine\network\protocol\Info;
use pocketmine\network\query\QueryHandler;
use pocketmine\network\query\QueryPacket;
use pocketmine\network\RakLibInterface;
use pocketmine\network\rcon\RCON;
use pocketmine\network\SourceInterface;
@ -161,6 +162,8 @@ class Server{
/** @var SourceInterface[] */
private $interfaces = [];
/** @var RakLibInterface */
private $mainInterface;
private $serverID;
@ -546,7 +549,35 @@ class Server{
* @param SourceInterface $interface
*/
public function addInterface(SourceInterface $interface){
$this->interfaces[] = $interface;
$this->interfaces[spl_object_hash($interface)] = $interface;
}
/**
* @param SourceInterface $interface
*/
public function removeInterface(SourceInterface $interface){
$interface->shutdown();
unset($this->interfaces[spl_object_hash($interface)]);
}
/**
* @param string $address
* @param int $port
* @param string $payload
*/
public function sendPacket($address, $port, $payload){
$this->mainInterface->putRaw($address, $port, $payload);
}
/**
* @param string $address
* @param int $port
* @param string $payload
*/
public function handlePacket($address, $port, $payload){
if(strlen($payload) > 2 and substr($payload, 0, 2) === "\xfe\xfd" and $this->queryHandler instanceof QueryHandler){
$this->queryHandler->handle($address, $port, $payload);
} //TODO: add raw packet events
}
/**
@ -1027,7 +1058,7 @@ class Server{
$this->levels[$level->getID()] = $level;
$this->getPluginManager()->callEvent(new LevelInitEvent($level));
$this->getPluginManager()->callEvent(new LevelLoadEvent($level));
for($Z = 5; $Z <= 11; ++$Z){
@ -1424,7 +1455,7 @@ class Server{
define("BOOTUP_RANDOM", Utils::getRandomBytes(16));
$this->serverID = Binary::readLong(substr(Utils::getUniqueID(true, $this->getIp() . $this->getPort()), 0, 8));
$this->interfaces[] = new RakLibInterface($this);
$this->addInterface($this->mainInterface = new RakLibInterface($this));
$this->logger->info("This server is running PocketMine-MP version " . ($version->isDev() ? TextFormat::YELLOW : "") . $version->get(false) . TextFormat::RESET . " \"" . $this->getCodename() . "\" (API " . $this->getApiVersion() . ")", true, true, 0);
$this->logger->info("PocketMine-MP is distributed under the LGPL License", true, true, 0);
@ -1676,8 +1707,8 @@ class Server{
public function start(){
if($this->getConfigBoolean("enable-query", true) === true){
//$this->queryHandler = new QueryHandler();
//TODO: query
$this->queryHandler = new QueryHandler();
}

View File

@ -153,6 +153,14 @@ class RakLibInterface implements ServerInstance, SourceInterface{
}
}
public function handleRaw($address, $port, $payload){
$this->server->handlePacket($address, $port, $payload);
}
public function putRaw($address, $port, $payload){
$this->interface->sendRaw($address, $port, $payload);
}
public function notifyACK($identifier, $identifierACK){
if(isset($this->players[$identifier])){
$this->players[$identifier]->handleACK($identifierACK);

View File

@ -32,6 +32,9 @@ use pocketmine\utils\Utils;
class QueryHandler{
private $socket, $server, $lastToken, $token, $longData, $timeout;
const HANDSHAKE = 9;
const STATISTICS = 0;
public function __construct(){
$this->server = Server::getInstance();
$this->server->getLogger()->info("Starting GS4 status listener");
@ -102,39 +105,37 @@ class QueryHandler{
return Binary::readInt(substr(hash("sha512", $salt . ":" . $token, true), 7, 4));
}
public function handle(QueryPacket $packet){
$packet->decode();
switch($packet->packetType){
case QueryPacket::HANDSHAKE: //Handshake
$pk = new QueryPacket;
$pk->ip = $packet->ip;
$pk->port = $packet->port;
$pk->packetType = QueryPacket::HANDSHAKE;
$pk->sessionID = $packet->sessionID;
$pk->payload = self::getTokenString($this->token, $packet->ip) . "\x00";
$pk->encode();
$this->server->sendPacket($pk);
public function handle($address, $port, $packet){
$offset = 2;
$packetType = ord($packet{$offset++});
$sessionID = Binary::readInt(substr($packet, $offset, 4));
$offset += 4;
$payload = substr($packet, $offset);
switch($packetType){
case self::HANDSHAKE: //Handshake
$reply = chr(self::HANDSHAKE);
$reply .= Binary::writeInt($sessionID);
$reply .= self::getTokenString($this->token, $address) . "\x00";
$this->server->sendPacket($address, $port, $reply);
break;
case QueryPacket::STATISTICS: //Stat
$token = Binary::readInt(substr($packet->payload, 0, 4));
if($token !== self::getTokenString($this->token, $packet->ip) and $token !== self::getTokenString($this->lastToken, $packet->ip)){
case self::STATISTICS: //Stat
$token = Binary::readInt(substr($payload, 0, 4));
if($token !== self::getTokenString($this->token, $address) and $token !== self::getTokenString($this->lastToken, $ip)){
break;
}
$pk = new QueryPacket;
$pk->ip = $packet->ip;
$pk->port = $packet->port;
$pk->packetType = QueryPacket::STATISTICS;
$pk->sessionID = $packet->sessionID;
if(strlen($packet->payload) === 8){
$reply = chr(self::STATISTICS);
$reply .= Binary::writeInt($sessionID);
if(strlen($payload) === 8){
if($this->timeout < microtime(true)){
$this->regenerateInfo();
}
$pk->payload = $this->longData;
$reply .= $this->longData;
}else{
$pk->payload = $this->server->getServerName() . "\x00" . (($this->server->getGamemode() & 0x01) === 0 ? "SMP" : "CMP") . "\x00" . $this->server->getDefaultLevel()->getName() . "\x00" . count($this->server->getOnlinePlayers()) . "\x00" . $this->server->getMaxPlayers() . "\x00" . Binary::writeLShort($this->server->getPort()) . $this->server->getIp() . "\x00";
$reply .= $this->server->getServerName() . "\x00" . (($this->server->getGamemode() & 0x01) === 0 ? "SMP" : "CMP") . "\x00" . $this->server->getDefaultLevel()->getName() . "\x00" . count($this->server->getOnlinePlayers()) . "\x00" . $this->server->getMaxPlayers() . "\x00" . Binary::writeLShort($this->server->getPort()) . $this->server->getIp() . "\x00";
}
$pk->encode();
$this->server->sendPacket($pk);
$this->server->sendPacket($address, $port, $reply);
break;
}
}

View File

@ -1,45 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* 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.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\network\query;
use pocketmine\utils\Binary;
class QueryPacket extends Packet{
const HANDSHAKE = 9;
const STATISTICS = 0;
public $packetType;
public $sessionID;
public $payload;
public function decode(){
$this->packetType = ord($this->buffer{2});
$this->sessionID = Binary::readInt(substr($this->buffer, 3, 4));
$this->payload = substr($this->buffer, 7);
}
public function encode(){
$this->buffer .= chr($this->packetType);
$this->buffer .= Binary::writeInt($this->sessionID);
$this->buffer .= $this->payload;
}
}

@ -1 +1 @@
Subproject commit 3f1f9ca18e76b80a37913312704dc982f3c101a6
Subproject commit 740c96f033322d99e8804bb1a0218abef0ef117a