2020-04-04 06:50:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
|
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Response;
|
2020-04-04 20:10:18 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-04-04 06:50:06 +00:00
|
|
|
use Lcobucci\JWT\Builder;
|
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
|
|
|
use Lcobucci\JWT\Signer\Key;
|
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\NodeRepository;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonTransferRepository;
|
|
|
|
|
|
|
|
class ServerTransferController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\NodeRepository
|
|
|
|
*/
|
|
|
|
private $nodeRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonTransferRepository
|
|
|
|
*/
|
|
|
|
private $daemonTransferRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ServerTransferController constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
|
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository
|
|
|
|
* @param DaemonTransferRepository $daemonTransferRepository
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ServerRepository $repository,
|
|
|
|
NodeRepository $nodeRepository,
|
|
|
|
DaemonTransferRepository $daemonTransferRepository
|
|
|
|
) {
|
|
|
|
$this->repository = $repository;
|
|
|
|
$this->nodeRepository = $nodeRepository;
|
|
|
|
$this->daemonTransferRepository = $daemonTransferRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 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.
|
|
|
|
if (!$request->input('successful')) {
|
|
|
|
// $this->suspensionService->toggle($server, 'unsuspend');
|
|
|
|
return JsonResponse::create([], Response::HTTP_NO_CONTENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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-04 06:50:06 +00:00
|
|
|
->getToken($signer, new Key($server->node->daemonSecret));
|
|
|
|
|
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.
|
|
|
|
$this->daemonTransferRepository
|
|
|
|
->setServer($server)
|
|
|
|
->setNode($this->nodeRepository->find($server->transfer->new_node))
|
|
|
|
->notify($server, $server->node, $token->__toString());
|
2020-04-04 06:50:06 +00:00
|
|
|
|
|
|
|
return JsonResponse::create([], Response::HTTP_NO_CONTENT);
|
|
|
|
}
|
|
|
|
}
|