Trying to fix the handling of responses

This commit is contained in:
Daniil Gentili 2016-08-23 05:50:53 -04:00
parent 5070c4616e
commit 09350d0cab
3 changed files with 29 additions and 32 deletions

23
composer.lock generated
View File

@ -123,22 +123,23 @@
}, },
{ {
"name": "paragonie/constant_time_encoding", "name": "paragonie/constant_time_encoding",
"version": "v2.0.3", "version": "v1.0.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/paragonie/constant_time_encoding.git", "url": "https://github.com/paragonie/constant_time_encoding.git",
"reference": "e085e08c939de49707dbf64315d178d90fbc708d" "reference": "d96e63b79a7135a65659ba5b1cb02826172bfedd"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/paragonie/constant_time_encoding/zipball/e085e08c939de49707dbf64315d178d90fbc708d", "url": "https://api.github.com/repos/paragonie/constant_time_encoding/zipball/d96e63b79a7135a65659ba5b1cb02826172bfedd",
"reference": "e085e08c939de49707dbf64315d178d90fbc708d", "reference": "d96e63b79a7135a65659ba5b1cb02826172bfedd",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": "^7" "php": "^5.3|^7"
}, },
"require-dev": { "require-dev": {
"paragonie/random_compat": "^1.4|^2.0",
"phpunit/phpunit": "4.*|5.*" "phpunit/phpunit": "4.*|5.*"
}, },
"type": "library", "type": "library",
@ -180,7 +181,7 @@
"hex2bin", "hex2bin",
"rfc4648" "rfc4648"
], ],
"time": "2016-07-11 20:32:06" "time": "2016-06-13 01:00:24"
}, },
{ {
"name": "paragonie/random_compat", "name": "paragonie/random_compat",
@ -232,16 +233,16 @@
}, },
{ {
"name": "phpseclib/phpseclib", "name": "phpseclib/phpseclib",
"version": "2.0.2", "version": "2.0.3",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/phpseclib/phpseclib.git", "url": "https://github.com/phpseclib/phpseclib.git",
"reference": "3d265f7c079f5b37d33475f996d7a383c5fc8aeb" "reference": "41f85e9c2582b3f6d1b7d20395fb40c687ad5370"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/3d265f7c079f5b37d33475f996d7a383c5fc8aeb", "url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/41f85e9c2582b3f6d1b7d20395fb40c687ad5370",
"reference": "3d265f7c079f5b37d33475f996d7a383c5fc8aeb", "reference": "41f85e9c2582b3f6d1b7d20395fb40c687ad5370",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -320,7 +321,7 @@
"x.509", "x.509",
"x509" "x509"
], ],
"time": "2016-05-13 01:15:21" "time": "2016-08-18 18:49:14"
} }
], ],
"packages-dev": [], "packages-dev": [],

View File

@ -21,9 +21,7 @@ class API
set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']);
$this->session = new Session($params); $this->session = new Session($params);
$future_salts = $this->get_future_salts(3); $future_salts = $this->get_future_salts(3);
$this->session->log->log($future_salts);
$future_salts = $this->get_future_salts(3); $future_salts = $this->get_future_salts(3);
$this->session->log->log($future_salts);
} }
public function __destruct() public function __destruct()

View File

@ -113,7 +113,6 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
public function check_message_id($new_message_id, $outgoing) public function check_message_id($new_message_id, $outgoing)
{ {
$new_message_id = $this->struct->unpack('<Q', $new_message_id)[0];
if (((int) ((time() + $this->timedelta - 300) * pow(2, 30)) * 4) > $new_message_id) { if (((int) ((time() + $this->timedelta - 300) * pow(2, 30)) * 4) > $new_message_id) {
throw new Exception('Given message id ('.$new_message_id.') is too old.'); 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) public function ack_outgoing_message_id($message_id)
{ {
$message_id = $this->struct->unpack('<Q', $message_id)[0];
// The server acknowledges that it received my message // The server acknowledges that it received my message
if (!in_array($message_id, $this->outgoing_message_ids)) { if (!in_array($message_id, $this->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?'); 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')) { 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; return;
} }
$message_id = $this->struct->unpack('<Q', $message_id)[0];
// I let the server know that I received its message // I let the server know that I received its message
if (!in_array($message_id, $this->incoming_message_ids)) { if (!in_array($message_id, $this->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?'); 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) public function send_message($message_data, $content_related)
{ {
$message_id = $this->struct->pack('<Q', (int) ((time() + $this->timedelta) * pow(2, 30)) * 4); $int_message_id = (int) ((time() + $this->timedelta) * pow(2, 30)) * 4;
$this->check_message_id($message_id, true); $message_id = $this->struct->pack('<Q', $int_message_id);
$this->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)) { 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('<I', strlen($message_data)).$message_data; $message = Tools::string2bin('\x00\x00\x00\x00\x00\x00\x00\x00').$message_id.$this->struct->pack('<I', strlen($message_data)).$message_data;
$this->last_sent = ['message_id' => $message_id]; $this->last_sent = ['message_id' => $int_message_id];
} else { } else {
$seq_no = $this->generate_seq_no($content_related); $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('<II', $seq_no, strlen($message_data)).$message_data; $encrypted_data = $this->settings['authorization']['temp_auth_key']['server_salt'].$this->settings['authorization']['session_id'].$message_id.$this->struct->pack('<II', $seq_no, strlen($message_data)).$message_data;
@ -202,7 +200,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
$padding = \phpseclib\Crypt\Random::string(Tools::posmod(-strlen($encrypted_data), 16)); $padding = \phpseclib\Crypt\Random::string(Tools::posmod(-strlen($encrypted_data), 16));
list($aes_key, $aes_iv) = $this->aes_calculate($message_key); list($aes_key, $aes_iv) = $this->aes_calculate($message_key);
$message = $this->settings['authorization']['temp_auth_key']['id'].$message_key.Crypt::ige_encrypt($encrypted_data.$padding, $aes_key, $aes_iv); $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); $this->sock->send_message($message);
} }
@ -218,7 +216,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
} }
$auth_key_id = fread($payload, 8); $auth_key_id = fread($payload, 8);
if ($auth_key_id == Tools::string2bin('\x00\x00\x00\x00\x00\x00\x00\x00')) { 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('<QI', fread($payload, 12));
$this->check_message_id($message_id, false); $this->check_message_id($message_id, false);
$message_data = fread($payload, $message_length); $message_data = fread($payload, $message_length);
$this->last_received = ['message_id' => $message_id]; $this->last_received = ['message_id' => $message_id];
@ -238,7 +236,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
throw new Exception('Session id mismatch.'); throw new Exception('Session id mismatch.');
} }
$message_id = substr($decrypted_data, 16, 8); $message_id = $this->struct->unpack('<Q', substr($decrypted_data, 16, 8))[0];
$this->check_message_id($message_id, false); $this->check_message_id($message_id, false);
$seq_no = $this->struct->unpack('<I', substr($decrypted_data, 24, 4)) [0]; $seq_no = $this->struct->unpack('<I', substr($decrypted_data, 24, 4)) [0];
@ -301,16 +299,17 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
foreach (range(1, $this->settings['max_tries']['query']) as $i) { foreach (range(1, $this->settings['max_tries']['query']) as $i) {
try { try {
$this->send_message($this->tl->serialize_obj($object, $kwargs), $this->tl->content_related($object)); $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) { } 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...'); $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); unset($this->sock);
$this->sock = new Connection($this->settings['connection']['ip_address'], $this->settings['connection']['port'], $this->settings['connection']['protocol']); $this->sock = new Connection($this->settings['connection']['ip_address'], $this->settings['connection']['port'], $this->settings['connection']['protocol']);
continue; continue;
} }
if ($server_answer == null) { return;
throw new Exception('An error occurred while calling object '.$object.'.'); // 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)); // $deserialized = $this->tl->deserialize(Tools::fopen_and_write('php://memory', 'rw+b', $server_answer));
// return $deserialized; // return $deserialized;
} }
@ -323,8 +322,8 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
case 'rpc_result': case 'rpc_result':
$this->ack_incoming_message_id($this->last_received['message_id']); // Acknowledge that I received the server's response $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 $this->ack_outgoing_message_id($response['req_msg_id']); // Acknowledge that the server received my
if ($response['req_msg_id'] != $last_sent['message_id']) { if ($response['req_msg_id'] != $this->last_sent['message_id']) {
throw new Exception('Message id mismatch.'); 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); return $this->handle_response($response['result'], $name, $args);
@ -351,11 +350,11 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
return $response; // I'm not handling this return $response; // I'm not handling this
break; break;
case 'future_salts': 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 $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']) { if ($response['req_msg_id'] != $this->last_sent['message_id']) {
throw new Exception('Message id mismatch.'); 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']; $this->future_salts = $response['salts'];
break; break;
case 'pong': case 'pong':
@ -374,7 +373,6 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
$this->log->log($response); $this->log->log($response);
break; break;
case 'msg_container': case 'msg_container':
$this->ack_incoming_message_id($this->last_received['message_id']); // Acknowledge that I received the server's response
$responses = []; $responses = [];
$this->log->log('Received container.'); $this->log->log('Received container.');
$this->log->log($response['messages']); $this->log->log($response['messages']);