From 09350d0cab9460064eba588f30115291437bfd02 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Tue, 23 Aug 2016 05:50:53 -0400 Subject: [PATCH] Trying to fix the handling of responses --- composer.lock | 23 +++++++++--------- src/danog/MadelineProto/API.php | 2 -- src/danog/MadelineProto/Session.php | 36 ++++++++++++++--------------- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/composer.lock b/composer.lock index 96dbe52eb..e16e94893 100644 --- a/composer.lock +++ b/composer.lock @@ -123,22 +123,23 @@ }, { "name": "paragonie/constant_time_encoding", - "version": "v2.0.3", + "version": "v1.0.1", "source": { "type": "git", "url": "https://github.com/paragonie/constant_time_encoding.git", - "reference": "e085e08c939de49707dbf64315d178d90fbc708d" + "reference": "d96e63b79a7135a65659ba5b1cb02826172bfedd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/paragonie/constant_time_encoding/zipball/e085e08c939de49707dbf64315d178d90fbc708d", - "reference": "e085e08c939de49707dbf64315d178d90fbc708d", + "url": "https://api.github.com/repos/paragonie/constant_time_encoding/zipball/d96e63b79a7135a65659ba5b1cb02826172bfedd", + "reference": "d96e63b79a7135a65659ba5b1cb02826172bfedd", "shasum": "" }, "require": { - "php": "^7" + "php": "^5.3|^7" }, "require-dev": { + "paragonie/random_compat": "^1.4|^2.0", "phpunit/phpunit": "4.*|5.*" }, "type": "library", @@ -180,7 +181,7 @@ "hex2bin", "rfc4648" ], - "time": "2016-07-11 20:32:06" + "time": "2016-06-13 01:00:24" }, { "name": "paragonie/random_compat", @@ -232,16 +233,16 @@ }, { "name": "phpseclib/phpseclib", - "version": "2.0.2", + "version": "2.0.3", "source": { "type": "git", "url": "https://github.com/phpseclib/phpseclib.git", - "reference": "3d265f7c079f5b37d33475f996d7a383c5fc8aeb" + "reference": "41f85e9c2582b3f6d1b7d20395fb40c687ad5370" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/3d265f7c079f5b37d33475f996d7a383c5fc8aeb", - "reference": "3d265f7c079f5b37d33475f996d7a383c5fc8aeb", + "url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/41f85e9c2582b3f6d1b7d20395fb40c687ad5370", + "reference": "41f85e9c2582b3f6d1b7d20395fb40c687ad5370", "shasum": "" }, "require": { @@ -320,7 +321,7 @@ "x.509", "x509" ], - "time": "2016-05-13 01:15:21" + "time": "2016-08-18 18:49:14" } ], "packages-dev": [], diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index 652dfab17..fe6219902 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -21,9 +21,7 @@ class API set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); $this->session = new Session($params); $future_salts = $this->get_future_salts(3); - $this->session->log->log($future_salts); $future_salts = $this->get_future_salts(3); - $this->session->log->log($future_salts); } public function __destruct() diff --git a/src/danog/MadelineProto/Session.php b/src/danog/MadelineProto/Session.php index fb6445cdf..d18541c1f 100644 --- a/src/danog/MadelineProto/Session.php +++ b/src/danog/MadelineProto/Session.php @@ -113,7 +113,6 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB public function check_message_id($new_message_id, $outgoing) { - $new_message_id = $this->struct->unpack('timedelta - 300) * pow(2, 30)) * 4) > $new_message_id) { throw new Exception('Given message id ('.$new_message_id.') is too old.'); } @@ -146,7 +145,6 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB public function ack_outgoing_message_id($message_id) { - $message_id = $this->struct->unpack('outgoing_message_ids)) { throw new Exception("Couldn't find message id ".$message_id.' in the array of outgoing message ids. Maybe try to increase its size?'); @@ -162,7 +160,6 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB if ($this->settings['authorization']['temp_auth_key']['id'] === null || $this->settings['authorization']['temp_auth_key']['id'] == Tools::string2bin('\x00\x00\x00\x00\x00\x00\x00\x00')) { return; } - $message_id = $this->struct->unpack('incoming_message_ids)) { throw new Exception("Couldn't find message id ".$message_id.' in the array of incoming message ids. Maybe try to increase its size?'); @@ -190,11 +187,12 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB */ public function send_message($message_data, $content_related) { - $message_id = $this->struct->pack('timedelta) * pow(2, 30)) * 4); - $this->check_message_id($message_id, true); + $int_message_id = (int) ((time() + $this->timedelta) * pow(2, 30)) * 4; + $message_id = $this->struct->pack('check_message_id($int_message_id, true); if (($this->settings['authorization']['temp_auth_key']['auth_key'] == null) || ($this->settings['authorization']['temp_auth_key']['server_salt'] == null)) { $message = Tools::string2bin('\x00\x00\x00\x00\x00\x00\x00\x00').$message_id.$this->struct->pack('last_sent = ['message_id' => $message_id]; + $this->last_sent = ['message_id' => $int_message_id]; } else { $seq_no = $this->generate_seq_no($content_related); $encrypted_data = $this->settings['authorization']['temp_auth_key']['server_salt'].$this->settings['authorization']['session_id'].$message_id.$this->struct->pack('aes_calculate($message_key); $message = $this->settings['authorization']['temp_auth_key']['id'].$message_key.Crypt::ige_encrypt($encrypted_data.$padding, $aes_key, $aes_iv); - $this->last_sent = ['message_id' => $message_id, 'seq_no' => $seq_no]; + $this->last_sent = ['message_id' => $int_message_id, 'seq_no' => $seq_no]; } $this->sock->send_message($message); } @@ -218,7 +216,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB } $auth_key_id = fread($payload, 8); if ($auth_key_id == Tools::string2bin('\x00\x00\x00\x00\x00\x00\x00\x00')) { - list($message_id, $message_length) = $this->struct->unpack('<8sI', fread($payload, 12)); + list($message_id, $message_length) = $this->struct->unpack('check_message_id($message_id, false); $message_data = fread($payload, $message_length); $this->last_received = ['message_id' => $message_id]; @@ -238,7 +236,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB throw new Exception('Session id mismatch.'); } - $message_id = substr($decrypted_data, 16, 8); + $message_id = $this->struct->unpack('check_message_id($message_id, false); $seq_no = $this->struct->unpack('settings['max_tries']['query']) as $i) { try { $this->send_message($this->tl->serialize_obj($object, $kwargs), $this->tl->content_related($object)); - $server_answer = $this->recv_message(); +// $server_answer = $this->recv_message(); } catch (Exception $e) { $this->log->log('An error occurred while calling object '.$object.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().'. Recreating connection and retrying to call object...'); unset($this->sock); $this->sock = new Connection($this->settings['connection']['ip_address'], $this->settings['connection']['port'], $this->settings['connection']['protocol']); continue; } - if ($server_answer == null) { - throw new Exception('An error occurred while calling object '.$object.'.'); - } + return; +// if ($server_answer == null) { +// throw new Exception('An error occurred while calling object '.$object.'.'); +// } // $deserialized = $this->tl->deserialize(Tools::fopen_and_write('php://memory', 'rw+b', $server_answer)); // return $deserialized; } @@ -323,8 +322,8 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB case 'rpc_result': $this->ack_incoming_message_id($this->last_received['message_id']); // Acknowledge that I received the server's response $this->ack_outgoing_message_id($response['req_msg_id']); // Acknowledge that the server received my - if ($response['req_msg_id'] != $last_sent['message_id']) { - throw new Exception('Message id mismatch.'); + if ($response['req_msg_id'] != $this->last_sent['message_id']) { + throw new Exception('Message id mismatch; req_msg_id ('.$response['req_msg_id'].') != last sent msg id ('.$this->last_sent['message_id'].').'); } return $this->handle_response($response['result'], $name, $args); @@ -351,11 +350,11 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB return $response; // I'm not handling this break; case 'future_salts': - $this->ack_incoming_message_id($this->last_received['message_id']); // Acknowledge that I received the server's response $this->ack_outgoing_message_id($this->last_sent['message_id']); // Acknowledge that the server received my message - if ($response['req_msg_id'] != $last_sent['message_id']) { - throw new Exception('Message id mismatch.'); + if ($response['req_msg_id'] != $this->last_sent['message_id']) { + throw new Exception('Message id mismatch; req_msg_id ('.$response['req_msg_id'].') != last sent msg id ('.$this->last_sent['message_id'].').'); } + $this->log->log("Received future salts."); $this->future_salts = $response['salts']; break; case 'pong': @@ -374,7 +373,6 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB $this->log->log($response); break; case 'msg_container': - $this->ack_incoming_message_id($this->last_received['message_id']); // Acknowledge that I received the server's response $responses = []; $this->log->log('Received container.'); $this->log->log($response['messages']);