ProcessLoginTask no longer depends on NetworkSession

This commit is contained in:
Dylan K. Taylor 2020-04-22 13:30:37 +01:00
parent dd37d286f0
commit 35d656c6e5
3 changed files with 38 additions and 15 deletions

View File

@ -163,11 +163,18 @@ class NetworkSession{
$this->connectTime = time();
$this->setHandler(new LoginPacketHandler($this->server, $this, function(PlayerInfo $info) : void{
$this->info = $info;
$this->logger->info("Player: " . TextFormat::AQUA . $info->getUsername() . TextFormat::RESET);
$this->logger->setPrefix($this->getLogPrefix());
}));
$this->setHandler(new LoginPacketHandler(
$this->server,
$this,
function(PlayerInfo $info) : void{
$this->info = $info;
$this->logger->info("Player: " . TextFormat::AQUA . $info->getUsername() . TextFormat::RESET);
$this->logger->setPrefix($this->getLogPrefix());
},
function(bool $isAuthenticated, bool $authRequired, ?string $error, ?PublicKeyInterface $clientPubKey) : void{
$this->setAuthenticationStatus($isAuthenticated, $authRequired, $error, $clientPubKey);
}
));
$this->manager->add($this);
$this->logger->info("Session opened");

View File

@ -28,7 +28,6 @@ use Mdanter\Ecc\Crypto\Signature\Signature;
use Mdanter\Ecc\Serializer\PublicKey\DerPublicKeySerializer;
use Mdanter\Ecc\Serializer\PublicKey\PemPublicKeySerializer;
use Mdanter\Ecc\Serializer\Signature\DerSignatureSerializer;
use pocketmine\network\mcpe\NetworkSession;
use pocketmine\network\mcpe\protocol\LoginPacket;
use pocketmine\scheduler\AsyncTask;
use function assert;
@ -46,7 +45,7 @@ use function time;
use const OPENSSL_ALGO_SHA384;
class ProcessLoginTask extends AsyncTask{
private const TLS_KEY_SESSION = "session";
private const TLS_KEY_ON_COMPLETION = "completion";
public const MOJANG_ROOT_PUBLIC_KEY = "MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE8ELkixyLcwlZryUQcu1TvPOmI2B7vX83ndnWRUaXm74wFfa5f/lwQNTfrLVHa2PmenpGI6JhIMUJaWZrjmMj90NoKNFSNBuKdm8rYiXsfaz3K36x/1U26HpG0ZxK/V1V";
@ -74,8 +73,11 @@ class ProcessLoginTask extends AsyncTask{
/** @var PublicKeyInterface|null */
private $clientPublicKey = null;
public function __construct(NetworkSession $session, LoginPacket $packet, bool $authRequired){
$this->storeLocal(self::TLS_KEY_SESSION, $session);
/**
* @phpstan-var \Closure(bool $isAuthenticated, bool $authRequired, ?string $error, ?PublicKeyInterface $clientPublicKey) : void $onCompletion
*/
public function __construct(LoginPacket $packet, bool $authRequired, \Closure $onCompletion){
$this->storeLocal(self::TLS_KEY_ON_COMPLETION, $onCompletion);
$this->packet = $packet;
$this->authRequired = $authRequired;
}
@ -169,8 +171,11 @@ class ProcessLoginTask extends AsyncTask{
}
public function onCompletion() : void{
/** @var NetworkSession $session */
$session = $this->fetchLocal(self::TLS_KEY_SESSION);
$session->setAuthenticationStatus($this->authenticated, $this->authRequired, $this->error, $this->clientPublicKey);
/**
* @var \Closure $callback
* @phpstan-var \Closure(bool, bool, ?string, ?PublicKeyInterface) : void $callback
*/
$callback = $this->fetchLocal(self::TLS_KEY_ON_COMPLETION);
$callback($this->authenticated, $this->authRequired, $this->error, $this->clientPublicKey);
}
}

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\handler;
use pocketmine\entity\Skin;
use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
use pocketmine\event\player\PlayerPreLoginEvent;
use pocketmine\network\BadPacketException;
use pocketmine\network\mcpe\auth\ProcessLoginTask;
@ -59,14 +59,25 @@ class LoginPacketHandler extends PacketHandler{
* @phpstan-var \Closure(PlayerInfo) : void
*/
private $playerInfoConsumer;
/**
* @var \Closure
* @phpstan-var \Closure(bool, bool, ?string, ?PublicKeyInterface) : void
*/
private $authCallback;
/**
* @phpstan-param \Closure(PlayerInfo) : void $playerInfoConsumer
* @phpstan-param \Closure(bool $isAuthenticated, bool $authRequired, ?string $error, ?PublicKeyInterface $clientPubKey) : void $authCallback
*/
public function __construct(Server $server, NetworkSession $session, \Closure $playerInfoConsumer){
public function __construct(Server $server, NetworkSession $session, \Closure $playerInfoConsumer, \Closure $authCallback){
$this->session = $session;
$this->server = $server;
$this->playerInfoConsumer = $playerInfoConsumer;
$this->authCallback = $authCallback;
}
private static function dummy() : void{
echo PublicKeyInterface::class; //this prevents the import getting removed by tools that don't understand phpstan
}
public function handleLogin(LoginPacket $packet) : bool{
@ -182,7 +193,7 @@ class LoginPacketHandler extends PacketHandler{
* @throws \InvalidArgumentException
*/
protected function processLogin(LoginPacket $packet, bool $authRequired) : void{
$this->server->getAsyncPool()->submitTask(new ProcessLoginTask($this->session, $packet, $authRequired));
$this->server->getAsyncPool()->submitTask(new ProcessLoginTask($packet, $authRequired, $this->authCallback));
$this->session->setHandler(NullPacketHandler::getInstance()); //drop packets received during login verification
}