mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-11 04:17:48 +00:00
PrepareEncryptionTask: remove cyclic dependency on NetworkSession
This commit is contained in:
parent
e86c243db5
commit
3d3a487422
@ -530,25 +530,23 @@ class NetworkSession{
|
|||||||
|
|
||||||
if($this->manager->kickDuplicates($this)){
|
if($this->manager->kickDuplicates($this)){
|
||||||
if(NetworkCipher::$ENABLED){
|
if(NetworkCipher::$ENABLED){
|
||||||
$this->server->getAsyncPool()->submitTask(new PrepareEncryptionTask($this, $clientPubKey));
|
$this->server->getAsyncPool()->submitTask(new PrepareEncryptionTask($clientPubKey, function(string $encryptionKey, string $handshakeJwt) : void{
|
||||||
|
if(!$this->connected){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$this->sendDataPacket(ServerToClientHandshakePacket::create($handshakeJwt), true); //make sure this gets sent before encryption is enabled
|
||||||
|
|
||||||
|
$this->cipher = new NetworkCipher($encryptionKey);
|
||||||
|
|
||||||
|
$this->setHandler(new HandshakePacketHandler($this));
|
||||||
|
$this->logger->debug("Enabled encryption");
|
||||||
|
}));
|
||||||
}else{
|
}else{
|
||||||
$this->onLoginSuccess();
|
$this->onLoginSuccess();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function enableEncryption(string $encryptionKey, string $handshakeJwt) : void{
|
|
||||||
if(!$this->connected){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$this->sendDataPacket(ServerToClientHandshakePacket::create($handshakeJwt), true); //make sure this gets sent before encryption is enabled
|
|
||||||
|
|
||||||
$this->cipher = new NetworkCipher($encryptionKey);
|
|
||||||
|
|
||||||
$this->setHandler(new HandshakePacketHandler($this));
|
|
||||||
$this->logger->debug("Enabled encryption");
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onLoginSuccess() : void{
|
public function onLoginSuccess() : void{
|
||||||
$this->loggedIn = true;
|
$this->loggedIn = true;
|
||||||
|
|
||||||
|
@ -30,7 +30,6 @@ use Mdanter\Ecc\Serializer\PrivateKey\DerPrivateKeySerializer;
|
|||||||
use Mdanter\Ecc\Serializer\PrivateKey\PemPrivateKeySerializer;
|
use Mdanter\Ecc\Serializer\PrivateKey\PemPrivateKeySerializer;
|
||||||
use Mdanter\Ecc\Serializer\PublicKey\DerPublicKeySerializer;
|
use Mdanter\Ecc\Serializer\PublicKey\DerPublicKeySerializer;
|
||||||
use Mdanter\Ecc\Serializer\Signature\DerSignatureSerializer;
|
use Mdanter\Ecc\Serializer\Signature\DerSignatureSerializer;
|
||||||
use pocketmine\network\mcpe\NetworkSession;
|
|
||||||
use pocketmine\scheduler\AsyncTask;
|
use pocketmine\scheduler\AsyncTask;
|
||||||
use function base64_encode;
|
use function base64_encode;
|
||||||
use function gmp_strval;
|
use function gmp_strval;
|
||||||
@ -47,7 +46,7 @@ use const STR_PAD_LEFT;
|
|||||||
|
|
||||||
class PrepareEncryptionTask extends AsyncTask{
|
class PrepareEncryptionTask extends AsyncTask{
|
||||||
|
|
||||||
private const TLS_KEY_SESSION = "session";
|
private const TLS_KEY_ON_COMPLETION = "completion";
|
||||||
|
|
||||||
/** @var PrivateKeyInterface|null */
|
/** @var PrivateKeyInterface|null */
|
||||||
private static $SERVER_PRIVATE_KEY = null;
|
private static $SERVER_PRIVATE_KEY = null;
|
||||||
@ -61,15 +60,24 @@ class PrepareEncryptionTask extends AsyncTask{
|
|||||||
private $handshakeJwt = null;
|
private $handshakeJwt = null;
|
||||||
/** @var PublicKeyInterface */
|
/** @var PublicKeyInterface */
|
||||||
private $clientPub;
|
private $clientPub;
|
||||||
|
/**
|
||||||
|
* @var \Closure
|
||||||
|
* @phpstan-var \Closure(string $encryptionKey, string $handshakeJwt) : void
|
||||||
|
*/
|
||||||
|
private $onCompletion;
|
||||||
|
|
||||||
public function __construct(NetworkSession $session, PublicKeyInterface $clientPub){
|
/**
|
||||||
|
* @phpstan-param \Closure(string $encryptionKey, string $handshakeJwt) : void $onCompletion
|
||||||
|
*/
|
||||||
|
public function __construct(PublicKeyInterface $clientPub, \Closure $onCompletion){
|
||||||
if(self::$SERVER_PRIVATE_KEY === null){
|
if(self::$SERVER_PRIVATE_KEY === null){
|
||||||
self::$SERVER_PRIVATE_KEY = EccFactory::getNistCurves()->generator384()->createPrivateKey();
|
self::$SERVER_PRIVATE_KEY = EccFactory::getNistCurves()->generator384()->createPrivateKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->serverPrivateKey = self::$SERVER_PRIVATE_KEY;
|
$this->serverPrivateKey = self::$SERVER_PRIVATE_KEY;
|
||||||
$this->clientPub = $clientPub;
|
$this->clientPub = $clientPub;
|
||||||
$this->storeLocal(self::TLS_KEY_SESSION, $session);
|
$this->storeLocal(self::TLS_KEY_ON_COMPLETION, $onCompletion);
|
||||||
|
$this->onCompletion = $onCompletion;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onRun() : void{
|
public function onRun() : void{
|
||||||
@ -107,8 +115,11 @@ class PrepareEncryptionTask extends AsyncTask{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function onCompletion() : void{
|
public function onCompletion() : void{
|
||||||
/** @var NetworkSession $session */
|
/**
|
||||||
$session = $this->fetchLocal(self::TLS_KEY_SESSION);
|
* @var \Closure $callback
|
||||||
$session->enableEncryption($this->aesKey, $this->handshakeJwt);
|
* @phpstan-var \Closure(string $encryptionKey, string $handshakeJwt) : void $callback
|
||||||
|
*/
|
||||||
|
$callback = $this->fetchLocal(self::TLS_KEY_ON_COMPLETION);
|
||||||
|
$callback($this->aesKey, $this->handshakeJwt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user