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\DataPacket;
use pocketmine\network\protocol\Info; use pocketmine\network\protocol\Info;
use pocketmine\network\query\QueryHandler; use pocketmine\network\query\QueryHandler;
use pocketmine\network\query\QueryPacket;
use pocketmine\network\RakLibInterface; use pocketmine\network\RakLibInterface;
use pocketmine\network\rcon\RCON; use pocketmine\network\rcon\RCON;
use pocketmine\network\SourceInterface; use pocketmine\network\SourceInterface;
@ -161,6 +162,8 @@ class Server{
/** @var SourceInterface[] */ /** @var SourceInterface[] */
private $interfaces = []; private $interfaces = [];
/** @var RakLibInterface */
private $mainInterface;
private $serverID; private $serverID;
@ -546,7 +549,35 @@ class Server{
* @param SourceInterface $interface * @param SourceInterface $interface
*/ */
public function addInterface(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
} }
/** /**
@ -1424,7 +1455,7 @@ class Server{
define("BOOTUP_RANDOM", Utils::getRandomBytes(16)); define("BOOTUP_RANDOM", Utils::getRandomBytes(16));
$this->serverID = Binary::readLong(substr(Utils::getUniqueID(true, $this->getIp() . $this->getPort()), 0, 8)); $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("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); $this->logger->info("PocketMine-MP is distributed under the LGPL License", true, true, 0);
@ -1676,8 +1707,8 @@ class Server{
public function start(){ public function start(){
if($this->getConfigBoolean("enable-query", true) === true){ if($this->getConfigBoolean("enable-query", true) === true){
//$this->queryHandler = new QueryHandler(); $this->queryHandler = new QueryHandler();
//TODO: query
} }

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){ public function notifyACK($identifier, $identifierACK){
if(isset($this->players[$identifier])){ if(isset($this->players[$identifier])){
$this->players[$identifier]->handleACK($identifierACK); $this->players[$identifier]->handleACK($identifierACK);

View File

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