2023-11-17 01:44:01 -05:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\V1\Server;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Services\ServerService;
|
|
|
|
|
use App\Services\UserService;
|
|
|
|
|
use App\Utils\CacheKey;
|
|
|
|
|
use App\Utils\Helper;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
2025-01-10 08:57:26 -05:00
|
|
|
|
use App\Services\UserOnlineService;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2023-11-17 01:44:01 -05:00
|
|
|
|
|
|
|
|
|
class UniProxyController extends Controller
|
|
|
|
|
{
|
2025-01-10 08:57:26 -05:00
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly UserOnlineService $userOnlineService
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 01:44:01 -05:00
|
|
|
|
// 后端获取用户
|
|
|
|
|
public function user(Request $request)
|
|
|
|
|
{
|
|
|
|
|
ini_set('memory_limit', -1);
|
2025-01-06 12:20:11 -05:00
|
|
|
|
$node = $request->input('node_info');
|
|
|
|
|
$nodeType = $node->type;
|
|
|
|
|
$nodeId = $node->id;
|
|
|
|
|
Cache::put(CacheKey::get('SERVER_' . strtoupper($nodeType) . '_LAST_CHECK_AT', $nodeId), time(), 3600);
|
|
|
|
|
$users = ServerService::getAvailableUsers($node->group_ids);
|
2023-11-17 01:44:01 -05:00
|
|
|
|
|
|
|
|
|
$response['users'] = $users;
|
|
|
|
|
|
|
|
|
|
$eTag = sha1(json_encode($response));
|
2024-04-09 12:51:03 -04:00
|
|
|
|
if (strpos($request->header('If-None-Match'), $eTag) !== false) {
|
2023-11-17 01:44:01 -05:00
|
|
|
|
return response(null, 304);
|
2024-04-09 12:51:03 -04:00
|
|
|
|
}
|
2023-11-17 01:44:01 -05:00
|
|
|
|
|
|
|
|
|
return response($response)->header('ETag', "\"{$eTag}\"");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 后端提交数据
|
|
|
|
|
public function push(Request $request)
|
|
|
|
|
{
|
2025-01-08 22:21:48 -05:00
|
|
|
|
$res = json_decode(request()->getContent(), true);
|
|
|
|
|
if (!is_array($res)) {
|
|
|
|
|
return $this->fail([422, 'Invalid data format']);
|
|
|
|
|
}
|
2024-04-27 05:06:57 -04:00
|
|
|
|
$data = array_filter($res, function ($item) {
|
2025-01-08 22:21:48 -05:00
|
|
|
|
return is_array($item)
|
|
|
|
|
&& count($item) === 2
|
|
|
|
|
&& is_numeric($item[0])
|
|
|
|
|
&& is_numeric($item[1]);
|
2024-04-27 05:06:57 -04:00
|
|
|
|
});
|
2025-01-08 22:21:48 -05:00
|
|
|
|
if (empty($data)) {
|
|
|
|
|
return $this->success(true);
|
|
|
|
|
}
|
2025-01-06 12:20:11 -05:00
|
|
|
|
$node = $request->input('node_info');
|
|
|
|
|
$nodeType = $node->type;
|
|
|
|
|
$nodeId = $node->id;
|
2025-01-08 22:21:48 -05:00
|
|
|
|
|
|
|
|
|
Cache::put(
|
|
|
|
|
CacheKey::get('SERVER_' . strtoupper($nodeType) . '_ONLINE_USER', $nodeId),
|
|
|
|
|
count($data),
|
|
|
|
|
3600
|
|
|
|
|
);
|
|
|
|
|
Cache::put(
|
|
|
|
|
CacheKey::get('SERVER_' . strtoupper($nodeType) . '_LAST_PUSH_AT', $nodeId),
|
|
|
|
|
time(),
|
|
|
|
|
3600
|
|
|
|
|
);
|
|
|
|
|
|
2023-11-17 01:44:01 -05:00
|
|
|
|
$userService = new UserService();
|
2025-01-06 12:20:11 -05:00
|
|
|
|
$userService->trafficFetch($node->toArray(), $nodeType, $data);
|
2023-12-06 15:01:32 -05:00
|
|
|
|
return $this->success(true);
|
2023-11-17 01:44:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 后端获取配置
|
|
|
|
|
public function config(Request $request)
|
|
|
|
|
{
|
2025-01-06 12:20:11 -05:00
|
|
|
|
$node = $request->input('node_info');
|
|
|
|
|
$nodeType = $node->type;
|
|
|
|
|
$protocolSettings = $node->protocol_settings;
|
2025-01-08 22:21:48 -05:00
|
|
|
|
|
|
|
|
|
$serverPort = $node->server_port;
|
|
|
|
|
$host = $node->host;
|
|
|
|
|
|
|
|
|
|
$baseConfig = [
|
2025-01-12 10:24:56 -05:00
|
|
|
|
'server_port' => (int) $serverPort,
|
2025-01-08 22:21:48 -05:00
|
|
|
|
'network' => $protocolSettings['network'] ?? null,
|
2025-01-09 07:02:19 -05:00
|
|
|
|
'networkSettings' => $protocolSettings['network_settings'] ?? null,
|
2025-01-08 22:21:48 -05:00
|
|
|
|
];
|
|
|
|
|
|
2025-01-06 12:20:11 -05:00
|
|
|
|
$response = match ($nodeType) {
|
|
|
|
|
'shadowsocks' => [
|
2025-01-08 22:21:48 -05:00
|
|
|
|
...$baseConfig,
|
2025-01-06 12:20:11 -05:00
|
|
|
|
'cipher' => $protocolSettings['cipher'],
|
|
|
|
|
'obfs' => $protocolSettings['obfs'],
|
|
|
|
|
'obfs_settings' => $protocolSettings['obfs_settings'],
|
2025-01-08 22:21:48 -05:00
|
|
|
|
'server_key' => match ($protocolSettings['cipher']) {
|
|
|
|
|
'2022-blake3-aes-128-gcm' => Helper::getServerKey($node->created_at, 16),
|
|
|
|
|
'2022-blake3-aes-256-gcm' => Helper::getServerKey($node->created_at, 32),
|
|
|
|
|
default => null
|
|
|
|
|
}
|
2025-01-06 12:20:11 -05:00
|
|
|
|
],
|
|
|
|
|
'vmess' => [
|
2025-01-08 22:21:48 -05:00
|
|
|
|
...$baseConfig,
|
2025-01-12 10:24:56 -05:00
|
|
|
|
'tls' => (int) $protocolSettings['tls']
|
2025-01-06 12:20:11 -05:00
|
|
|
|
],
|
|
|
|
|
'trojan' => [
|
2025-01-08 22:21:48 -05:00
|
|
|
|
...$baseConfig,
|
|
|
|
|
'host' => $host,
|
2025-01-06 12:20:11 -05:00
|
|
|
|
'server_name' => $protocolSettings['server_name'],
|
|
|
|
|
],
|
|
|
|
|
'vless' => [
|
2025-01-08 22:21:48 -05:00
|
|
|
|
...$baseConfig,
|
2025-01-12 10:24:56 -05:00
|
|
|
|
'tls' => (int) $protocolSettings['tls'],
|
2025-01-06 12:20:11 -05:00
|
|
|
|
'flow' => $protocolSettings['flow'],
|
2025-01-08 22:21:48 -05:00
|
|
|
|
'tls_settings' => (int) $protocolSettings['tls'] === 1
|
|
|
|
|
? $protocolSettings['tls_settings']
|
|
|
|
|
: $protocolSettings['reality_settings']
|
2025-01-06 12:20:11 -05:00
|
|
|
|
],
|
|
|
|
|
'hysteria' => [
|
2025-01-12 10:24:56 -05:00
|
|
|
|
'version' => (int) $protocolSettings['version'],
|
2025-01-08 22:21:48 -05:00
|
|
|
|
'host' => $host,
|
2025-01-06 12:20:11 -05:00
|
|
|
|
'server_name' => $protocolSettings['tls']['server_name'],
|
2025-01-12 10:24:56 -05:00
|
|
|
|
'up_mbps' => (int) $protocolSettings['bandwidth']['up'],
|
|
|
|
|
'down_mbps' => (int) $protocolSettings['bandwidth']['down'],
|
|
|
|
|
...match ((int) $protocolSettings['version']) {
|
|
|
|
|
1 => ['obfs' => $protocolSettings['obfs']['password'] ?? null],
|
|
|
|
|
2 => [
|
|
|
|
|
'obfs' => $protocolSettings['obfs']['open'] ? $protocolSettings['obfs']['type'] : null,
|
|
|
|
|
'obfs-password' => $protocolSettings['obfs']['password'] ?? null
|
|
|
|
|
],
|
|
|
|
|
default => []
|
|
|
|
|
}
|
2025-01-06 12:20:11 -05:00
|
|
|
|
],
|
|
|
|
|
default => []
|
|
|
|
|
};
|
2025-01-08 22:21:48 -05:00
|
|
|
|
|
2023-11-17 01:44:01 -05:00
|
|
|
|
$response['base_config'] = [
|
2024-04-09 12:51:03 -04:00
|
|
|
|
'push_interval' => (int) admin_setting('server_push_interval', 60),
|
|
|
|
|
'pull_interval' => (int) admin_setting('server_pull_interval', 60)
|
2023-11-17 01:44:01 -05:00
|
|
|
|
];
|
2025-01-08 22:21:48 -05:00
|
|
|
|
|
|
|
|
|
if (!empty($node['route_ids'])) {
|
|
|
|
|
$response['routes'] = ServerService::getRoutes($node['route_ids']);
|
2023-11-17 01:44:01 -05:00
|
|
|
|
}
|
2025-01-08 22:21:48 -05:00
|
|
|
|
|
2023-11-17 01:44:01 -05:00
|
|
|
|
$eTag = sha1(json_encode($response));
|
2025-01-10 09:32:31 -05:00
|
|
|
|
if (strpos($request->header('If-None-Match', '') ?? '', $eTag) !== false) {
|
2024-04-09 12:51:03 -04:00
|
|
|
|
return response(null, 304);
|
2023-11-17 01:44:01 -05:00
|
|
|
|
}
|
|
|
|
|
return response($response)->header('ETag', "\"{$eTag}\"");
|
|
|
|
|
}
|
2023-11-17 20:10:55 -05:00
|
|
|
|
|
2025-01-10 08:57:26 -05:00
|
|
|
|
// 获取在线用户数据(wyx2685
|
|
|
|
|
public function alivelist(Request $request): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$node = $request->input('node_info');
|
|
|
|
|
$deviceLimitUsers = ServerService::getAvailableUsers($node->group_ids)
|
|
|
|
|
->where('device_limit', '>', 0);
|
|
|
|
|
$alive = $this->userOnlineService->getAliveList($deviceLimitUsers);
|
|
|
|
|
return response()->json(['alive' => (object) $alive]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 12:51:03 -04:00
|
|
|
|
// 后端提交在线数据
|
2025-01-10 08:57:26 -05:00
|
|
|
|
public function alive(Request $request): JsonResponse
|
2024-04-09 12:51:03 -04:00
|
|
|
|
{
|
2025-01-10 08:57:26 -05:00
|
|
|
|
$node = $request->input('node_info');
|
|
|
|
|
$data = json_decode(request()->getContent(), true);
|
|
|
|
|
if ($data === null) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'error' => 'Invalid online data'
|
|
|
|
|
], 400);
|
|
|
|
|
}
|
|
|
|
|
$this->userOnlineService->updateAliveData($data, $node->type, $node->id);
|
|
|
|
|
return response()->json(['data' => true]);
|
2024-04-09 12:51:03 -04:00
|
|
|
|
}
|
2023-11-17 01:44:01 -05:00
|
|
|
|
}
|