2023-11-17 14:44:01 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
2024-04-10 00:51:03 +08:00
|
|
|
|
use App\Exceptions\ApiException;
|
2025-01-21 14:57:54 +08:00
|
|
|
|
use App\Models\Server as ServerModel;
|
2026-04-17 02:27:47 +08:00
|
|
|
|
use App\Models\ServerMachine;
|
2024-04-10 00:51:03 +08:00
|
|
|
|
use App\Services\ServerService;
|
2023-11-17 14:44:01 +08:00
|
|
|
|
use Closure;
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
|
|
class Server
|
|
|
|
|
|
{
|
2025-01-21 14:57:54 +08:00
|
|
|
|
public function handle(Request $request, Closure $next, ?string $nodeType = null)
|
|
|
|
|
|
{
|
2026-04-17 02:27:47 +08:00
|
|
|
|
// 优先尝试 machine token 认证,兜底走旧的 server token 认证
|
|
|
|
|
|
if ($request->filled('machine_id')) {
|
|
|
|
|
|
$this->authenticateByMachine($request, $nodeType);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$this->authenticateByServerToken($request, $nodeType);
|
2025-01-21 14:57:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-17 02:27:47 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 旧模式:全局 server_token + node_id
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function authenticateByServerToken(Request $request, ?string $nodeType): void
|
2023-11-17 14:44:01 +08:00
|
|
|
|
{
|
|
|
|
|
|
$request->validate([
|
2024-04-15 00:01:14 +08:00
|
|
|
|
'token' => [
|
2026-04-17 02:27:47 +08:00
|
|
|
|
'string', 'required',
|
2024-04-15 00:01:14 +08:00
|
|
|
|
function ($attribute, $value, $fail) {
|
|
|
|
|
|
if ($value !== admin_setting('server_token')) {
|
2025-01-21 14:57:54 +08:00
|
|
|
|
$fail("Invalid {$attribute}");
|
2024-04-15 00:01:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
2023-11-19 09:54:40 +08:00
|
|
|
|
'node_id' => 'required',
|
2023-11-17 14:44:01 +08:00
|
|
|
|
'node_type' => [
|
2024-04-12 17:34:06 +08:00
|
|
|
|
'nullable',
|
2025-01-21 14:57:54 +08:00
|
|
|
|
function ($attribute, $value, $fail) use ($request) {
|
2026-04-17 02:27:47 +08:00
|
|
|
|
if ($value === 'v2node') {
|
2025-11-22 20:33:38 +08:00
|
|
|
|
$value = null;
|
|
|
|
|
|
}
|
2025-01-21 14:57:54 +08:00
|
|
|
|
if (!ServerModel::isValidType($value)) {
|
2026-04-17 02:27:47 +08:00
|
|
|
|
$fail('Invalid node type specified');
|
2025-01-21 14:57:54 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
$request->merge([$attribute => ServerModel::normalizeType($value)]);
|
2023-11-17 14:44:01 +08:00
|
|
|
|
},
|
2023-11-19 09:54:40 +08:00
|
|
|
|
]
|
2023-11-17 14:44:01 +08:00
|
|
|
|
]);
|
2026-04-17 02:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
$nodeType = $request->input('node_type', $nodeType);
|
|
|
|
|
|
$normalizedNodeType = ServerModel::normalizeType($nodeType);
|
|
|
|
|
|
$serverInfo = ServerService::getServer(
|
|
|
|
|
|
$request->input('node_id'),
|
|
|
|
|
|
$normalizedNodeType
|
|
|
|
|
|
);
|
|
|
|
|
|
if (!$serverInfo) {
|
|
|
|
|
|
throw new ApiException('Server does not exist');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$request->attributes->set('node_info', $serverInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 新模式:machine_id + machine token + node_id
|
|
|
|
|
|
*
|
|
|
|
|
|
* machine 认证后,node_id 必须属于该 machine 下的已启用节点。
|
|
|
|
|
|
* 下游控制器拿到的 node_info 与旧模式完全一致。
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function authenticateByMachine(Request $request, ?string $nodeType): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$isHandshake = $request->is('*/server/handshake') || $request->is('api/v2/server/handshake');
|
|
|
|
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
|
'machine_id' => 'required|integer',
|
|
|
|
|
|
'token' => 'required|string',
|
|
|
|
|
|
'node_id' => $isHandshake ? 'nullable|integer' : 'required|integer',
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
$machine = ServerMachine::where('id', $request->input('machine_id'))
|
|
|
|
|
|
->where('token', $request->input('token'))
|
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
|
|
if (!$machine) {
|
|
|
|
|
|
throw new ApiException('Machine not found or invalid token', 401);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!$machine->is_active) {
|
|
|
|
|
|
throw new ApiException('Machine is disabled', 403);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$nodeId = (int) $request->input('node_id');
|
|
|
|
|
|
$serverInfo = null;
|
|
|
|
|
|
|
|
|
|
|
|
if ($nodeId > 0) {
|
|
|
|
|
|
$serverInfo = ServerModel::where('id', $nodeId)
|
|
|
|
|
|
->where('machine_id', $machine->id)
|
|
|
|
|
|
->where('enabled', true)
|
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
|
|
if (!$serverInfo) {
|
|
|
|
|
|
throw new ApiException('Node not found on this machine');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$request->attributes->set('node_info', $serverInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新机器心跳
|
|
|
|
|
|
$machine->forceFill(['last_seen_at' => now()->timestamp])->saveQuietly();
|
|
|
|
|
|
|
|
|
|
|
|
$request->attributes->set('machine_info', $machine);
|
2023-11-17 14:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|