2016-12-26 18:23:46 +01:00
|
|
|
<?php
|
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
|
|
|
/**
|
|
|
|
* Serialization 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>
|
2020-02-17 14:13:46 +01:00
|
|
|
* @copyright 2016-2020 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-25 17:50:03 +01:00
|
|
|
|
2016-12-26 18:23:46 +01:00
|
|
|
namespace danog\MadelineProto;
|
|
|
|
|
2020-09-24 11:45:20 +02:00
|
|
|
use Amp\Deferred;
|
2020-07-09 21:42:06 +02:00
|
|
|
use Amp\Loop;
|
2020-09-22 23:10:56 +02:00
|
|
|
use Amp\Promise;
|
|
|
|
use danog\MadelineProto\Ipc\Server;
|
2020-09-24 11:45:20 +02:00
|
|
|
use danog\MadelineProto\MTProtoSession\Session;
|
2020-07-09 21:42:06 +02:00
|
|
|
|
2020-02-25 18:02:32 +01:00
|
|
|
use function Amp\File\exists;
|
|
|
|
use function Amp\File\get;
|
2020-09-22 23:10:56 +02:00
|
|
|
use function Amp\Ipc\connect;
|
2020-02-25 18:02:32 +01:00
|
|
|
|
2016-12-26 18:23:46 +01:00
|
|
|
/**
|
2016-12-26 20:24:24 +01:00
|
|
|
* Manages serialization of the MadelineProto instance.
|
2016-12-26 18:23:46 +01:00
|
|
|
*/
|
2020-09-22 23:10:56 +02:00
|
|
|
abstract class Serialization
|
2016-12-26 20:24:24 +01:00
|
|
|
{
|
2020-02-25 18:02:32 +01:00
|
|
|
/**
|
2020-09-22 23:10:56 +02:00
|
|
|
* Header for session files.
|
|
|
|
*/
|
|
|
|
const PHP_HEADER = '<?php __HALT_COMPILER();';
|
|
|
|
/**
|
|
|
|
* Serialization version.
|
|
|
|
*/
|
|
|
|
const VERSION = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unserialize session.
|
|
|
|
*
|
|
|
|
* Logic for deserialization is as follows.
|
|
|
|
* - If the session is unlocked
|
|
|
|
* - Try starting IPC server:
|
|
|
|
* - Fetch light state
|
|
|
|
* - If don't need event handler
|
|
|
|
* - Unlock
|
|
|
|
* - Fork
|
|
|
|
* - Lock (fork)
|
|
|
|
* - Deserialize full (fork)
|
|
|
|
* - Start IPC server (fork)
|
|
|
|
* - Store IPC state (fork)
|
|
|
|
* - If need event handler
|
|
|
|
* - If have event handler class
|
|
|
|
* - Deserialize full
|
|
|
|
* - Start IPC server
|
|
|
|
* - Store IPC state
|
|
|
|
* - Else Fallthrough
|
|
|
|
* - Wait for a new IPC state for a maximum of 30 seconds, then throw
|
|
|
|
* - Execute original request via IPC
|
|
|
|
*
|
|
|
|
* - If the session is locked
|
|
|
|
* - In parallel (concurrent):
|
|
|
|
* - The IPC server should be running, connect
|
|
|
|
* - Try starting full session
|
|
|
|
* - Fetch light state
|
|
|
|
* - If don't need event handler
|
|
|
|
* - Wait lock
|
|
|
|
* - Unlock
|
|
|
|
* - Fork
|
|
|
|
* - Lock (fork)
|
|
|
|
* - Deserialize full (fork)
|
|
|
|
* - Start IPC server (fork)
|
|
|
|
* - Store IPC state (fork)
|
|
|
|
* - If need event handler and have event handler class
|
|
|
|
* - Wait lock
|
|
|
|
* - Deserialize full
|
|
|
|
* - Start IPC server
|
|
|
|
* - Store IPC state
|
|
|
|
* - Wait for a new IPC session for a maximum of 30 seconds, then throw
|
|
|
|
* - Execute original request via IPC
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* - If receiving a startAndLoop or setEventHandler request on an IPC session:
|
|
|
|
* - Shutdown remote IPC server
|
|
|
|
* - Deserialize full
|
|
|
|
* - Start IPC server
|
|
|
|
* - Store IPC state
|
2020-02-25 18:02:32 +01:00
|
|
|
*
|
2020-09-24 11:45:20 +02:00
|
|
|
* @param SessionPaths $session Session name
|
|
|
|
* @param bool $forceFull Whether to force full session deserialization
|
2020-02-25 18:02:32 +01:00
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*/
|
2020-09-24 11:45:20 +02:00
|
|
|
public static function unserialize(SessionPaths $session, bool $forceFull = false): \Generator
|
2020-02-25 18:02:32 +01:00
|
|
|
{
|
2020-09-22 17:30:07 +02:00
|
|
|
if (yield exists($session->getSessionPath())) {
|
2020-09-22 23:10:56 +02:00
|
|
|
// Is new session
|
|
|
|
$isNew = true;
|
|
|
|
} elseif (yield exists($session->getLegacySessionPath())) {
|
|
|
|
// Is old session
|
|
|
|
$isNew = false;
|
|
|
|
} else {
|
|
|
|
// No session exists yet, lock for when we create it
|
|
|
|
return [null, yield Tools::flock($session->getLockPath(), LOCK_EX, 1)];
|
|
|
|
}
|
|
|
|
|
2020-09-23 00:57:49 +02:00
|
|
|
//Logger::log('Waiting for exclusive session lock...');
|
2020-09-22 23:10:56 +02:00
|
|
|
$warningId = Loop::delay(1000, static function () use (&$warningId) {
|
|
|
|
Logger::log("It seems like the session is busy.");
|
|
|
|
if (\defined(\MADELINE_WORKER::class)) {
|
|
|
|
Logger::log("Exiting since we're in a worker");
|
|
|
|
Magic::shutdown(1);
|
|
|
|
}
|
|
|
|
Logger::log("Telegram does not support starting multiple instances of the same session, make sure no other instance of the session is running.");
|
|
|
|
$warningId = Loop::repeat(5000, fn () => Logger::log('Still waiting for exclusive session lock...'));
|
|
|
|
Loop::unreference($warningId);
|
|
|
|
});
|
|
|
|
Loop::unreference($warningId);
|
|
|
|
|
|
|
|
$lightState = null;
|
2020-09-24 11:45:20 +02:00
|
|
|
$cancelFlock = new Deferred;
|
|
|
|
$cancelIpc = new Deferred;
|
2020-09-22 23:10:56 +02:00
|
|
|
$canContinue = true;
|
|
|
|
$ipcSocket = null;
|
2020-09-24 11:45:20 +02:00
|
|
|
$unlock = yield from Tools::flockGenerator($session->getLockPath(), LOCK_EX, 1, $cancelFlock->promise(), $forceFull ? null : static function () use ($session, $cancelFlock, $cancelIpc, &$canContinue, &$ipcSocket, &$lightState) {
|
|
|
|
$ipcSocket = Tools::call(self::tryConnect($session->getIpcPath(), $cancelIpc->promise(), $cancelFlock));
|
|
|
|
$session->getLightState()->onResolve(static function (?\Throwable $e, ?LightState $res) use ($cancelFlock, &$canContinue, &$lightState) {
|
2020-09-22 23:10:56 +02:00
|
|
|
if ($res) {
|
|
|
|
$lightState = $res;
|
|
|
|
if (!$res->canStartIpc()) {
|
|
|
|
$canContinue = false;
|
2020-09-24 11:45:20 +02:00
|
|
|
$cancelFlock->resolve(true);
|
2020-09-22 23:10:56 +02:00
|
|
|
}
|
2020-09-23 00:57:49 +02:00
|
|
|
} else {
|
|
|
|
$lightState = false;
|
2020-07-12 01:12:20 +02:00
|
|
|
}
|
2020-07-09 21:42:06 +02:00
|
|
|
});
|
2020-09-22 23:10:56 +02:00
|
|
|
});
|
|
|
|
Loop::cancel($warningId);
|
|
|
|
|
|
|
|
if (!$unlock) { // Canceled, don't have lock
|
2020-09-23 00:57:49 +02:00
|
|
|
return $ipcSocket;
|
2020-09-22 23:10:56 +02:00
|
|
|
}
|
2020-09-23 00:57:49 +02:00
|
|
|
if (!$canContinue) { // Have lock, can't use it
|
2020-09-24 11:45:20 +02:00
|
|
|
Logger::log("Session has event handler, but it's not started.", Logger::ERROR);
|
|
|
|
Logger::log("We don't have access to the event handler class, so we can't start it.", Logger::ERROR);
|
|
|
|
Logger::log("Please start the event handler or unset it to use the IPC server.", Logger::ERROR);
|
2020-09-22 23:10:56 +02:00
|
|
|
$unlock();
|
2020-09-23 00:57:49 +02:00
|
|
|
return $ipcSocket;
|
2020-09-22 23:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
/** @var LightState */
|
2020-09-24 11:45:20 +02:00
|
|
|
$lightState ??= yield $session->getLightState();
|
2020-09-22 23:10:56 +02:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
}
|
|
|
|
|
2020-09-24 11:45:20 +02:00
|
|
|
if ($lightState && !$forceFull) {
|
2020-09-22 23:10:56 +02:00
|
|
|
if (!$class = $lightState->getEventHandler()) {
|
|
|
|
// Unlock and fork
|
2020-09-22 17:30:07 +02:00
|
|
|
$unlock();
|
2020-09-24 11:45:20 +02:00
|
|
|
$cancelIpc->resolve(Server::startMe($session));
|
|
|
|
return $ipcSocket ?? yield from self::tryConnect($session->getIpcPath(), $cancelIpc->promise());
|
2020-09-22 23:10:56 +02:00
|
|
|
} elseif (!\class_exists($class)) {
|
2020-09-24 11:45:20 +02:00
|
|
|
// Have lock, can't use it
|
|
|
|
$unlock();
|
|
|
|
Logger::log("Session has event handler, but it's not started.", Logger::ERROR);
|
|
|
|
Logger::log("We don't have access to the event handler class, so we can't start it.", Logger::ERROR);
|
|
|
|
Logger::log("Please start the event handler or unset it to use the IPC server.", Logger::ERROR);
|
|
|
|
return $ipcSocket ?? yield from self::tryConnect($session->getIpcPath(), $cancelIpc->promise());
|
2020-09-22 23:10:56 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-09 21:42:06 +02:00
|
|
|
|
2020-09-22 23:10:56 +02:00
|
|
|
$tempId = Shutdown::addCallback($unlock = static function () use ($unlock) {
|
|
|
|
Logger::log("Unlocking exclusive session lock!");
|
|
|
|
$unlock();
|
|
|
|
Logger::log("Unlocked exclusive session lock!");
|
|
|
|
});
|
|
|
|
Logger::log("Got exclusive session lock!");
|
2020-09-22 17:30:07 +02:00
|
|
|
|
2020-09-22 23:10:56 +02:00
|
|
|
if ($isNew) {
|
2020-09-24 11:45:20 +02:00
|
|
|
$unserialized = yield from $session->unserialize();
|
2020-09-22 23:10:56 +02:00
|
|
|
} else {
|
2020-09-23 00:57:49 +02:00
|
|
|
$unserialized = yield from self::legacyUnserialize($session->getLegacySessionPath());
|
2020-09-22 23:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($unserialized === false) {
|
|
|
|
throw new Exception(\danog\MadelineProto\Lang::$current_lang['deserialization_error']);
|
|
|
|
}
|
|
|
|
|
|
|
|
Shutdown::removeCallback($tempId);
|
|
|
|
return [$unserialized, $unlock];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try connecting to IPC socket.
|
|
|
|
*
|
2020-09-24 11:45:20 +02:00
|
|
|
* @param string $ipcPath IPC path
|
|
|
|
* @param Promise $cancelConnect Cancelation token (triggers cancellation of connection)
|
|
|
|
* @param ?Deferred $cancelFull Cancelation token source (can trigger cancellation of full unserialization)
|
2020-09-22 23:10:56 +02:00
|
|
|
*
|
2020-09-24 11:45:20 +02:00
|
|
|
* @return \Generator
|
2020-09-22 23:10:56 +02:00
|
|
|
*/
|
2020-09-24 11:45:20 +02:00
|
|
|
private static function tryConnect(string $ipcPath, Promise $cancelConnect, ?Deferred $cancelFull = null): \Generator
|
2020-09-22 23:10:56 +02:00
|
|
|
{
|
|
|
|
for ($x = 0; $x < 30; $x++) {
|
|
|
|
Logger::log("Trying to connect to IPC socket...");
|
2020-02-25 18:02:32 +01:00
|
|
|
try {
|
2020-09-22 23:10:56 +02:00
|
|
|
\clearstatcache(true, $ipcPath);
|
|
|
|
$socket = yield connect($ipcPath);
|
2020-09-24 11:45:20 +02:00
|
|
|
if ($cancelFull) {
|
|
|
|
$cancelFull->resolve(true);
|
2020-02-25 18:02:32 +01:00
|
|
|
}
|
2020-09-23 00:57:49 +02:00
|
|
|
return [$socket, null];
|
2020-02-25 18:02:32 +01:00
|
|
|
} catch (\Throwable $e) {
|
2020-09-22 23:10:56 +02:00
|
|
|
$e = $e->getMessage();
|
|
|
|
Logger::log("$e while connecting to IPC socket");
|
|
|
|
}
|
2020-09-24 11:45:20 +02:00
|
|
|
if ($res = yield Tools::timeoutWithDefault($cancelConnect, 1000, null)) {
|
|
|
|
if ($res instanceof \Throwable) {
|
|
|
|
return [$res, null];
|
|
|
|
}
|
|
|
|
$cancelConnect = (new Deferred)->promise();
|
|
|
|
}
|
2020-09-22 23:10:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserialize legacy session.
|
|
|
|
*
|
2020-09-23 00:57:49 +02:00
|
|
|
* @param string $session
|
2020-09-22 23:10:56 +02:00
|
|
|
* @return \Generator
|
|
|
|
*/
|
2020-09-23 00:57:49 +02:00
|
|
|
private static function legacyUnserialize(string $session): \Generator
|
2020-09-22 23:10:56 +02:00
|
|
|
{
|
2020-09-23 00:57:49 +02:00
|
|
|
$tounserialize = yield get($session);
|
2020-09-22 23:10:56 +02:00
|
|
|
|
|
|
|
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\\TLMethods', 'TL\\TLConstructors', '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) {
|
|
|
|
if ($e->getFile() === 'MadelineProto' && $e->getLine() === 1) {
|
2020-02-25 18:02:32 +01:00
|
|
|
throw $e;
|
|
|
|
}
|
2020-09-22 23:10:56 +02:00
|
|
|
if (@\constant("MADELINEPROTO_TEST") === 'pony') {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
\class_exists('\\Volatile');
|
|
|
|
foreach (['RSA', 'TL\\TLMethods', 'TL\\TLConstructors', 'MTProto', 'API', 'DataCenter', 'Connection', 'TL\\Types\\Button', 'TL\\Types\\Bytes', 'APIFactory'] as $class) {
|
|
|
|
\class_exists('\\danog\\MadelineProto\\'.$class);
|
|
|
|
}
|
|
|
|
$changed = false;
|
|
|
|
if (\strpos($tounserialize, 'O:26:"danog\\MadelineProto\\Button":') !== false) {
|
|
|
|
Logger::log("SUBBING BUTTONS!");
|
|
|
|
$tounserialize = \str_replace('O:26:"danog\\MadelineProto\\Button":', 'O:35:"danog\\MadelineProto\\TL\\Types\\Button":', $tounserialize);
|
|
|
|
$changed = true;
|
|
|
|
}
|
|
|
|
if (\strpos($e->getMessage(), "Erroneous data format for unserializing 'phpseclib\\Math\\BigInteger'") === 0) {
|
|
|
|
Logger::log("SUBBING BIGINTEGOR!");
|
|
|
|
$tounserialize = \str_replace('phpseclib\\Math\\BigInteger', 'phpseclib\\Math\\BigIntegor', $tounserialize);
|
|
|
|
$changed = true;
|
2020-02-25 18:02:32 +01:00
|
|
|
}
|
2020-09-22 23:10:56 +02:00
|
|
|
if (\strpos($tounserialize, 'C:25:"phpseclib\\Math\\BigInteger"') !== false) {
|
|
|
|
Logger::log("SUBBING TGSECLIB old!");
|
|
|
|
$tounserialize = \str_replace('C:25:"phpseclib\\Math\\BigInteger"', 'C:24:"tgseclib\\Math\\BigInteger"', $tounserialize);
|
|
|
|
$changed = true;
|
2020-02-25 18:02:32 +01:00
|
|
|
}
|
2020-09-22 23:10:56 +02:00
|
|
|
if (\strpos($tounserialize, 'C:26:"phpseclib3\\Math\\BigInteger"') !== false) {
|
|
|
|
Logger::log("SUBBING TGSECLIB!");
|
|
|
|
$tounserialize = \str_replace('C:26:"phpseclib3\\Math\\BigInteger"', 'C:24:"tgseclib\\Math\\BigInteger"', $tounserialize);
|
|
|
|
$changed = true;
|
|
|
|
}
|
|
|
|
Logger::log((string) $e, Logger::ERROR);
|
|
|
|
if (!$changed) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$unserialized = \danog\Serialization::unserialize($tounserialize);
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
$unserialized = \unserialize($tounserialize);
|
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
Logger::log((string) $e, Logger::ERROR);
|
|
|
|
throw $e;
|
2020-02-25 18:02:32 +01:00
|
|
|
}
|
2020-09-22 23:10:56 +02:00
|
|
|
if ($unserialized instanceof \danog\PlaceHolder) {
|
|
|
|
$unserialized = \danog\Serialization::unserialize($tounserialize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $unserialized;
|
2020-02-25 18:02:32 +01:00
|
|
|
}
|
2018-02-25 17:50:03 +01:00
|
|
|
}
|