2019-10-29 15:33:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Http\Requests\Admin\ServerSave;
|
2020-04-20 16:07:06 +08:00
|
|
|
use App\Http\Requests\Admin\ServerSort;
|
2019-11-18 18:19:28 +08:00
|
|
|
use App\Http\Requests\Admin\ServerUpdate;
|
2020-03-30 23:47:54 +08:00
|
|
|
use App\Services\ServerService;
|
2020-06-05 00:25:15 +08:00
|
|
|
use App\Utils\CacheKey;
|
2019-10-29 15:33:36 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\ServerGroup;
|
|
|
|
use App\Models\Server;
|
|
|
|
use App\Models\Plan;
|
|
|
|
use App\Models\User;
|
2020-01-12 01:27:36 +08:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2020-04-20 16:07:06 +08:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2019-10-29 15:33:36 +08:00
|
|
|
|
|
|
|
class ServerController extends Controller
|
|
|
|
{
|
2020-01-11 13:36:52 +08:00
|
|
|
public function fetch(Request $request)
|
|
|
|
{
|
2020-04-20 16:07:06 +08:00
|
|
|
$server = Server::orderBy('sort', 'ASC')->get();
|
2019-11-04 19:46:57 +08:00
|
|
|
for ($i = 0; $i < count($server); $i++) {
|
2019-11-04 19:48:20 +08:00
|
|
|
if (!empty($server[$i]['tags'])) {
|
2019-11-04 19:53:57 +08:00
|
|
|
$server[$i]['tags'] = json_decode($server[$i]['tags']);
|
2019-11-04 19:48:20 +08:00
|
|
|
}
|
2019-11-04 19:53:57 +08:00
|
|
|
$server[$i]['group_id'] = json_decode($server[$i]['group_id']);
|
2020-06-05 13:26:22 +08:00
|
|
|
$serverStat = Cache::get(CacheKey::get('SERVER_ONLINE_USER', $server[$i]['parent_id'] ? $server[$i]['parent_id'] : $server[$i]['id']));
|
2020-06-05 00:25:15 +08:00
|
|
|
if ($serverStat) {
|
|
|
|
$serverStat = json_decode($serverStat, true);
|
|
|
|
$server[$i]['u'] = $serverStat['u'];
|
|
|
|
$server[$i]['d'] = $serverStat['d'];
|
|
|
|
$server[$i]['online'] = $serverStat['online'];
|
|
|
|
}
|
2019-12-29 13:21:36 +08:00
|
|
|
if ($server[$i]['parent_id']) {
|
2020-06-05 13:26:22 +08:00
|
|
|
$server[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_LAST_CHECK_AT', $server[$i]['parent_id']));
|
2019-12-29 13:21:36 +08:00
|
|
|
} else {
|
2020-06-05 13:26:22 +08:00
|
|
|
$server[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_LAST_CHECK_AT', $server[$i]['id']));
|
2019-12-29 13:21:36 +08:00
|
|
|
}
|
2019-11-04 19:46:57 +08:00
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
return response([
|
2019-11-04 19:46:57 +08:00
|
|
|
'data' => $server
|
2019-10-29 15:33:36 +08:00
|
|
|
]);
|
|
|
|
}
|
2019-12-29 13:21:36 +08:00
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function save(ServerSave $request)
|
|
|
|
{
|
2020-02-10 15:51:01 +08:00
|
|
|
$params = $request->only(array_keys(ServerSave::RULES));
|
2019-12-29 13:21:36 +08:00
|
|
|
$params['group_id'] = json_encode($params['group_id']);
|
|
|
|
if (isset($params['tags'])) {
|
|
|
|
$params['tags'] = json_encode($params['tags']);
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
2020-03-10 13:27:04 +08:00
|
|
|
|
2020-03-31 00:34:25 +08:00
|
|
|
if (isset($params['dnsSettings'])) {
|
|
|
|
if (!is_object(json_decode($params['dnsSettings']))) {
|
|
|
|
abort(500, 'DNS规则配置格式不正确');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 13:27:04 +08:00
|
|
|
if (isset($params['ruleSettings'])) {
|
|
|
|
if (!is_object(json_decode($params['ruleSettings']))) {
|
2020-02-10 18:48:06 +08:00
|
|
|
abort(500, '审计规则配置格式不正确');
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 13:21:36 +08:00
|
|
|
|
2020-03-10 13:27:04 +08:00
|
|
|
if (isset($params['networkSettings'])) {
|
|
|
|
if (!is_object(json_decode($params['networkSettings']))) {
|
2019-11-06 01:01:46 +08:00
|
|
|
abort(500, '传输协议配置格式不正确');
|
2020-03-10 13:27:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params['tlsSettings'])) {
|
|
|
|
if (!is_object(json_decode($params['tlsSettings']))) {
|
|
|
|
abort(500, 'TLS配置格式不正确');
|
2019-11-06 01:01:46 +08:00
|
|
|
}
|
2019-12-29 13:21:36 +08:00
|
|
|
}
|
2020-01-08 01:34:48 +08:00
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
if ($request->input('id')) {
|
|
|
|
$server = Server::find($request->input('id'));
|
|
|
|
if (!$server) {
|
|
|
|
abort(500, '服务器不存在');
|
|
|
|
}
|
2020-03-17 14:28:47 +08:00
|
|
|
try {
|
|
|
|
$server->update($params);
|
|
|
|
} catch (\Exception $e) {
|
2020-01-11 13:36:52 +08:00
|
|
|
abort(500, '保存失败');
|
|
|
|
}
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2020-01-08 01:34:48 +08:00
|
|
|
|
2019-12-29 13:21:36 +08:00
|
|
|
if (!Server::create($params)) {
|
|
|
|
abort(500, '创建失败');
|
2019-11-06 00:59:49 +08:00
|
|
|
}
|
2019-12-29 13:21:36 +08:00
|
|
|
|
2019-10-29 15:33:36 +08:00
|
|
|
return response([
|
2019-12-29 13:21:36 +08:00
|
|
|
'data' => true
|
2019-10-29 15:33:36 +08:00
|
|
|
]);
|
|
|
|
}
|
2020-01-08 01:34:48 +08:00
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function groupFetch(Request $request)
|
|
|
|
{
|
2019-10-29 15:33:36 +08:00
|
|
|
if ($request->input('group_id')) {
|
|
|
|
return response([
|
|
|
|
'data' => [ServerGroup::find($request->input('group_id'))]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
return response([
|
|
|
|
'data' => ServerGroup::get()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function groupSave(Request $request)
|
|
|
|
{
|
2019-10-29 15:33:36 +08:00
|
|
|
if (empty($request->input('name'))) {
|
|
|
|
abort(500, '组名不能为空');
|
|
|
|
}
|
2020-01-08 01:34:48 +08:00
|
|
|
|
2019-10-29 15:33:36 +08:00
|
|
|
if ($request->input('id')) {
|
|
|
|
$serverGroup = ServerGroup::find($request->input('id'));
|
|
|
|
} else {
|
|
|
|
$serverGroup = new ServerGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
$serverGroup->name = $request->input('name');
|
|
|
|
return response([
|
|
|
|
'data' => $serverGroup->save()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function groupDrop(Request $request)
|
|
|
|
{
|
2019-10-29 15:33:36 +08:00
|
|
|
if ($request->input('id')) {
|
|
|
|
$serverGroup = ServerGroup::find($request->input('id'));
|
|
|
|
if (!$serverGroup) {
|
|
|
|
abort(500, '组不存在');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$servers = Server::all();
|
|
|
|
foreach ($servers as $server) {
|
|
|
|
$groupId = json_decode($server->group_id);
|
|
|
|
if (in_array($request->input('id'), $groupId)) {
|
|
|
|
abort(500, '该组已被节点所使用,无法删除');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Plan::where('group_id', $request->input('id'))->first()) {
|
|
|
|
abort(500, '该组已被订阅所使用,无法删除');
|
|
|
|
}
|
|
|
|
if (User::where('group_id', $request->input('id'))->first()) {
|
|
|
|
abort(500, '该组已被用户所使用,无法删除');
|
|
|
|
}
|
|
|
|
return response([
|
|
|
|
'data' => $serverGroup->delete()
|
|
|
|
]);
|
|
|
|
}
|
2020-01-08 01:34:48 +08:00
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function drop(Request $request)
|
|
|
|
{
|
2019-10-29 15:33:36 +08:00
|
|
|
if ($request->input('id')) {
|
|
|
|
$server = Server::find($request->input('id'));
|
|
|
|
if (!$server) {
|
|
|
|
abort(500, '节点ID不存在');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response([
|
|
|
|
'data' => $server->delete()
|
|
|
|
]);
|
|
|
|
}
|
2019-11-18 18:19:28 +08:00
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function update(ServerUpdate $request)
|
|
|
|
{
|
2019-12-29 13:21:36 +08:00
|
|
|
$params = $request->only([
|
2019-11-18 18:19:28 +08:00
|
|
|
'show',
|
|
|
|
]);
|
2020-01-08 01:34:48 +08:00
|
|
|
|
2019-11-18 18:19:28 +08:00
|
|
|
$server = Server::find($request->input('id'));
|
2019-12-29 13:21:36 +08:00
|
|
|
|
2019-11-18 18:19:28 +08:00
|
|
|
if (!$server) {
|
|
|
|
abort(500, '该服务器不存在');
|
|
|
|
}
|
2020-03-17 14:28:47 +08:00
|
|
|
try {
|
|
|
|
$server->update($params);
|
|
|
|
} catch (\Exception $e) {
|
2019-11-18 18:19:28 +08:00
|
|
|
abort(500, '保存失败');
|
|
|
|
}
|
|
|
|
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2020-03-10 13:45:48 +08:00
|
|
|
|
|
|
|
public function copy(Request $request)
|
|
|
|
{
|
|
|
|
$server = Server::find($request->input('id'));
|
|
|
|
if (!$server) {
|
|
|
|
abort(500, '服务器不存在');
|
|
|
|
}
|
2020-03-10 13:47:50 +08:00
|
|
|
if (!Server::create($server->toArray())) {
|
2020-03-10 13:45:48 +08:00
|
|
|
abort(500, '复制失败');
|
|
|
|
}
|
|
|
|
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2020-03-30 23:47:54 +08:00
|
|
|
|
|
|
|
public function viewConfig(Request $request)
|
|
|
|
{
|
|
|
|
$serverService = new ServerService();
|
|
|
|
$config = $serverService->getConfig($request->input('node_id'), 23333);
|
2020-03-31 00:23:09 +08:00
|
|
|
return response([
|
|
|
|
'data' => $config
|
|
|
|
]);
|
2020-03-30 23:47:54 +08:00
|
|
|
}
|
2020-04-20 16:07:06 +08:00
|
|
|
|
|
|
|
public function sort(ServerSort $request)
|
|
|
|
{
|
|
|
|
DB::beginTransaction();
|
|
|
|
foreach ($request->input('server_ids') as $k => $v) {
|
|
|
|
if (!Server::find($v)->update(['sort' => $k + 1])) {
|
|
|
|
DB::rollBack();
|
|
|
|
abort(500, '保存失败');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|