From 75e5e5cf610d462ad894c7db88833816e5499778 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 15 Aug 2022 17:43:32 +0200 Subject: [PATCH] Fix final update issues --- src/danog/MadelineProto/Connection.php | 2 +- .../MadelineProto/DataCenterConnection.php | 3 + src/danog/MadelineProto/MTProto.php | 18 +++--- .../MTProtoTools/AuthKeyHandler.php | 57 +++++++------------ .../SecretChats/AuthKeyHandler.php | 10 ++-- 5 files changed, 41 insertions(+), 49 deletions(-) diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index 36da012db..9c3b80adc 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -347,7 +347,7 @@ class Connection if (!isset($this->waiter)) { $this->waiter = new HttpWaitLoop($this); } - if (!isset($this->pinger) && !$this->ctx->isMedia() && !$this->ctx->isCDN()) { // && ($this->ctx->hasStreamName(WssStream::class) || $this->ctx->hasStreamName(WsStream::class))) { + if (!isset($this->pinger) && !$this->ctx->isMedia() && !$this->ctx->isCDN()) { $this->pinger = new PingLoop($this); } foreach ($this->new_outgoing as $message_id => $message) { diff --git a/src/danog/MadelineProto/DataCenterConnection.php b/src/danog/MadelineProto/DataCenterConnection.php index 8de7d2764..4097b12c3 100644 --- a/src/danog/MadelineProto/DataCenterConnection.php +++ b/src/danog/MadelineProto/DataCenterConnection.php @@ -212,6 +212,9 @@ class DataCenterConnection implements JsonSerializable } finally { $lock->release(); } + if ($this->hasTempAuthKey()) { + $connection->pingHttpWaiter(); + } } /** * Bind temporary and permanent auth keys. diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index f8b3d99db..9ddd13bf5 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -1541,10 +1541,12 @@ class MTProto extends AsyncConstruct implements TLCallback public function hasAllAuth(): bool { if ($this->isInitingAuthorization()) { + $this->logger("Initing auth"); return false; } - foreach ($this->datacenter->getDataCenterConnections() as $dc) { + foreach ($this->datacenter->getDataCenterConnections() as $id => $dc) { if ((!$dc->isAuthorized() || !$dc->hasTempAuthKey()) && !$dc->isCDN()) { + $this->logger("Initing auth $id"); return false; } } @@ -1702,7 +1704,8 @@ class MTProto extends AsyncConstruct implements TLCallback if (!isset($this->secretFeeders[$id])) { $this->secretFeeders[$id] = new SecretFeedLoop($this, $id); } - if ($this->secretFeeders[$id]->start() && isset($this->secretFeeders[$id])) { + $this->secretFeeders[$id]->start(); + if (isset($this->secretFeeders[$id])) { $this->secretFeeders[$id]->resume(); } } @@ -1722,17 +1725,18 @@ class MTProto extends AsyncConstruct implements TLCallback if (!isset($this->updaters[$channelId])) { $this->updaters[$channelId] = new UpdateLoop($this, $channelId); } - if ($this->feeders[$channelId]->start() && isset($this->feeders[$channelId])) { + $this->feeders[$channelId]->start(); + if (isset($this->feeders[$channelId])) { $this->feeders[$channelId]->resume(); } - if ($this->updaters[$channelId]->start() && isset($this->updaters[$channelId])) { + $this->updaters[$channelId]->start(); + if (isset($this->updaters[$channelId])) { $this->updaters[$channelId]->resume(); } } $this->flushAll(); - if ($this->seqUpdater->start()) { - $this->seqUpdater->resume(); - } + $this->seqUpdater->start(); + $this->seqUpdater->resume(); } /** * Flush all datacenter connections. diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 15295c4c1..cab11bddb 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -2,6 +2,7 @@ namespace danog\MadelineProto\MTProtoTools; +use Amp\Sync\LocalMutex; use danog\MadelineProto\DataCenter; use danog\MadelineProto\Tools; use phpseclib3\Math\BigInteger; @@ -11,13 +12,7 @@ use phpseclib3\Math\BigInteger; */ trait AuthKeyHandler { - /** - * Whether another initAuthorization is pending. - * - * @var boolean - */ - private $pending_auth = false; - + private ?LocalMutex $auth_mutex = null; /** * Asynchronously create, bind and check auth keys for all DCs. * @@ -27,43 +22,35 @@ trait AuthKeyHandler */ public function initAuthorization(): \Generator { - if ($this->initing_authorization) { - if ($this->pending_auth) { - $this->logger("Pending auth check, not queueing further auth check"); - return; - } - $this->logger("Queueing further auth check"); - $this->pending_auth = true; - return; - } + $this->auth_mutex ??= new LocalMutex; + $lock = yield $this->auth_mutex->acquire(); $this->logger("Initing authorization..."); $this->initing_authorization = true; try { - do { - $this->pending_auth = false; - $main = []; - $media = []; - foreach ($this->datacenter->getDataCenterConnections() as $socket) { - if (!$socket->hasCtx()) { - continue; - } - if ($socket->isMedia()) { - $media []= [$socket, 'initAuthorization']; - } else { - $main []= [$socket, 'initAuthorization']; - } + $main = []; + $media = []; + foreach ($this->datacenter->getDataCenterConnections() as $socket) { + if (!$socket->hasCtx()) { + continue; } - if ($main) { - $first = \array_shift($main)(); - yield from $first; + if ($socket->isMedia()) { + $media []= [$socket, 'initAuthorization']; + } else { + $main []= [$socket, 'initAuthorization']; } - yield Tools::all(\array_map(fn ($cb) => $cb(), $main)); - yield Tools::all(\array_map(fn ($cb) => $cb(), $media)); - } while ($this->pending_auth); + } + if ($main) { + $first = \array_shift($main)(); + yield from $first; + } + yield Tools::all(\array_map(fn ($cb) => $cb(), $main)); + yield Tools::all(\array_map(fn ($cb) => $cb(), $media)); } finally { + $lock->release(); $this->logger("Done initing authorization!"); $this->initing_authorization = false; } + $this->startUpdateSystem(true); } /** * Get diffie-hellman configuration. diff --git a/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php b/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php index e3260460a..c652aa04a 100644 --- a/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php +++ b/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php @@ -93,9 +93,8 @@ trait AuthKeyHandler 'mtproto' => 1 ]; $this->secretFeeders[$params['id']] = new SecretFeedLoop($this, $params['id']); - if ($this->secretFeeders[$params['id']]->start()) { - $this->secretFeeders[$params['id']]->resume(); - } + $this->secretFeeders[$params['id']]->start(); + $this->secretFeeders[$params['id']]->resume(); $g_b = $dh_config['g']->powMod($b, $dh_config['p']); Crypt::checkG($g_b, $dh_config['p']); yield from $this->methodCallAsyncRead('messages.acceptEncryption', ['peer' => $params['id'], 'g_b' => $g_b->toBytes(), 'key_fingerprint' => $key['fingerprint']]); @@ -158,9 +157,8 @@ trait AuthKeyHandler $key['visualization_46'] = \substr(\hash('sha256', $key['auth_key'], true), 20); $this->secret_chats[$params['id']] = ['key' => $key, 'admin' => true, 'user_id' => $params['participant_id'], 'InputEncryptedChat' => ['chat_id' => $params['id'], 'access_hash' => $params['access_hash'], '_' => 'inputEncryptedChat'], 'in_seq_no_x' => 0, 'out_seq_no_x' => 1, 'in_seq_no' => 0, 'out_seq_no' => 0, 'layer' => 8, 'ttl' => 0, 'ttr' => 100, 'updated' => \time(), 'incoming' => [], 'outgoing' => [], 'created' => \time(), 'rekeying' => [0], 'key_x' => 'to server', 'mtproto' => 1]; $this->secretFeeders[$params['id']] = new SecretFeedLoop($this, $params['id']); - if ($this->secretFeeders[$params['id']]->start()) { - $this->secretFeeders[$params['id']]->resume(); - } + $this->secretFeeders[$params['id']]->start(); + $this->secretFeeders[$params['id']]->resume(); yield from $this->notifyLayer($params['id']); $this->logger->logger('Secret chat '.$params['id'].' completed successfully!', \danog\MadelineProto\Logger::NOTICE); }