Skip to content

Commit

Permalink
Merge pull request #15 from LittleJake/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
LittleJake authored Feb 7, 2024
2 parents cb81965 + 4f64ac0 commit fa48d1a
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[MONITOR]
OFFLINE_THRESHOLD = 600
DATA_TIMEOUT = 259200
RETENTION_TIME = 86400

[REDIS]
HOST = ''
Expand Down
9 changes: 5 additions & 4 deletions application/api/controller/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
use app\common\lib\SystemMonitor;
use think\Controller;

//TODO: Input sanitization.
class Report extends Controller
{
protected $middleware = ['FlowControl', 'CheckToken', 'CheckUUID', 'CheckJson'];
protected $middleware = ['FlowControl', 'CheckToken', 'CheckUUID' => ['except' => ['hash']]];


public function collection($uuid = '')
{
$json = $this-> request -> input();
return json(SystemMonitor::setCollection($uuid, $json));
$json = $this-> request -> post();
return json(SystemMonitor::setCollection($uuid, json_encode($json)));
}

public function info($uuid = '')
{
$json = $this-> request -> input();
$json = $this-> request -> post();
return json(SystemMonitor::setInfo($uuid, $json));
}

Expand Down
25 changes: 21 additions & 4 deletions application/common/lib/SystemMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use think\facade\Cache;
use think\facade\Env;
use think\facade\Log;
use think\exception\HttpException;

class SystemMonitor
{
Expand Down Expand Up @@ -49,8 +50,20 @@ static public function getUUIDs()

static public function setUUID($uuid, $ip)
{
return Cache::store('redis')->handler()
->hSet("system_monitor:hashes", $uuid, $ip);
try {
Cache::store('redis')->handler()->hSet("system_monitor:hashes", $uuid, $ip);
Cache::store('redis')->handler()->expire("system_monitor:hashes", Env::get("MONITOR.DATA_TIMEOUT"));
$uuids = SystemMonitor::getUUIDs();
if (empty($uuids[$uuid])) {
Cache::rm("system_monitor:hashes");
return ['code' => 200, 'message' => "Authorization OK, welcome aboard."];
}

return ['code' => 200, 'message' => "OK"];
} catch (Exception $e) {
Log::error($e->getMessage());
return ['code' => 500, 'message' => "Fail."];
}
}

static public function getLatest($uuid)
Expand Down Expand Up @@ -87,7 +100,8 @@ static public function getInfo($uuid)
static public function setInfo($uuid, $data)
{
try {
Cache::store('redis')->handler()->hSetAll("system_monitor:info:$uuid", $data);
Cache::store('redis')->handler()->hMset("system_monitor:info:$uuid", $data);
Cache::store('redis')->handler()->expire("system_monitor:info:$uuid", Env::get("MONITOR.DATA_TIMEOUT"));
return ['code' => 200, 'message' => "OK"];
} catch (Exception $e) {
Log::error($e->getMessage());
Expand Down Expand Up @@ -152,6 +166,9 @@ static public function setCollection($uuid, $data)
try {
Cache::store('redis')->handler()
->zAdd("system_monitor:collection:$uuid", time(), $data);
Cache::store('redis')->handler()
->zRemRangeByScore("system_monitor:collection:$uuid", 0, time() - Env::get("MONITOR.RETENTION_TIME"));
Cache::store('redis')->handler()->expire("system_monitor:collection:$uuid", Env::get("MONITOR.DATA_TIMEOUT"));
return ['code' => 200, 'message' => "OK"];
} catch (Exception $e) {
Log::error($e->getMessage());
Expand Down Expand Up @@ -237,7 +254,7 @@ static public function getIPByUUID($uuid)
{
$uuids = SystemMonitor::getUUIDs();
if (empty($uuids[$uuid]))
throw new Exception("Wrong Token", 403);
throw new HttpException(403, "Wrong Token");
return $uuids[$uuid];
}

Expand Down
4 changes: 2 additions & 2 deletions application/http/middleware/CheckJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace app\http\middleware;

use think\Exception;
use think\exception\HttpException;

class CheckJson
{
public function handle($request, \Closure $next)
{
if(!$request -> isJson())
throw new Exception("Not Allow", 405);
throw new HttpException(405, 'Method Not Allowed');

return $next($request);
}
Expand Down
5 changes: 3 additions & 2 deletions application/http/middleware/CheckToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace app\http\middleware;

use think\Exception;
use think\exception\HttpException;
use think\facade\Cache;

class CheckToken
{
Expand All @@ -12,7 +13,7 @@ public function handle($request, \Closure $next)
$auth = $request->header('authorization');
$node_token = Cache::store('token')->has("node_token")?json_decode(Cache::store("token")->get("node_token"), true):[];
if($node_token[$uuid] != $auth)
throw new Exception("Authorization Failed.", 403);
throw new HttpException(403, "Authorization Failed.");

return $next($request);
}
Expand Down
4 changes: 2 additions & 2 deletions application/http/middleware/CheckUUID.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace app\http\middleware;

use app\common\lib\SystemMonitor;
use think\Exception;
use think\exception\HttpException;

class CheckUUID
{
public function handle($request, \Closure $next)
{
$uuid = $request->param('uuid');
if(strlen($uuid) != 32 || empty(SystemMonitor::getUUIDs()[$uuid]))
throw new Exception("Wrong UUID", 403);
throw new HttpException(403, "Wrong UUID");

return $next($request);
}
Expand Down
4 changes: 2 additions & 2 deletions application/http/middleware/FlowControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace app\http\middleware;

use think\Exception;
use think\exception\HttpException;
use think\facade\Cache;

class FlowControl
Expand All @@ -11,7 +11,7 @@ public function handle($request, \Closure $next)
{
$ip = $request->ip();
if(Cache::inc("FlowControl:$ip") > 50)
throw new Exception("Trigger Flow Control", 503);
throw new HttpException(503, "Trigger Flow Control");
else
Cache::set("FlowControl:$ip", 0, 1);
return $next($request);
Expand Down
6 changes: 3 additions & 3 deletions config/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// 缓存前缀
'prefix'=> 'monitor',
// 缓存目录
'path' => preg_match("/cli/i", php_sapi_name())?'./runtime/cache/':'../runtime/cache/',
'path' => __DIR__.'/../runtime/cache/',
],
'redis' => [
'type' => 'Redis',
Expand All @@ -44,12 +44,12 @@
// 缓存前缀
'prefix'=> 'flag',
// 缓存目录
'path' => preg_match("/cli/i", php_sapi_name())?'./runtime/cache/':'../runtime/cache/',
'path' => __DIR__.'/../runtime/cache/',
],
'token' => [
'type' => 'file',
'expire' => 0,
'prefix' => 'token',
'path' => preg_match("/cli/i", php_sapi_name())?'./runtime/cache/':'../runtime/cache/',
'path' => __DIR__.'/../runtime/cache/',
]
];
7 changes: 4 additions & 3 deletions route/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
Route::get('api/network/:uuid','api/network/get');
Route::get('api/thermal/:uuid','api/thermal/get');
Route::get('api/battery/:uuid','api/battery/get');
Route::post('api/report/collection/:uuid','api/collection/info');

Route::post('api/report/collection/:uuid','api/report/collection');
Route::post('api/report/info/:uuid','api/report/info');
Route::post('api/report/hash/:uuid','api/report/hash');

Route::any('admin/info/:uuid','admin/info/index');
Route::any('admin/','admin/index/index');

Route::rule('admin/login','admin/index/login', 'GET|POST');
Route::rule('admin/logout','admin/index/logout', 'GET');


Route::rule('/manifest.json','index/index/manifest', 'GET');

return [];

0 comments on commit fa48d1a

Please sign in to comment.