TelegramApiServer/server.php

102 lines
3.1 KiB
PHP
Raw Normal View History

<?php
use TelegramApiServer\Config;
2020-03-28 11:44:14 +01:00
use TelegramApiServer\Files;
2020-01-19 04:17:14 +01:00
use TelegramApiServer\Migrations\SessionsMigration;
use TelegramApiServer\Migrations\SwooleToAmpMigration;
use TelegramApiServer\Server\Fork;
use TelegramApiServer\Server\HealthCheck;
if (PHP_SAPI !== 'cli') {
throw new RuntimeException('Start in CLI');
}
2020-06-25 00:33:18 +02:00
$shortopts = 'a::p::s::e::';
2019-03-19 00:36:20 +01:00
$longopts = [
2020-06-25 00:33:18 +02:00
'address::', // ip адресс сервера
'port::', // порт сервера
2019-03-09 00:28:30 +01:00
'session::', //префикс session файла
2020-06-25 00:33:18 +02:00
'env::', //путь до .env файла
'docker::', //включить настройки для запуска внутри docker
'help', //нужна ли справка?
];
$options = getopt($shortopts, $longopts);
$options = [
2019-03-19 00:36:20 +01:00
'address' => $options['address'] ?? $options['a'] ?? '',
'port' => $options['port'] ?? $options['p'] ?? '',
2020-01-19 04:17:14 +01:00
'session' => (array) ($options['session'] ?? $options['s'] ?? []),
2020-06-25 00:33:18 +02:00
'env' => $options['env'] ?? $options['e'] ?? '.env',
2020-03-11 00:20:14 +01:00
'docker' => isset($options['docker']),
2019-03-19 00:36:20 +01:00
'help' => isset($options['help']),
];
if ($options['help']) {
$help = 'Fast, simple, async php telegram parser: MadelineProto + Swoole Server
2020-06-25 00:33:18 +02:00
usage: php server.php [--help] [-a=|--address=127.0.0.1] [-p=|--port=9503] [-s=|--session=] [-e=|--env=.env] [--docker]
Options:
--help Show this message
2020-01-15 00:00:58 +01:00
-a --address Server ip (optional) (default: 127.0.0.1)
To listen external connections use 0.0.0.0 and fill IP_WHITELIST in .env
-p --port Server port (optional) (default: 9503)
2020-01-19 04:17:14 +01:00
-s --session Name for session file (optional)
2020-01-15 00:00:58 +01:00
Multiple sessions can be specified: "--session=user --session=bot"
2020-01-14 23:44:03 +01:00
Each session is stored in `sessions/{$session}.madeline`.
Nested folders supported.
See README for more examples.
2020-06-25 00:33:18 +02:00
-e --env .env file name. (default: .env).
Helpful when need multiple instances with different settings
--docker Apply some settings for docker: add docker network to whitelist.
2020-03-11 00:20:14 +01:00
2020-06-25 00:33:18 +02:00
Also some options can be set in .env file (see .env.example)
Example:
php server.php
';
echo $help;
exit;
}
2020-01-11 21:53:58 +01:00
2020-03-11 00:20:14 +01:00
require_once __DIR__ . '/bootstrap.php';
2020-02-19 19:49:37 +01:00
SessionsMigration::move();
2020-01-19 04:17:14 +01:00
SwooleToAmpMigration::check();
$mainProcessPid = getmypid();
if (Config::getInstance()->get('health_check.enabled')) {
Fork::run(function() use($mainProcessPid) {
HealthCheck::start($mainProcessPid);
});
}
2020-03-31 16:09:20 +02:00
$sessions = [];
2020-01-11 21:53:58 +01:00
foreach ($options['session'] as $session) {
2020-01-14 18:55:31 +01:00
$session = trim($session);
if (mb_substr($session, -1) === '/') {
throw new InvalidArgumentException('Session name specified as directory');
}
2020-03-28 11:44:14 +01:00
$session = Files::getSessionFile($session);
2020-01-20 02:04:39 +01:00
2020-02-19 17:20:34 +01:00
if (preg_match('~[' . preg_quote('*?[]!', '~') . ']~', $session)) {
2020-03-28 11:44:14 +01:00
$sessions = Files::globRecursive($session);
2020-01-20 02:04:39 +01:00
} else {
$sessions[] = $session;
}
$sessions = array_filter($sessions);
2020-03-28 16:23:27 +01:00
$sessions = array_unique($sessions);
2019-03-09 00:28:30 +01:00
}
2020-01-20 02:38:14 +01:00
new TelegramApiServer\Server\Server(
$options,
2020-03-28 16:23:27 +01:00
$sessions
2020-01-20 02:38:14 +01:00
);