Skip to content

Commit

Permalink
Merge pull request #5 from LittleJake/develop
Browse files Browse the repository at this point in the history
Develop merge
  • Loading branch information
LittleJake authored Nov 3, 2023
2 parents 8bb0737 + d0fa2f4 commit f92a614
Show file tree
Hide file tree
Showing 27 changed files with 316 additions and 201 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ HOST = ''
PORT = 6027
PASSWORD = ''

[ADMIN]
PASSWORD = ''

[RECAPTCHA]
HTML_TOKEN = ''
SERVER_TOKEN = ''
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ composer install
cp .env.example .env
```

#### 后台登录密钥重置
#### 上报节点密钥操作(增/删/查)

```bash
php think token:generate
php think token help
```

#### 配置Redis数据源
Expand Down
8 changes: 4 additions & 4 deletions application/admin/controller/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public function index()
{
if (!session('?is_login'))
$this->redirect('admin/index/login');
$hash = SystemMonitor::getHashes();
$ip = SystemMonitor::fetchIPInfo(array_values($hash));
$uuids = SystemMonitor::getUUIDs();
$ip = SystemMonitor::fetchIPInfo(array_values($uuids));
$hide = array_flip(SystemMonitor::getHide());

asort($hash);
$this->assign("hash", $hash);
asort($uuids);
$this->assign("uuids", $uuids);
$this->assign("hide", $hide);
$this->assign("ip", $ip);
return $this->fetch();
Expand Down
4 changes: 2 additions & 2 deletions application/admin/validate/LoginTokenValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


use app\common\lib\reCaptcha;
use think\facade\Cache;
use think\facade\Env;
use think\Validate;

class LoginTokenValidate extends Validate
Expand All @@ -28,7 +28,7 @@ protected function checkCaptcha($value, $rule, $data = []){
}

protected function checkToken($value, $rule, $data = []){
return (Cache::store('token')->get('ADMIN.TOKEN')==$value)
return (!empty(Env::get('ADMIN.PASSWORD')) && Env::get('ADMIN.PASSWORD') == $value)
?true:'Token invalid.';
}
}
4 changes: 2 additions & 2 deletions application/admin/view/index/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<title>Control Panel</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.4.6/css/flag-icon.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mdui/0.4.3/css/mdui.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mdui/1.0.2/css/mdui.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
</head>
<body class="mdui-appbar-with-toolbar mdui-theme-primary-indigo mdui-theme-accent-indigo">
Expand Down Expand Up @@ -39,7 +39,7 @@
<p class="mdui-text-color-white-secondary">{:lang('License')}</p>
</div>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mdui/0.4.3/js/mdui.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mdui/1.0.2/js/mdui.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$('.delete').on('click', function(){
Expand Down
12 changes: 6 additions & 6 deletions application/admin/view/index/index_ajax.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div class="mdui-col-md-4">
<ul class="mdui-list word-wrap">
{volist name="hash" id="vo"}
{volist name="uuids" id="vo"}
<div class="mdui-list-item mdui-ripple mdui-p-l-0">
<div class="mdui-list-item-content ajax-load" data-href="{:url('admin/info/index',['token'=>$key])}">
<div class="mdui-list-item-content ajax-load" data-href="{:url('admin/info/index',['uuid'=>$key])}">
<div class="mdui-list-item-title">
{$vo}
</div>
Expand All @@ -12,7 +12,7 @@
</div>
<div class="mdui-divide"></div>
<label class="mdui-switch">
<input class="switch" type="checkbox" data-href="{:url('admin/info/index',['token'=>$key])}"
<input class="switch" type="checkbox" data-href="{:url('admin/info/index',['uuid'=>$key])}"
{if !isset($hide[$key])}checked{/if} />
<i class="mdui-switch-icon"></i>
</label>
Expand All @@ -21,18 +21,18 @@
</button>
<ul class="mdui-menu" id="list-{$i}">
<li class="mdui-menu-item" disabled>
<a href="javascript:;" class="mdui-ripple reboot" data-href="{:url('admin/info/index',['token'=>$key])}">
<a href="javascript:;" class="mdui-ripple reboot" data-href="{:url('admin/info/index',['uuid'=>$key])}">
<i class="mdui-menu-item-icon mdui-icon material-icons">&#xe5d5;</i>Reboot
</a>
</li>
<li class="mdui-menu-item" disabled>
<a href="javascript:;" class="mdui-ripple reset" data-href="{:url('admin/info/index',['token'=>$key])}">
<a href="javascript:;" class="mdui-ripple reset" data-href="{:url('admin/info/index',['uuid'=>$key])}">
<i class="mdui-menu-item-icon mdui-icon material-icons">&#xe8b3;</i>Reset Data
</a>
</li>
<li class="mdui-divider"></li>
<li class="mdui-menu-item">
<a href="javascript:;" class="mdui-ripple delete" data-href="{:url('admin/info/index',['token'=>$key])}">
<a href="javascript:;" class="mdui-ripple delete" data-href="{:url('admin/info/index',['uuid'=>$key])}">
<i class="mdui-menu-item-icon mdui-icon material-icons">delete</i>Remove
</a>
</li>
Expand Down
7 changes: 3 additions & 4 deletions application/admin/view/index/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.4.6/css/flag-icon.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mdui/0.4.3/css/mdui.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mdui/1.0.2/css/mdui.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script type="text/javascript">
var onloadCallback = function() {grecaptcha.render('recaptcha', {'sitekey' : '{:env("RECAPTCHA.HTML_TOKEN")}'});};
Expand Down Expand Up @@ -48,13 +48,12 @@
<p class="mdui-text-color-white-secondary">{:lang('License')}</p>
</div>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mdui/0.4.3/js/mdui.min.js"></script>
<script type="text/javascript" src="/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mdui/1.0.2/js/mdui.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://www.recaptcha.net/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
<script>
function checkForm(){
var captcha = $('textarea[name="g-recaptcha-response"]').val(), token = $('input[name="token"]').val();
if (token.length !== 64){mdui.alert('Wrong Token');return false;}
if (captcha == null || captcha === ''){mdui.alert('Wrong Captcha');return false;}
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions application/api/controller/Cpu.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

class Cpu extends Controller
{
protected $middleware = ['FlowControl', 'CheckToken'];
protected $middleware = ['FlowControl', 'CheckUUID', 'CheckJson'];

/**
* 显示资源列表
*
* @param string $token
* @param string $uuid
* @return Response
* @throws Exception
*/
public function get($token = '')
public function get($uuid = '')
{
return json(SystemMonitor::collectionFormat(SystemMonitor::getCollection($token), "Load"));
return json(SystemMonitor::collectionFormat(SystemMonitor::getCollection($uuid), "Load"));
}
}
8 changes: 4 additions & 4 deletions application/api/controller/Disk.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

class Disk extends Controller
{
protected $middleware = ['FlowControl', 'CheckToken'];
protected $middleware = ['FlowControl', 'CheckUUID', 'CheckJson'];

/**
* 显示资源列表
*
* @param string $token
* @param string $uuid
* @return Response
* @throws Exception
*/
public function get($token = '')
public function get($uuid = '')
{
return json(SystemMonitor::diskFormat(SystemMonitor::getCollection($token)));
return json(SystemMonitor::diskFormat(SystemMonitor::getCollection($uuid)));
}
}
8 changes: 4 additions & 4 deletions application/api/controller/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

class Memory extends Controller
{
protected $middleware = ['FlowControl', 'CheckToken'];
protected $middleware = ['FlowControl', 'CheckUUID', 'CheckJson'];

/**
* 显示资源列表
*
* @param string $token
* @param string $uuid
* @return Response
* @throws Exception
*/
public function get($token = '')
public function get($uuid = '')
{
return json(SystemMonitor::memoryFormat(SystemMonitor::getCollection($token)));
return json(SystemMonitor::memoryFormat(SystemMonitor::getCollection($uuid)));
}
}
8 changes: 4 additions & 4 deletions application/api/controller/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

class Network extends Controller
{
protected $middleware = ['FlowControl', 'CheckToken'];
protected $middleware = ['FlowControl', 'CheckUUID', 'CheckJson'];

/**
* 显示资源列表
*
* @param string $token
* @param string $uuid
* @return Response
* @throws Exception
*/
public function get($token = '')
public function get($uuid = '')
{
return json(SystemMonitor::networkFormat(SystemMonitor::getCollection($token)));
return json(SystemMonitor::networkFormat(SystemMonitor::getCollection($uuid)));
}
}
30 changes: 30 additions & 0 deletions application/api/controller/Report.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace app\api\controller;

use app\common\lib\SystemMonitor;
use think\Controller;

class Report extends Controller
{
protected $middleware = ['FlowControl', 'CheckToken', 'CheckUUID', 'CheckJson'];


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

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

public function hash($uuid = '')
{
$ip = $this-> request->post('ip');
return json(SystemMonitor::setUUID($uuid, $ip));
}
}
8 changes: 4 additions & 4 deletions application/api/controller/Thermal.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@

class Thermal extends Controller
{
protected $middleware = ['FlowControl', 'CheckToken'];
protected $middleware = ['FlowControl', 'CheckUUID', 'CheckJson'];

/**
* 显示资源列表
*
* @param string $token
* @param string $uuid
* @return Response
* @throws Exception
*/
public function get($token = '')
public function get($uuid = '')
{
return json(SystemMonitor::collectionFormat(SystemMonitor::getCollection($token), 'Thermal'));
return json(SystemMonitor::collectionFormat(SystemMonitor::getCollection($uuid), 'Thermal'));
}
}
3 changes: 2 additions & 1 deletion application/command.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
// +----------------------------------------------------------------------

return [
'app\command\token'
'token' => 'app\command\Token',
'refresh' => 'app\command\Refresh',
];
2 changes: 1 addition & 1 deletion application/command/refresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use think\console\Input;
use think\console\Output;

class refresh extends Command
class Refresh extends Command
{
protected function configure()
{
Expand Down
68 changes: 57 additions & 11 deletions application/command/token.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,75 @@

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Cache;

class token extends Command
class Token extends Command
{
protected function configure()
{
// 指令配置
$this->setName('token:generate')
->setDescription('generate admin token.');
$this->setName('token')
-> addArgument('cmd', Argument::OPTIONAL, "Command: add, del, get, list")
-> addOption('uuid', null, Option::VALUE_OPTIONAL, 'Device UUID')
-> setDescription('Node token operation');
// 设置参数

}

protected function execute(Input $input, Output $output)
{
$str = "0123456789abcdefghijklnmopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ";
$token = '';
for($i = 64;$i;$i--)
$token .= substr($str, rand(0,strlen($str)-1), 1);

Cache::store('token')->set('ADMIN.TOKEN', $token);
// 指令输出
$output->writeln("The new token is: \n$token\nPlease keep it nice and safe.");

$cmd = trim($input->getArgument('cmd'));
$node_token = Cache::store('token')->has("node_token")?json_decode(Cache::store("token")->get("node_token"), true):[];

switch ($cmd) {
case "add":
if ($input->hasOption('uuid')) {
$uuid = trim($input->getOption('uuid'));
$str = "0123456789abcdefghijklnmopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ";
$token = '';
for($i = 128;$i;$i--)
$token .= substr($str, rand(0,strlen($str)-1), 1);

$node_token[$uuid] = $token;

$output->writeln("The token is: \n$token\nPlease keep it nice and safe.");
} else {
$output->writeln("No UUID specify.");
}
break;
case "del":
if ($input->hasOption('uuid')) {
$uuid = trim($input->getOption('uuid'));
try {
$node_token[$uuid];
unset($node_token[$uuid]);
$output->writeln("OK");
}catch (\Exception $e) {
$output->writeln("No match UUID found.");
}
} else {
$output->writeln("No UUID specify.");
}
break;
case "list":
foreach ($node_token as $key => $value)
$output->writeln("$key: $value");
exit(0);
case "help":
$output->writeln("Node UUID token manager");
$output->writeln("");
$output->writeln("add --uuid <UUID> - Add UUID token.");
$output->writeln("del --uuid <UUID> - Delete existing UUID token.");
$output->writeln("list - List existing UUID token.");
break;
default:
$output->writeln("No command specify. Using `php think token help`");
exit(1);
}
Cache::store("token")->set("node_token", json_encode($node_token));
}
}
Loading

0 comments on commit f92a614

Please sign in to comment.