From 417d9343c2d23d7b93768a023a86698b6b1e4e1a Mon Sep 17 00:00:00 2001 From: danogentili Date: Tue, 15 Nov 2016 23:42:52 +0300 Subject: [PATCH] Fixed binding of authorization keys. Finally can use auth.sendCode method. --- src/danog/MadelineProto/Connection.php | 7 +++++++ src/danog/MadelineProto/DataCenter.php | 14 ++++++++++++++ src/danog/MadelineProto/MTProto.php | 10 +--------- .../MadelineProto/MTProtoTools/AuthKeyHandler.php | 8 ++++---- .../MadelineProto/MTProtoTools/MessageHandler.php | 3 +++ .../MadelineProto/MTProtoTools/MsgIdHandler.php | 14 +++++++------- 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index 388f4b3a..9d4188be 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -19,6 +19,7 @@ class Connection extends Tools { public $sock = null; public $protocol = null; + private $_delta = 0; public function __construct($ip, $port, $protocol = 'tcp_full') { @@ -78,6 +79,12 @@ class Connection extends Tools } } + public function set_time_delta($delta) { + $this->_delta = $delta; + } + public function get_time_delta() { + return $this->_delta; + } /** * Function to get hex crc32. * diff --git a/src/danog/MadelineProto/DataCenter.php b/src/danog/MadelineProto/DataCenter.php index e4ced9f3..87688289 100644 --- a/src/danog/MadelineProto/DataCenter.php +++ b/src/danog/MadelineProto/DataCenter.php @@ -68,6 +68,20 @@ class DataCenter extends Tools $this->curdc = $dc_number; } + public function set_time_delta($delta, $dc_number = -1) + { + if ($dc_number == -1) { + $dc_number = $this->curdc; + } + return $this->sockets[$dc_number]->set_time_delta($delta); + } + public function get_time_delta($dc_number = -1) + { + if ($dc_number == -1) { + $dc_number = $this->curdc; + } + return $this->sockets[$dc_number]->get_time_delta(); + } public function send_message($message, $dc_number = -1) { if ($dc_number == -1) { diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 5801431a..35bd7f48 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -123,7 +123,6 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB $this->tl = new TL\TL($this->settings['tl_schema']['src']); $this->seq_no = 0; - $this->timedelta = 0; // time delta $this->incoming_messages = []; $this->outgoing_messages = []; $this->future_salts = []; @@ -138,14 +137,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB } $this->write_client_info(); $this->bind_temp_auth_key($this->settings['authorization']['default_temp_auth_key_expires_in']); - $nearestDc = $this->method_call('auth.sendCode', [ - 'phone_number' => '393373737', - 'sms_type' => 5, - 'api_id' => $this->settings['app_info']['api_id'], - 'api_hash' => $this->settings['app_info']['api_hash'], - 'lang_code' => $this->settings['app_info']['lang_code'], - ]); - var_dump($nearestDc); + \danog\MadelineProto\Logging::log('You may now login to Telegram.'); } public function write_client_info() diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 0c12f289..95118cc6 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -252,9 +252,9 @@ class AuthKeyHandler extends AckHandler * Time delta */ $server_time = $server_DH_inner_data['server_time']; - $this->timedelta = $server_time - time(); + $this->connection->set_time_delta($server_time - time()); - \danog\MadelineProto\Logging::log(sprintf('Server-client time delta = %.1f s', $this->timedelta)); + \danog\MadelineProto\Logging::log(sprintf('Server-client time delta = %.1f s', $this->connection->get_time_delta())); /* @@ -431,7 +431,6 @@ class AuthKeyHandler extends AckHandler } \danog\MadelineProto\Logging::log('Auth key generated'); - $this->timedelta = 0; return $res_authorization; case 'dh_gen_retry': @@ -461,6 +460,7 @@ class AuthKeyHandler extends AckHandler public function bind_temp_auth_key($expires_in) { + \danog\MadelineProto\Logging::log('Binding authorization keys...'); $nonce = \danog\PHP\Struct::unpack('settings['authorization']['temp_auth_key']['id'])[0]; @@ -484,7 +484,7 @@ class AuthKeyHandler extends AckHandler list($aes_key, $aes_iv) = $this->aes_calculate($message_key, $this->settings['authorization']['auth_key']['auth_key']); $encrypted_message = $this->settings['authorization']['auth_key']['id'].$message_key.$this->ige_encrypt($encrypted_data.$padding, $aes_key, $aes_iv); - if ($this->method_call('auth.bindTempAuthKey', ['perm_auth_key_id' => $perm_auth_key_id, 'nonce' => $nonce, 'expires_at' => $expires_at, 'encrypted_message' => $encrypted_message], $message_id)) { + if ($this->method_call('auth.bindTempAuthKey', ['perm_auth_key_id' => $perm_auth_key_id, 'nonce' => $nonce, 'expires_at' => $expires_at, 'encrypted_message' => $encrypted_message], $int_message_id)) { \danog\MadelineProto\Logging::log('Successfully binded temporary and permanent authorization keys.'); $this->write_client_info(); diff --git a/src/danog/MadelineProto/MTProtoTools/MessageHandler.php b/src/danog/MadelineProto/MTProtoTools/MessageHandler.php index e86b7f08..3e4c73a0 100644 --- a/src/danog/MadelineProto/MTProtoTools/MessageHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/MessageHandler.php @@ -26,6 +26,9 @@ class MessageHandler extends Crypt if ($int_message_id == null) { $int_message_id = $this->generate_message_id(); } + if (!is_int($int_message_id)) { + throw new Exception("Given message id isn't an integer!"); + } $message_id = \danog\PHP\Struct::pack('settings['authorization']['temp_auth_key']['auth_key'] == null) || ($this->settings['authorization']['temp_auth_key']['server_salt'] == null)) { $message = $this->string2bin('\x00\x00\x00\x00\x00\x00\x00\x00').$message_id.\danog\PHP\Struct::pack('timedelta - 300) * pow(2, 30)) * 4) > $new_message_id) { + if (((int) ((time() + $this->connection->get_time_delta() - 300) * pow(2, 30)) * 4) > $new_message_id) { throw new Exception('Given message id ('.$new_message_id.') is too old.'); } - if (((int) ((time() + $this->timedelta + 30) * pow(2, 30)) * 4) < $new_message_id) { + if (((int) ((time() + $this->connection->get_time_delta() + 30) * pow(2, 30)) * 4) < $new_message_id) { throw new Exception('Given message id ('.$new_message_id.') is too new.'); } if ($outgoing) { @@ -66,17 +66,17 @@ class MsgIdHandler extends MessageHandler public function generate_message_id() { - $ms_time = (time() + $this->timedelta) * 1000; - $int_message_id = (int) ( + $int_message_id = (int) ((time() + $this->connection->get_time_delta()) << 32); +/* $int_message_id = (int) ( ((int) ($ms_time / 1000) << 32) | ($this->posmod($ms_time, 1000) << 22) | rand(0, 524288) << 2 - ); + );*/ $keys = array_keys($this->outgoing_messages); asort($keys); $keys = end($keys); - while ($int_message_id <= $keys) { - $int_message_id += 4; + if ($int_message_id <= $keys) { + $int_message_id = $keys + 4; } $this->check_message_id($int_message_id, true);