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

Localize errors

This commit is contained in:
Daniil Gentili 2023-07-10 19:56:44 +02:00
parent e7d8c746cc
commit 400590edd2
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
6 changed files with 67 additions and 34 deletions

View File

@ -1,4 +1,11 @@
{
"noReportPeers": "Warning: no report peers are set, please add the following method to your event handler",
"manualAdminActionRequired": "!!!!!!!!! MANUAL SYSTEM ADMIN ACTION REQUIRED !!!!!!!!!",
"mmapError": "The maximum number of mmap'ed regions was reached (%s): please increase the vm.max_map_count kernel config to 262144 to fix.\nTo fix, run the following command as root: echo 262144 | sudo tee /proc/sys/vm/max_map_count\nTo persist the change across reboots: echo vm.max_map_count=262144 | sudo tee /etc/sysctl.d/40-madelineproto.conf\nOn Windows and WSL, increasing the size of the pagefile might help; please switch to native Linux if the issue persists.",
"extensionRequired": "MadelineProto requires the %s extension to run. %s",
"extensionRequiredInstallWithApt": "Try running sudo apt-get install %s.",
"extensionRequiredInstallWithCustomInstructions": "Follow the instructions at %s to install it.",
"extensionRecommended": "Warning: the %s extension is not installed, please install it to speed up MadelineProto!",
"go": "Go",
"apiChooseManualAutoTip": "Note that you can also provide the API ID\/hash directly in the code using the settings: %s",
"apiChooseManualAutoTipWeb": "Note that you can also provide the API ID\/hash directly in the code using the <a target=\"_blank\" href=\"%s\">settings<\/a>.",

View File

@ -346,7 +346,7 @@ abstract class EventHandler extends AbstractAPI
Assert::true(\is_subclass_of($plugin, PluginEventHandler::class), "$plugin must extend ".PluginEventHandler::class);
Assert::notEq($plugin, PluginEventHandler::class);
Assert::true(\str_contains(\ltrim($plugin, '\\'), '\\'), "$plugin must be in a namespace!");
self::validatePlugin($plugin);
self::validateEventHandler($plugin);
}
return $plugins;
@ -373,7 +373,14 @@ abstract class EventHandler extends AbstractAPI
PDO::class,
mysqli::class,
];
private static function validatePlugin(string $class): void
/**
* Perform static analysis on a certain event handler class, to make sure it satisfies some performance requirements.
*
* @param class-string<EventHandler> $class Class name
*
* @throws AssertionError If validation fails.
*/
final public static function validateEventHandler(string $class): void
{
$file = read((new ReflectionClass($class))->getFileName());
$file = (new ParserFactory)->create(ParserFactory::ONLY_PHP7)->parse($file);
@ -384,13 +391,13 @@ abstract class EventHandler extends AbstractAPI
/** @var DeclareDeclare|null $call */
$declare = $finder->findFirstInstanceOf($file, DeclareDeclare::class);
Assert::true(
$declare !== null
&& $declare->key->name === 'strict_types'
&& $declare->value instanceof LNumber
&& $declare->value->value === 1,
"An error occurred while analyzing plugin $class: for performance reasons, the first statement of a plugin must be declare(strict_types=1);"
);
if ($declare === null
|| $declare->key->name !== 'strict_types'
|| !$declare->value instanceof LNumber
|| $declare->value->value !== 1
) {
throw new AssertionError("An error occurred while analyzing plugin $class: for performance reasons, the first statement of a plugin must be declare(strict_types=1);");
}
/** @var FuncCall $call */
foreach ($finder->findInstanceOf($file, FuncCall::class) as $call) {

View File

@ -59,15 +59,16 @@ class Exception extends \Exception
*/
public static function extension(string $extensionName): self
{
$additional = 'Try running sudo apt-get install php'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'-'.$extensionName.'.';
if ($extensionName === 'libtgvoip') {
$additional = 'Follow the instructions @ https://voip.madelineproto.xyz to install it.';
$additional = \sprintf(Lang::$current_lang['extensionRequiredInstallWithCustomInstructions'], 'https://voip.madelineproto.xyz');
} elseif ($extensionName === 'prime') {
$additional = 'Follow the instructions @ https://prime.madelineproto.xyz to install it.';
$additional = \sprintf(Lang::$current_lang['extensionRequiredInstallWithCustomInstructions'], 'https://prime.madelineproto.xyz');
} else {
$additional = \sprintf(Lang::$current_lang['extensionRequiredInstallWithApt'], 'php'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'-'.$extensionName);
}
$message = 'MadelineProto requires the '.$extensionName.' extension to run. '.$additional;
$message = \sprintf(Lang::$current_lang['extensionRequired'], $extensionName, $additional);
if (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') {
echo $message.'<br>';
echo \htmlentities($message).'<br>';
}
$file = 'MadelineProto';
$line = 1;
@ -106,26 +107,22 @@ class Exception extends \Exception
if (\str_contains($exception->getMessage(), 'Fiber stack protect failed')
|| \str_contains($exception->getMessage(), 'Fiber stack allocate failed')
) {
$maps = "";
$maps = "?";
try {
$maps = '~'.\substr_count(\file_get_contents('/proc/self/maps'), "\n");
$pid = \getmypid();
$maps = '~'.\substr_count(\file_get_contents("/proc/$pid/maps"), "\n");
} catch (\Throwable) {
}
if ($maps !== '') {
$maps = " ($maps)";
Logger::log(Lang::$current_lang['manualAdminActionRequired'], Logger::FATAL_ERROR);
Logger::log(Lang::$current_lang['manualAdminActionRequired'], Logger::FATAL_ERROR);
Logger::log(Lang::$current_lang['manualAdminActionRequired']);
foreach (\explode("\n", \trim(Lang::$current_lang['mmapError'])) as $line) {
Logger::log(\sprintf($line, $maps), Logger::FATAL_ERROR);
}
Logger::log("!!!!!!!!! MANUAL SYSTEM ADMIN ACTION REQUIRED !!!!!!!!!", Logger::FATAL_ERROR);
Logger::log("!!!!!!!!! MANUAL SYSTEM ADMIN ACTION REQUIRED !!!!!!!!!", Logger::FATAL_ERROR);
Logger::log("!!!!!!!!! MANUAL SYSTEM ADMIN ACTION REQUIRED !!!!!!!!!", Logger::FATAL_ERROR);
Logger::log("The maximum number of mmap'ed regions was reached$maps: please increase the vm.max_map_count kernel config to 262144 to fix.");
Logger::log("To fix, run the following command as root: echo 262144 | sudo tee /proc/sys/vm/max_map_count");
Logger::log("To persist the change across reboots: echo vm.max_map_count=262144 | sudo tee /etc/sysctl.d/40-madelineproto.conf");
Logger::log("On Windows and WSL, increasing the size of the pagefile might help; please switch to native Linux if the issue persists.");
Logger::log("!!!!!!!!! MANUAL SYSTEM ADMIN ACTION REQUIRED !!!!!!!!!", Logger::FATAL_ERROR);
Logger::log("!!!!!!!!! MANUAL SYSTEM ADMIN ACTION REQUIRED !!!!!!!!!", Logger::FATAL_ERROR);
Logger::log("!!!!!!!!! MANUAL SYSTEM ADMIN ACTION REQUIRED !!!!!!!!!", Logger::FATAL_ERROR);
Logger::log(Lang::$current_lang['manualAdminActionRequired']);
Logger::log(Lang::$current_lang['manualAdminActionRequired'], Logger::FATAL_ERROR);
Logger::log(Lang::$current_lang['manualAdminActionRequired'], Logger::FATAL_ERROR);
}
Logger::log($exception, Logger::FATAL_ERROR);
die(1);

View File

@ -25,6 +25,7 @@ use Amp\Dns\DnsResolver;
use Amp\Future;
use Amp\Http\Client\HttpClient;
use Amp\Sync\LocalMutex;
use AssertionError;
use danog\MadelineProto\Broadcast\Broadcast;
use danog\MadelineProto\Db\DbArray;
use danog\MadelineProto\Db\DbPropertiesFactory;
@ -1582,14 +1583,28 @@ final class MTProto implements TLCallback, LoggerGetter
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);
if ($this->hasEventHandler()) {
if (!$this->hasReportPeers()) {
Logger::log('!!! '.Lang::$current_lang['noReportPeers'].' !!!', 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 .= "<h2 style='color:red;'>".\htmlentities(Lang::$current_lang['noReportPeers'])."</h2>";
$warning .= "<code>public function getReportPeers() { return '@yourtelegramusername'; }</code>";
}
if ($this->event_handler_instance instanceof EventHandler) {
try {
EventHandler::validateEventHandler($this->event_handler_instance::class);
} catch (AssertionError $e) {
Logger::log($e->getMessage(), Logger::FATAL_ERROR);
$e = \htmlentities($e->getMessage());
$warning .= "<h2 style='color:red;'>{$e}</h2>";
}
}
}
if (!Magic::$hasOpenssl) {
$warning .= "<h2 style='color:red;'>Warning: the openssl extension is not installed, please install it to speed up MadelineProto</h2>";
$warning .= "<h2 style='color:red;'>".\htmlentities(\sprintf(Lang::$current_lang['extensionRecommended'], 'openssl'))."</h2>";
}
if (!\extension_loaded('gmp')) {
$warning .= "<h2 style='color:red;'>".\htmlentities(\sprintf(Lang::$current_lang['extensionRecommended'], 'gmp'))."</h2>";
}
return "<html><body><h1>$message</h1>$warning</body></html>";
}

View File

@ -25,4 +25,11 @@ namespace danog\MadelineProto;
*/
abstract class PluginEventHandler extends EventHandler
{
/**
* Plugins can require other plugins ONLY with the getPlugins() method.
*/
final public function getPluginPaths(): string|array|null
{
return null;
}
}

View File

@ -870,7 +870,7 @@ final class TL implements TLInterface
*/
public static function extractWaveform(string $x): array
{
$values = array_pad(\array_values(\unpack('C*', $x)), 63, 0);
$values = \array_pad(\array_values(\unpack('C*', $x)), 63, 0);
$result = \array_fill(0, 100, 0);
$bitPos = 0;