v2board/app/Http/Controllers/V1/Admin/Server/VlessController.php

120 lines
3.4 KiB
PHP
Raw Normal View History

2023-07-17 15:40:08 +08:00
<?php
namespace App\Http\Controllers\V1\Admin\Server;
use App\Http\Controllers\Controller;
use App\Models\ServerVless;
use Illuminate\Http\Request;
use ParagonIE_Sodium_Compat as SodiumCompat;
2023-08-02 01:52:25 +08:00
use App\Utils\Helper;
2023-07-17 15:40:08 +08:00
class VlessController extends Controller
{
public function save(Request $request)
{
$params = $request->validate([
'group_id' => 'required',
'route_id' => 'nullable|array',
'name' => 'required',
'parent_id' => 'nullable|integer',
'host' => 'required',
'port' => 'required',
'server_port' => 'required',
2023-08-02 01:52:25 +08:00
'tls' => 'required|in:0,1,2',
2023-07-17 15:40:08 +08:00
'tls_settings' => 'nullable|array',
2023-07-17 15:50:33 +08:00
'flow' => 'nullable|in:xtls-rprx-vision',
2023-07-17 15:40:08 +08:00
'network' => 'required',
'network_settings' => 'nullable|array',
'tags' => 'nullable|array',
'rate' => 'required',
'show' => 'nullable|in:0,1',
'sort' => 'nullable'
]);
2023-08-02 01:52:25 +08:00
if (isset($params['tls']) && (int)$params['tls'] === 2) {
2023-07-17 15:40:08 +08:00
$keyPair = SodiumCompat::crypto_box_keypair();
2023-08-02 01:52:25 +08:00
$params['tls_settings'] = $params['tls_settings'] ?? [];
2023-07-17 15:40:08 +08:00
if (!isset($params['tls_settings']['public_key'])) {
2023-08-02 01:52:25 +08:00
$params['tls_settings']['public_key'] = Helper::base64EncodeUrlSafe(SodiumCompat::crypto_box_publickey($keyPair));
2023-07-17 15:40:08 +08:00
}
if (!isset($params['tls_settings']['private_key'])) {
2023-08-02 01:52:25 +08:00
$params['tls_settings']['private_key'] = Helper::base64EncodeUrlSafe(SodiumCompat::crypto_box_secretkey($keyPair));
2023-07-17 15:40:08 +08:00
}
}
if ($request->input('id')) {
$server = ServerVless::find($request->input('id'));
if (!$server) {
abort(500, '服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
}
return response([
'data' => true
]);
}
if (!ServerVless::create($params)) {
abort(500, '创建失败');
}
return response([
'data' => true
]);
}
public function drop(Request $request)
{
if ($request->input('id')) {
$server = ServerVless::find($request->input('id'));
if (!$server) {
abort(500, '节点ID不存在');
}
}
return response([
'data' => $server->delete()
]);
}
public function update(Request $request)
{
$params = $request->validate([
'show' => 'nullable|in:0,1',
]);
$server = ServerVless::find($request->input('id'));
if (!$server) {
abort(500, '该服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
}
return response([
'data' => true
]);
}
public function copy(Request $request)
{
$server = ServerVless::find($request->input('id'));
$server->show = 0;
if (!$server) {
abort(500, '服务器不存在');
}
if (!ServerVless::create($server->toArray())) {
abort(500, '复制失败');
}
return response([
'data' => true
]);
}
}