-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
112 lines (92 loc) · 3.12 KB
/
index.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
111
112
<?php
use danog\MadelineProto\API;
use danog\MadelineProto\Serialization;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Server;
use TelegramServer\Config;
use TelegramServer\Logger;
use TelegramServer\TelegramServer;
require __DIR__ . '/vendor/autoload.php';
Config::config(require __DIR__ . '/config.php');
// quick fix for windows environments
if (!function_exists('posix_isatty')) {
function posix_isatty()
{
return false;
}
}
array_shift($argv);
Config::config(Config::TEST_MODE, in_array('test', $argv));
$mtpSession = Config::config(Config::CACHE_PATH) . '/session' . (Config::config(Config::TEST_MODE) ? '-test-mode' : '');
if (in_array('clean', $argv)) {
@unlink($mtpSession);
@unlink($mtpSession . '.lock');
clearstatcache();
}
try {
if (!file_exists($mtpSession)) {
throw new \Exception('');
}
$mtp = Serialization::deserialize($mtpSession);
} catch (\Exception $e) {
$mtp = new API([
'connection_settings' => [
'all' => [
'test_mode' => Config::config(Config::TEST_MODE)
]
],
'app_info' => [
'api_id' => Config::config(Config::API_ID),
'api_hash' => Config::config(Config::API_HASH)
],
'updates' => [
'handle_updates' => false
]
]);
$mtp->serialize($mtpSession);
}
$loop = Factory::create();
$telegram = new TelegramServer($mtp);
$server = new Server(function (ServerRequestInterface $request) use ($telegram) {
$status = 200;
try {
$params = $request->getQueryParams();
if (!isset($params['__signature'])) {
throw new \Exception('Missing request signature.');
}
$signature = $params['__signature'];
unset($params['__signature']);
if ($signature !== sha1(json_encode($params) . Config::config(Config::SERVER_SALT))) {
throw new \Exception('Invalid request signature.');
}
$response = $telegram->handle($params);
$result = [
'ok' => true,
'response' => $response,
'signature' => sha1(json_encode($response) . Config::config(Config::SERVER_SALT))
];
} catch (\Exception $e) {
$status = 500;
$result = [
'ok' => false,
'error' => $e->getMessage()
];
}
$result = json_encode($result, defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0);
$length = mb_strlen($result);
return new Response($status, [
'Content-Type' => 'application/json',
'Content-Length' => $length
], $result);
});
$socket = new React\Socket\Server(Config::config(Config::IP) . ':' . Config::config(Config::PORT), $loop);
$server->listen($socket);
$loop->addPeriodicTimer(10, [$telegram, 'expireSessions']);
Logger::log(PHP_EOL . str_repeat('-', 80));
Logger::log('|' . str_repeat(' ', 78) . '|');
Logger::log('|' . str_pad('Server running on ' . $socket->getAddress(), 78, ' ', STR_PAD_BOTH) . '|');
Logger::log('|' . str_repeat(' ', 78) . '|');
Logger::log(str_repeat('-', 80) . PHP_EOL);
$loop->run();