forked from xtrime-ru/TelegramApiServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
110 lines (85 loc) · 3.34 KB
/
server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
use TelegramApiServer\Config;
use TelegramApiServer\Files;
use TelegramApiServer\Migrations\SessionsMigration;
use TelegramApiServer\Migrations\StartUpFixes;
use TelegramApiServer\Migrations\SwooleToAmpMigration;
use TelegramApiServer\Server\Fork;
use TelegramApiServer\Server\HealthCheck;
if (PHP_SAPI !== 'cli') {
throw new RuntimeException('Start in CLI');
}
$shortopts = 'a::p::s::e::';
$longopts = [
'address::', // ip адресс сервера
'port::', // порт сервера
'session::', //префикс session файла
'env::', //путь до .env файла
'docker::', //включить настройки для запуска внутри docker
'help', //нужна ли справка?
];
$options = getopt($shortopts, $longopts);
$options = [
'address' => $options['address'] ?? $options['a'] ?? '',
'port' => $options['port'] ?? $options['p'] ?? '',
'session' => (array)($options['session'] ?? $options['s'] ?? []),
'env' => $options['env'] ?? $options['e'] ?? '.env',
'docker' => isset($options['docker']),
'help' => isset($options['help']),
];
if ($options['help']) {
$help = 'Fast, simple, async php telegram parser: MadelineProto + Swoole Server
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
-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)
-s --session Name for session file (optional)
Multiple sessions can be specified: "--session=user --session=bot"
Each session is stored in `sessions/{$session}.madeline`.
Nested folders supported.
See README for more examples.
-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.
Also some options can be set in .env file (see .env.example)
Example:
php server.php
';
echo $help;
exit;
}
require_once __DIR__ . '/bootstrap.php';
$mainProcessPid = getmypid();
if (Config::getInstance()->get('health_check.enabled')) {
if (!defined('SIGINT')) {
throw new RuntimeException('pcintl required for healthcheck. Use docker.');
}
Fork::run(static function () use ($mainProcessPid) {
HealthCheck::start($mainProcessPid);
});
}
$sessions = [];
foreach ($options['session'] as $session) {
$session = trim($session);
if (mb_substr($session, -1) === '/') {
throw new InvalidArgumentException('Session name specified as directory');
}
$session = Files::getSessionFile($session);
if (preg_match('~[' . preg_quote('*?[]!', '~') . ']~', $session)) {
$sessions = Files::globRecursive($session);
} else {
$sessions[] = $session;
}
$sessions = array_filter($sessions);
$sessions = array_unique($sessions);
}
StartUpFixes::fix();
foreach ($sessions as $session) {
StartUpFixes::removeBrokenIpc($session);
}
new TelegramApiServer\Server\Server(
$options,
$sessions
);