1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-22 19:11:16 +01:00

Share self-restart logic

This commit is contained in:
Daniil Gentili 2023-06-14 18:27:56 +02:00
parent 9914df223d
commit 5ae62f3574
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 27 additions and 35 deletions

View File

@ -89,33 +89,33 @@ final class WebRunner extends RunnerAbstract
'cwd' => Magic::getcwd(),
];
$params = \http_build_query($params);
self::selfStart(self::$runPath.'?'.\http_build_query($params));
return true;
}
public static function selfStart(string $uri): void {
$payload = "GET $uri HTTP/1.1\r\nHost: {$_SERVER['SERVER_NAME']}\r\n\r\n";
foreach ([(int) $_SERVER['SERVER_PORT'], null] as $portSuggested) {
foreach (($_SERVER['HTTPS'] ?? 'off') === 'on' ? ['tls', 'tcp'] : ['tcp', 'tls'] as $proto) {
$port = $portSuggested ?? ($proto === 'tls' ? 443 : 80);
try {
$address = $proto.'://'.$_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];
$res = \fsockopen($address, (int) $port);
break;
Logger::log("Successfully connected to {$address}:{$port}!");
Logger::log("Sending payload: $payload");
// We don't care for results or timeouts here, PHP doesn't count IOwait time as execution time anyway
// Technically should use amphp/socket, but I guess it's OK to not introduce another dependency just for a socket that will be used once.
\fwrite($res, $payload);
self::$resources []= $res;
} catch (Throwable $e) {
Logger::log("Error while connecting to ourselves: $e");
Logger::log("Error while sending to {$address}:{$port}: $e");
}
}
}
if (!isset($res)) {
throw new Exception('Could not connect to ourselves, please check the server configuration!');
}
$uri = self::$runPath.'?'.$params;
$payload = "GET $uri HTTP/1.1\r\nHost: {$_SERVER['SERVER_NAME']}\r\n\r\n";
Logger::log("Sending payload: $payload");
// We don't care for results or timeouts here, PHP doesn't count IOwait time as execution time anyway
// Technically should use amphp/socket, but I guess it's OK to not introduce another dependency just for a socket that will be used once.
\fwrite($res, $payload);
self::$resources []= $res;
return true;
}
}

View File

@ -23,6 +23,7 @@ namespace danog\MadelineProto\Wrappers;
use Amp\DeferredFuture;
use Amp\Future;
use danog\MadelineProto\Exception;
use danog\MadelineProto\Ipc\Runner\WebRunner;
use danog\MadelineProto\Logger;
use danog\MadelineProto\Settings;
use danog\MadelineProto\Shutdown;
@ -64,23 +65,14 @@ trait Loop
$this->logger->logger('Adding restart callback!');
$logger = $this->logger;
$id = Shutdown::addCallback(static function () use (&$logger): void {
$address = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];
$uri = $_SERVER['REQUEST_URI'];
$params = $_GET;
$params['MadelineSelfRestart'] = Tools::randomInt();
$url = \explode('?', $uri, 2)[0] ?? '';
$url = \explode('?', $_SERVER['REQUEST_URI'], 2)[0] ?? '';
$query = \http_build_query($params);
$uri = \implode('?', [$url, $query]);
$payload = $_SERVER['REQUEST_METHOD'].' '.$uri." HTTP/1.1\r\n".'Host: '.$_SERVER['SERVER_NAME']."\r\n\r\n";
$logger->logger("Connecting to {$address}:{$port}");
$a = \fsockopen($address, (int) $port);
$logger->logger('Sending self-restart payload');
$logger->logger($payload);
\fwrite($a, $payload);
WebRunner::selfStart("$url?$query");
$logger->logger("Payload sent with token {$params['MadelineSelfRestart']}, waiting for self-restart");
// Keep around resource for a bit more
$GLOBALS['MadelineShutdown'] = $a;
$logger->logger('Shutdown of self-restart callback');
}, 'restarter');
$this->logger->logger("Added restart callback with ID $id!");