2023-11-17 14:44:01 +08:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-21 14:57:54 +08:00
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
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;
|
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)
|
|
|
|
|
{
|
|
|
|
|
$this->validateRequest($request);
|
2025-11-22 20:33:38 +08:00
|
|
|
$nodeType = $request->input('node_type', $nodeType);
|
|
|
|
|
$normalizedNodeType = ServerModel::normalizeType($nodeType);
|
2025-01-21 14:57:54 +08:00
|
|
|
$serverInfo = ServerService::getServer(
|
|
|
|
|
$request->input('node_id'),
|
2025-11-22 20:33:38 +08:00
|
|
|
$normalizedNodeType
|
2025-01-21 14:57:54 +08:00
|
|
|
);
|
|
|
|
|
if (!$serverInfo) {
|
|
|
|
|
throw new ApiException('Server does not exist');
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-24 13:54:57 +08:00
|
|
|
$request->attributes->set('node_info', $serverInfo);
|
2025-01-21 14:57:54 +08:00
|
|
|
return $next($request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function validateRequest(Request $request): void
|
2023-11-17 14:44:01 +08:00
|
|
|
{
|
|
|
|
|
$request->validate([
|
2024-04-15 00:01:14 +08:00
|
|
|
'token' => [
|
2025-01-21 14:57:54 +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) {
|
2025-11-22 20:33:38 +08:00
|
|
|
if ($value === "v2node") {
|
|
|
|
|
$value = null;
|
|
|
|
|
}
|
2025-01-21 14:57:54 +08:00
|
|
|
if (!ServerModel::isValidType($value)) {
|
|
|
|
|
$fail("Invalid node type specified");
|
|
|
|
|
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
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|