Path: blob/1.0-develop/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php
14056 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;34use Illuminate\Http\Request;5use Pterodactyl\Models\Node;6use Webmozart\Assert\Assert;7use Illuminate\Http\Response;8use Illuminate\Http\JsonResponse;9use Pterodactyl\Models\Allocation;10use Illuminate\Support\Facades\Log;11use Pterodactyl\Models\ServerTransfer;12use Illuminate\Database\ConnectionInterface;13use Pterodactyl\Http\Controllers\Controller;14use Pterodactyl\Exceptions\Http\HttpForbiddenException;15use Pterodactyl\Repositories\Eloquent\ServerRepository;16use Pterodactyl\Repositories\Wings\DaemonServerRepository;17use Symfony\Component\HttpKernel\Exception\ConflictHttpException;18use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;1920class ServerTransferController extends Controller21{22/**23* ServerTransferController constructor.24*/25public function __construct(26private ConnectionInterface $connection,27private ServerRepository $repository,28private DaemonServerRepository $daemonServerRepository,29) {30}3132/**33* The daemon notifies us about a transfer failure.34*35* @throws \Throwable36*/37public function failure(Request $request, string $uuid): JsonResponse38{39$server = $this->repository->getByUuid($uuid);40$transfer = $server->transfer;41if (is_null($transfer)) {42throw new ConflictHttpException('Server is not being transferred.');43}4445/* @var Node $node */46Assert::isInstanceOf($node = $request->attributes->get('node'), Node::class);4748// Either node can tell the panel that the transfer has failed. Only the new node49// can tell the panel that it was successful.50if (! $node->is($transfer->newNode) && ! $node->is($transfer->oldNode)) {51throw new HttpForbiddenException('Requesting node does not have permission to access this server.');52}5354return $this->processFailedTransfer($transfer);55}5657/**58* The daemon notifies us about a transfer success.59*60* @throws \Throwable61*/62public function success(Request $request, string $uuid): JsonResponse63{64$server = $this->repository->getByUuid($uuid);65$transfer = $server->transfer;66if (is_null($transfer)) {67throw new ConflictHttpException('Server is not being transferred.');68}6970/* @var Node $node */71Assert::isInstanceOf($node = $request->attributes->get('node'), Node::class);7273// Only the new node communicates a successful state to the panel, so we should74// not allow the old node to hit this endpoint.75if (! $node->is($transfer->newNode)) {76throw new HttpForbiddenException('Requesting node does not have permission to access this server.');77}7879/** @var \Pterodactyl\Models\Server $server */80$server = $this->connection->transaction(function () use ($server, $transfer) {81$allocations = array_merge([$transfer->old_allocation], $transfer->old_additional_allocations);8283// Remove the old allocations for the server and re-assign the server to the new84// primary allocation and node.85Allocation::query()->whereIn('id', $allocations)->update(['server_id' => null]);86$server->update([87'allocation_id' => $transfer->new_allocation,88'node_id' => $transfer->new_node,89]);9091$server = $server->fresh();92$server->transfer->update(['successful' => true]);9394return $server;95});9697// Delete the server from the old node making sure to point it to the old node so98// that we do not delete it from the new node the server was transferred to.99try {100$this->daemonServerRepository101->setServer($server)102->setNode($transfer->oldNode)103->delete();104} catch (DaemonConnectionException $exception) {105Log::warning($exception, ['transfer_id' => $server->transfer->id]);106}107108return new JsonResponse([], Response::HTTP_NO_CONTENT);109}110111/**112* Release all the reserved allocations for this transfer and mark it as failed in113* the database.114*115* @throws \Throwable116*/117protected function processFailedTransfer(ServerTransfer $transfer): JsonResponse118{119$this->connection->transaction(function () use (&$transfer) {120$transfer->forceFill(['successful' => false])->saveOrFail();121122$allocations = array_merge([$transfer->new_allocation], $transfer->new_additional_allocations);123Allocation::query()->whereIn('id', $allocations)->update(['server_id' => null]);124});125126return new JsonResponse([], Response::HTTP_NO_CONTENT);127}128}129130131