From 353f8b6281d35f582eddda82a6d2ea83c302f326 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 24 Mar 2018 19:06:01 +0100 Subject: [PATCH] Hugely speed up startup --- magna.php | 2 +- src/danog/MadelineProto/API.php | 3 +- src/danog/MadelineProto/Logger.php | 47 +++++++++------- src/danog/MadelineProto/MTProto.php | 8 +-- src/danog/MadelineProto/Serialization.php | 68 ----------------------- 5 files changed, 33 insertions(+), 95 deletions(-) diff --git a/magna.php b/magna.php index 5e79653f..0702eef2 100755 --- a/magna.php +++ b/magna.php @@ -51,7 +51,7 @@ function configureCall($call) { $call->configuration['enable_AGC'] = false; $call->configuration['enable_AEC'] = false; $call->configuration['shared_config'] = [ - 'audio_init_bitrate' => 500 * 1000, + 'audio_init_bitrate' => 100 * 1000, 'audio_max_bitrate' => 500 * 1000, 'audio_min_bitrate' => 100 * 1000, 'audio_congestion_window' => 4*1024 diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index 28a1a485..737ac96b 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -47,18 +47,19 @@ class API extends APIFactory flock($realpaths['lockfile'], LOCK_UN); fclose($realpaths['lockfile']); } - class_exists('\\Volatile'); \danog\MadelineProto\Logger::class_exists(); try { $unserialized = unserialize($tounserialize); } catch (\danog\MadelineProto\Bug74586Exception $e) { + class_exists('\\Volatile'); $tounserialize = str_replace('O:26:"danog\\MadelineProto\\Button":', 'O:35:"danog\\MadelineProto\\TL\\Types\\Button":', $tounserialize); foreach (['RSA', 'TL\\TLMethod', 'TL\\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\\Types\\Button', 'TL\\Types\\Bytes', 'APIFactory'] as $class) { class_exists('\\danog\\MadelineProto\\'.$class); } $unserialized = \danog\Serialization::unserialize($tounserialize); } catch (\danog\MadelineProto\Exception $e) { + class_exists('\\Volatile'); $tounserialize = str_replace('O:26:"danog\\MadelineProto\\Button":', 'O:35:"danog\\MadelineProto\\TL\\Types\\Button":', $tounserialize); foreach (['RSA', 'TL\\TLMethod', 'TL\\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\\Types\\Button', 'TL\\Types\\Bytes', 'APIFactory'] as $class) { class_exists('\\danog\\MadelineProto\\'.$class); diff --git a/src/danog/MadelineProto/Logger.php b/src/danog/MadelineProto/Logger.php index 31cdc1ad..a21587f9 100644 --- a/src/danog/MadelineProto/Logger.php +++ b/src/danog/MadelineProto/Logger.php @@ -35,8 +35,11 @@ class Logger public static $is_fork = false; public static $can_getmypid = true; public static $processed_fork = false; + public static $ipv6; private static $pid; + public static $inited = false; + const ULTRA_VERBOSE = 5; const VERBOSE = 4; const NOTICE = 3; @@ -46,28 +49,32 @@ class Logger public static function class_exists() { - self::$has_thread = class_exists('\\Thread') && method_exists('\\Thread', 'getCurrentThread'); - self::$BIG_ENDIAN = pack('L', 1) === pack('N', 1); - self::$bigint = PHP_INT_SIZE < 8; - 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]) && \danog\MadelineProto\MTProto::V < (int) $matches[1]) { - throw new \danog\MadelineProto\Exception(hex2bin(\danog\MadelineProto\Lang::$current_lang['v_error']), 0, null, 'MadelineProto', 1); - } - if (class_exists('\\danog\\MadelineProto\\VoIP')) { - if (!defined('\\danog\\MadelineProto\\VoIP::PHP_LIBTGVOIP_VERSION') || \danog\MadelineProto\VoIP::PHP_LIBTGVOIP_VERSION !== '1.1.2') { - throw new \danog\MadelineProto\Exception(hex2bin(\danog\MadelineProto\Lang::$current_lang['v_tgerror']), 0, null, 'MadelineProto', 1); + if (!self::$inited) { + self::$has_thread = class_exists('\\Thread') && method_exists('\\Thread', 'getCurrentThread'); + self::$BIG_ENDIAN = pack('L', 1) === pack('N', 1); + self::$bigint = PHP_INT_SIZE < 8; + self::$ipv6 = (bool) strlen(@file_get_contents('http://v6.ipv6-test.com/api/myip.php', 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]) && \danog\MadelineProto\MTProto::V < (int) $matches[1]) { + throw new \danog\MadelineProto\Exception(hex2bin(\danog\MadelineProto\Lang::$current_lang['v_error']), 0, null, 'MadelineProto', 1); } - } - self::$colors[self::ULTRA_VERBOSE] = implode(';', [self::foreground['light_gray'], self::set['dim']]); - self::$colors[self::VERBOSE] = implode(';', [self::foreground['green'], self::set['bold']]); - self::$colors[self::NOTICE] = implode(';', [self::foreground['yellow'], self::set['bold']]); - self::$colors[self::WARNING] = implode(';', [self::foreground['white'], self::set['dim'], self::background['red']]); - self::$colors[self::ERROR] = implode(';', [self::foreground['white'], self::set['bold'], self::background['red']]); - self::$colors[self::FATAL_ERROR] = implode(';', [self::foreground['red'], self::set['bold'], self::background['light_gray']]); + if (class_exists('\\danog\\MadelineProto\\VoIP')) { + if (!defined('\\danog\\MadelineProto\\VoIP::PHP_LIBTGVOIP_VERSION') || \danog\MadelineProto\VoIP::PHP_LIBTGVOIP_VERSION !== '1.1.2') { + throw new \danog\MadelineProto\Exception(hex2bin(\danog\MadelineProto\Lang::$current_lang['v_tgerror']), 0, null, 'MadelineProto', 1); + } + } + self::$colors[self::ULTRA_VERBOSE] = implode(';', [self::foreground['light_gray'], self::set['dim']]); + self::$colors[self::VERBOSE] = implode(';', [self::foreground['green'], self::set['bold']]); + self::$colors[self::NOTICE] = implode(';', [self::foreground['yellow'], self::set['bold']]); + self::$colors[self::WARNING] = implode(';', [self::foreground['white'], self::set['dim'], self::background['red']]); + self::$colors[self::ERROR] = implode(';', [self::foreground['white'], self::set['bold'], self::background['red']]); + self::$colors[self::FATAL_ERROR] = implode(';', [self::foreground['red'], self::set['bold'], self::background['light_gray']]); - try { - self::$isatty = defined('STDOUT') && function_exists('posix_isatty') && posix_isatty(STDOUT); - } catch (\danog\MadelineProto\Exception $e) { + try { + self::$isatty = defined('STDOUT') && function_exists('posix_isatty') && posix_isatty(STDOUT); + } catch (\danog\MadelineProto\Exception $e) { + } + self::$inited = true; } } diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index d48a7235..b6a81cf3 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -111,6 +111,7 @@ class MTProto public function __magic_construct($settings = []) { + \danog\MadelineProto\Logger::class_exists(); // Parse settings $this->parse_settings($settings); if (!defined('\\phpseclib\\Crypt\\Common\\SymmetricKey::MODE_IGE') || \phpseclib\Crypt\Common\SymmetricKey::MODE_IGE !== 6) { @@ -120,7 +121,6 @@ class MTProto throw new Exception(['extension', 'xml']); } $this->emojis = json_decode(self::JSON_EMOJIS); - \danog\MadelineProto\Logger::class_exists(); // Connect to servers \danog\MadelineProto\Logger::log(\danog\MadelineProto\Lang::$current_lang['inst_dc'], Logger::ULTRA_VERBOSE); if (!isset($this->datacenter)) { @@ -195,13 +195,11 @@ class MTProto } $this->altervista = isset($_SERVER['SERVER_ADMIN']) && strpos($_SERVER['SERVER_ADMIN'], 'altervista.org'); - $this->settings['connection_settings']['all']['ipv6'] = (bool) strlen(@file_get_contents('http://ipv6.test-ipv6.com/', false, stream_context_create(['http' => ['timeout' => 1]]))) > 0; + $this->settings['connection_settings']['all']['ipv6'] = \danog\MadelineProto\Logger::$ipv6; /*if (isset($this->settings['pwr']['update_handler']) && $this->settings['pwr']['update_handler'] === $this->settings['updates']['callback']) { unset($this->settings['pwr']['update_handler']); $this->updates = []; }*/ - // decides whether to use ipv6, ipv6 attribute of API attribute of API class contains autodetected boolean - preg_match('/const V = (\\d+);/', @file_get_contents('https://raw.githubusercontent.com/danog/MadelineProto/master/src/danog/MadelineProto/MTProto.php'), $matches); $keys = array_keys((array) get_object_vars($this)); if (count($keys) !== count(array_unique($keys))) { throw new Bug74586Exception(); @@ -451,7 +449,7 @@ class MTProto // can be tcp_full, tcp_abridged, tcp_intermediate, http, https, obfuscated2, udp (unsupported) 'test_mode' => false, // decides whether to connect to the main telegram servers or to the testing servers (deep telegram) - 'ipv6' => (bool) strlen(@file_get_contents('http://ipv6.test-ipv6.com/', false, stream_context_create(['http' => ['timeout' => 1]]))) > 0, + 'ipv6' => \danog\MadelineProto\Logger::$ipv6, // decides whether to use ipv6, ipv6 attribute of API attribute of API class contains autodetected boolean 'timeout' => 2, // timeout for sockets diff --git a/src/danog/MadelineProto/Serialization.php b/src/danog/MadelineProto/Serialization.php index cef3a510..ac782d41 100644 --- a/src/danog/MadelineProto/Serialization.php +++ b/src/danog/MadelineProto/Serialization.php @@ -80,72 +80,4 @@ class Serialization return $wrote; } - - /* - * Deserialize API class. - * - * @param string $filename - * - * @throws \danog\MadelineProto\Exception - * - * @return API - */ - /* - public static function deserialize($filename, $no_updates = false) - { - $realpaths = self::realpaths($filename); - if (file_exists($realpaths['file'])) { - if (!file_exists($realpaths['lockfile'])) { - touch($realpaths['lockfile']); - clearstatcache(); - } - $realpaths['lockfile'] = fopen($realpaths['lockfile'], 'r'); - \danog\MadelineProto\Logger::log('Waiting for shared lock of serialization lockfile...'); - flock($realpaths['lockfile'], LOCK_SH); - \danog\MadelineProto\Logger::log('Lock acquired, deserializing'); - - try { - $tounserialize = file_get_contents($realpaths['file']); - } finally { - flock($realpaths['lockfile'], LOCK_UN); - fclose($realpaths['lockfile']); - } - \danog\MadelineProto\Logger::class_exists(); - - try { - $unserialized = unserialize($tounserialize); - } catch (\danog\MadelineProto\Bug74586Exception $e) { - $tounserialize = str_replace('O:26:"danog\\MadelineProto\\Button":', 'O:35:"danog\\MadelineProto\\TL\\Types\\Button":', $unserialized); - foreach (['RSA', 'TL\\TLMethod', 'TL\\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\\Types\\Button', 'TL\\Types\\Bytes', 'APIFactory'] as $class) { - class_exists('\\danog\\MadelineProto\\'.$class); - } - class_exists('\\Volatile'); - $unserialized = \danog\Serialization::unserialize($tounserialize); - } catch (\danog\MadelineProto\Exception $e) { - $tounserialize = str_replace('O:26:"danog\\MadelineProto\\Button":', 'O:35:"danog\\MadelineProto\\TL\\Types\\Button":', $unserialized); - foreach (['RSA', 'TL\\TLMethod', 'TL\\TLConstructor', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\\Types\\Button', 'TL\\Types\\Bytes', 'APIFactory'] as $class) { - class_exists('\\danog\\MadelineProto\\'.$class); - } - class_exists('\\Volatile'); - Logger::log((string) $e, Logger::ERROR); - if (strpos($e->getMessage(), "Erroneous data format for unserializing 'phpseclib\\Math\\BigInteger'") === 0) { - $tounserialize = str_replace('phpseclib\\Math\\BigInteger', 'phpseclib\\Math\\BigIntegor', $unserialized); - } - $unserialized = \danog\Serialization::unserialize($tounserialize); - } - if ($unserialized instanceof \danog\PlaceHolder) { - $unserialized = \danog\Serialization::unserialize($tounserialize); - } - } else { - throw new Exception(\danog\MadelineProto\Lang::$current_lang['file_not_exist']); - } - if ($unserialized === false) { - throw new Exception(\danog\MadelineProto\Lang::$current_lang['deserialization_error']); - } - if ($unserialized instanceof \danog\MadelineProto\API) { - $unserialized->session = $filename; - } - - return $unserialized; - }*/ }