-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
start-analyzer.php
260 lines (226 loc) · 8.84 KB
/
start-analyzer.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/**
* Entry file for IP Analyzer worker
* use terminal command to start service
* ---------
* php start-analyzer.php start -d
* php start-analyzer.php restart -d
* php start-analyzer.php reload
* php start-analyzer.php stop
* php start-analyzer.php status
* php start-analyzer.php connections
* ---------
* This file is part of the IP Analyzer service.
*
* @license MIT
* (c) 2022 Eric Chow <https://cmchow.com>
* License at https://opensource.org/licenses/MIT
*/
require_once __DIR__ . '/vendor/autoload.php';
use Core\Config;
use Core\Logger;
use Core\Analyzer;
use Core\IpSum;
use Core\Validator;
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Timer;
use Workerman\Crontab\Crontab;
use GeoIp2\Database\Reader as MmdbReader;
const ANALYZER_VERSION = '1.0.1';
const ANALYZER_NAME = 'IpAnalyzer';
// init Config and Validator instance
$env = null;
$basePath = null;
$validator = null;
$config = null;
if (isset($argv[1])) {
echo ANALYZER_NAME . ' - version ' . ANALYZER_VERSION . PHP_EOL;
echo '---------------------------' . PHP_EOL;
// accept CLI params
if (($argv[1] === 'start' || $argv[1] === 'restart')) {
$cmdEnv = array_search('--env', $argv);
if ($cmdEnv !== false && isset($argv[$cmdEnv+1])) {
$env = $argv[$cmdEnv+1];
}
$baseEnv = array_search('--basepath', $argv);
if ($baseEnv !== false && isset($argv[$baseEnv+1])) {
$basePath = $argv[$baseEnv+1];
}
$validator = Validator::createInstance();
$config = Config::createInstance($env, $basePath);
}
}
// check service is running in PHAR
if (!empty(\Phar::running(false))) {
$parts = explode('/', \Phar::running(false));
array_pop($parts);
$pharPath = implode('/', $parts) . '/';
Worker::$logFile = $pharPath . 'ip-analyzer-workerman.log';
Worker::$pidFile = $pharPath . 'ip-analyzer-workerman.pid';
Worker::$statusFile = $pharPath . 'ip-analyzer-workerman.status';
}
$analyzer = null; // primary service worker
// enable service SSL if needed
if (Config::isSslEnabled()) {
if (is_readable(Config::getEnv('ANALYZER_SSL_CERT')) && is_readable(Config::getEnv('ANALYZER_SSL_KEY'))) {
$context = [
'ssl' => [
'local_cert' => Config::getEnv('ANALYZER_SSL_CERT'),
'local_pk' => Config::getEnv('ANALYZER_SSL_KEY'),
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
];
$analyzer = new Worker(Config::serviceListenAddress(), $context);
$analyzer->transport = 'ssl';
} else {
throw new \Exception('unable to read SSL cert/key file');
}
} else {
$analyzer = new Worker(Config::serviceListenAddress());
}
$analyzer->count = Config::getEnv('ANALYZER_WORKERS'); // worker threads
$analyzer->name = ANALYZER_NAME; // worker name
$analyzer->geoIpReaders = []; // IP Analyzer instances
// enable Redis connections
if (Config::isRedisEnabled()) {
$analyzer->redisConnection = [];
}
$analyzer->onWorkerStart = function (Worker $worker) use ($analyzer) {
$id = $worker->id;
$maxMemory = Config::getEnv('ANALYZER_MAX_MEMORY');
Timer::add(60, function () use ($worker, $maxMemory) {
if (memory_get_usage(true) > $maxMemory * 1024 * 1024 && count($worker->connections) == 0) {
// Restart current process if memory leak is detected.
Worker::stopAll();
}
});
$restartCron = Config::getEnv('ANALYZER_RESTART_CRON');
if (!empty($restartCron)) {
// Restart current process according to Cron schedule
new Crontab($restartCron, function () use ($id) {
// avoid restarting all workers at the same time
Timer::add(30 * $id, function () {
Worker::stopAll();
}, [], false);
});
}
$analyzer->redisConnection[$id] = null;
if (Config::isRedisEnabled()) {
try {
// init Redis server connection
$analyzer->redisConnection[$id] = new \Redis();
if (!Config::connectRedis($analyzer->redisConnection[$id])) {
Logger::log('error', "Redis connection failed");
}
} catch (\Throwable $e) {
Logger::log('error', "Redis connection exception {$e->getMessage()}");
}
}
$analyzer->ipSum[$id] = [];
if (Config::isIpSumEnabled()) {
$analyzer->ipSum[$id] = IpSum::parse();
Logger::log('debug', "IPsum loaded");
if (!empty(Config::getEnv('IPSUM_UPDATE_CRON'))) {
// Download and update IPsum list
new Crontab(Config::getEnv('IPSUM_UPDATE_CRON'), function () use ($analyzer, $id) {
if ($id === 0) {
// only perform download in single worker
$updated = IpSum::update();
if ($updated) {
$analyzer->ipSum[$id] = IpSum::parse();
Logger::log('debug', "IPsum updated and reloaded");
} else {
Logger::log('error', "IPsum update failed");
}
} else {
// check update status
if (isset($analyzer->reloadIpSumTimer)) {
Timer::del($analyzer->reloadIpSumTimer);
}
$analyzer->reloadIpSumTimer = Timer::add(1, function () use ($analyzer, $id) {
if (!IpSum::isUpdating()) {
$analyzer->ipSum[$id] = IpSum::parse();
Logger::log('debug', "IPsum reloaded");
Timer::del($analyzer->reloadIpSumTimer);
}
});
}
});
}
}
$mmdbDir = Config::getEnv('MMDB_DIR');
if (!is_readable($mmdbDir) && !empty(Config::getEnv('MMDB_FALLBACK'))) {
// use fallback mmdb if not found
$mmdbDir = Config::getEnv('MMDB_FALLBACK');
}
try {
// init IP Analyzer
$analyzer->ipReaderList[$id] = new MmdbReader($mmdbDir);
Logger::log('debug', "GeoIP reader init");
} catch (\Throwable $e) {
echo "GeoIP reader init failed {$e}";
Logger::log('error', "GeoIP reader init failed {$e}");
$analyzer->ipReaderList[$id] = null;
}
if ($analyzer->ipReaderList[$id] !== null && !empty(Config::getEnv('MMDB_RELOAD_CRON'))) {
// reload mmdb at specified cron time
new Crontab(Config::getEnv('MMDB_RELOAD_CRON'), function () use ($analyzer, $id) {
$mmdbDir = Config::getEnv('MMDB_DIR');
if (!is_readable($mmdbDir) && !empty(Config::getEnv('MMDB_FALLBACK'))) {
// use fallback mmdb if not found
$mmdbDir = Config::getEnv('MMDB_FALLBACK');
}
$analyzer->ipReaderList[$id] = new MmdbReader($mmdbDir);
Logger::log('info', "GeoIP reader reloaded");
});
}
if ($id === 0) {
// init Redis data
if (Config::isRedisEnabled() && !is_null($analyzer->redisConnection[$id])) {
Analyzer::resetRedisStats($analyzer->redisConnection[$id]);
}
}
};
$analyzer->onMessage = function (TcpConnection $connection, $payload) use ($analyzer) {
$id = $connection->worker->id;
$response = Analyzer::response('error', null, null);
try {
$data = json_decode($payload, true); // decode incoming payload
$response = Analyzer::authenticateRequest($data, $analyzer->ipReaderList[$id], $analyzer->redisConnection[$id], $analyzer->ipSum[$id]);
} catch (\Throwable $e) {
Logger::log('error', "service worker exception: {$e->getMessage()}, Trace: {$e->getTraceAsString()}");
$response = Analyzer::response('error', null, 'exception occurred');
}
$connection->send(json_encode($response)); // return response
$maxRequest = Config::getEnv('ANALYZER_MAX_REQUEST');
if ($maxRequest > 0) {
static $requestCount = 0;
if (++$requestCount >= $maxRequest) {
// Restart current process if max request is exceeded
Worker::stopAll();
}
}
};
$analyzer->onError = function (TcpConnection $connection, $code, $msg) {
Logger::log('error', "service worker error ({$code}): {$msg}");
};
// when service is reloaded
Worker::$onMasterReload = function () use ($env, $basePath) {
$validator = Validator::reloadInstance();
$config = Config::reloadInstance($env, $basePath);
// get active workers
$activeWorkers = [];
foreach (Worker::getAllWorkers() as $worker) {
$activeWorkers[$worker->name] = $worker;
}
// change old worker config
foreach ($activeWorkers as $service => $worker) {
if ($worker->name === ANALYZER_NAME) {
$worker->count = Config::getEnv('ANALYZER_WORKERS');
}
}
};
Worker::runAll();