From c51e4cb9d88e8c4159087496621b5ecb937d4761 Mon Sep 17 00:00:00 2001 From: danogentili Date: Sat, 10 Sep 2016 19:53:08 +0200 Subject: [PATCH] Cleaning up..: --- src/danog/MadelineProto/MTProto.php | 20 +++ src/danog/MadelineProto/Session.php | 9 +- src/danog/MadelineProto/TL/TL.php | 5 + .../MadelineProto/TelegramEncryption.php | 120 ------------------ 4 files changed, 29 insertions(+), 125 deletions(-) create mode 100644 src/danog/MadelineProto/MTProto.php delete mode 100644 src/danog/MadelineProto/TelegramEncryption.php diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php new file mode 100644 index 00000000..bb10f38d --- /dev/null +++ b/src/danog/MadelineProto/MTProto.php @@ -0,0 +1,20 @@ +. +*/ + +namespace danog\MadelineProto; + +/** + * Manages encryption and message frames. + */ +class MTProto extends Tools { + +} \ No newline at end of file diff --git a/src/danog/MadelineProto/Session.php b/src/danog/MadelineProto/Session.php index b2ca0826..94f4c943 100644 --- a/src/danog/MadelineProto/Session.php +++ b/src/danog/MadelineProto/Session.php @@ -13,7 +13,7 @@ If not, see . namespace danog\MadelineProto; /** - * Manages encryption and message frames. + * Manages session and the creation of authorization keys. */ class Session extends Tools { @@ -176,8 +176,6 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB $in = $content_related ? 1 : 0; $value = $this->seq_no; $this->seq_no += $in; - var_dump((($value * 2) + $in)); - return ($value * 2) + $in; } @@ -274,10 +272,11 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB public function method_call($method, $args) { + $opts = $this->tl->get_opts($method); foreach (range(1, $this->settings['max_tries']['query']) as $i) { try { $this->send_message($this->tl->serialize_method($method, $args), $this->tl->content_related($method)); - $server_answer = $this->recv_message(); + if ($opts["requires_answer"]) $server_answer = $this->recv_message(); } catch (Exception $e) { $this->log->log('An error occurred while calling method '.$method.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().'. Recreating connection and retrying to call method...'); unset($this->sock); @@ -355,7 +354,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB 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->log->log('Received future salts.'); $this->future_salts = $response['salts']; break; case 'pong': diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 50439ad1..143e6b39 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -263,4 +263,9 @@ class TL 'msg_resend_ans_req', ]); } + public function get_opts($method) { + $opts = ["requires_answer" => !in_array($method, [ + 'msgs_ack', + ]] + } } diff --git a/src/danog/MadelineProto/TelegramEncryption.php b/src/danog/MadelineProto/TelegramEncryption.php deleted file mode 100644 index 7fa7d256..00000000 --- a/src/danog/MadelineProto/TelegramEncryption.php +++ /dev/null @@ -1,120 +0,0 @@ -key = $key; - $this->iv = $iv; - $this->debug = $debug; - $this->rijndael = new \phpseclib\Crypt\Rijndael(\phpseclib\Crypt\Rijndael::MODE_ECB); - $this->rijndael->setKeyLength(128); - $this->rijndael->setKey($this->key); - } - - public function decrypt($message = null) - { - $key = $this->key; - $blockSize = $this->rijndael->block_size; - - $xPrev = substr($this->iv, 0, $blockSize); - $yPrev = substr($this->iv, $blockSize, strlen($this->iv)); - - $decrypted = ''; - - for ($i = 0; $i < strlen($message); $i += $blockSize) { - $x = substr($message, $i, $blockSize); - - $this->debugLog('x: '.$this->_c($x)."\n"); - - $yXOR = $this->exor($x, $yPrev); - $this->debugLog('yPrev: '.$this->_c($yPrev)."\n"); - $this->debugLog('yXOR: '.$this->_c($yXOR)."\n"); - $yFinal = $this->rijndael->encrypt($yXOR); - $yFinal = str_pad($yFinal, strlen($xPrev), "\x00"); - $this->debugLog('yFinal: '.$this->_c($yFinal)."\n"); - - $y = $this->exor($yFinal, $xPrev); - $this->debugLog('xPrev: '.$this->_c($xPrev)."\n"); - $this->debugLog('y: '.$this->_c($y)."\n"); - - $xPrev = $x; - $yPrev = $y; - $decrypted .= $y; - - $this->debugLog('Currently Decrypted: '.$this->_c($decrypted)."\n\n"); - } - - return $decrypted; - } - - public function encrypt() - { - $key = $this->key; - $message = $this->plainText; - $blockSize = $this->rijndael->block_size; - - $xPrev = substr($this->iv, $blockSize, strlen($this->iv)); - $yPrev = substr($this->iv, 0, $blockSize); - - $encrypted = ''; - - for ($i = 0; $i < strlen($message); $i += $blockSize) { - $x = substr($message, $i, $blockSize); - $this->debugLog('x: '.$this->_c($x)."\n"); - - $yXOR = $this->exor($x, $yPrev); - $this->debugLog('yPrev: '.$this->_c($yPrev)."\n"); - $this->debugLog('yXOR: '.$this->_c($yXOR)."\n"); - $yFinal = $this->rijndael->encrypt($yXOR); - $yFinal = str_pad($yFinal, strlen($xPrev), "\x00"); - $this->debugLog('yFinal: '.$this->_c($yFinal)."\n"); - $y = $this->exor($yFinal, $xPrev); - $this->debugLog('xPrev: '.$this->_c($xPrev)."\n"); - $this->debugLog('y: '.$this->_c($y)."\n"); - - $xPrev = $x; - $yPrev = $y; - - $encrypted .= $y; - $this->debugLog('Currently encrypted: '.$this->_c($encrypted)."\n\n"); - } - - return $encrypted; - } - - public function debugLog($message) - { - if ($this->debug) { - echo $message; - } - } - - public function exor($array1, $array2) - { - $len = (strlen($array1) <= strlen($array2)) ? strlen($array2) : strlen($array1); - - $array1 = str_pad($array1, $len, "\x00"); - $array2 = str_pad($array2, $len, "\x00"); - - $res = ''; - for ($i = 0; $i < $len; $i++) { - $res .= $array1[$i] ^ $array2[$i]; - } - - return $res; - } - - public function _c($binary) - { - return sprintf('[%s]', chunk_split(bin2hex($binary), 4, ' ')); - } -}