forked from ellermister/ServerStatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.php
493 lines (447 loc) · 18.6 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<?php
define('SERVER_IP', '0.0.0.0');
define('SERVER_PORT', 35602);
define('CONFIG_FILE', 'config.json');
define('API_URL', 'ws://{host}/v2/');
if (!is_file('.token')) file_put_contents('.token', md5(uniqid(microtime(true), true)));
$token = file_get_contents('.token');
define('TOKEN', $token);
use Swoole\Process;
class Server
{
/**
* @var \Swoole\Table
*/
protected $table;
/**
* @var \Swoole\Table
*/
protected $users;
protected $tmp; //存放单个服务器的状态结果
protected $cache;//存放IP解析经纬度的结果
protected $web;// web目录
protected $filterIP = [
'0.0.0.0/8', '10.0.0.0/8', '100.64.0.0/10', '127.0.0.0/8', '169.254.0.0/16', '172.16.0.0/12', '192.0.0.0/24',
'192.0.2.0/24', '192.88.99.0/24', '192.168.0.0/16', '198.18.0.0/15', '198.51.100.0/24', '203.0.113.0/24',
'224.0.0.0/4', '240.0.0.0/4', '255.255.255.255/32'
];
public function __construct()
{
$this->tmp = __DIR__ . DIRECTORY_SEPARATOR . 'tmp';
$this->cache = __DIR__ . DIRECTORY_SEPARATOR . 'cache';
if (!is_dir($this->tmp)) {
@mkdir($this->tmp);
}
if (!is_dir($this->cache)) {
@mkdir($this->cache);
}
$this->web = __DIR__ . DIRECTORY_SEPARATOR . 'web';
}
protected function createTable()
{
$table = new Swoole\Table(1024); // key作fd
$table->column('fd', Swoole\Table::TYPE_INT, 4); //1,2,4,8
$table->column('state', Swoole\Table::TYPE_INT, 4); // 存储当前链接认证状态
$table->column('username', Swoole\Table::TYPE_STRING, 64); // 存储当前链接对应的服务器username
$table->create();
$this->table = $table;
$users = new Swoole\Table(1024);// key作服务器username(ID)
$users->column('net_id', Swoole\Table::TYPE_INT, 4); // 存储当前服务器对应的链接ID
$users->column('username', Swoole\Table::TYPE_STRING, 64);// c
$users->create();
$this->users = $users;
}
protected function debug($from, $text)
{
echo sprintf("[$from]: $text\n");
}
public function start($host, $port)
{
$this->createTable();
$server = new Swoole\WebSocket\Server($host, $port);
// $server->set(array('task_worker_num' => 4));
$this->debug("server", sprintf("Bound to %s:%d", $host, $port));
$server->on('open', function (Swoole\WebSocket\Server $server, $request) {
$this->debug('server', "handshake success with fd{$request->fd}");
$this->debug('server', 'send --> Authentication required:');
$server->push($request->fd, 'Authentication required:');
$this->table->set($request->fd, ['fd' => $request->fd, 'state' => 0]);
});
$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
$configRaw = file_get_contents(CONFIG_FILE);
$clients = json_decode($configRaw, true);
if ($clients == null) {
$this->debug('server', 'send --> Server Config valid!.');
$server->push($frame->fd, "Server Config valid!.");
$server->disconnect($frame->fd, 1000, 'Fuck off.');
return;
}
$clients = $clients['servers'];
$frame->data = rtrim($frame->data, "\n");
$this->debug('server', "receive <-- {$frame->data}");
if (false === ($client = $this->table->get($frame->fd))) {
$this->debug('server', 'send --> Fuck off.');
$server->disconnect($frame->fd, 1000, 'Fuck off.');
return;
}
if ($client['state'] === 0) {
if (false === strpos($frame->data, ':')) {
$this->debug('server', 'send --> You\'re an idiot, go away.');
$server->push($frame->fd, "You're an idiot, go away.");
$server->disconnect($frame->fd, 1000, 'Fuck off.');
return;
}
$buffer = explode(':', $frame->data);
$username = $buffer[0];
$password = $buffer[1];
if (empty($username) || empty($password)) {
$this->debug('server', 'send --> You\'re an idiot, go away.');
$server->push($frame->fd, "You're an idiot, go away.");
$server->disconnect($frame->fd, 1000, "Username and password must not be blank.");
return;
}
$serverId = null;
foreach ($clients as $key => $_client) {
// echo "{$_client['username']} == $username && {$_client['password']} == $password\n";
if ($_client['username'] == $username && $_client['password'] == $password) {
$serverId = $_client['username'];
}
}
$netId = -1;
if ($serverId !== null) {
$user = $this->users->get($serverId);
$netId = $user ? ($user['net_id'] ?: -1) : -1;
}
if ($serverId === null) {
$this->debug('server', 'send --> Wrong username and/or password.');
$server->push($frame->fd, "Wrong username and/or password.");
$server->disconnect($frame->fd, 1000, "Wrong username and/or password.");
} else if ($netId != -1) {
$this->debug('server', 'send --> Only one connection per user allowed.');
$server->disconnect($frame->fd, 1000, "Only one connection per user allowed.");
} else {
$this->debug('server', 'send --> Authentication successful. Access granted.');
$client['state'] = 1;
$client['username'] = $serverId;
$this->table->set($client['fd'], $client);
$user = $this->users->get($serverId);
$user['net_id'] = $client['fd'];
$this->users->set($serverId, $user);
$server->push($frame->fd, "Authentication successful. Access granted.");
//不区分ipv4/ipv6
$server->push($frame->fd, "You are connecting via: IPv4");
}
} else if ($client['state'] == 1) {
if (strpos($frame->data, 'logout') !== false) {
$this->debug('server', 'send --> Logout. Bye Bye ~');
$server->disconnect($frame->fd, 1000, "Logout. Bye Bye ~");
} else {
// main loop
if (strpos($frame->data, 'update') === 0) {
$jsonText = substr($frame->data, strlen('update') + 1);
$jsonArr = json_decode($jsonText, true);
$writeFile = $this->tmp . DIRECTORY_SEPARATOR . $client['username'] . '.json';
if (is_file($writeFile)) {
$oldJson = json_decode(file_get_contents($writeFile), true);
if ($oldJson) {
$jsonArr = array_merge($oldJson, $jsonArr);
}
}
$jsonArr['online4'] = true;
!isset($jsonArr['online6']) && $jsonArr['online6'] = false;
file_put_contents($writeFile, json_encode($jsonArr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
// $task_id = $server->task($client['username']);
}
}
}
});
$server->on('request', function (\Swoole\Http\Request $request, \Swoole\Http\Response $response) {
$response->header('Content-type', 'text/plain; charset=utf-8');
$html = "No Access";
if ($request->server['request_uri'] == '/api/client-linux.py') {
if (trim($request->get['token']) === trim(TOKEN)) {
$api = str_replace('{host}', $request->header['host'], API_URL);;
$clientIp = $request->server['remote_addr'];
$clientIp = isset($request->header['x-real-ip']) ? $request->header['x-real-ip'] : $clientIp;
if ($server = $this->findServer('host', $clientIp)) {
$username = $server['username'];
$password = $server['password'];
} else {
$username = "AS" . strval($this->serverCount() + 1);
$password = md5(uniqid(microtime(true), true));
}
try {
$this->saveServer(array_merge([
'host' => $clientIp,
'username' => $username,
'password' => $password
], $this->array_only($request->get, ['host', 'username', 'password', 'type', 'name', 'location'])));
} catch (Exception $exception) {
return $response->end("#-*- coding: UTF-8 -*-\n" . 'print ("' . $exception->getMessage() . '")');
}
$text = file_get_contents('./client-linux.py');
$text = preg_replace('/(SERVER)\s*=\s*"([^"]+)"/is', '$1 = "' . $api . '"', $text);
$text = preg_replace('/(USER)\s*=\s*"([^"]+)"/is', '$1 = "' . $username . '"', $text);
$text = preg_replace('/(PASSWORD)\s*=\s*"([^"]+)"/is', '$1 = "' . $password . '"', $text);
$html = $text;
}
}
$response->end($html);
});
$server->on('close', function ($ser, $fd) {
$connect = $ser->connection_info($fd);
if ($connect['websocket_status'] > 0) {
$this->debug('server', "fd: $fd closed");
$session = $this->table->get($fd);
$serverId = $session['username'];
$this->table->del($fd);
$this->users->del($serverId);
}
});
$server->set([
'document_root' => $this->web,
'enable_static_handler' => true,
]);
for ($n = 1; $n <= 2; $n++) {
$process = new Process(function () use ($n, $server) {
if ($n == 1) {
$server->start();
} else {
while (1) {
$this->flushJson();
sleep(1);
}
}
});
$process->start();
}
for ($n = 3; $n--;) {
$status = Process::wait(true);
echo "Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}" . PHP_EOL;
}
}
/**
* 获取数组中需要的元素
*
* @param array $arr
* @param array $only
* @return array
*/
protected function array_only(array $arr, array $only)
{
$ret = [];
foreach ($only as $name) {
if (isset($arr[$name])) {
$ret[$name] = $arr[$name];
}
}
return $ret;
}
/**
* 根据配置文件中某个值判断服务器是否存在
*
* @param $name
* @param $value
* @return bool
*/
protected function findServer($name, $value)
{
$configs = file_get_contents(CONFIG_FILE);
$users = json_decode($configs, true);
unset($configs);
if ($users != null) {
foreach ($users['servers'] as $server) {
if (isset($server[$name]) && $server[$name] == $value) {
return $server;
}
}
}
return false;
}
/**
* 获取服务器总数
*
* @return int
*/
protected function serverCount()
{
$configs = file_get_contents(CONFIG_FILE);
$users = json_decode($configs, true);
unset($configs);
if ($users != null) {
return count($users['servers']);
}
return 0;
}
/**
* 保存/新增服务器到配置文件
*
* @param array $config
* @return array
* @throws Exception
*/
protected function saveServer(array $config)
{
$default = [
'username' => '',
'name' => '',
'type' => 'kvm',
'host' => '',
'location' => '',
'password' => '',
];
$config = array_merge($default, $config);
if (empty($config['host'])) throw new Exception(CONFIG_FILE . "参数 host 不能为空.");
$api = sprintf("http://ip-api.com/json/%s?lang=zh-CN", trim($config['host']));
$ipInfo = file_get_contents($api);
$ipInfo = json_decode($ipInfo, true);
if (!isset($config['location']) && $ipInfo && isset($ipInfo['country'])) {
$config['location'] = $ipInfo['country'];
}
if (!isset($config['name']) && $ipInfo && isset($ipInfo['country'])) {
$config['name'] = $ipInfo['country'] . ' ' . $ipInfo['city'];
}
$configs = file_get_contents(CONFIG_FILE);
$users = json_decode($configs, true);
unset($configs);
if ($users == null) throw new Exception(CONFIG_FILE . "配置文件无法解析出 JSON.");
$isMatch = false;
$usernameDuplicate = false;
foreach ($users['servers'] as &$server) {
if (isset($server['username']) && $server['username'] == $config['username']) {
$usernameDuplicate = true;
}
if (isset($server['host']) && $server['host'] == $config['host']) {
$server = array_merge($server, $config);
$isMatch = true;
break;
}
}
// 如果是新增用户而且用户名相同则拒绝
if (!$isMatch && $usernameDuplicate) throw new Exception("用户名已经存在, 请修改!");
if ($isMatch === false) {
$users['servers'][] = $config;
}
file_put_contents(CONFIG_FILE, json_encode($users, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
return $config;
}
protected function format(array &$json)
{
$days = intval($json['uptime']) / 86400;
if ($days > 0) {
$json['uptime'] = sprintf("%d 天", $days);;
} else {
$json['uptime'] = sprintf("%02d:%02d:%02d", intval($json['uptime']) / 60 / 60, (intval($json['uptime']) / 60) % 60, intval($json['uptime']) % 60);
}
if (!$user = $this->users->get($json['username'])) {
$json['online4'] = $json['online4'] = $json['ip_status'] = false;
}
}
protected function flushJson()
{
$configRaw = file_get_contents(CONFIG_FILE);
$clients = json_decode($configRaw, true);
if ($clients == null) {
return;
}
$clients = $clients['servers'];
$files = scandir($this->tmp . DIRECTORY_SEPARATOR);
$output = ['servers' => []];
foreach ($files as $file) {
if ($file == '.' || $file == '..') {
continue;
}
$username = basename($file, '.json');
$jsonText = file_get_contents($this->tmp . DIRECTORY_SEPARATOR . $file);
$json = json_decode($jsonText, true);
if ($json !== null) {
foreach ($clients as $client) {
if ($client['username'] == $username) {
$json = array_merge($json, $client);
break;
}
}
$json['lat'] = $json['lon'] = null;
$json['connected_xy'] = [];
if (isset($json['connected_ip']) && is_array($json['connected_ip'])) {
foreach ($json['connected_ip'] as $_ip) {
$ipInfo = $this->getIpInfo($_ip);
if (is_array($ipInfo) && isset($ipInfo['lat'])) {
$xy = 'xy_'.$ipInfo['lat'].'_'.$ipInfo['lon'];
$json['connected_xy'][$xy] = [
'lat' => $ipInfo['lat'],
'lon' => $ipInfo['lon'],
];
}
}
}
ksort($json['connected_xy']);
$ip = $json['host'];
$ipInfo = $this->getIpInfo($ip);
if (!empty($ipInfo['lat']) && !empty($ipInfo['lon'])) {
$json['lat'] = $ipInfo['lat'];
$json['lon'] = $ipInfo['lon'];
}
$this->format($json);
unset($json['connected_ip']);
unset($json['password']);
unset($json['host']);
unset($json['username']);
$output['servers'][] = $json;
}
}
file_put_contents($this->web . '/json/stats.json', json_encode($output, JSON_UNESCAPED_UNICODE));
//返回任务执行的结果
}
/**
* 判断是否是过滤IP
*
* @param $ip
* @return bool|int
*/
protected function isFilterIP($ip)
{
$ipInt = ip2long($ip);
foreach ($this->filterIP as $ipSegment) {
list($ipBegin, $type) = explode('/', $ipSegment);
$ipBegin = ip2long($ipBegin);
$mask = 0xFFFFFFFF << (32 - intval($type));
if (intval($ipInt & $mask) == intval($ipBegin & $mask)) {
return true;
}
}
return false;
}
/**
* 获取IP信息
*
* @param $ip
* @return bool|false|mixed|string
*/
protected function getIpInfo($ip)
{
if ($this->isFilterIP($ip)) {
return false;
}
$ipCachePath = $this->cache . DIRECTORY_SEPARATOR . $ip;
if (is_file($ipCachePath)) {
$ipInfo = file_get_contents($ipCachePath);
$ipInfo = json_decode($ipInfo, true);
} else {
$api = sprintf("http://ip-api.com/json/%s?lang=zh-CN", trim($ip));
$opts = array(
'http' => array(
'method' => "GET",
'timeout' => 3,
)
);
$ipInfo = file_get_contents($api, false, stream_context_create($opts));
$ipInfo = json_decode($ipInfo, true);
if ($ipInfo && isset($ipInfo['lon']) && isset($ipInfo['lat'])) {
file_put_contents($ipCachePath, json_encode($ipInfo));
}
}
return $ipInfo ? $ipInfo : false;
}
}
(new Server())->start(SERVER_IP, SERVER_PORT);