Files
pterodactyl-panel/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php
T

80 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
use Illuminate\Http\JsonResponse;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\Request;
use Pterodactyl\Contracts\Nodes\ResetsServerStates;
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Http\Requests\Api\Remote\RemoteRequestNode;
use Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Server;
use Pterodactyl\Services\Eggs\EggConfigurationService;
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
2026-10-05 18:10:18 -04:00
use Throwable;
class ServerDetailsController extends Controller
{
/**
* Returns details about the server that allows Wings to self-recover and ensure
* that the state of the server matches the Panel at all times.
*/
2026-10-05 18:10:18 -04:00
public function __invoke(Request $request, EggConfigurationService $eggConfiguration, ServerConfigurationStructureService $configuration, string $uuid): JsonResponse
{
2026-10-05 18:10:18 -04:00
$node = RemoteRequestNode::get($request);
2026-10-05 18:10:18 -04:00
$server = Server::query()->with('node')->whereUuidOrShort($uuid)->firstOrFail();
$transfer = $server->transfer;
// If the server is being transferred allow either node to request information about
// the server. If the server is not being transferred only the target node is allowed
// to fetch these details.
$valid = $transfer
? $node->id === $transfer->old_node || $node->id === $transfer->new_node
: $node->id === $server->node_id;
2026-10-05 18:10:18 -04:00
throw_unless($valid, HttpForbiddenException::class, 'Requesting node does not have permission to access this server.');
return new JsonResponse([
2026-10-05 18:10:18 -04:00
'settings' => $configuration->handle($server),
'process_configuration' => $eggConfiguration->handle($server),
]);
}
/**
* Lists all servers with their configurations that are assigned to the requesting node.
*/
2026-10-05 18:10:18 -04:00
public function list(Request $request, ServerConfigurationStructureService $configuration, EggConfigurationService $eggConfiguration): JsonResponse
{
2026-10-05 18:10:18 -04:00
$node = RemoteRequestNode::get($request);
2022-10-14 10:59:20 -06:00
// Avoid run-away N+1 SQL queries by preloading the relationships that are used
// within each of the services called below.
2026-10-05 18:10:18 -04:00
$servers = Server::query()->with('allocations', 'egg.variables', 'egg.configFrom', 'serverVariables', 'mounts', 'location')
->where('node_id', $node->id)
// If you don't cast this to a string you'll end up with a stringified per_page returned in
// the metadata, and then Wings will panic crash as a result.
2026-10-05 18:10:18 -04:00
->paginate($request->integer('per_page', 50));
2026-10-05 18:10:18 -04:00
return (new ServerConfigurationCollection($servers, $eggConfiguration, $configuration))->response();
}
/**
* Resets the state of all servers on the node to be normal. This is triggered
* when Wings restarts and is useful for ensuring that any servers on the node
* do not get incorrectly stuck in installing/restoring from backup states since
* a Wings reboot would completely stop those processes.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
*/
2026-10-05 18:10:18 -04:00
public function resetState(Request $request, ResetsServerStates $states): JsonResponse
{
2026-10-05 18:10:18 -04:00
$states->reset(RemoteRequestNode::get($request));
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
}