2020-04-05 02:54:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
|
|
|
use Carbon\CarbonImmutable;
|
2020-05-10 02:43:58 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2020-04-05 02:54:59 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
|
|
|
use Pterodactyl\Models\Server;
|
2020-05-10 02:43:58 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-04-07 03:28:14 +00:00
|
|
|
use Pterodactyl\Services\Nodes\NodeJWTService;
|
2020-04-05 02:54:59 +00:00
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory;
|
2020-05-10 02:43:58 +00:00
|
|
|
use Pterodactyl\Extensions\Backups\BackupManager;
|
2020-04-05 02:54:59 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
2020-05-10 02:43:58 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2020-04-05 02:54:59 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Backups\DownloadBackupRequest;
|
|
|
|
|
|
|
|
class DownloadBackupController extends ClientApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonBackupRepository
|
|
|
|
*/
|
|
|
|
private $daemonBackupRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Routing\ResponseFactory
|
|
|
|
*/
|
|
|
|
private $responseFactory;
|
|
|
|
|
2020-04-07 03:28:14 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Nodes\NodeJWTService
|
|
|
|
*/
|
|
|
|
private $jwtService;
|
|
|
|
|
2020-05-10 02:43:58 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Extensions\Backups\BackupManager
|
|
|
|
*/
|
|
|
|
private $backupManager;
|
|
|
|
|
2020-04-05 02:54:59 +00:00
|
|
|
/**
|
|
|
|
* DownloadBackupController constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonBackupRepository $daemonBackupRepository
|
2020-04-07 03:28:14 +00:00
|
|
|
* @param \Pterodactyl\Services\Nodes\NodeJWTService $jwtService
|
2020-05-10 02:43:58 +00:00
|
|
|
* @param \Pterodactyl\Extensions\Backups\BackupManager $backupManager
|
2020-04-05 02:54:59 +00:00
|
|
|
* @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
DaemonBackupRepository $daemonBackupRepository,
|
2020-04-07 03:28:14 +00:00
|
|
|
NodeJWTService $jwtService,
|
2020-05-10 02:43:58 +00:00
|
|
|
BackupManager $backupManager,
|
2020-04-05 02:54:59 +00:00
|
|
|
ResponseFactory $responseFactory
|
|
|
|
) {
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->daemonBackupRepository = $daemonBackupRepository;
|
|
|
|
$this->responseFactory = $responseFactory;
|
2020-04-07 03:28:14 +00:00
|
|
|
$this->jwtService = $jwtService;
|
2020-05-10 02:43:58 +00:00
|
|
|
$this->backupManager = $backupManager;
|
2020-04-05 02:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download the backup for a given server instance. For daemon local files, the file
|
|
|
|
* will be streamed back through the Panel. For AWS S3 files, a signed URL will be generated
|
|
|
|
* which the user is redirected to.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Backups\DownloadBackupRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @param \Pterodactyl\Models\Backup $backup
|
2020-05-10 02:43:58 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2020-04-05 02:54:59 +00:00
|
|
|
*/
|
|
|
|
public function __invoke(DownloadBackupRequest $request, Server $server, Backup $backup)
|
2020-05-10 02:43:58 +00:00
|
|
|
{
|
|
|
|
switch ($backup->disk) {
|
|
|
|
case Backup::ADAPTER_WINGS:
|
|
|
|
$url = $this->getLocalBackupUrl($backup, $server, $request->user());
|
|
|
|
break;
|
|
|
|
case Backup::ADAPTER_AWS_S3:
|
|
|
|
$url = $this->getS3BackupUrl($backup, $server);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new BadRequestHttpException;
|
|
|
|
}
|
|
|
|
|
2020-07-12 22:42:32 +00:00
|
|
|
return new JsonResponse([
|
2020-05-10 02:43:58 +00:00
|
|
|
'object' => 'signed_url',
|
|
|
|
'attributes' => [
|
|
|
|
'url' => $url,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a signed URL that allows us to download a file directly out of a non-public
|
|
|
|
* S3 bucket by using a signed URL.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Backup $backup
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getS3BackupUrl(Backup $backup, Server $server)
|
|
|
|
{
|
|
|
|
/** @var \League\Flysystem\AwsS3v3\AwsS3Adapter $adapter */
|
|
|
|
$adapter = $this->backupManager->adapter(Backup::ADAPTER_AWS_S3);
|
|
|
|
|
|
|
|
$client = $adapter->getClient();
|
|
|
|
|
|
|
|
$request = $client->createPresignedRequest(
|
|
|
|
$client->getCommand('GetObject', [
|
|
|
|
'Bucket' => $adapter->getBucket(),
|
|
|
|
'Key' => sprintf('%s/%s.tar.gz', $server->uuid, $backup->uuid),
|
|
|
|
'ContentType' => 'application/x-gzip',
|
|
|
|
]),
|
|
|
|
CarbonImmutable::now()->addMinutes(5)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $request->getUri()->__toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a download link a backup stored on a wings instance.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Backup $backup
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @param \Pterodactyl\Models\User $user
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getLocalBackupUrl(Backup $backup, Server $server, User $user)
|
2020-04-05 02:54:59 +00:00
|
|
|
{
|
2020-04-07 03:28:14 +00:00
|
|
|
$token = $this->jwtService
|
|
|
|
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
|
|
|
|
->setClaims([
|
|
|
|
'backup_uuid' => $backup->uuid,
|
|
|
|
'server_uuid' => $server->uuid,
|
|
|
|
])
|
2020-05-10 02:43:58 +00:00
|
|
|
->handle($server->node, $user->id . $server->uuid);
|
2020-04-05 02:54:59 +00:00
|
|
|
|
2020-05-10 02:43:58 +00:00
|
|
|
return sprintf(
|
|
|
|
'%s/download/backup?token=%s',
|
|
|
|
$server->node->getConnectionAddress(),
|
2021-01-23 20:09:16 +00:00
|
|
|
$token->toString()
|
2020-05-10 02:43:58 +00:00
|
|
|
);
|
2020-04-05 02:54:59 +00:00
|
|
|
}
|
|
|
|
}
|