From 4c05506bafd3c588ca3bc3bfe5c7e062a06e161b Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 24 Jul 2017 09:18:10 +0200 Subject: [PATCH] Some CDN fixes --- src/danog/MadelineProto/MTProto.php | 2 +- src/danog/MadelineProto/MTProtoTools/Files.php | 13 ++++++++++--- tests/testing.php | 4 +++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 2ee8010b..4ff37cd2 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -195,7 +195,7 @@ class MTProto extends \Volatile \danog\MadelineProto\Logger::class_exists(); // Detect ipv6 - $this->ipv6 = strlen(@file_get_contents('http://ipv6.test-ipv6.com/', false, stream_context_create(['http' => ['timeout' => 1]]))) > 0; + $this->ipv6 = (bool) strlen(@file_get_contents('http://ipv6.test-ipv6.com/', false, stream_context_create(['http' => ['timeout' => 1]]))) > 0; preg_match('/const V = (\d+);/', file_get_contents('https://raw.githubusercontent.com/danog/MadelineProto/master/src/danog/MadelineProto/MTProto.php'), $matches); if (isset($matches[1]) && self::V < (int) $matches[1]) { diff --git a/src/danog/MadelineProto/MTProtoTools/Files.php b/src/danog/MadelineProto/MTProtoTools/Files.php index e9a2b6ff..a7302c81 100644 --- a/src/danog/MadelineProto/MTProtoTools/Files.php +++ b/src/danog/MadelineProto/MTProtoTools/Files.php @@ -325,6 +325,7 @@ trait Files } if (isset($message_media['key'])) { $res['bytes'] = $ige->decrypt($res['bytes']); + $this->check_cdn_hash($msssage_media['file_token'], $offset, $res['bytes'], $datacenter); } if ($start_at) { $res['bytes'] = substr($res['bytes'], $start_at); @@ -350,7 +351,9 @@ trait Files if ($end === -1) { $cb(100); } - + if ($cdn) { + $this->clear_cdn_hashes($message_media['file_token']); + } return true; } @@ -366,7 +369,7 @@ trait Files } } - private function check_cdn_hashes($file, $offset, $data, &$datacenter) + private function check_cdn_hash($file, $offset, $data, &$datacenter) { if (!isset($this->cdn_hashes[$file][$offset])) { $this->add_cdn_hashes($this->method_call('upload.getCdnFileHashes', ['file_token' => $file, 'offset' => $offset], ['datacenter' => &$datacenter])); @@ -374,12 +377,16 @@ trait Files if (!isset($this->cdn_hashes[$file][$offset])) { throw new \danog\MadelineProto\Exception('Could not fetch CDN hashes for offset '.$offset); } - if (hash('sha256', substr($data, 0, $this->cdn_hashes[$file][$offset]['limit']), true) !== $this->cdn_hashes[$file][$offset]['hash']) { + if (hash('sha256', $data, true) !== $this->cdn_hashes[$file][$offset]['hash']) { throw new \danog\MadelineProto\SecurityException('CDN hashe mismatch for offset '.$offset); } + unset($this->cdn_hashes[$file][$offset]); + return true; } private function clear_cdn_hashes($file) { + unset($this->cdn_hashes[$file]); + return true; } } diff --git a/tests/testing.php b/tests/testing.php index eb701329..02b21f6a 100755 --- a/tests/testing.php +++ b/tests/testing.php @@ -29,7 +29,9 @@ if (file_exists('.env')) { $dotenv = new Dotenv\Dotenv(getcwd()); $dotenv->load(); } - +if (getenv('TEST_SECRET_CHAT') == '') { + die('TEST_SECRET_CHAT is not defined in .env, please define it.'.PHP_EOL); +} echo 'Loading settings...'.PHP_EOL; $settings = json_decode(getenv('MTPROTO_SETTINGS'), true) ?: [];