From 4f3ce8332badf41c753b7da36b20ff3af4b408bf Mon Sep 17 00:00:00 2001 From: danogentili Date: Mon, 22 Aug 2016 22:05:41 +0200 Subject: [PATCH] Trying to fix bugs --- src/danog/MadelineProto/Session.php | 72 +++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/src/danog/MadelineProto/Session.php b/src/danog/MadelineProto/Session.php index d9877fe5..e86e27a2 100644 --- a/src/danog/MadelineProto/Session.php +++ b/src/danog/MadelineProto/Session.php @@ -96,6 +96,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB $this->outgoing_message_ids = []; $this->ack_incoming_message_ids = []; $this->ack_outgoing_message_ids = []; + $this->future_salts = []; if ($this->settings['authorization']['temp_auth_key'] == null || $this->settings['authorization']['auth_key'] == null) { if ($this->settings['authorization']['auth_key'] == null) { @@ -114,19 +115,14 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB { $new_message_id = $this->struct->unpack('timedelta - 300) * pow(2, 30)) * 4) > $new_message_id) { - throw new Exception('Given message id is too old.'); + 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) { - throw new Exception('Given message id is too new.'); + throw new Exception('Given message id ('.$new_message_id.') is too new.'); } if ($outgoing) { if ($new_message_id % 4 != 0) { - throw new Exception('Given message id is not divisible by 4.'); - } - foreach ($this->outgoing_message_ids as $message_id) { - if ($new_message_id <= $message_id) { - throw new Exception('Given message id is lower than or equal than the current limit ('.$message_id.').'); - } + throw new Exception('Given message id ('.$new_message_id.') is not divisible by 4.'); } $this->outgoing_message_ids[] = $new_message_id; if (count($this->outgoing_message_ids) > $this->settings['authorization']['message_ids_limit']) { @@ -139,7 +135,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB } foreach ($this->incoming_message_ids as $message_id) { if ($new_message_id <= $message_id) { - throw new Exception('Given message id is lower than or equal than the current limit ('.$message_id.').'); + throw new Exception('Given message id ('.$new_message_id.') is lower than or equal than the current limit ('.$message_id.').'); } } $this->incoming_message_ids[] = $new_message_id; @@ -161,6 +157,9 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB } } public function ack_incoming_message_id($message_id) { + 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)) { @@ -309,8 +308,8 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB 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; +// $deserialized = $this->tl->deserialize(Tools::fopen_and_write('php://memory', 'rw+b', $server_answer)); +// return $deserialized; } throw new Exception('An error occurred while calling object '.$object.'.'); } @@ -319,8 +318,10 @@ 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 - - return $this->handle_response($response, $name, $args); + if ($response["req_msg_id"] != $last_sent["message_id"]) { + throw new Exception("Message id mismatch."); + } + return $this->handle_response($response["result"], $name, $args); break; case "rpc_error": throw new Exception("Got rpc error " . $response["error_code"] . ": " . $response["error_message"]); @@ -344,15 +345,56 @@ 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."); + } + $this->future_salts = $response["salts"]; + break; + case "pong": + $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->log->log("pong"); + break; + case "msgs_ack": + foreach ($response["msg_ids"] as $msg_id) { + $this->ack_outgoing_message_id($msg_id); // Acknowledge that the server received my message + } + break; + case "new_session_created": + $this->ack_incoming_message_id($this->last_received["message_id"]); // Acknowledge that I received the server's response + $this->log->log("new session created"); + $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"]); + foreach ($response["messages"] as $message) { + $this->last_recieved = ["message_id" => $message["msg_id"], "seq_no" => $message["seqno"]]; + $responses[] = $this->handle_response($message["body"], $name, $args); + } + foreach ($responses as $response) { + if ($response != null) return $response; + } + break; + case "msg_copy": + $this->handle_response($response["orig_message"], $name, $args); + break; + case "gzip_packed": + return $this->handle_response(gzdecode($response)); + break; + case "http_wait": + $this->log->log("Received http wait."); + $this->log->log($response); break; default: $this->ack_incoming_message_id($this->last_received["message_id"]); // Acknowledge that I received the server's response return $response; break; } - } public function create_auth_key($expires_in = -1)