2023-11-17 01:44:01 -05:00
|
|
|
<?php
|
|
|
|
|
2025-01-06 12:20:11 -05:00
|
|
|
|
2023-11-17 01:44:01 -05:00
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2024-04-09 12:51:03 -04:00
|
|
|
use App\Exceptions\ApiException;
|
2025-01-06 12:20:11 -05:00
|
|
|
use App\Models\Server as ServerModel;
|
2024-04-09 12:51:03 -04:00
|
|
|
use App\Services\ServerService;
|
2023-11-17 01:44:01 -05:00
|
|
|
use Closure;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class Server
|
|
|
|
{
|
2025-01-06 12:20:11 -05:00
|
|
|
public function handle(Request $request, Closure $next, ?string $nodeType = null)
|
|
|
|
{
|
|
|
|
$this->validateRequest($request);
|
|
|
|
|
|
|
|
$serverInfo = ServerService::getServer(
|
|
|
|
$request->input('node_id'),
|
|
|
|
$request->input('node_type') ?? $nodeType
|
|
|
|
);
|
|
|
|
if (!$serverInfo) {
|
|
|
|
throw new ApiException('Server does not exist');
|
|
|
|
}
|
|
|
|
|
|
|
|
$request->merge(['node_info' => $serverInfo]);
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function validateRequest(Request $request): void
|
2023-11-17 01:44:01 -05:00
|
|
|
{
|
|
|
|
$request->validate([
|
2024-04-14 12:01:14 -04:00
|
|
|
'token' => [
|
2025-01-06 12:20:11 -05:00
|
|
|
'string',
|
|
|
|
'required',
|
2024-04-14 12:01:14 -04:00
|
|
|
function ($attribute, $value, $fail) {
|
|
|
|
if ($value !== admin_setting('server_token')) {
|
2025-01-06 12:20:11 -05:00
|
|
|
$fail("Invalid {$attribute}");
|
2024-04-14 12:01:14 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
2023-11-18 20:54:40 -05:00
|
|
|
'node_id' => 'required',
|
2023-11-17 01:44:01 -05:00
|
|
|
'node_type' => [
|
2024-10-06 12:27:11 -04:00
|
|
|
'required',
|
2024-04-12 05:34:06 -04:00
|
|
|
'nullable',
|
2025-01-06 12:20:11 -05:00
|
|
|
function ($attribute, $value, $fail) use ($request) {
|
|
|
|
if (!ServerModel::isValidType($value)) {
|
|
|
|
$fail("Invalid node type specified");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$request->merge([$attribute => ServerModel::normalizeType($value)]);
|
2023-11-17 01:44:01 -05:00
|
|
|
},
|
2023-11-18 20:54:40 -05:00
|
|
|
]
|
2023-11-17 01:44:01 -05:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|