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;
|
2019-12-23 15:26:08 +08:00
|
|
|
use Illuminate\Support\Facades\Redis;
|
2019-10-29 15:33:36 +08:00
|
|
|
|
|
|
|
class ServerController extends Controller
|
|
|
|
{
|
2019-12-23 16:02:56 +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-23 15:26:08 +08:00
|
|
|
$server[$i]['last_check_at'] = Redis::get('server_last_check_at_' . $server[$i]['id']);
|
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
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save (ServerSave $request) {
|
|
|
|
if ($request->input('id')) {
|
|
|
|
$server = Server::find($request->input('id'));
|
|
|
|
if (!$server) {
|
|
|
|
abort(500, '服务器不存在');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$server = new Server();
|
|
|
|
}
|
|
|
|
$server->group_id = json_encode($request->input('group_id'));
|
|
|
|
$server->name = $request->input('name');
|
|
|
|
$server->host = $request->input('host');
|
|
|
|
$server->port = $request->input('port');
|
|
|
|
$server->server_port = $request->input('server_port');
|
|
|
|
$server->tls = $request->input('tls');
|
2019-11-04 19:45:14 +08:00
|
|
|
$server->tags = $request->input('tags') ? json_encode($request->input('tags')) : NULL;
|
2019-10-29 15:33:36 +08:00
|
|
|
$server->rate = $request->input('rate');
|
2019-11-06 00:53:02 +08:00
|
|
|
$server->network = $request->input('network');
|
2019-11-06 01:01:46 +08:00
|
|
|
if ($request->input('settings')) {
|
|
|
|
if (!is_object(json_decode($request->input('settings')))) {
|
|
|
|
abort(500, '传输协议配置格式不正确');
|
|
|
|
}
|
2019-11-06 00:59:49 +08:00
|
|
|
$server->settings = $request->input('settings');
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
return response([
|
|
|
|
'data' => $server->save()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-12-23 16:02:56 +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()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function groupSave (Request $request) {
|
|
|
|
if (empty($request->input('name'))) {
|
|
|
|
abort(500, '组名不能为空');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->input('id')) {
|
|
|
|
$serverGroup = ServerGroup::find($request->input('id'));
|
|
|
|
} else {
|
|
|
|
$serverGroup = new ServerGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
$serverGroup->name = $request->input('name');
|
|
|
|
return response([
|
|
|
|
'data' => $serverGroup->save()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function groupDrop (Request $request) {
|
|
|
|
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()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function drop (Request $request) {
|
|
|
|
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
|
|
|
|
|
|
|
public function update (ServerUpdate $request) {
|
|
|
|
$updateData = $request->only([
|
|
|
|
'show',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$server = Server::find($request->input('id'));
|
|
|
|
if (!$server) {
|
|
|
|
abort(500, '该服务器不存在');
|
|
|
|
}
|
|
|
|
if (!$server->update($updateData)) {
|
|
|
|
abort(500, '保存失败');
|
|
|
|
}
|
|
|
|
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|