From a8dc0027f1b3c5f9434723c72f936d39c7316e11 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 1 Jun 2017 22:21:15 +0200 Subject: [PATCH] Fixes to secret chats --- secret_bot.php | 88 +++++++++++++++++++ .../MTProtoTools/UpdateHandler.php | 2 +- .../SecretChats/AuthKeyHandler.php | 3 + .../SecretChats/MessageHandler.php | 1 + .../SecretChats/SeqNoHandler.php | 4 +- 5 files changed, 95 insertions(+), 3 deletions(-) create mode 100755 secret_bot.php diff --git a/secret_bot.php b/secret_bot.php new file mode 100755 index 000000000..c5753ddca --- /dev/null +++ b/secret_bot.php @@ -0,0 +1,88 @@ +#!/usr/bin/env php +. +*/ +require_once 'vendor/autoload.php'; +if (file_exists('web_data.php')) { + require_once 'web_data.php'; +} + +echo 'Deserializing MadelineProto from s.madeline...'.PHP_EOL; +$MadelineProto = false; +try { + $MadelineProto = \danog\MadelineProto\Serialization::deserialize('s.madeline'); +} catch (\danog\MadelineProto\Exception $e) { + var_dump($e->getMessage()); +} +var_dump(file_exists('.env')); +if (file_exists('.env')) { + echo 'Loading .env...'.PHP_EOL; + $dotenv = new Dotenv\Dotenv(getcwd()); + $dotenv->load(); +} + +echo 'Loading settings...'.PHP_EOL; +$settings = json_decode(getenv('MTPROTO_SETTINGS'), true) ?: []; + +if ($MadelineProto === false) { + echo 'Loading MadelineProto...'.PHP_EOL; + $MadelineProto = new \danog\MadelineProto\API($settings); + if (getenv('TRAVIS_COMMIT') == '') { + $checkedPhone = $MadelineProto->auth->checkPhone(// auth.checkPhone becomes auth->checkPhone + [ + 'phone_number' => getenv('MTPROTO_NUMBER'), + ] + ); + + \danog\MadelineProto\Logger::log([$checkedPhone], \danog\MadelineProto\Logger::NOTICE); + $sentCode = $MadelineProto->phone_login(getenv('MTPROTO_NUMBER')); + \danog\MadelineProto\Logger::log([$sentCode], \danog\MadelineProto\Logger::NOTICE); + echo 'Enter the code you received: '; + $code = fgets(STDIN, (isset($sentCode['type']['length']) ? $sentCode['type']['length'] : 5) + 1); + $authorization = $MadelineProto->complete_phone_login($code); + \danog\MadelineProto\Logger::log([$authorization], \danog\MadelineProto\Logger::NOTICE); + if ($authorization['_'] === 'account.noPassword') { + throw new \danog\MadelineProto\Exception('2FA is enabled but no password is set!'); + } + if ($authorization['_'] === 'account.password') { + \danog\MadelineProto\Logger::log(['2FA is enabled'], \danog\MadelineProto\Logger::NOTICE); + $authorization = $MadelineProto->complete_2fa_login(readline('Please enter your password (hint '.$authorization['hint'].'): ')); + } + if ($authorization['_'] === 'account.needSignup') { + \danog\MadelineProto\Logger::log(['Registering new user'], \danog\MadelineProto\Logger::NOTICE); + $authorization = $MadelineProto->complete_signup(readline('Please enter your first name: '), readline('Please enter your last name (can be empty): ')); + } + + echo 'Serializing MadelineProto to s.madeline...'.PHP_EOL; + echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('s.madeline', $MadelineProto).' bytes'.PHP_EOL; + } else { + $MadelineProto->bot_login(getenv('BOT_TOKEN')); + } +} +$message = (getenv('TRAVIS_COMMIT') == '') ? 'I iz works always (io laborare sembre) (yo lavorar siempre) (mi labori ĉiam) (я всегда работать) (Ik werkuh altijd) (Ngimbonga ngaso sonke isikhathi ukusebenza)' : ('Travis ci tests in progress: commit '.getenv('TRAVIS_COMMIT').', job '.getenv('TRAVIS_JOB_NUMBER').', PHP version: '.getenv('TRAVIS_PHP_VERSION')); + +echo 'Serializing MadelineProto to s.madeline...'.PHP_EOL; +echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('s.madeline', $MadelineProto).' bytes'.PHP_EOL; + +$offset = 0; +while (true) { + $updates = $MadelineProto->API->get_updates(['offset' => $offset, 'limit' => 50, 'timeout' => 0]); // Just like in the bot API, you can specify an offset, a limit and a timeout + //\danog\MadelineProto\Logger::log([$updates]); + foreach ($updates as $update) { + $offset = $update['update_id'] + 1; // Just like in the bot API, the offset must be set to the last update_id + switch ($update['update']['_']) { + case 'updateNewEncryptedMessage': + $i = 0; + while (true) { $MadelineProto->messages->sendEncrypted(['peer' => $update['update']['message']['chat_id'], 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => $i++]]); } + } + } + echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('bot.madeline', $MadelineProto).' bytes'.PHP_EOL; +} diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 469ac76e0..b5faa5fa6 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -550,7 +550,7 @@ trait UpdateHandler return false; } \danog\MadelineProto\Logger::log(['Applying qts: '.$update['qts'].' over current qts '.$cur_state['qts'].', chat id: '.$update['message']['chat_id']], \danog\MadelineProto\Logger::VERBOSE); - $cur_state['qts'] = $update['qts']; + $this->method_call('messages.receivedQueue', ['max_qts' => $cur_state['qts'] = $update['qts']], ['datacenter' => $this->datacenter->curdc]); $this->should_serialize = true; $this->handle_encrypted_update($update); diff --git a/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php b/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php index 1ffb0cf5d..b11470008 100644 --- a/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php +++ b/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php @@ -31,12 +31,14 @@ trait AuthKeyHandler $params['g_a'] = new \phpseclib\Math\BigInteger($params['g_a'], 256); $this->check_G($params['g_a'], $dh_config['p']); $key = ['auth_key' => str_pad($params['g_a']->powMod($b, $dh_config['p'])->toBytes(), 256, chr(0), \STR_PAD_LEFT)]; + //var_dump($key); $key['fingerprint'] = substr(sha1($key['auth_key'], true), -8); $key['visualization_orig'] = substr(sha1($key['auth_key'], true), 16); $key['visualization_46'] = substr(hash('sha256', $key['auth_key'], true), 20); $this->secret_chats[$params['id']] = ['key' => $key, 'admin' => false, 'user_id' => $params['admin_id'], 'InputEncryptedChat' => ['_' => 'inputEncryptedChat', 'chat_id' => $params['id'], 'access_hash' => $params['access_hash']], 'in_seq_no_x' => 1, 'out_seq_no_x' => 0, 'in_seq_no' => 0, 'out_seq_no' => 0, 'layer' => 8, 'ttl' => 0, 'ttr' => 100, 'updated' => time(), 'incoming' => [], 'outgoing' => [], 'created' => time(), 'rekeying' => [0]]; $g_b = $dh_config['g']->powMod($b, $dh_config['p']); $this->check_G($g_b, $dh_config['p']); + $this->method_call('messages.acceptEncryption', ['peer' => $params['id'], 'g_b' => $g_b->toBytes(), 'key_fingerprint' => $key['fingerprint']], ['datacenter' => $this->datacenter->curdc]); $this->notify_layer($params['id']); $this->handle_pending_updates(); \danog\MadelineProto\Logger::log(['Secret chat '.$params['id'].' accepted successfully!'], \danog\MadelineProto\Logger::NOTICE); @@ -82,6 +84,7 @@ trait AuthKeyHandler $key = ['auth_key' => str_pad($params['g_a_or_b']->powMod($this->temp_requested_secret_chats[$params['id']], $dh_config['p'])->toBytes(), 256, chr(0), \STR_PAD_LEFT)]; unset($this->temp_requested_secret_chats[$params['id']]); $key['fingerprint'] = substr(sha1($key['auth_key'], true), -8); + //var_dump($key); if ($key['fingerprint'] !== $params['key_fingerprint']) { $this->method_call('messages.discardEncryption', ['chat_id' => $params['id']], ['datacenter' => $this->datacenter->curdc]); diff --git a/src/danog/MadelineProto/SecretChats/MessageHandler.php b/src/danog/MadelineProto/SecretChats/MessageHandler.php index 3ccddcb57..452432a96 100644 --- a/src/danog/MadelineProto/SecretChats/MessageHandler.php +++ b/src/danog/MadelineProto/SecretChats/MessageHandler.php @@ -57,6 +57,7 @@ trait MessageHandler $auth_key_id = substr($message['message']['bytes'], 0, 8); $old = false; if ($auth_key_id !== $this->secret_chats[$message['message']['chat_id']]['key']['fingerprint']) { +//var_dump($auth_key_id, $this->secret_chats[$message['message']['chat_id']]['key']['fingerprint']); if (isset($this->secret_chats[$message['message']['chat_id']]['old_key']['fingerprint'])) { if ($auth_key_id !== $this->secret_chats[$message['message']['chat_id']]['old_key']['fingerprint']) { throw new \danog\MadelineProto\SecurityException('Key fingerprint mismatch'); diff --git a/src/danog/MadelineProto/SecretChats/SeqNoHandler.php b/src/danog/MadelineProto/SecretChats/SeqNoHandler.php index c97bd5148..9af1dd7d7 100644 --- a/src/danog/MadelineProto/SecretChats/SeqNoHandler.php +++ b/src/danog/MadelineProto/SecretChats/SeqNoHandler.php @@ -68,11 +68,11 @@ trait SeqNoHandler public function generate_secret_in_seq_no($chat) { - return ($this->secret_chats[$chat]['in_seq_no'] * 2) + $this->secret_chats[$chat]['in_seq_no_x']; + return $this->secret_chats[$chat]['layer'] > 8 ? ($this->secret_chats[$chat]['in_seq_no'] * 2) + $this->secret_chats[$chat]['in_seq_no_x'] : -1; } public function generate_secret_out_seq_no($chat) { - return ($this->secret_chats[$chat]['out_seq_no'] * 2) + $this->secret_chats[$chat]['out_seq_no_x']; + return $this->secret_chats[$chat]['layer'] > 8 ? ($this->secret_chats[$chat]['out_seq_no'] * 2) + $this->secret_chats[$chat]['out_seq_no_x'] : -1; } }