2020-04-04 17:59:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
2021-01-18 01:51:09 +00:00
|
|
|
use Illuminate\Http\Request;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
2020-04-04 17:59:25 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2021-01-17 19:46:08 +00:00
|
|
|
use Pterodactyl\Models\AuditLog;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2021-01-18 01:51:09 +00:00
|
|
|
use Pterodactyl\Models\Permission;
|
2021-01-31 03:12:22 +00:00
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
2020-04-10 05:08:09 +00:00
|
|
|
use Pterodactyl\Services\Backups\DeleteBackupService;
|
2021-01-18 01:51:09 +00:00
|
|
|
use Pterodactyl\Services\Backups\DownloadLinkService;
|
2020-04-04 19:26:39 +00:00
|
|
|
use Pterodactyl\Services\Backups\InitiateBackupService;
|
2021-08-05 03:34:00 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
2021-01-18 01:51:09 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
|
2021-01-26 03:20:51 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\BackupTransformer;
|
2020-04-04 17:59:25 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
2021-01-18 01:51:09 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2020-04-04 17:59:25 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Backups\StoreBackupRequest;
|
|
|
|
|
|
|
|
class BackupController extends ClientApiController
|
|
|
|
{
|
2021-08-05 03:34:00 +00:00
|
|
|
private InitiateBackupService $initiateBackupService;
|
|
|
|
private DeleteBackupService $deleteBackupService;
|
|
|
|
private DownloadLinkService $downloadLinkService;
|
|
|
|
private DaemonBackupRepository $daemonRepository;
|
|
|
|
private BackupRepository $repository;
|
2020-04-10 05:35:38 +00:00
|
|
|
|
2020-04-04 19:26:39 +00:00
|
|
|
/**
|
|
|
|
* BackupController constructor.
|
|
|
|
*/
|
2020-04-10 05:35:38 +00:00
|
|
|
public function __construct(
|
2021-08-05 03:34:00 +00:00
|
|
|
DaemonBackupRepository $daemonRepository,
|
2020-04-10 05:35:38 +00:00
|
|
|
DeleteBackupService $deleteBackupService,
|
2021-01-18 01:51:09 +00:00
|
|
|
InitiateBackupService $initiateBackupService,
|
2021-08-05 03:34:00 +00:00
|
|
|
DownloadLinkService $downloadLinkService,
|
|
|
|
BackupRepository $repository
|
2020-04-10 05:35:38 +00:00
|
|
|
) {
|
2020-04-04 17:59:25 +00:00
|
|
|
parent::__construct();
|
2020-04-04 19:26:39 +00:00
|
|
|
|
2021-01-18 01:51:09 +00:00
|
|
|
$this->repository = $repository;
|
2020-04-04 19:26:39 +00:00
|
|
|
$this->initiateBackupService = $initiateBackupService;
|
2020-04-10 05:08:09 +00:00
|
|
|
$this->deleteBackupService = $deleteBackupService;
|
2021-01-18 01:51:09 +00:00
|
|
|
$this->downloadLinkService = $downloadLinkService;
|
2021-08-05 03:34:00 +00:00
|
|
|
$this->daemonRepository = $daemonRepository;
|
2020-04-04 17:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-08-05 03:34:00 +00:00
|
|
|
* Returns all the backups for a given server instance in a paginated
|
2020-04-04 17:59:25 +00:00
|
|
|
* result set.
|
|
|
|
*
|
2021-01-31 03:12:22 +00:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2020-04-04 17:59:25 +00:00
|
|
|
*/
|
2021-01-26 03:25:15 +00:00
|
|
|
public function index(Request $request, Server $server): array
|
2020-04-04 17:59:25 +00:00
|
|
|
{
|
2021-01-26 03:20:51 +00:00
|
|
|
if (!$request->user()->can(Permission::ACTION_BACKUP_READ, $server)) {
|
2021-01-31 03:12:22 +00:00
|
|
|
throw new AuthorizationException();
|
2021-01-18 01:51:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 20:01:42 +00:00
|
|
|
$limit = min($request->query('per_page') ?? 20, 50);
|
2021-01-17 19:46:08 +00:00
|
|
|
|
2020-12-06 20:01:42 +00:00
|
|
|
return $this->fractal->collection($server->backups()->paginate($limit))
|
2020-04-04 17:59:25 +00:00
|
|
|
->transformWith($this->getTransformer(BackupTransformer::class))
|
2021-08-05 03:34:00 +00:00
|
|
|
->addMeta([
|
|
|
|
'backup_count' => $this->repository->getNonFailedBackups($server)->count(),
|
|
|
|
])
|
2020-04-04 17:59:25 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the backup process for a server.
|
|
|
|
*
|
2021-01-26 03:25:15 +00:00
|
|
|
* @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation
|
|
|
|
* @throws \Spatie\Fractalistic\Exceptions\NoTransformerSpecified
|
|
|
|
* @throws \Throwable
|
2020-04-04 17:59:25 +00:00
|
|
|
*/
|
2021-01-26 03:25:15 +00:00
|
|
|
public function store(StoreBackupRequest $request, Server $server): array
|
2020-04-04 17:59:25 +00:00
|
|
|
{
|
2021-01-17 19:46:08 +00:00
|
|
|
/** @var \Pterodactyl\Models\Backup $backup */
|
2021-01-17 23:25:49 +00:00
|
|
|
$backup = $server->audit(AuditLog::SERVER__BACKUP_STARTED, function (AuditLog $model, Server $server) use ($request) {
|
2021-05-04 04:26:09 +00:00
|
|
|
$action = $this->initiateBackupService
|
|
|
|
->setIgnoredFiles(explode(PHP_EOL, $request->input('ignored') ?? ''));
|
|
|
|
|
|
|
|
// Only set the lock status if the user even has permission to delete backups,
|
|
|
|
// otherwise ignore this status. This gets a little funky since it isn't clear
|
|
|
|
// how best to allow a user to create a backup that is locked without also preventing
|
|
|
|
// them from just filling up a server with backups that can never be deleted?
|
|
|
|
if ($request->user()->can(Permission::ACTION_BACKUP_DELETE, $server)) {
|
|
|
|
$action->setIsLocked((bool) $request->input('is_locked'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$backup = $action->handle($server, $request->input('name'));
|
2021-01-17 19:46:08 +00:00
|
|
|
|
|
|
|
$model->metadata = ['backup_uuid' => $backup->uuid];
|
|
|
|
|
|
|
|
return $backup;
|
|
|
|
});
|
2020-04-04 19:26:39 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($backup)
|
|
|
|
->transformWith($this->getTransformer(BackupTransformer::class))
|
|
|
|
->toArray();
|
2020-04-04 17:59:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 04:26:09 +00:00
|
|
|
/**
|
|
|
|
* Toggles the lock status of a given backup for a server.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function toggleLock(Request $request, Server $server, Backup $backup): array
|
|
|
|
{
|
|
|
|
if (!$request->user()->can(Permission::ACTION_BACKUP_DELETE, $server)) {
|
|
|
|
throw new AuthorizationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$action = $backup->is_locked ? AuditLog::SERVER__BACKUP_UNLOCKED : AuditLog::SERVER__BACKUP_LOCKED;
|
|
|
|
$server->audit($action, function (AuditLog $audit) use ($backup) {
|
|
|
|
$audit->metadata = ['backup_uuid' => $backup->uuid];
|
|
|
|
|
|
|
|
$backup->update(['is_locked' => !$backup->is_locked]);
|
|
|
|
});
|
|
|
|
|
|
|
|
$backup->refresh();
|
|
|
|
|
|
|
|
return $this->fractal->item($backup)
|
|
|
|
->transformWith($this->getTransformer(BackupTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
/**
|
|
|
|
* Returns information about a single backup.
|
|
|
|
*
|
2021-01-31 03:12:22 +00:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2020-04-10 05:08:09 +00:00
|
|
|
*/
|
2021-01-26 03:25:15 +00:00
|
|
|
public function view(Request $request, Server $server, Backup $backup): array
|
2020-04-04 17:59:25 +00:00
|
|
|
{
|
2021-01-26 03:20:51 +00:00
|
|
|
if (!$request->user()->can(Permission::ACTION_BACKUP_READ, $server)) {
|
2021-01-31 03:12:22 +00:00
|
|
|
throw new AuthorizationException();
|
2021-01-18 01:51:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
return $this->fractal->item($backup)
|
|
|
|
->transformWith($this->getTransformer(BackupTransformer::class))
|
|
|
|
->toArray();
|
2020-04-04 17:59:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
/**
|
|
|
|
* Deletes a backup from the panel as well as the remote source where it is currently
|
|
|
|
* being stored.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2021-01-26 03:25:15 +00:00
|
|
|
public function delete(Request $request, Server $server, Backup $backup): JsonResponse
|
2020-04-04 17:59:25 +00:00
|
|
|
{
|
2021-01-26 03:20:51 +00:00
|
|
|
if (!$request->user()->can(Permission::ACTION_BACKUP_DELETE, $server)) {
|
2021-01-31 03:12:22 +00:00
|
|
|
throw new AuthorizationException();
|
2021-01-18 01:51:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 23:25:49 +00:00
|
|
|
$server->audit(AuditLog::SERVER__BACKUP_DELETED, function (AuditLog $audit) use ($backup) {
|
|
|
|
$audit->metadata = ['backup_uuid' => $backup->uuid];
|
|
|
|
|
2021-01-17 19:46:08 +00:00
|
|
|
$this->deleteBackupService->handle($backup);
|
|
|
|
});
|
2020-04-04 17:59:25 +00:00
|
|
|
|
2021-01-17 19:46:08 +00:00
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
2020-04-04 17:59:25 +00:00
|
|
|
}
|
2021-01-18 01:51:09 +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.
|
2021-01-31 03:12:22 +00:00
|
|
|
*
|
2021-03-07 17:45:27 +00:00
|
|
|
* @throws \Throwable
|
2021-01-31 03:12:22 +00:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2021-01-18 01:51:09 +00:00
|
|
|
*/
|
2021-01-26 03:25:15 +00:00
|
|
|
public function download(Request $request, Server $server, Backup $backup): JsonResponse
|
2021-01-18 01:51:09 +00:00
|
|
|
{
|
2021-01-26 03:20:51 +00:00
|
|
|
if (!$request->user()->can(Permission::ACTION_BACKUP_DOWNLOAD, $server)) {
|
2021-01-31 03:12:22 +00:00
|
|
|
throw new AuthorizationException();
|
2021-01-18 01:51:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 17:45:27 +00:00
|
|
|
if ($backup->disk !== Backup::ADAPTER_AWS_S3 && $backup->disk !== Backup::ADAPTER_WINGS) {
|
|
|
|
throw new BadRequestHttpException('The backup requested references an unknown disk driver type and cannot be downloaded.');
|
2021-01-18 01:51:09 +00:00
|
|
|
}
|
2021-03-07 17:45:27 +00:00
|
|
|
|
|
|
|
$url = $this->downloadLinkService->handle($backup, $request->user());
|
|
|
|
$server->audit(AuditLog::SERVER__BACKUP_DOWNLOADED, function (AuditLog $audit) use ($backup) {
|
|
|
|
$audit->metadata = ['backup_uuid' => $backup->uuid];
|
|
|
|
});
|
|
|
|
|
|
|
|
return new JsonResponse([
|
|
|
|
'object' => 'signed_url',
|
|
|
|
'attributes' => ['url' => $url],
|
|
|
|
]);
|
2021-01-18 01:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles restoring a backup by making a request to the Wings instance telling it
|
|
|
|
* to begin the process of finding (or downloading) the backup and unpacking it
|
|
|
|
* over the server files.
|
|
|
|
*
|
|
|
|
* If the "truncate" flag is passed through in this request then all of the
|
|
|
|
* files that currently exist on the server will be deleted before restoring.
|
|
|
|
* Otherwise the archive will simply be unpacked over the existing files.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2021-01-26 03:25:15 +00:00
|
|
|
public function restore(Request $request, Server $server, Backup $backup): JsonResponse
|
2021-01-18 01:51:09 +00:00
|
|
|
{
|
2021-01-26 03:20:51 +00:00
|
|
|
if (!$request->user()->can(Permission::ACTION_BACKUP_RESTORE, $server)) {
|
2021-01-31 03:12:22 +00:00
|
|
|
throw new AuthorizationException();
|
2021-01-18 01:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cannot restore a backup unless a server is fully installed and not currently
|
|
|
|
// processing a different backup restoration request.
|
2021-01-26 03:20:51 +00:00
|
|
|
if (!is_null($server->status)) {
|
2021-01-18 01:51:09 +00:00
|
|
|
throw new BadRequestHttpException('This server is not currently in a state that allows for a backup to be restored.');
|
|
|
|
}
|
|
|
|
|
2021-08-04 02:45:25 +00:00
|
|
|
if (!$backup->is_successful && is_null($backup->completed_at)) {
|
2021-01-19 04:11:49 +00:00
|
|
|
throw new BadRequestHttpException('This backup cannot be restored at this time: not completed or failed.');
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:51:09 +00:00
|
|
|
$server->audit(AuditLog::SERVER__BACKUP_RESTORE_STARTED, function (AuditLog $audit, Server $server) use ($backup, $request) {
|
|
|
|
$audit->metadata = ['backup_uuid' => $backup->uuid];
|
|
|
|
|
|
|
|
// If the backup is for an S3 file we need to generate a unique Download link for
|
|
|
|
// it that will allow Wings to actually access the file.
|
|
|
|
if ($backup->disk === Backup::ADAPTER_AWS_S3) {
|
|
|
|
$url = $this->downloadLinkService->handle($backup, $request->user());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the status right away for the server so that we know not to allow certain
|
|
|
|
// actions against it via the Panel API.
|
|
|
|
$server->update(['status' => Server::STATUS_RESTORING_BACKUP]);
|
|
|
|
|
2021-08-05 03:34:00 +00:00
|
|
|
$this->daemonRepository->setServer($server)->restore($backup, $url ?? null, $request->input('truncate'));
|
2021-01-18 01:51:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
|
|
|
}
|
2020-04-04 17:59:25 +00:00
|
|
|
}
|