2020-04-04 06:50:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
|
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Lcobucci\JWT\Builder;
|
2020-04-04 06:50:06 +00:00
|
|
|
use Illuminate\Http\Request;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Lcobucci\JWT\Signer\Key;
|
2020-04-05 00:28:09 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2020-04-04 06:50:06 +00:00
|
|
|
use Illuminate\Http\Response;
|
2020-04-04 22:24:58 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-04-04 06:50:06 +00:00
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
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-04-04 22:24:58 +00:00
|
|
|
use Pterodactyl\Services\Servers\SuspensionService;
|
2020-04-04 06:50:06 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\NodeRepository;
|
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\Contracts\Repository\AllocationRepositoryInterface;
|
|
|
|
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
|
|
|
|
{
|
2020-04-04 22:16:18 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
2020-04-04 06:50:06 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $allocationRepository;
|
|
|
|
|
2020-04-04 06:50:06 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\NodeRepository
|
|
|
|
*/
|
|
|
|
private $nodeRepository;
|
|
|
|
|
2020-04-05 00:28:09 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
|
|
|
|
*/
|
|
|
|
private $daemonServerRepository;
|
|
|
|
|
2020-04-04 06:50:06 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonTransferRepository
|
|
|
|
*/
|
|
|
|
private $daemonTransferRepository;
|
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
|
|
|
|
*/
|
|
|
|
private $configurationStructureService;
|
|
|
|
|
2020-04-05 00:28:09 +00:00
|
|
|
/**
|
|
|
|
* @var \Psr\Log\LoggerInterface
|
|
|
|
*/
|
|
|
|
private $writer;
|
|
|
|
|
2020-04-04 06:50:06 +00:00
|
|
|
/**
|
|
|
|
* ServerTransferController constructor.
|
|
|
|
*
|
2020-04-04 22:16:18 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2020-04-04 06:50:06 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
|
2020-04-04 22:16:18 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
|
2020-04-04 06:50:06 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository
|
2020-04-05 00:28:09 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
|
2020-04-04 22:16:18 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonTransferRepository $daemonTransferRepository
|
|
|
|
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
|
2020-04-05 00:28:09 +00:00
|
|
|
* @param \Psr\Log\LoggerInterface $writer
|
2020-04-04 06:50:06 +00:00
|
|
|
*/
|
|
|
|
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-04 22:16:18 +00:00
|
|
|
AllocationRepositoryInterface $allocationRepository,
|
2020-04-04 06:50:06 +00:00
|
|
|
NodeRepository $nodeRepository,
|
2020-04-05 00:28:09 +00:00
|
|
|
DaemonServerRepository $daemonServerRepository,
|
2020-04-04 22:16:18 +00:00
|
|
|
DaemonTransferRepository $daemonTransferRepository,
|
|
|
|
ServerConfigurationStructureService $configurationStructureService,
|
2020-04-05 00:28:09 +00:00
|
|
|
LoggerInterface $writer
|
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-04 22:16:18 +00:00
|
|
|
$this->allocationRepository = $allocationRepository;
|
2020-04-04 06:50:06 +00:00
|
|
|
$this->nodeRepository = $nodeRepository;
|
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-04-05 00:28:09 +00:00
|
|
|
$this->writer = $writer;
|
2020-04-04 06:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The daemon notifies us about the archive status.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2020-04-04 20:10:18 +00:00
|
|
|
* @param string $uuid
|
2020-04-04 06:50:06 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
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
|
|
|
*/
|
2020-04-04 20:10:18 +00:00
|
|
|
public function archive(Request $request, string $uuid)
|
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.
|
2020-04-04 22:24:58 +00:00
|
|
|
if (! $request->input('successful')) {
|
2020-12-17 02:24:39 +00:00
|
|
|
$transfer = $server->transfer;
|
|
|
|
|
|
|
|
$transfer->forceFill([
|
2020-12-16 16:34:47 +00:00
|
|
|
'successful' => false,
|
|
|
|
])->saveOrFail();
|
2020-04-04 22:25:41 +00:00
|
|
|
|
2020-12-17 02:24:39 +00:00
|
|
|
$allocationIds = json_decode($transfer->new_additional_allocations);
|
|
|
|
array_push($allocationIds, $transfer->new_allocation);
|
|
|
|
|
|
|
|
// Release the reserved allocations.
|
|
|
|
$this->allocationRepository->updateWhereIn('id', $allocationIds, ['server_id' => null]);
|
|
|
|
|
2020-12-16 16:34:47 +00:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2020-04-04 06:50:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
$server->node_id = $server->transfer->new_node;
|
|
|
|
|
|
|
|
$data = $this->configurationStructureService->handle($server);
|
|
|
|
$data['suspended'] = false;
|
|
|
|
$data['service']['skip_scripts'] = true;
|
|
|
|
|
|
|
|
$allocations = $server->getAllocationMappings();
|
|
|
|
$data['allocations']['default']['ip'] = array_key_first($allocations);
|
|
|
|
$data['allocations']['default']['port'] = $allocations[$data['allocations']['default']['ip']][0];
|
|
|
|
|
2020-04-04 06:50:06 +00:00
|
|
|
$now = Chronos::now();
|
|
|
|
$signer = new Sha256;
|
|
|
|
|
|
|
|
$token = (new Builder)->issuedBy(config('app.url'))
|
|
|
|
->permittedFor($server->node->getConnectionAddress())
|
|
|
|
->identifiedBy(hash('sha256', $server->uuid), true)
|
|
|
|
->issuedAt($now->getTimestamp())
|
|
|
|
->canOnlyBeUsedAfter($now->getTimestamp())
|
|
|
|
->expiresAt($now->addMinutes(15)->getTimestamp())
|
2020-04-04 20:10:18 +00:00
|
|
|
->relatedTo($server->uuid, true)
|
2020-04-10 22:15:38 +00:00
|
|
|
->getToken($signer, new Key($server->node->getDecryptedKey()));
|
2020-04-04 06:50:06 +00:00
|
|
|
|
2020-12-17 01:54:01 +00:00
|
|
|
// 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();
|
|
|
|
|
2020-04-04 20:10:18 +00:00
|
|
|
// 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
|
|
|
try {
|
2020-12-16 16:34:47 +00:00
|
|
|
/** @var \Pterodactyl\Models\Node $newNode */
|
|
|
|
$newNode = $this->nodeRepository->find($server->transfer->new_node);
|
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
$this->daemonTransferRepository
|
|
|
|
->setServer($server)
|
2020-12-16 16:34:47 +00:00
|
|
|
->setNode($newNode)
|
2020-04-04 22:16:18 +00:00
|
|
|
->notify($server, $data, $server->node, $token->__toString());
|
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:34:47 +00:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2020-04-04 22:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The daemon notifies us about a transfer failure.
|
|
|
|
*
|
|
|
|
* @param string $uuid
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
public function failure(string $uuid)
|
|
|
|
{
|
|
|
|
$server = $this->repository->getByUuid($uuid);
|
|
|
|
$transfer = $server->transfer;
|
|
|
|
|
|
|
|
$allocationIds = json_decode($transfer->new_additional_allocations);
|
|
|
|
array_push($allocationIds, $transfer->new_allocation);
|
|
|
|
|
|
|
|
// Remove the new allocations.
|
|
|
|
$this->allocationRepository->updateWhereIn('id', $allocationIds, ['server_id' => null]);
|
|
|
|
|
2020-12-16 16:34:47 +00:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2020-04-04 22:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The daemon notifies us about a transfer success.
|
|
|
|
*
|
|
|
|
* @param string $uuid
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
public function success(string $uuid)
|
|
|
|
{
|
|
|
|
$server = $this->repository->getByUuid($uuid);
|
|
|
|
$transfer = $server->transfer;
|
|
|
|
|
|
|
|
$allocationIds = json_decode($transfer->old_additional_allocations);
|
|
|
|
array_push($allocationIds, $transfer->old_allocation);
|
|
|
|
|
|
|
|
// Begin a transaction.
|
|
|
|
$this->connection->beginTransaction();
|
|
|
|
|
|
|
|
// Remove the old allocations.
|
|
|
|
$this->allocationRepository->updateWhereIn('id', $allocationIds, ['server_id' => null]);
|
|
|
|
|
|
|
|
// Update the server's allocation_id and node_id.
|
|
|
|
$server->allocation_id = $transfer->new_allocation;
|
|
|
|
$server->node_id = $transfer->new_node;
|
2020-12-16 16:34:47 +00:00
|
|
|
$server->saveOrFail();
|
2020-04-04 22:16:18 +00:00
|
|
|
|
|
|
|
// Mark the transfer as successful.
|
|
|
|
$transfer->successful = true;
|
2020-12-16 16:34:47 +00:00
|
|
|
$transfer->saveOrFail();
|
2020-04-04 22:16:18 +00:00
|
|
|
|
|
|
|
// Commit the transaction.
|
|
|
|
$this->connection->commit();
|
|
|
|
|
2020-04-05 00:28:09 +00:00
|
|
|
// Delete the server from the old node
|
|
|
|
try {
|
|
|
|
$this->daemonServerRepository->setServer($server)->delete();
|
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
$this->writer->warning($exception);
|
|
|
|
}
|
|
|
|
|
2020-04-04 22:16:18 +00:00
|
|
|
// Unsuspend the server
|
|
|
|
$server->load('node');
|
2020-04-04 06:50:06 +00:00
|
|
|
|
2020-12-16 16:34:47 +00:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2020-04-04 06:50:06 +00:00
|
|
|
}
|
|
|
|
}
|