mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-03 10:30:51 +08:00
- Implement Workerman-based `xboard:ws-server` for real-time node synchronization. - Support custom routes, outbounds, and certificate configurations via JSON. - Optimize scheduled tasks with `lazyById` to minimize memory footprint. - Enhance reactivity using Observers for `Plan`, `Server`, and `ServerRoute`. - Expand protocol support for `httpupgrade`, `h2`, and `mieru`.
151 lines
5.4 KiB
PHP
151 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\V2\Server;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Jobs\UserAliveSyncJob;
|
|
use App\Services\UserService;
|
|
use App\Utils\CacheKey;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Log;
|
|
|
|
class ServerController extends Controller
|
|
{
|
|
/**
|
|
* server handshake api
|
|
*/
|
|
public function handshake(Request $request): JsonResponse
|
|
{
|
|
$websocket = ['enabled' => false];
|
|
|
|
if ((bool) admin_setting('server_ws_enable', 1)) {
|
|
$customUrl = trim((string) admin_setting('server_ws_url', ''));
|
|
|
|
if ($customUrl !== '') {
|
|
$wsUrl = rtrim($customUrl, '/');
|
|
} else {
|
|
$wsScheme = $request->isSecure() ? 'wss' : 'ws';
|
|
$wsUrl = "{$wsScheme}://{$request->getHost()}:8076";
|
|
}
|
|
|
|
$websocket = [
|
|
'enabled' => true,
|
|
'ws_url' => $wsUrl,
|
|
];
|
|
}
|
|
|
|
return response()->json([
|
|
'websocket' => $websocket
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* node report api - merge traffic + alive + status
|
|
* POST /api/v2/server/node/report
|
|
*/
|
|
public function report(Request $request): JsonResponse
|
|
{
|
|
$node = $request->attributes->get('node_info');
|
|
$nodeType = $node->type;
|
|
$nodeId = $node->id;
|
|
|
|
Cache::put(CacheKey::get('SERVER_' . strtoupper($nodeType) . '_LAST_CHECK_AT', $nodeId), time(), 3600);
|
|
|
|
// hanle traffic data
|
|
$traffic = $request->input('traffic');
|
|
if (is_array($traffic) && !empty($traffic)) {
|
|
$data = array_filter($traffic, function ($item) {
|
|
return is_array($item)
|
|
&& count($item) === 2
|
|
&& is_numeric($item[0])
|
|
&& is_numeric($item[1]);
|
|
});
|
|
|
|
if (!empty($data)) {
|
|
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
|
|
);
|
|
$userService = new UserService();
|
|
$userService->trafficFetch($node, $nodeType, $data);
|
|
}
|
|
}
|
|
|
|
// handle alive data
|
|
$alive = $request->input('alive');
|
|
if (is_array($alive) && !empty($alive)) {
|
|
UserAliveSyncJob::dispatch($alive, $nodeType, $nodeId);
|
|
}
|
|
|
|
// handle active connections
|
|
$online = $request->input('online');
|
|
if (is_array($online) && !empty($online)) {
|
|
$cacheTime = max(300, (int) admin_setting('server_push_interval', 60) * 3);
|
|
foreach ($online as $uid => $conn) {
|
|
$cacheKey = CacheKey::get("USER_ONLINE_CONN_{$nodeType}_{$nodeId}", $uid);
|
|
Cache::put($cacheKey, (int) $conn, $cacheTime);
|
|
}
|
|
}
|
|
|
|
// handle node status
|
|
$status = $request->input('status');
|
|
if (is_array($status) && !empty($status)) {
|
|
$statusData = [
|
|
'cpu' => (float) ($status['cpu'] ?? 0),
|
|
'mem' => [
|
|
'total' => (int) ($status['mem']['total'] ?? 0),
|
|
'used' => (int) ($status['mem']['used'] ?? 0),
|
|
],
|
|
'swap' => [
|
|
'total' => (int) ($status['swap']['total'] ?? 0),
|
|
'used' => (int) ($status['swap']['used'] ?? 0),
|
|
],
|
|
'disk' => [
|
|
'total' => (int) ($status['disk']['total'] ?? 0),
|
|
'used' => (int) ($status['disk']['used'] ?? 0),
|
|
],
|
|
'updated_at' => now()->timestamp,
|
|
];
|
|
|
|
$cacheTime = max(300, (int) admin_setting('server_push_interval', 60) * 3);
|
|
cache([
|
|
CacheKey::get('SERVER_' . strtoupper($nodeType) . '_LOAD_STATUS', $nodeId) => $statusData,
|
|
CacheKey::get('SERVER_' . strtoupper($nodeType) . '_LAST_LOAD_AT', $nodeId) => now()->timestamp,
|
|
], $cacheTime);
|
|
}
|
|
|
|
// handle node metrics (Metrics)
|
|
$metrics = $request->input('metrics');
|
|
if (is_array($metrics) && !empty($metrics)) {
|
|
$metricsData = [
|
|
'uptime' => (int) ($metrics['uptime'] ?? 0),
|
|
'inbound_speed' => (int) ($metrics['inbound_speed'] ?? 0),
|
|
'outbound_speed' => (int) ($metrics['outbound_speed'] ?? 0),
|
|
'active_connections' => (int) ($metrics['active_connections'] ?? 0),
|
|
'total_connections' => (int) ($metrics['total_connections'] ?? 0),
|
|
'speed_limiter' => $metrics['speed_limiter'] ?? [],
|
|
'cpu_per_core' => $metrics['cpu_per_core'] ?? [],
|
|
'gc' => $metrics['gc'] ?? [],
|
|
'api' => $metrics['api'] ?? [],
|
|
'updated_at' => now()->timestamp,
|
|
];
|
|
$cacheTime = max(300, (int) admin_setting('server_push_interval', 60) * 3);
|
|
Cache::put(
|
|
CacheKey::get('SERVER_' . strtoupper($nodeType) . '_METRICS', $nodeId),
|
|
$metricsData,
|
|
$cacheTime
|
|
);
|
|
}
|
|
|
|
return response()->json(['data' => true]);
|
|
}
|
|
}
|