2022-12-30 21:54:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-02-24 17:54:13 +01:00
|
|
|
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
/**
|
|
|
|
* DataCenter module.
|
|
|
|
*
|
|
|
|
* This file is part of MadelineProto.
|
|
|
|
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU Affero General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License along with MadelineProto.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author Daniil Gentili <daniil@daniil.it>
|
2023-01-04 12:43:01 +01:00
|
|
|
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
2019-10-31 15:07:35 +01:00
|
|
|
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
*/
|
2018-02-24 17:54:39 +01:00
|
|
|
|
2016-10-09 17:42:39 +02:00
|
|
|
namespace danog\MadelineProto;
|
|
|
|
|
2023-01-11 21:06:10 +01:00
|
|
|
use Amp\Dns\DnsResolver;
|
2019-12-13 12:57:47 +01:00
|
|
|
use Amp\Http\Client\Cookie\CookieJar;
|
2023-01-11 18:47:27 +01:00
|
|
|
use Amp\Http\Client\HttpClient;
|
2019-12-13 15:37:57 +01:00
|
|
|
use Amp\Http\Client\Request;
|
2019-12-13 13:29:53 +01:00
|
|
|
use Amp\Socket\ConnectContext;
|
2019-09-02 15:30:29 +02:00
|
|
|
use danog\MadelineProto\MTProto\PermAuthKey;
|
|
|
|
use danog\MadelineProto\MTProto\TempAuthKey;
|
2020-09-22 11:48:12 +02:00
|
|
|
use danog\MadelineProto\Settings\Connection as ConnectionSettings;
|
2023-01-08 16:41:42 +01:00
|
|
|
use danog\MadelineProto\Stream\Common\BufferedRawStream;
|
|
|
|
use danog\MadelineProto\Stream\Common\UdpBufferedStream;
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
use danog\MadelineProto\Stream\ConnectionContext;
|
2023-01-08 16:41:42 +01:00
|
|
|
use danog\MadelineProto\Stream\MTProtoTransport\AbridgedStream;
|
|
|
|
use danog\MadelineProto\Stream\MTProtoTransport\FullStream;
|
|
|
|
use danog\MadelineProto\Stream\MTProtoTransport\HttpsStream;
|
|
|
|
use danog\MadelineProto\Stream\MTProtoTransport\HttpStream;
|
|
|
|
use danog\MadelineProto\Stream\MTProtoTransport\IntermediatePaddedStream;
|
|
|
|
use danog\MadelineProto\Stream\MTProtoTransport\IntermediateStream;
|
|
|
|
use danog\MadelineProto\Stream\MTProtoTransport\ObfuscatedStream;
|
|
|
|
use danog\MadelineProto\Stream\Transport\DefaultStream;
|
|
|
|
use danog\MadelineProto\Stream\Transport\WssStream;
|
|
|
|
use danog\MadelineProto\Stream\Transport\WsStream;
|
2022-12-30 19:21:36 +01:00
|
|
|
use Throwable;
|
|
|
|
|
2016-10-09 17:42:39 +02:00
|
|
|
/**
|
2023-01-08 16:06:50 +01:00
|
|
|
* @psalm-type TDcOption=array{
|
|
|
|
* _: 'dcOption',
|
|
|
|
* cdn: bool,
|
|
|
|
* this_port_only: bool,
|
|
|
|
* tcpo_only: bool,
|
|
|
|
* ip_address: string,
|
|
|
|
* port: int,
|
|
|
|
* secret?: string
|
|
|
|
* }
|
|
|
|
* @psalm-type TDcList=array{
|
|
|
|
* test: array{
|
|
|
|
* ipv4: non-empty-array<int, TDcOption>,
|
|
|
|
* ipv6: non-empty-array<int, TDcOption>,
|
|
|
|
* },
|
|
|
|
* main: array{
|
|
|
|
* ipv4: non-empty-array<int, TDcOption>,
|
|
|
|
* ipv6: non-empty-array<int, TDcOption>,
|
|
|
|
* },
|
|
|
|
* }
|
|
|
|
* @internal Manages datacenters.
|
2016-10-09 17:42:39 +02:00
|
|
|
*/
|
2023-01-15 12:05:38 +01:00
|
|
|
final class DataCenter
|
2016-10-09 17:42:39 +02:00
|
|
|
{
|
2019-09-01 14:07:04 +02:00
|
|
|
/**
|
|
|
|
* All socket connections to DCs.
|
|
|
|
*
|
2023-01-08 16:06:50 +01:00
|
|
|
* @var array<int, DataCenterConnection>
|
2019-09-01 14:07:04 +02:00
|
|
|
*/
|
2023-01-28 14:53:07 +01:00
|
|
|
private array $sockets = [];
|
2019-09-01 14:07:04 +02:00
|
|
|
/**
|
|
|
|
* Current DC ID.
|
|
|
|
*/
|
2023-01-08 16:06:50 +01:00
|
|
|
public int $currentDatacenter = 1;
|
2019-09-01 14:07:04 +02:00
|
|
|
/**
|
|
|
|
* Main instance.
|
|
|
|
*/
|
2023-01-04 15:13:55 +01:00
|
|
|
private MTProto $API;
|
2019-09-01 14:07:04 +02:00
|
|
|
/**
|
|
|
|
* DC list.
|
2023-01-08 16:23:18 +01:00
|
|
|
*
|
2023-01-08 16:06:50 +01:00
|
|
|
* @param TDcList
|
2019-09-01 14:07:04 +02:00
|
|
|
*/
|
2023-01-04 15:13:55 +01:00
|
|
|
private array $dclist = [];
|
2019-09-01 14:07:04 +02:00
|
|
|
/**
|
|
|
|
* Settings.
|
|
|
|
*/
|
2023-01-04 15:13:55 +01:00
|
|
|
private ConnectionSettings $settings;
|
2023-01-04 16:04:05 +01:00
|
|
|
private DoHWrapper $dohWrapper;
|
2020-02-28 14:14:02 +01:00
|
|
|
|
2017-03-24 21:01:36 +01:00
|
|
|
public function __sleep()
|
|
|
|
{
|
2023-01-08 16:06:50 +01:00
|
|
|
return ['sockets', 'currentDatacenter', 'dclist', 'settings'];
|
|
|
|
}
|
2023-01-08 16:23:18 +01:00
|
|
|
public static function isTest(int $dc): bool
|
|
|
|
{
|
|
|
|
return \abs($dc) > 10000;
|
2023-01-08 16:06:50 +01:00
|
|
|
}
|
2023-01-08 16:23:18 +01:00
|
|
|
public static function isMedia(int $dc): bool
|
|
|
|
{
|
2023-01-08 16:06:50 +01:00
|
|
|
return $dc < 0;
|
|
|
|
}
|
2023-01-08 16:23:18 +01:00
|
|
|
public function isCdn(int $dc): bool
|
|
|
|
{
|
2023-01-08 16:06:50 +01:00
|
|
|
$test = $this->settings->getTestMode() ? 'test' : 'main';
|
|
|
|
$ipv6 = $this->settings->getIpv6() ? 'ipv6' : 'ipv4';
|
2023-01-08 16:41:42 +01:00
|
|
|
return $this->dclist[$test][$ipv6][$dc]['cdn'] ?? false;
|
2017-03-24 21:01:08 +01:00
|
|
|
}
|
2022-12-08 20:16:40 +01:00
|
|
|
public function __wakeup(): void
|
2019-08-31 22:43:58 +02:00
|
|
|
{
|
2019-09-01 23:39:29 +02:00
|
|
|
$array = [];
|
|
|
|
foreach ($this->sockets as $id => $socket) {
|
2022-12-30 19:21:36 +01:00
|
|
|
if ($socket instanceof Connection) {
|
2020-10-03 12:36:08 +02:00
|
|
|
if (isset($socket->temp_auth_key) && $socket->temp_auth_key) {
|
2019-09-01 23:39:29 +02:00
|
|
|
$array[$id]['tempAuthKey'] = $socket->temp_auth_key;
|
2019-08-31 22:43:58 +02:00
|
|
|
}
|
2020-10-03 12:36:08 +02:00
|
|
|
if (isset($socket->auth_key) && $socket->auth_key) {
|
2019-09-02 16:54:36 +02:00
|
|
|
$array[$id]['permAuthKey'] = $socket->auth_key;
|
2020-10-03 12:36:08 +02:00
|
|
|
/** @psalm-suppress UndefinedPropertyFetch */
|
2019-09-02 16:54:36 +02:00
|
|
|
$array[$id]['permAuthKey']['authorized'] = $socket->authorized;
|
2019-09-01 23:39:29 +02:00
|
|
|
}
|
2020-10-13 10:03:35 +02:00
|
|
|
$array[$id] = [];
|
2019-09-01 23:39:29 +02:00
|
|
|
}
|
2023-01-08 16:23:18 +01:00
|
|
|
if (!\is_int($id)) {
|
2023-01-08 16:06:50 +01:00
|
|
|
unset($this->sockets[$id]);
|
|
|
|
}
|
2019-09-01 23:39:29 +02:00
|
|
|
}
|
|
|
|
$this->setDataCenterConnections($array);
|
|
|
|
}
|
|
|
|
/**
|
2019-09-02 17:08:36 +02:00
|
|
|
* Set auth key information from saved auth array.
|
2019-09-01 23:39:29 +02:00
|
|
|
*
|
|
|
|
* @param array $saved Saved auth array
|
|
|
|
*/
|
2022-12-08 20:16:40 +01:00
|
|
|
public function setDataCenterConnections(array $saved): void
|
2019-09-01 23:39:29 +02:00
|
|
|
{
|
|
|
|
foreach ($saved as $id => $data) {
|
2020-01-31 19:29:43 +01:00
|
|
|
$connection = $this->sockets[$id] = new DataCenterConnection();
|
2019-09-02 16:54:36 +02:00
|
|
|
if (isset($data['permAuthKey'])) {
|
|
|
|
$connection->setPermAuthKey(new PermAuthKey($data['permAuthKey']));
|
2019-09-01 23:39:29 +02:00
|
|
|
}
|
|
|
|
if (isset($data['linked'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isset($data['tempAuthKey'])) {
|
|
|
|
$connection->setTempAuthKey(new TempAuthKey($data['tempAuthKey']));
|
2019-09-02 16:54:36 +02:00
|
|
|
if (($data['tempAuthKey']['bound'] ?? false) && $connection->hasPermAuthKey()) {
|
2019-09-01 23:39:29 +02:00
|
|
|
$connection->bind();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($saved[$id]);
|
|
|
|
}
|
|
|
|
foreach ($saved as $id => $data) {
|
|
|
|
$connection = $this->sockets[$id];
|
|
|
|
$connection->link($data['linked']);
|
|
|
|
if (isset($data['tempAuthKey'])) {
|
|
|
|
$connection->setTempAuthKey(new TempAuthKey($data['tempAuthKey']));
|
2019-09-02 16:54:36 +02:00
|
|
|
if (($data['tempAuthKey']['bound'] ?? false) && $connection->hasPermAuthKey()) {
|
2019-09-01 23:39:29 +02:00
|
|
|
$connection->bind();
|
2019-08-31 22:43:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 22:42:38 +02:00
|
|
|
/**
|
|
|
|
* Constructor function.
|
|
|
|
*
|
2020-09-22 11:48:12 +02:00
|
|
|
* @param MTProto $API Main MTProto instance
|
|
|
|
* @param ConnectionSettings $settings Settings
|
|
|
|
* @param boolean $reconnectAll Whether to reconnect to all DCs or just to changed ones
|
|
|
|
* @param CookieJar $jar Cookie jar
|
2019-09-17 22:42:38 +02:00
|
|
|
*/
|
2023-01-15 13:07:48 +01:00
|
|
|
public function __construct(MTProto $API, array $dclist, ConnectionSettings $settings, bool $reconnectAll = true, ?CookieJar $jar = null)
|
2016-10-09 17:42:53 +02:00
|
|
|
{
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
$this->API = $API;
|
2019-09-17 22:42:38 +02:00
|
|
|
$changed = [];
|
2020-09-26 21:57:06 +02:00
|
|
|
$changedSettings = $settings->hasChanged();
|
2019-09-17 22:42:38 +02:00
|
|
|
if (!$reconnectAll) {
|
|
|
|
$changed = [];
|
2020-01-31 19:29:43 +01:00
|
|
|
$test = $API->getCachedConfig()['test_mode'] ?? false ? 'test' : 'main';
|
2023-01-08 16:23:18 +01:00
|
|
|
foreach ($this->dclist[$test] as $ipv6 => $dcs) {
|
2019-09-17 22:42:38 +02:00
|
|
|
foreach ($dcs as $id => $dc) {
|
|
|
|
if ($dc !== ($this->dclist[$test][$ipv6][$id] ?? [])) {
|
|
|
|
$changed[$id] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 18:47:27 +01:00
|
|
|
$this->dclist = $dclist;
|
2017-04-21 13:13:24 +02:00
|
|
|
$this->settings = $settings;
|
2017-05-31 11:22:42 +02:00
|
|
|
foreach ($this->sockets as $key => $socket) {
|
2023-01-08 16:23:18 +01:00
|
|
|
if ($socket instanceof DataCenterConnection && \is_int($key)) {
|
2019-09-17 22:42:38 +02:00
|
|
|
if ($reconnectAll || isset($changed[$id])) {
|
2023-01-04 15:13:55 +01:00
|
|
|
$this->API->logger->logger('Disconnecting all before reconnect!');
|
2019-09-17 22:42:38 +02:00
|
|
|
$socket->needReconnect(true);
|
|
|
|
$socket->setExtra($this->API);
|
|
|
|
$socket->disconnect();
|
|
|
|
}
|
2017-05-31 11:23:36 +02:00
|
|
|
} else {
|
|
|
|
unset($this->sockets[$key]);
|
|
|
|
}
|
2017-02-12 19:55:49 +01:00
|
|
|
}
|
2023-01-04 16:04:05 +01:00
|
|
|
if ($reconnectAll || $changedSettings || !isset($this->dohWrapper)) {
|
|
|
|
$this->dohWrapper = new DoHWrapper(
|
|
|
|
$settings,
|
|
|
|
$API,
|
|
|
|
$jar
|
2023-01-04 15:13:55 +01:00
|
|
|
);
|
2019-09-17 22:42:38 +02:00
|
|
|
}
|
2020-09-22 11:48:12 +02:00
|
|
|
$this->settings->applyChanges();
|
2019-06-12 20:51:38 +02:00
|
|
|
}
|
2020-06-15 20:06:48 +02:00
|
|
|
/**
|
|
|
|
* Connect to specified DC.
|
|
|
|
*
|
2023-01-08 16:06:50 +01:00
|
|
|
* @param int $dc_number DC to connect to
|
2020-06-15 20:06:48 +02:00
|
|
|
* @param integer $id Connection ID to re-establish (optional)
|
|
|
|
*/
|
2023-01-08 16:06:50 +01:00
|
|
|
public function dcConnect(int $dc_number, int $id = -1): bool
|
2016-10-09 17:42:53 +02:00
|
|
|
{
|
2023-01-15 18:47:29 +01:00
|
|
|
$old = isset($this->sockets[$dc_number]) && (
|
|
|
|
$this->sockets[$dc_number]->shouldReconnect()
|
|
|
|
|| (
|
|
|
|
$id !== -1
|
|
|
|
&& $this->sockets[$dc_number]->hasConnection($id)
|
|
|
|
&& $this->sockets[$dc_number]->getConnection($id)->shouldReconnect()
|
|
|
|
)
|
|
|
|
);
|
2019-09-13 18:03:18 +02:00
|
|
|
if (isset($this->sockets[$dc_number]) && !$old) {
|
2023-01-15 13:07:48 +01:00
|
|
|
$this->API->logger->logger("Not reconnecting to DC {$dc_number} ({$id})");
|
2016-11-24 13:16:06 +01:00
|
|
|
return false;
|
2016-10-09 19:40:20 +02:00
|
|
|
}
|
2019-05-10 20:01:39 +02:00
|
|
|
$ctxs = $this->generateContexts($dc_number);
|
2018-12-31 14:02:24 +01:00
|
|
|
if (empty($ctxs)) {
|
|
|
|
return false;
|
|
|
|
}
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
foreach ($ctxs as $ctx) {
|
|
|
|
try {
|
2019-12-29 02:58:15 +01:00
|
|
|
if ($old) {
|
2020-06-15 20:06:48 +02:00
|
|
|
$this->API->logger->logger("Reconnecting to DC {$dc_number} ({$id}) from existing", Logger::WARNING);
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
$this->sockets[$dc_number]->setExtra($this->API);
|
2022-12-30 22:31:20 +01:00
|
|
|
$this->sockets[$dc_number]->connect($ctx, $id);
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
} else {
|
2020-06-15 20:06:48 +02:00
|
|
|
$this->API->logger->logger("Connecting to DC {$dc_number} from scratch", Logger::WARNING);
|
2019-09-01 14:07:04 +02:00
|
|
|
$this->sockets[$dc_number] = new DataCenterConnection();
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
$this->sockets[$dc_number]->setExtra($this->API);
|
2022-12-30 22:31:20 +01:00
|
|
|
$this->sockets[$dc_number]->connect($ctx);
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
}
|
2021-04-20 15:01:53 +02:00
|
|
|
if ($ctx->getIpv6()) {
|
|
|
|
Magic::setIpv6(true);
|
|
|
|
}
|
2020-06-15 20:06:48 +02:00
|
|
|
$this->API->logger->logger('OK!', Logger::WARNING);
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
return true;
|
2022-12-30 19:21:36 +01:00
|
|
|
} catch (Throwable $e) {
|
2023-01-04 15:13:55 +01:00
|
|
|
if (\defined('MADELINEPROTO_TEST') && \constant('MADELINEPROTO_TEST') === 'pony') {
|
2019-05-10 13:34:44 +02:00
|
|
|
throw $e;
|
|
|
|
}
|
2023-08-06 16:44:16 +02:00
|
|
|
$this->API->logger->logger("Connection failed ({$dc_number}): $e", Logger::ERROR);
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 00:11:06 +01:00
|
|
|
throw new Exception("Could not connect to DC {$dc_number}");
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
}
|
2023-01-11 18:47:27 +01:00
|
|
|
public function getHTTPClient(): HttpClient
|
|
|
|
{
|
|
|
|
return $this->dohWrapper->HTTPClient;
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:06:10 +01:00
|
|
|
public function getDNSClient(): DnsResolver
|
2023-01-11 18:47:27 +01:00
|
|
|
{
|
|
|
|
return $this->dohWrapper->DoHClient;
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:22:47 +02:00
|
|
|
/**
|
2020-06-15 20:06:48 +02:00
|
|
|
* Generate contexts.
|
|
|
|
*
|
|
|
|
* @param integer $dc_number DC ID to generate contexts for
|
|
|
|
* @param ConnectContext $context Connection context
|
2023-01-04 15:13:55 +01:00
|
|
|
* @return array<ConnectionContext>
|
2020-04-05 22:22:47 +02:00
|
|
|
*/
|
2023-01-08 16:23:18 +01:00
|
|
|
public function generateContexts(int $dc_number, ?ConnectContext $context = null): array
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
{
|
2023-01-08 16:23:18 +01:00
|
|
|
$ctxs = [];
|
|
|
|
$combos = [];
|
|
|
|
$test = $this->settings->getTestMode() ? 'test' : 'main';
|
|
|
|
$ipv6 = $this->settings->getIpv6() ? 'ipv6' : 'ipv4';
|
2023-01-08 16:41:42 +01:00
|
|
|
$default = match ($this->settings->getProtocol()) {
|
|
|
|
AbridgedStream::class =>
|
|
|
|
[[DefaultStream::class, []], [BufferedRawStream::class, []], [AbridgedStream::class, []]],
|
|
|
|
IntermediateStream::class =>
|
|
|
|
[[DefaultStream::class, []], [BufferedRawStream::class, []], [IntermediateStream::class, []]],
|
|
|
|
IntermediatePaddedStream::class =>
|
|
|
|
[[DefaultStream::class, []], [BufferedRawStream::class, []], [IntermediatePaddedStream::class, []]],
|
|
|
|
FullStream::class =>
|
|
|
|
[[DefaultStream::class, []], [BufferedRawStream::class, []], [FullStream::class, []]],
|
|
|
|
HttpStream::class =>
|
|
|
|
[[DefaultStream::class, []], [BufferedRawStream::class, []], [HttpStream::class, []]],
|
|
|
|
HttpsStream::class =>
|
|
|
|
[[DefaultStream::class, []], [BufferedRawStream::class, []], [HttpsStream::class, []]],
|
|
|
|
UdpBufferedStream::class =>
|
|
|
|
[[DefaultStream::class, []], [UdpBufferedStream::class, []]],
|
|
|
|
};
|
2023-06-28 15:50:38 +02:00
|
|
|
if ($this->settings->getObfuscated() && !\in_array($default[2][0], [HttpsStream::class, HttpStream::class], true)) {
|
2023-01-08 16:23:18 +01:00
|
|
|
$default = [[DefaultStream::class, []], [BufferedRawStream::class, []], [ObfuscatedStream::class, []], \end($default)];
|
|
|
|
}
|
2023-06-28 15:50:38 +02:00
|
|
|
if ($this->settings->getTransport() && !\in_array($default[2][0], [HttpsStream::class, HttpStream::class], true)) {
|
2023-01-08 16:23:18 +01:00
|
|
|
switch ($this->settings->getTransport()) {
|
|
|
|
case DefaultStream::class:
|
|
|
|
if ($this->settings->getObfuscated()) {
|
|
|
|
$default = [[DefaultStream::class, []], [BufferedRawStream::class, []], [ObfuscatedStream::class, []], \end($default)];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WssStream::class:
|
|
|
|
$default = [[DefaultStream::class, []], [WssStream::class, []], [BufferedRawStream::class, []], [ObfuscatedStream::class, []], \end($default)];
|
|
|
|
break;
|
|
|
|
case WsStream::class:
|
|
|
|
$default = [[DefaultStream::class, []], [WsStream::class, []], [BufferedRawStream::class, []], [ObfuscatedStream::class, []], \end($default)];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$combos[] = $default;
|
2023-08-16 13:52:10 +02:00
|
|
|
|
|
|
|
$only = $this->dclist[$test][$ipv6][$dc_number]['tcpo_only'];
|
|
|
|
if ($only || isset($this->dclist[$test][$ipv6][$dc_number]['secret'])) {
|
|
|
|
$extra = isset($this->dclist[$test][$ipv6][$dc_number]['secret']) ? ['secret' => $this->dclist[$test][$ipv6][$dc_number]['secret']] : [];
|
|
|
|
$combo = [[DefaultStream::class, []], [BufferedRawStream::class, []], [ObfuscatedStream::class, $extra], [IntermediatePaddedStream::class, []]];
|
|
|
|
if ($only) {
|
|
|
|
\array_unshift($combos, $combo);
|
|
|
|
} else {
|
|
|
|
$combos []= $combo;
|
2023-01-08 16:23:18 +01:00
|
|
|
}
|
2023-08-16 13:52:10 +02:00
|
|
|
}
|
|
|
|
$proxyCombos = [];
|
|
|
|
foreach ($this->settings->getProxies() as $proxy => $extras) {
|
|
|
|
foreach ($extras as $extra) {
|
|
|
|
if ($proxy === ObfuscatedStream::class && \in_array(\strlen($extra['secret']), [17, 34], true)) {
|
|
|
|
$combos[] = [[DefaultStream::class, []], [BufferedRawStream::class, []], [$proxy, $extra], [IntermediatePaddedStream::class, []]];
|
|
|
|
}
|
|
|
|
foreach ($combos as $orig) {
|
|
|
|
$combo = [];
|
|
|
|
if ($proxy === ObfuscatedStream::class) {
|
|
|
|
$combo = $orig;
|
|
|
|
if ($combo[\count($combo) - 2][0] === ObfuscatedStream::class) {
|
|
|
|
$combo[\count($combo) - 2][1] = $extra;
|
2023-01-08 16:23:18 +01:00
|
|
|
} else {
|
2023-08-16 13:52:10 +02:00
|
|
|
$mtproto = \end($combo);
|
|
|
|
$combo[\count($combo) - 1] = [$proxy, $extra];
|
|
|
|
$combo[] = $mtproto;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($orig[1][0] === BufferedRawStream::class) {
|
|
|
|
[$first, $second] = [\array_slice($orig, 0, 2), \array_slice($orig, 2)];
|
|
|
|
$first[] = [$proxy, $extra];
|
|
|
|
$combo = \array_merge($first, $second);
|
|
|
|
} elseif (\in_array($orig[1][0], [WsStream::class, WssStream::class], true)) {
|
|
|
|
[$first, $second] = [\array_slice($orig, 0, 1), \array_slice($orig, 1)];
|
|
|
|
$first[] = [BufferedRawStream::class, []];
|
|
|
|
$first[] = [$proxy, $extra];
|
|
|
|
$combo = \array_merge($first, $second);
|
2023-01-08 16:23:18 +01:00
|
|
|
}
|
|
|
|
}
|
2023-08-16 13:52:10 +02:00
|
|
|
$proxyCombos []= $combo;
|
2023-01-08 16:23:18 +01:00
|
|
|
}
|
|
|
|
}
|
2023-08-16 13:52:10 +02:00
|
|
|
}
|
|
|
|
if ($this->settings->getRetry()) {
|
2023-01-08 16:23:18 +01:00
|
|
|
$combos = \array_merge($proxyCombos, $combos);
|
|
|
|
$combos[] = [[DefaultStream::class, []], [BufferedRawStream::class, []], [HttpsStream::class, []]];
|
2023-08-16 13:52:10 +02:00
|
|
|
} elseif ($proxyCombos) {
|
|
|
|
$combos = $proxyCombos;
|
2023-01-08 16:23:18 +01:00
|
|
|
}
|
2023-08-16 13:52:10 +02:00
|
|
|
$combos = \array_unique($combos, SORT_REGULAR);
|
|
|
|
|
2023-01-26 19:57:50 +01:00
|
|
|
$context ??= (new ConnectContext())->withConnectTimeout($this->settings->getTimeout())->withBindTo($this->settings->getBindTo());
|
2023-01-08 16:23:18 +01:00
|
|
|
foreach ($combos as $combo) {
|
|
|
|
foreach ([true, false] as $useDoH) {
|
|
|
|
$ipv6Combos = [
|
|
|
|
$this->settings->getIpv6() ? 'ipv6' : 'ipv4',
|
|
|
|
$this->settings->getIpv6() ? 'ipv4' : 'ipv6'
|
|
|
|
];
|
|
|
|
foreach ($ipv6Combos as $ipv6) {
|
|
|
|
if (!isset($this->dclist[$test][$ipv6][$dc_number]['ip_address'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$address = $this->dclist[$test][$ipv6][$dc_number]['ip_address'];
|
|
|
|
if ($ipv6 === 'ipv6') {
|
|
|
|
$address = "[$address]";
|
|
|
|
}
|
|
|
|
$port = $this->dclist[$test][$ipv6][$dc_number]['port'];
|
|
|
|
foreach (\array_unique([$port, 443, 80, 88, 5222]) as $port) {
|
|
|
|
$stream = \end($combo)[0];
|
|
|
|
if ($stream === HttpsStream::class) {
|
2023-01-15 13:07:48 +01:00
|
|
|
$subdomain = $this->settings->getSslSubdomains()[\abs($dc_number)] ?? null;
|
2023-01-08 16:23:18 +01:00
|
|
|
if (!$subdomain) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (DataCenter::isMedia($dc_number)) {
|
|
|
|
$subdomain .= '-1';
|
|
|
|
}
|
|
|
|
$path = $this->settings->getTestMode() ? 'apiw_test1' : 'apiw1';
|
|
|
|
$uri = 'tcp://'.$subdomain.'.web.telegram.org:'.$port.'/'.$path;
|
|
|
|
} elseif ($stream === HttpStream::class) {
|
|
|
|
$uri = 'tcp://'.$address.':'.$port.'/api';
|
|
|
|
} else {
|
|
|
|
$uri = 'tcp://'.$address.':'.$port;
|
|
|
|
}
|
|
|
|
if ($combo[1][0] === WssStream::class) {
|
2023-01-15 13:07:48 +01:00
|
|
|
$subdomain = $this->settings->getSslSubdomains()[\abs($dc_number)] ?? null;
|
2023-01-08 16:23:18 +01:00
|
|
|
if (!$subdomain) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (DataCenter::isMedia($dc_number)) {
|
|
|
|
$subdomain .= '-1';
|
|
|
|
}
|
|
|
|
$path = $this->settings->getTestMode() ? 'apiws_test' : 'apiws';
|
|
|
|
$uri = 'tcp://'.$subdomain.'.web.telegram.org:'.$port.'/'.$path;
|
|
|
|
} elseif ($combo[1][0] === WsStream::class) {
|
|
|
|
$path = $this->settings->getTestMode() ? 'apiws_test' : 'apiws';
|
2023-07-23 16:10:13 +02:00
|
|
|
$uri = 'tcp://'.$address.':'.$port.'/'.$path;
|
2023-01-08 16:23:18 +01:00
|
|
|
}
|
|
|
|
$ctx = (new ConnectionContext())
|
|
|
|
->setDc($dc_number)
|
|
|
|
->setCdn($this->isCdn($dc_number))
|
|
|
|
->setSocketContext($context)
|
|
|
|
->setUri($uri)
|
|
|
|
->setIpv6($ipv6 === 'ipv6');
|
|
|
|
foreach ($combo as $stream) {
|
|
|
|
if ($stream[0] === DefaultStream::class && $stream[1] === []) {
|
|
|
|
$stream[1] = $useDoH ? new DoHConnector($this->dohWrapper, $ctx) : $this->dohWrapper->dnsConnector;
|
|
|
|
}
|
2023-06-28 15:50:38 +02:00
|
|
|
if (\in_array($stream[0], [WsStream::class, WssStream::class], true) && $stream[1] === []) {
|
2023-01-08 16:23:18 +01:00
|
|
|
$stream[1] = $this->dohWrapper->webSocketConnector;
|
|
|
|
}
|
|
|
|
/** @var array{0: class-string, 1: mixed} $stream */
|
2023-01-14 19:18:23 +01:00
|
|
|
/** @psalm-suppress TooFewArguments Psalm bug */
|
2023-01-08 16:23:18 +01:00
|
|
|
$ctx->addStream(...$stream);
|
|
|
|
}
|
|
|
|
$ctxs[] = $ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 14:02:24 +01:00
|
|
|
if (empty($ctxs)) {
|
|
|
|
unset($this->sockets[$dc_number]);
|
2020-06-15 20:06:48 +02:00
|
|
|
$this->API->logger->logger("No info for DC {$dc_number}", Logger::ERROR);
|
2023-01-04 15:13:55 +01:00
|
|
|
} elseif (\defined('MADELINEPROTO_TEST') && \constant('MADELINEPROTO_TEST') === 'pony') {
|
2019-05-10 13:34:44 +02:00
|
|
|
return [$ctxs[0]];
|
|
|
|
}
|
Merge alpha into master (async, huge bugfixes and more) (#546)
* Implement async and lots of bugfixes
* Implement more async
* Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr
* Apply fixes from StyleCI
* Bugfixes
* Apply fixes from StyleCI
* Bugfixes, implement combined promises
* Apply fixes from StyleCI
* Support passing method arguments as callable
* Starting to write async upload logic
* Apply fixes from StyleCI
* Start implementing async file upload
* Apply fixes from StyleCI
* bugfix
* Apply fixes from StyleCI
* Start rewriting connection module
* Add PHP file docblocks for all classes
* Start working on new async stream API
* Finish writing stream API
* More stream API fixes
* Apply fixes from StyleCI
* Rewrite DataCenter and Connection modules
* Clean up stream API documentation
* Fixes
* Apply fixes from StyleCI
* Add referenced parameter to get length of buffer to read in getReadBuffer API
* Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar
* Start fixing reads
* Fix all protocol stream wrappers
* Apply fixes from StyleCI
* Implement disconnection, and remove end function
* Working async RPC
* Implement async file upload
* Bugfix
* Method recall bugfixes
* Bugfixes
* Trait bugfixes
* Fix FIFO buffer
* Bugfixes and speedtests
* Async logging
* Implement websocket streams
* Implement loop API, signal API, clean closing and start changing layer
* Small magna, websocket and HTTP fixes
* Clean up loop API
* Improved stack traces, 2FA and async
* Login fixes
* Added instructions for manual verification
* Small fixes
* More app info improvements
* More app info improvements
* TL and 2FA fixes
* Update to layer 89
* More bugfixes
* Implement broken media reporting
* Remove debug comments
* PHP 7.2 backwards compatibility
* Bugfixes
* Async key generation
* Some simplifications
* Transport fixes
* Cleanup
* async API
* Performance fixes
* Fixes to async API
* Bugfixes
* Implement one-time async loop
* Authorization and logging fixes
* Update to layer 91
* 7to5 fix
* Null coalesce conversion
* Implement socks5 proxy
* Implement HTTP proxy
* Fixes to HTTP proxy
* MTProxy and socks5 fixes
* Disable PHP 5 conversion
* Proxies have higher priority
* Avoid error handling in vendor
* Override composer dependencies
* Fix travis build
* Final composer fixes
* Proxy logic fixes
* Fix get_updates update handling
* Do not use parallel file driver if not supported
* Refactor loader and implement HTTP fixes
* Suppress errors in loader
* HTTP and authorization fixes
* HTTP fixes
* Improved peer management
* Use HTTP protocol on altervista
* Small bugfixes
* Minor fixes
* Docufix
* Docufix
* Legacy fixes
* Fix message queue
* Avoid updating if using MTProxy
* Improve logs and examples
* Trim final newlines while converting parse mode
* Reimplement noResponse flag
* Async combined event handler and APIFactory fixes
* Actually return config
* Case-insensitive methods
* Bugfix
* Apply fixes from StyleCI (#545)
* MTProxy fixes
* PHP 5 warning
* Improved PHP 5 warning
* Use <br> along with newlines in web logs
* Update docs
2018-12-26 20:51:14 +01:00
|
|
|
return $ctxs;
|
2016-10-09 17:42:39 +02:00
|
|
|
}
|
2019-11-02 13:49:58 +01:00
|
|
|
/**
|
2019-12-11 16:22:34 +01:00
|
|
|
* Get contents of file.
|
2019-11-02 13:49:58 +01:00
|
|
|
*
|
|
|
|
* @param string $url URL to fetch
|
|
|
|
*/
|
2023-01-04 12:37:12 +01:00
|
|
|
public function fileGetContents(string $url): string
|
2019-05-12 13:45:52 +02:00
|
|
|
{
|
2023-01-04 16:04:05 +01:00
|
|
|
return ($this->dohWrapper->HTTPClient->request(new Request($url)))->getBody()->buffer();
|
2019-05-12 13:45:52 +02:00
|
|
|
}
|
2019-09-01 23:39:29 +02:00
|
|
|
/**
|
2019-09-02 17:08:36 +02:00
|
|
|
* Get Connection instance for authorization.
|
2019-09-01 23:39:29 +02:00
|
|
|
*
|
2023-01-08 16:06:50 +01:00
|
|
|
* @param int $dc DC ID
|
2019-09-01 23:39:29 +02:00
|
|
|
*/
|
2023-01-08 16:06:50 +01:00
|
|
|
public function getAuthConnection(int $dc): Connection
|
2019-09-01 23:39:29 +02:00
|
|
|
{
|
|
|
|
return $this->sockets[$dc]->getAuthConnection();
|
|
|
|
}
|
2019-09-01 01:52:28 +02:00
|
|
|
/**
|
2019-09-01 14:07:04 +02:00
|
|
|
* Get Connection instance.
|
|
|
|
*
|
2023-01-08 16:06:50 +01:00
|
|
|
* @param int $dc DC ID
|
2019-09-01 01:52:28 +02:00
|
|
|
*/
|
2023-01-08 16:06:50 +01:00
|
|
|
public function getConnection(int $dc): Connection
|
2019-09-01 01:52:28 +02:00
|
|
|
{
|
|
|
|
return $this->sockets[$dc]->getConnection();
|
|
|
|
}
|
2019-12-29 14:04:02 +01:00
|
|
|
/**
|
|
|
|
* Get Connection instance asynchronously.
|
|
|
|
*
|
2023-01-08 16:06:50 +01:00
|
|
|
* @param int $dc DC ID
|
2019-12-29 14:04:02 +01:00
|
|
|
*/
|
2023-01-08 16:06:50 +01:00
|
|
|
public function waitGetConnection(int $dc): Connection
|
2019-12-29 14:04:02 +01:00
|
|
|
{
|
|
|
|
return $this->sockets[$dc]->waitGetConnection();
|
|
|
|
}
|
2019-09-01 01:52:28 +02:00
|
|
|
/**
|
2019-09-01 14:07:04 +02:00
|
|
|
* Get DataCenterConnection instance.
|
2019-09-01 01:52:28 +02:00
|
|
|
*
|
2023-01-08 16:06:50 +01:00
|
|
|
* @param int $dc DC ID
|
2019-09-01 14:07:04 +02:00
|
|
|
*/
|
2023-01-08 16:06:50 +01:00
|
|
|
public function getDataCenterConnection(int $dc): DataCenterConnection
|
2019-09-01 14:07:04 +02:00
|
|
|
{
|
|
|
|
return $this->sockets[$dc];
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get all DataCenterConnection instances.
|
|
|
|
*
|
2023-01-08 16:06:50 +01:00
|
|
|
* @return array<int, DataCenterConnection>
|
2019-09-01 14:07:04 +02:00
|
|
|
*/
|
|
|
|
public function getDataCenterConnections(): array
|
|
|
|
{
|
|
|
|
return $this->sockets;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Check if a DC is present.
|
|
|
|
*
|
2023-01-08 16:06:50 +01:00
|
|
|
* @param int $dc DC ID
|
2019-09-01 01:52:28 +02:00
|
|
|
*/
|
2023-01-08 16:06:50 +01:00
|
|
|
public function has(int $dc): bool
|
2019-09-01 01:52:28 +02:00
|
|
|
{
|
|
|
|
return isset($this->sockets[$dc]);
|
|
|
|
}
|
2019-09-17 21:35:53 +02:00
|
|
|
/**
|
|
|
|
* Check if connected to datacenter directly using IP address.
|
|
|
|
*
|
2023-01-08 16:06:50 +01:00
|
|
|
* @param int $datacenter DC ID
|
2019-09-17 21:35:53 +02:00
|
|
|
*/
|
2023-01-08 16:06:50 +01:00
|
|
|
public function byIPAddress(int $datacenter): bool
|
2019-09-17 21:35:53 +02:00
|
|
|
{
|
|
|
|
return $this->sockets[$datacenter]->byIPAddress();
|
|
|
|
}
|
2019-09-02 14:37:30 +02:00
|
|
|
/**
|
2019-09-17 22:42:38 +02:00
|
|
|
* Get all DC IDs.
|
2019-09-02 14:37:30 +02:00
|
|
|
*
|
2019-09-17 22:42:38 +02:00
|
|
|
* @param boolean $all Whether to get all possible DC IDs, or only connected ones
|
2019-09-02 14:37:30 +02:00
|
|
|
*/
|
2023-01-15 18:47:29 +01:00
|
|
|
public function getDcs(bool $all): array
|
2017-03-24 21:01:36 +01:00
|
|
|
{
|
2020-09-22 11:48:12 +02:00
|
|
|
$test = $this->settings->getTestMode() ? 'test' : 'main';
|
|
|
|
$ipv6 = $this->settings->getIpv6() ? 'ipv6' : 'ipv4';
|
2023-01-08 16:06:50 +01:00
|
|
|
return $all ? \array_keys($this->dclist[$test][$ipv6]) : \array_keys($this->sockets);
|
2017-03-24 21:01:08 +01:00
|
|
|
}
|
2016-10-09 17:42:53 +02:00
|
|
|
}
|