2020-04-04 06:50:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
2020-12-24 18:10:40 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2020-04-04 06:50:06 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Response;
|
2020-12-24 18:10:40 +00:00
|
|
|
use Pterodactyl\Models\Allocation;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Pterodactyl\Models\ServerTransfer;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2020-04-04 06:50:06 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2020-12-24 18:10:40 +00:00
|
|
|
use Pterodactyl\Services\Nodes\NodeJWTService;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
2020-04-05 00:28:09 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
2020-04-04 06:50:06 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonTransferRepository;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2020-04-04 22:16:18 +00:00
|
|
|
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
|
2020-04-04 06:50:06 +00:00
|
|
|
|
|
|
|
class ServerTransferController extends Controller
|
|
|
|
{
|
2021-03-05 17:03:12 +00:00
|
|
|
private ConnectionInterface $connection;
|
|
|
|
private ServerRepository $repository;
|
|
|
|
private DaemonServerRepository $daemonServerRepository;
|
|
|
|
private DaemonTransferRepository $daemonTransferRepository;
|
|
|
|
private ServerConfigurationStructureService $configurationStructureService;
|
|
|
|
private NodeJWTService $jwtService;
|
2020-04-05 00:28:09 +00:00
|
|
|
|
2020-04-04 06:50:06 +00:00
|
|
|
/**
|
|
|
|
* ServerTransferController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2020-04-04 22:16:18 +00:00
|
|
|
ConnectionInterface $connection,
|
2020-04-04 06:50:06 +00:00
|
|
|
ServerRepository $repository,
|
2020-04-05 00:28:09 +00:00
|
|
|
DaemonServerRepository $daemonServerRepository,
|
2020-04-04 22:16:18 +00:00
|
|
|
DaemonTransferRepository $daemonTransferRepository,
|
|
|
|
ServerConfigurationStructureService $configurationStructureService,
|
2020-12-24 18:10:40 +00:00
|
|
|
NodeJWTService $jwtService
|
2020-04-04 06:50:06 +00:00
|
|
|
) {
|
2020-04-04 22:16:18 +00:00
|
|
|
$this->connection = $connection;
|
2020-04-04 06:50:06 +00:00
|
|
|
$this->repository = $repository;
|
2020-04-05 00:28:09 +00:00
|
|
|
$this->daemonServerRepository = $daemonServerRepository;
|
2020-04-04 06:50:06 +00:00
|
|
|
$this->daemonTransferRepository = $daemonTransferRepository;
|
2020-04-04 22:16:18 +00:00
|
|
|
$this->configurationStructureService = $configurationStructureService;
|
2020-12-24 18:10:40 +00:00
|
|
|
$this->jwtService = $jwtService;
|
2020-04-04 06:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The daemon notifies us about the archive status.
|
|
|
|
*
|
2020-04-04 20:10:18 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2020-04-04 22:16:18 +00:00
|
|
|
* @throws \Throwable
|
2020-04-04 06:50:06 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function archive(Request $request, string $uuid): Response
|
2020-04-04 06:50:06 +00:00
|
|
|
{
|
2020-04-04 20:10:18 +00:00
|
|
|
$server = $this->repository->getByUuid($uuid);
|
|
|
|
|
2020-04-04 06:50:06 +00:00
|
|
|
// Unsuspend the server and don't continue the transfer.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$request->input('successful')) {
|
2020-12-24 18:10:40 +00:00
|
|
|
return $this->processFailedTransfer($server->transfer);
|
2020-04-04 06:50:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 18:10:40 +00:00
|
|
|
// We want to generate a new configuration using the new node_id value from the
|
|
|
|
// transfer, and not the old node value.
|
|
|
|
$data = $this->configurationStructureService->handle($server, [
|
|
|
|
'node_id' => $server->transfer->new_node,
|
|
|
|
]);
|
2020-04-04 22:16:18 +00:00
|
|
|
|
|
|
|
$allocations = $server->getAllocationMappings();
|
2020-12-24 18:10:40 +00:00
|
|
|
$primary = array_key_first($allocations);
|
|
|
|
Arr::set($data, 'allocations.default.ip', $primary);
|
|
|
|
Arr::set($data, 'allocations.default.port', $allocations[$primary][0]);
|
|
|
|
Arr::set($data, 'service.skip_scripts', true);
|
|
|
|
Arr::set($data, 'suspended', false);
|
|
|
|
|
|
|
|
$this->connection->transaction(function () use ($data, $server) {
|
2021-01-23 20:09:16 +00:00
|
|
|
// This token is used by the new node the server is being transferred to. It allows
|
2020-12-24 18:10:40 +00:00
|
|
|
// that node to communicate with the old node during the process to initiate the
|
|
|
|
// actual file transfer.
|
|
|
|
$token = $this->jwtService
|
2021-01-23 20:09:16 +00:00
|
|
|
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
|
2020-12-24 18:10:40 +00:00
|
|
|
->setSubject($server->uuid)
|
|
|
|
->handle($server->node, $server->uuid, 'sha256');
|
|
|
|
|
|
|
|
// Update the archived field on the transfer to make clients connect to the websocket
|
|
|
|
// on the new node to be able to receive transfer logs.
|
|
|
|
$server->transfer->forceFill(['archived' => true])->saveOrFail();
|
|
|
|
|
|
|
|
// On the daemon transfer repository, make sure to set the node after the server
|
|
|
|
// because setServer() tells the repository to use the server's node and not the one
|
|
|
|
// we want to specify.
|
2020-04-04 22:16:18 +00:00
|
|
|
$this->daemonTransferRepository
|
|
|
|
->setServer($server)
|
2020-12-24 18:10:40 +00:00
|
|
|
->setNode($server->transfer->newNode)
|
2021-01-23 20:09:16 +00:00
|
|
|
->notify($server, $data, $server->node, $token->toString());
|
2020-12-24 18:10:40 +00:00
|
|
|
});
|
2020-04-04 22:16:18 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return new Response('', Response::HTTP_NO_CONTENT);
|
2020-04-04 22:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The daemon notifies us about a transfer failure.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function failure(string $uuid): Response
|
2020-04-04 22:16:18 +00:00
|
|
|
{
|
|
|
|
$server = $this->repository->getByUuid($uuid);
|
|
|
|
|
2020-12-24 18:10:40 +00:00
|
|
|
return $this->processFailedTransfer($server->transfer);
|
2020-04-04 22:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The daemon notifies us about a transfer success.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function success(string $uuid): Response
|
2020-04-04 22:16:18 +00:00
|
|
|
{
|
|
|
|
$server = $this->repository->getByUuid($uuid);
|
|
|
|
$transfer = $server->transfer;
|
|
|
|
|
2020-12-24 18:10:40 +00:00
|
|
|
/** @var \Pterodactyl\Models\Server $server */
|
|
|
|
$server = $this->connection->transaction(function () use ($server, $transfer) {
|
2021-08-18 18:58:41 +00:00
|
|
|
$allocations = array_merge([$transfer->old_allocation], $transfer->old_additional_allocations);
|
2020-12-24 18:10:40 +00:00
|
|
|
|
|
|
|
// Remove the old allocations for the server and re-assign the server to the new
|
|
|
|
// primary allocation and node.
|
|
|
|
Allocation::query()->whereIn('id', $allocations)->update(['server_id' => null]);
|
|
|
|
$server->update([
|
|
|
|
'allocation_id' => $transfer->new_allocation,
|
|
|
|
'node_id' => $transfer->new_node,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$server = $server->fresh();
|
|
|
|
$server->transfer->update(['successful' => true]);
|
|
|
|
|
|
|
|
return $server;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Delete the server from the old node making sure to point it to the old node so
|
2021-03-05 17:03:12 +00:00
|
|
|
// that we do not delete it from the new node the server was transferred to.
|
2020-12-24 18:10:40 +00:00
|
|
|
try {
|
|
|
|
$this->daemonServerRepository
|
|
|
|
->setServer($server)
|
|
|
|
->setNode($transfer->oldNode)
|
|
|
|
->delete();
|
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
Log::warning($exception, ['transfer_id' => $server->transfer->id]);
|
|
|
|
}
|
2020-04-04 22:16:18 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return new Response('', Response::HTTP_NO_CONTENT);
|
2020-12-24 18:10:40 +00:00
|
|
|
}
|
2020-04-04 22:16:18 +00:00
|
|
|
|
2020-12-24 18:10:40 +00:00
|
|
|
/**
|
|
|
|
* Release all of the reserved allocations for this transfer and mark it as failed in
|
|
|
|
* the database.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
protected function processFailedTransfer(ServerTransfer $transfer): Response
|
2020-12-24 18:10:40 +00:00
|
|
|
{
|
|
|
|
$this->connection->transaction(function () use (&$transfer) {
|
|
|
|
$transfer->forceFill(['successful' => false])->saveOrFail();
|
2020-04-04 22:16:18 +00:00
|
|
|
|
2021-08-18 18:58:41 +00:00
|
|
|
$allocations = array_merge([$transfer->new_allocation], $transfer->new_additional_allocations);
|
2020-12-24 18:10:40 +00:00
|
|
|
Allocation::query()->whereIn('id', $allocations)->update(['server_id' => null]);
|
|
|
|
});
|
2020-04-05 00:28:09 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return new Response('', Response::HTTP_NO_CONTENT);
|
2020-04-04 06:50:06 +00:00
|
|
|
}
|
|
|
|
}
|