2019-10-29 15:33:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Http\Requests\Admin\ServerSave;
|
2019-11-18 18:19:28 +08:00
|
|
|
use App\Http\Requests\Admin\ServerUpdate;
|
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;
|
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)
|
|
|
|
{
|
2019-12-15 22:03:17 +08:00
|
|
|
$server = Server::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']);
|
2019-12-29 13:21:36 +08:00
|
|
|
if ($server[$i]['parent_id']) {
|
2020-01-08 01:34:48 +08:00
|
|
|
$server[$i]['last_check_at'] = Cache::get('server_last_check_at_' . $server[$i]['parent_id']);
|
2019-12-29 13:21:36 +08:00
|
|
|
} else {
|
2020-01-08 01:34:48 +08:00
|
|
|
$server[$i]['last_check_at'] = Cache::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)
|
|
|
|
{
|
2019-12-29 13:21:36 +08:00
|
|
|
$params = $request->only([
|
|
|
|
'show',
|
|
|
|
'group_id',
|
|
|
|
'parent_id',
|
|
|
|
'name',
|
|
|
|
'host',
|
|
|
|
'port',
|
|
|
|
'server_port',
|
|
|
|
'tls',
|
|
|
|
'tags',
|
|
|
|
'rate',
|
|
|
|
'network',
|
|
|
|
'settings'
|
|
|
|
]);
|
|
|
|
$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
|
|
|
}
|
2019-12-29 13:21:36 +08:00
|
|
|
|
|
|
|
if (isset($params['settings'])) {
|
|
|
|
if (!is_object(json_decode($params['settings']))) {
|
2019-11-06 01:01:46 +08:00
|
|
|
abort(500, '传输协议配置格式不正确');
|
|
|
|
}
|
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, '服务器不存在');
|
|
|
|
}
|
|
|
|
if (!$server->update($params)) {
|
|
|
|
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, '该服务器不存在');
|
|
|
|
}
|
2019-12-29 13:21:36 +08:00
|
|
|
if (!$server->update($params)) {
|
2019-11-18 18:19:28 +08:00
|
|
|
abort(500, '保存失败');
|
|
|
|
}
|
|
|
|
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|