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\Models\ServerVless;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use ParagonIE_Sodium_Compat as SodiumCompat;
|
|
|
|
use App\Utils\Helper;
|
|
|
|
|
|
|
|
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',
|
|
|
|
'tls' => 'required|in:0,1,2',
|
|
|
|
'tls_settings' => 'nullable|array',
|
|
|
|
'flow' => 'nullable|in:xtls-rprx-vision',
|
|
|
|
'network' => 'required',
|
|
|
|
'network_settings' => 'nullable|array',
|
|
|
|
'tags' => 'nullable|array',
|
|
|
|
'excludes' => 'nullable|array',
|
|
|
|
'ips' => 'nullable|array',
|
|
|
|
'rate' => 'required',
|
|
|
|
'show' => 'nullable|in:0,1',
|
|
|
|
'sort' => 'nullable'
|
2023-11-23 00:37:41 -05:00
|
|
|
],[
|
|
|
|
'name.required' => '节点名称不能为空',
|
|
|
|
'group_id.required' => '权限组不能为空',
|
|
|
|
'host.required' => '节点地址不能为空',
|
|
|
|
'port.required' => '连接端口不能为空',
|
|
|
|
'server_port' => '服务端口不能为空',
|
|
|
|
'rate.required' => '倍率不能为空',
|
|
|
|
'network.required' => '协议不能为空',
|
2023-11-17 01:44:01 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
if (isset($params['tls']) && (int)$params['tls'] === 2) {
|
|
|
|
$keyPair = SodiumCompat::crypto_box_keypair();
|
|
|
|
$params['tls_settings'] = $params['tls_settings'] ?? [];
|
|
|
|
if (!isset($params['tls_settings']['public_key'])) {
|
|
|
|
$params['tls_settings']['public_key'] = Helper::base64EncodeUrlSafe(SodiumCompat::crypto_box_publickey($keyPair));
|
|
|
|
}
|
|
|
|
if (!isset($params['tls_settings']['private_key'])) {
|
|
|
|
$params['tls_settings']['private_key'] = Helper::base64EncodeUrlSafe(SodiumCompat::crypto_box_secretkey($keyPair));
|
|
|
|
}
|
|
|
|
if (!isset($params['tls_settings']['short_id'])) {
|
|
|
|
$params['tls_settings']['short_id'] = substr(sha1($params['tls_settings']['private_key']), 0, 8);
|
|
|
|
}
|
|
|
|
if (!isset($params['tls_settings']['server_port'])) {
|
|
|
|
$params['tls_settings']['server_port'] = "443";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->input('id')) {
|
|
|
|
$server = ServerVless::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 (!ServerVless::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 = ServerVless::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(Request $request)
|
|
|
|
{
|
|
|
|
$params = $request->validate([
|
|
|
|
'show' => 'nullable|in:0,1',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$server = ServerVless::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 = ServerVless::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 (!ServerVless::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
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|