Xboard/app/Http/Controllers/V1/Admin/Server/TrojanController.php

101 lines
2.6 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
namespace App\Http\Controllers\V1\Admin\Server;
2023-12-04 07:40:49 -05:00
use App\Exceptions\ApiException;
2023-11-17 01:44:01 -05:00
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ServerTrojanSave;
use App\Http\Requests\Admin\ServerTrojanUpdate;
use App\Models\ServerTrojan;
use App\Services\ServerService;
use Illuminate\Http\Request;
class TrojanController extends Controller
{
public function save(ServerTrojanSave $request)
{
$params = $request->validated();
if ($request->input('id')) {
$server = ServerTrojan::find($request->input('id'));
if (!$server) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '服务器不存在');
2023-11-17 01:44:01 -05:00
}
try {
$server->update($params);
} catch (\Exception $e) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '保存失败');
2023-11-17 01:44:01 -05:00
}
return response([
'data' => true
]);
}
if (!ServerTrojan::create($params)) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '创建失败');
2023-11-17 01:44:01 -05:00
}
return response([
'data' => true
]);
}
public function drop(Request $request)
{
if ($request->input('id')) {
$server = ServerTrojan::find($request->input('id'));
if (!$server) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '节点ID不存在');
2023-11-17 01:44:01 -05:00
}
}
return response([
'data' => $server->delete()
]);
}
public function update(ServerTrojanUpdate $request)
{
$params = $request->only([
'show',
]);
$server = ServerTrojan::find($request->input('id'));
if (!$server) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '该服务器不存在');
2023-11-17 01:44:01 -05:00
}
try {
$server->update($params);
} catch (\Exception $e) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '保存失败');
2023-11-17 01:44:01 -05:00
}
return response([
'data' => true
]);
}
public function copy(Request $request)
{
$server = ServerTrojan::find($request->input('id'));
$server->show = 0;
if (!$server) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '服务器不存在');
2023-11-17 01:44:01 -05:00
}
if (!ServerTrojan::create($server->toArray())) {
2023-12-04 07:40:49 -05:00
throw new ApiException(500, '复制失败');
2023-11-17 01:44:01 -05:00
}
return response([
'data' => true
]);
}
public function viewConfig(Request $request)
{
$serverService = new ServerService();
$config = $serverService->getTrojanConfig($request->input('node_id'), 23333);
return response([
'data' => $config
]);
}
}