This commit is contained in:
Daniil Gentili 2019-07-01 13:14:56 +02:00
parent c451471f78
commit 53bf8176f5
11 changed files with 51 additions and 17 deletions

View File

@ -215,7 +215,13 @@ class Connection
$this->disconnect(); $this->disconnect();
yield $this->API->datacenter->dcConnectAsync($this->ctx->getDc()); yield $this->API->datacenter->dcConnectAsync($this->ctx->getDc());
if ($this->API->hasAllAuth() && !$this->hasPendingCalls()) { if ($this->API->hasAllAuth() && !$this->hasPendingCalls()) {
$this->callFork($this->API->method_call_async_read('ping', ['ping_id' => $this->random_int()], ['datacenter' => $this->datacenter])); $this->callFork((function () {
try {
$this->API->method_call_async_read('ping', ['ping_id' => $this->random_int()], ['datacenter' => $this->datacenter]);
} catch (\Throwable $e) {
$this->API->logger("Got an error while pinging on reconnect: $e", Logger::FATAL_ERROR);
}
})());
} }
} }

View File

@ -181,6 +181,8 @@ class Logger
{ {
if (!is_null(self::$default)) { if (!is_null(self::$default)) {
self::$default->logger($param, $level, basename(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['file'], '.php')); self::$default->logger($param, $level, basename(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['file'], '.php'));
} else {
echo $param.PHP_EOL;
} }
} }

View File

@ -58,7 +58,7 @@ class ReadLoop extends SignalLoop
while (true) { while (true) {
try { try {
$error = yield $this->waitSignal($this->readMessage()); $error = yield $this->waitSignal($this->readMessage());
} catch (NothingInTheSocketException | StreamException | PendingReadError $e) { } catch (NothingInTheSocketException | StreamException | PendingReadError | \Error $e) {
if (isset($connection->old)) { if (isset($connection->old)) {
return; return;
} }

View File

@ -302,7 +302,6 @@ class MTProto extends AsyncConstruct implements TLCallback
public function __wakeup_async($backtrace) public function __wakeup_async($backtrace)
{ {
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']); set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
set_exception_handler(['\\danog\\MadelineProto\\Serialization', 'serialize_all']);
$this->setup_logger(); $this->setup_logger();
if (\danog\MadelineProto\Magic::$has_thread && is_object(\Thread::getCurrentThread())) { if (\danog\MadelineProto\Magic::$has_thread && is_object(\Thread::getCurrentThread())) {
return; return;

View File

@ -66,7 +66,7 @@ class Magic
public static function class_exists() public static function class_exists()
{ {
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']); set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
//set_exception_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionHandler']); set_exception_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionHandler']);
if (!self::$inited) { if (!self::$inited) {
self::$has_thread = class_exists('\\Thread') && method_exists('\\Thread', 'getCurrentThread'); self::$has_thread = class_exists('\\Thread') && method_exists('\\Thread', 'getCurrentThread');
self::$BIG_ENDIAN = pack('L', 1) === pack('N', 1); self::$BIG_ENDIAN = pack('L', 1) === pack('N', 1);

View File

@ -87,7 +87,7 @@ class RPCErrorException extends \Exception
|| !isset(self::$descriptions[$error]) || !isset(self::$descriptions[$error])
|| $code === 500 || $code === 500
) { ) {
$res = json_decode(@file_get_contents('https://rpc.pwrtelegram.xyz/?method='.$this->caller.'&code='.$this->code.'&error='.$this->rpc), true); $res = json_decode(@file_get_contents('https://rpc.pwrtelegram.xyz/?method='.$method.'&code='.$code.'&error='.$error), true);
if (isset($res['ok']) && $res['ok']) { if (isset($res['ok']) && $res['ok']) {
$description = $res['result']; $description = $res['result'];

View File

@ -24,11 +24,6 @@ namespace danog\MadelineProto;
*/ */
class Serialization class Serialization
{ {
public static function serialize_all($exception)
{
echo $exception.PHP_EOL;
}
public static function realpaths($file) public static function realpaths($file)
{ {
$file = Absolute::absolute($file); $file = Absolute::absolute($file);

View File

@ -1050,17 +1050,20 @@ invokeWithTakeout#aca9fd2e {X:Type} takeout_id:long query:!X = X;
auth.sendCode#a677244f phone_number:string api_id:int api_hash:string settings:CodeSettings = auth.SentCode; auth.sendCode#a677244f phone_number:string api_id:int api_hash:string settings:CodeSettings = auth.SentCode;
auth.signUp#1b067634 phone_number:string phone_code_hash:string phone_code:string first_name:string last_name:string = auth.Authorization; auth.signUp#1b067634 phone_number:string phone_code_hash:string phone_code:string first_name:string last_name:string = auth.Authorization;
auth.signIn#bcd51581 phone_number:string phone_code_hash:string phone_code:string = auth.Authorization; auth.signIn#bcd51581 phone_number:string phone_code_hash:string phone_code:string = auth.Authorization;
auth.resendCode#3ef1a9bf phone_number:string phone_code_hash:string = auth.SentCode;
auth.cancelCode#1f040578 phone_number:string phone_code_hash:string = Bool;
auth.logOut#5717da40 = Bool; auth.logOut#5717da40 = Bool;
auth.resetAuthorizations#9fab0d1a = Bool; auth.resetAuthorizations#9fab0d1a = Bool;
auth.exportAuthorization#e5bfffcd dc_id:int = auth.ExportedAuthorization; auth.exportAuthorization#e5bfffcd dc_id:int = auth.ExportedAuthorization;
auth.importAuthorization#e3ef9613 id:int bytes:bytes = auth.Authorization; auth.importAuthorization#e3ef9613 id:int bytes:bytes = auth.Authorization;
auth.bindTempAuthKey#cdd42a05 perm_auth_key_id:long nonce:long expires_at:int encrypted_message:bytes = Bool; auth.bindTempAuthKey#cdd42a05 perm_auth_key_id:long nonce:long expires_at:int encrypted_message:bytes = Bool;
auth.importBotAuthorization#67a3ff2c flags:int api_id:int api_hash:string bot_auth_token:string = auth.Authorization; auth.importBotAuthorization#67a3ff2c flags:int api_id:int api_hash:string bot_auth_token:string = auth.Authorization;
auth.checkPassword#d18b4d16 password:InputCheckPasswordSRP = auth.Authorization; auth.checkPassword#d18b4d16 password:InputCheckPasswordSRP = auth.Authorization;
auth.requestPasswordRecovery#d897bc66 = auth.PasswordRecovery; auth.requestPasswordRecovery#d897bc66 = auth.PasswordRecovery;
auth.recoverPassword#4ea56e92 code:string = auth.Authorization; auth.recoverPassword#4ea56e92 code:string = auth.Authorization;
auth.resendCode#3ef1a9bf phone_number:string phone_code_hash:string = auth.SentCode;
auth.cancelCode#1f040578 phone_number:string phone_code_hash:string = Bool;
auth.dropTempAuthKeys#8e48a188 except_auth_keys:Vector<long> = Bool; auth.dropTempAuthKeys#8e48a188 except_auth_keys:Vector<long> = Bool;
account.registerDevice#5cbea590 token_type:int token:string app_sandbox:Bool secret:bytes other_uids:Vector<int> = Bool; account.registerDevice#5cbea590 token_type:int token:string app_sandbox:Bool secret:bytes other_uids:Vector<int> = Bool;

View File

@ -191,6 +191,7 @@ trait Tools
try { try {
Loop::run(function () use (&$resolved, &$value, &$exception, $promise) { Loop::run(function () use (&$resolved, &$value, &$exception, $promise) {
$promise->onResolve(function ($e, $v) use (&$resolved, &$value, &$exception) { $promise->onResolve(function ($e, $v) use (&$resolved, &$value, &$exception) {
Loop::stop(); Loop::stop();
$resolved = true; $resolved = true;
$exception = $e; $exception = $e;
@ -329,7 +330,7 @@ trait Tools
return; return;
} }
$b = self::call($b()); $b = self::call($b());
$b->onResolve(static function ($e, $res) use ($deferred) { $b->onResolve(function ($e, $res) use ($deferred) {
if ($e) { if ($e) {
if (isset($this)) { if (isset($this)) {
$this->rethrow($e, $file); $this->rethrow($e, $file);

View File

@ -68,6 +68,9 @@ trait Loop
} catch (\danog\MadelineProto\Exception $e) { } catch (\danog\MadelineProto\Exception $e) {
$needs_restart = true; $needs_restart = true;
} }
if (isset($_REQUEST['MadelineSelfRestart'])) {
$this->logger->logger("Self-restarted, restart token ".$_REQUEST['MadelineSelfRestart']);
}
$this->logger->logger($needs_restart ? 'Will self-restart' : 'Will not self-restart'); $this->logger->logger($needs_restart ? 'Will self-restart' : 'Will not self-restart');
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
@ -105,9 +108,31 @@ trait Loop
if ($needs_restart) { if ($needs_restart) {
$logger = &$this->logger; $logger = &$this->logger;
Shutdown::addCallback(static function () use (&$logger) { Shutdown::addCallback(static function () use (&$logger) {
$a = fsockopen((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT']); $address = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME'];
fwrite($a, $_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI'].' '.$_SERVER['SERVER_PROTOCOL']."\r\n".'Host: '.$_SERVER['SERVER_NAME']."\r\n\r\n"); $port = $_SERVER['SERVER_PORT'];
$logger->logger('Self-restarted');
$uri = $_SERVER['REQUEST_URI'];
$params = $_GET;
$params['MadelineSelfRestart'] = $this->random_int();
list($url, $query) = explode($uri, '?', 2);
$query = http_build_query($params);
$uri = implode('?', [$url, $query]);
$payload = $_SERVER['REQUEST_METHOD'].' '.$uri.' '.$_SERVER['SERVER_PROTOCOL']."\r\n".'Host: '.$_SERVER['SERVER_NAME']."\r\n\r\n";
$logger->logger("Connecting to $address:$port");
$a = fsockopen($address, $port);
$logger->logger("Sending self-restart payload");
$logger->logger($payload);
fwrite($a, $payload);
$logger->logger("Payload sent with token {$params['MadelineSelfRestart']}, waiting for self-restart");
sleep(10);
fclose($a);
}, 'restarter'); }, 'restarter');
} }
@ -164,5 +189,8 @@ trait Loop
ob_end_flush(); ob_end_flush();
flush(); flush();
$GLOBALS['exited'] = true; $GLOBALS['exited'] = true;
if (function_exists('fastcgi_finish_request')) {
\fastcgi_finish_request();
}
} }
} }