diff --git a/src/Socket.php b/src/Socket.php index 476491ae..d9114333 100644 --- a/src/Socket.php +++ b/src/Socket.php @@ -129,9 +129,13 @@ if (!extension_loaded('pthreads')) { private $protocol; private $timeout = ['sec' => 0, 'usec' => 0]; private $blocking = false; + private $domain; + private $type; public function __construct(int $domain, int $type, int $protocol) { + $this->domain = $domain; + $this->type = $type; $this->protocol = getprotobynumber($protocol); } @@ -179,8 +183,12 @@ if (!extension_loaded('pthreads')) { throw new \danog\MadelineProto\Exception('Not supported'); } - public function connect(string $address, int $port = 0) + public function connect(string $address, int $port = -1) { + if ($this->domain === AF_INET6 && strpos($address, ':') !== false) { + $address = '['.$address.']'; + } + $this->sock = fsockopen($this->protocol.'://'.$address, $port); return true; diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index ba064d56..716272e0 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -46,13 +46,6 @@ class Connection public $call_queue = []; public $i = []; - /* public function __get($name) { - echo "GETTING $name\n"; - if (isset($this->i[$name]) && $this->{$name} === null) var_dump($this->i[$name]); - if ($this->{$name} instanceof \Volatile) $this->i[$name] = debug_backtrace(0); - var_dump(is_null($this->{$name})); - return $this->{$name}; - }*/ public function ___construct($proxy, $extra, $ip, $port, $protocol, $timeout, $ipv6) { @@ -164,6 +157,7 @@ class Connection case 'http': case 'https': $this->parsed = parse_url($ip); + if ($this->parsed['host'][0] === '[') $this->parsed['host'] = substr($this->parsed['host'], 1, -1); $this->sock = new $proxy($ipv6 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, getprotobyname($this->protocol === 'https' ? 'tls' : 'tcp')); if ($has_proxy && $this->extra !== []) { $this->sock->setExtra($this->extra); diff --git a/src/danog/MadelineProto/DataCenter.php b/src/danog/MadelineProto/DataCenter.php index cfdaa87e..59534c2f 100644 --- a/src/danog/MadelineProto/DataCenter.php +++ b/src/danog/MadelineProto/DataCenter.php @@ -100,8 +100,8 @@ class DataCenter } if ($this->settings[$dc_config_number]['protocol'] === 'http') { + if ($ipv6) $address = '['.$address.']'; $address = $this->settings[$dc_config_number]['protocol'].'://'.$address.'/api'; - $port = 80; } \danog\MadelineProto\Logger::log([sprintf(\danog\MadelineProto\Lang::$current_lang['dc_con_test_start'], $dc_number, $test, $ipv6, $this->settings[$dc_config_number]['protocol'])], \danog\MadelineProto\Logger::VERBOSE); diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index c08df767..4b6f23d8 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -195,7 +195,6 @@ class MTProto { // Parse settings $this->parse_settings($settings); - if (!defined('\phpseclib\Crypt\AES::MODE_IGE')) { throw new Exception(\danog\MadelineProto\Lang::$current_lang['phpseclib_fork']); } @@ -274,7 +273,6 @@ class MTProto if (isset($this->settings['app_info']['lang_code']) && isset(Lang::$lang[$this->settings['app_info']['lang_code']])) { Lang::$current_lang = &Lang::$lang[$this->settings['app_info']['lang_code']]; } - if (!defined('\phpseclib\Crypt\AES::MODE_IGE')) { throw new Exception(\danog\MadelineProto\Lang::$current_lang['phpseclib_fork']); } @@ -581,9 +579,9 @@ class MTProto throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['api_not_set'], 0, null, 'MadelineProto', 1); } - if ($settings['app_info']['api_id'] < 20) { + /*if ($settings['app_info']['api_id'] < 20) { $settings['connection_settings']['all']['protocol'] = 'obfuscated2'; - } + }*/ switch ($settings['logger']['logger_level']) { case 'ULTRA_VERBOSE': $settings['logger']['logger_level'] = 5; break; case 'VERBOSE': $settings['logger']['logger_level'] = 4; break; @@ -632,6 +630,16 @@ class MTProto $socket->new_incoming = []; } } + public function is_http($datacenter) { + return in_array($this->datacenter->sockets[$datacenter]->protocol, ['http', 'https']); + } + + public function close_and_reopen($datacenter) { + $this->datacenter->sockets[$datacenter]->close_and_reopen(); + if ($this->is_http($datacenter)) { + $this->method_call('http_wait', ['max_wait' => 0, 'wait_after' => 0, 'max_delay' => 0], ['datacenter' => $datacenter]); + } + } // Connects to all datacenters and if necessary creates authorization keys, binds them and writes client info public function connect_to_all_dcs() @@ -681,9 +689,6 @@ class MTProto $this->sync_authorization($id); $this->get_config($config); } - if (in_array($socket->protocol, ['http', 'https'])) { - $this->method_call('http_wait', ['max_wait' => 0, 'wait_after' => 0, 'max_delay' => 0], ['datacenter' => $id]); - } } elseif (!$cdn) { $this->sync_authorization($id); } diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 3f2d2e68..01d423e2 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -553,6 +553,8 @@ trait AuthKeyHandler if ($res === true) { \danog\MadelineProto\Logger::log(['Successfully binded temporary and permanent authorization keys, DC '.$datacenter], \danog\MadelineProto\Logger::NOTICE); + if ($this->is_http($datacenter)) $this->close_and_reopen($datacenter); + return true; } } catch (\danog\MadelineProto\SecurityException $e) { diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index 40eeca44..0db3d2c7 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -203,7 +203,7 @@ trait CallHandler //if (in_array($this->datacenter->sockets[$aargs['datacenter']]->protocol, ['http', 'https']) && $method !== 'http_wait') { //$this->method_call('http_wait', ['max_wait' => $this->datacenter->sockets[$aargs['datacenter']]->timeout, 'wait_after' => 0, 'max_delay' => 0], ['datacenter' => $aargs['datacenter']]); //} else { - $this->datacenter->sockets[$aargs['datacenter']]->close_and_reopen(); + $this->close_and_reopen($aargs['datacenter']); //} continue; } catch (\RuntimeException $e) { @@ -212,7 +212,7 @@ trait CallHandler //if (in_array($this->datacenter->sockets[$aargs['datacenter']]->protocol, ['http', 'https']) && $method !== 'http_wait') { //$this->method_call('http_wait', ['max_wait' => $this->datacenter->sockets[$aargs['datacenter']]->timeout, 'wait_after' => 0, 'max_delay' => 0], ['datacenter' => $aargs['datacenter']]); //} else { - $this->datacenter->sockets[$aargs['datacenter']]->close_and_reopen(); + $this->close_and_reopen($aargs['datacenter']); //} continue; } finally { @@ -282,7 +282,7 @@ trait CallHandler } } catch (Exception $e) { \danog\MadelineProto\Logger::log(['An error occurred while calling object '.$object.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().'. Recreating connection and retrying to call object...'], \danog\MadelineProto\Logger::WARNING); - $this->datacenter->sockets[$aargs['datacenter']]->close_and_reopen(); + $this->close_and_reopen($aargs['datacenter']); continue; } diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index 24ff8985..cf2e318a 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -351,7 +351,7 @@ trait PeerHandler if (isset($full['full']['profile_photo']['sizes'])) { $res['photo'] = $this->photosize_to_botapi(end($full['full']['profile_photo']['sizes']), []); } - $bio = ''; + /*$bio = ''; if ($full['type'] === 'user' && isset($res['username']) && !isset($res['about']) && $fullfetch) { if (preg_match('/meta property="og:description" content=".+/', file_get_contents('https://telegram.me/'.$res['username']), $biores)) { $bio = html_entity_decode(preg_replace_callback('/(&#[0-9]+;)/', function ($m) { @@ -361,7 +361,7 @@ trait PeerHandler if ($bio != '' && $bio != 'You can contact @'.$res['username'].' right away.') { $res['about'] = $bio; } - } + }*/ break; case 'chat': foreach (['title', 'participants_count', 'admin', 'admins_enabled'] as $key) {