1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 05:34:42 +01:00

Support for oldstyle CGI

This commit is contained in:
Daniil Gentili 2021-05-12 20:43:55 +02:00
parent be6db61966
commit 17286f3f1b
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
7 changed files with 65 additions and 31 deletions

View File

@ -197,6 +197,7 @@ class API extends InternalDoc
if (yield from $this->connectToMadelineProto($settings)) {
return; // OK
}
if (!$settings instanceof Settings) {
$newSettings = new Settings;
$newSettings->merge($settings);
@ -234,7 +235,7 @@ class API extends InternalDoc
try {
if (!isset($_GET['MadelineSelfRestart']) && ((yield $this->hasEventHandler()) || !(yield $this->isIpcWorker()))) {
$this->logger->logger("Restarting to full instance: the bot is already running!");
MTProto::closeConnection('The bot is already running!');
Tools::closeConnection(yield $this->getWebMessage("The bot is already running!"));
return false;
}
$this->logger->logger("Restarting to full instance: stopping IPC server...");
@ -498,6 +499,7 @@ class API extends InternalDoc
{
$this->async(true);
yield $this->start();
if (!yield from $this->reconnectFull()) {
return;
}
@ -505,7 +507,6 @@ class API extends InternalDoc
$errors = [];
while (true) {
try {
yield $this->start();
yield $this->setEventHandler($eventHandler);
$started = true;
return yield from $this->API->loop();

View File

@ -3,6 +3,7 @@
namespace danog\MadelineProto\Ipc\Runner;
use danog\MadelineProto\Logger;
use danog\MadelineProto\Magic;
final class ProcessRunner extends RunnerAbstract
{
@ -52,8 +53,14 @@ final class ProcessRunner extends RunnerAbstract
]);
Logger::log("Starting process with $command");
$params = [
'argv' => ['madeline-ipc', $session, $startupId],
'cwd' => Magic::getcwd()
];
$params = \http_build_query($params);
$pipes = [];
self::$resources []= \proc_open($command, [], $pipes);
self::$resources []= \proc_open($command, [], $pipes, null, ['QUERY_STRING' => $params]);
}
private static function locateBinary(): string
{

View File

@ -39,6 +39,10 @@ use danog\MadelineProto\Tools;
class Server extends SignalLoop
{
use InternalLoop;
/**
* Server version.
*/
const VERSION = 1;
/**
* Shutdown server.
*/

View File

@ -1336,6 +1336,7 @@ class MTProto extends AsyncConstruct implements TLCallback
}
if (isset($this->datacenter)) {
foreach ($this->datacenter->getDataCenterConnections() as $datacenter) {
$datacenter->setExtra($this);
$datacenter->disconnect();
}
}
@ -1886,6 +1887,25 @@ class MTProto extends AsyncConstruct implements TLCallback
{
return (bool) $this->reportDest;
}
/**
* Get a message to show to the user when starting the bot.
*
* @param string $message
*/
public function getWebMessage(string $message): string
{
Logger::log($message);
$warning = '';
if (!$this->hasReportPeers() && $this->hasEventHandler()) {
Logger::log("!!! Warning: no report peers are set, please add the following method to your event handler !!!", Logger::FATAL_ERROR);
Logger::log("!!! public function getReportPeers() { return '@yourtelegramusername'; } !!!", Logger::FATAL_ERROR);
$warning .= "<h2 style='color:red;'>Warning: no report peers are set, please add the following method to your event handler:</h2>";
$warning .= "<code>public function getReportPeers() { return '@yourtelegramusername'; }</code>";
}
return "<html><body><h1>$message</h1>$warning</body></html>";
}
/**
* Set peer(s) where to send errors occurred in the event loop.
*

View File

@ -187,6 +187,11 @@ abstract class Serialization
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());
}
} elseif (!$forceFull) {
// Unlock and fork
$unlock();
$cancelIpc->resolve(Server::startMe($session));
return $ipcSocket ?? yield from self::tryConnect($session->getIpcPath(), $cancelIpc->promise());
}
$tempId = Shutdown::addCallback($unlock = static function () use ($unlock) {

View File

@ -867,6 +867,30 @@ abstract class Tools extends StrTools
$header[165] = $stripped[2];
return $header.\substr($stripped, 3).$footer;
}
/**
* Close connection with client, connected via web.
*
* @param string $message Message
*
* @return void
*/
public static function closeConnection($message)
{
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' || isset($GLOBALS['exited']) || \headers_sent() || isset($_GET['MadelineSelfRestart']) || Magic::$isIpcWorker) {
return;
}
$buffer = @\ob_get_clean() ?: '';
$buffer .= $message;
\ignore_user_abort(true);
\header('Connection: close');
\header('Content-Type: text/html');
echo $buffer;
\flush();
$GLOBALS['exited'] = true;
if (\function_exists('fastcgi_finish_request')) {
\fastcgi_finish_request();
}
}
/**
* Get maximum photo size.
*

View File

@ -21,8 +21,6 @@ namespace danog\MadelineProto\Wrappers;
use Amp\Loop as AmpLoop;
use Amp\Promise;
use danog\MadelineProto\Logger;
use danog\MadelineProto\Magic;
use danog\MadelineProto\Settings;
use danog\MadelineProto\Shutdown;
@ -87,7 +85,7 @@ trait Loop
$this->logger->logger("Added restart callback with ID $id!");
}
$this->logger->logger("Done webhost init process!");
$this->closeConnection('Bot was started');
Tools::closeConnection($this->getWebMessage("The bot was started!"));
$inited = true;
}
}
@ -181,29 +179,4 @@ trait Loop
{
return Tools::callFork($this->loop());
}
/**
* Close connection with client, connected via web.
*
* @param string $message Message
*
* @return void
*/
public static function closeConnection($message = 'OK!')
{
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' || isset($GLOBALS['exited']) || \headers_sent() || isset($_GET['MadelineSelfRestart']) || Magic::$isIpcWorker) {
return;
}
Logger::log($message);
$buffer = @\ob_get_clean() ?: '';
$buffer .= '<html><body><h1>'.\htmlentities($message).'</h1></body></html>';
\ignore_user_abort(true);
\header('Connection: close');
\header('Content-Type: text/html');
echo $buffer;
\flush();
$GLOBALS['exited'] = true;
if (\function_exists('fastcgi_finish_request')) {
\fastcgi_finish_request();
}
}
}