From 794cf9d9dd784de58741236f566ab24c0811ca6e Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 27 Dec 2020 16:41:53 -0800 Subject: [PATCH] Make backup throttling configurable --- app/Repositories/Eloquent/BackupRepository.php | 6 +++--- app/Services/Backups/InitiateBackupService.php | 16 ++++++++++------ config/backups.php | 10 ++++++++++ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/Repositories/Eloquent/BackupRepository.php b/app/Repositories/Eloquent/BackupRepository.php index adbbd9c93..46e400142 100644 --- a/app/Repositories/Eloquent/BackupRepository.php +++ b/app/Repositories/Eloquent/BackupRepository.php @@ -19,16 +19,16 @@ class BackupRepository extends EloquentRepository * Determines if too many backups have been generated by the server. * * @param int $server - * @param int $minutes + * @param int $seconds * @return \Pterodactyl\Models\Backup[]|\Illuminate\Support\Collection */ - public function getBackupsGeneratedDuringTimespan(int $server, int $minutes = 10) + public function getBackupsGeneratedDuringTimespan(int $server, int $seconds = 600) { return $this->getBuilder() ->withTrashed() ->where('server_id', $server) ->where('is_successful', true) - ->where('created_at', '>=', Carbon::now()->subMinutes($minutes)->toDateTimeString()) + ->where('created_at', '>=', Carbon::now()->subSeconds($seconds)->toDateTimeString()) ->get() ->toBase(); } diff --git a/app/Services/Backups/InitiateBackupService.php b/app/Services/Backups/InitiateBackupService.php index 615f357ce..811af7c67 100644 --- a/app/Services/Backups/InitiateBackupService.php +++ b/app/Services/Backups/InitiateBackupService.php @@ -108,12 +108,16 @@ class InitiateBackupService */ public function handle(Server $server, string $name = null, bool $override = false): Backup { - $previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, 10); - if ($previous->count() >= 2) { - throw new TooManyRequestsHttpException( - CarbonImmutable::now()->diffInSeconds($previous->last()->created_at->addMinutes(10)), - 'Only two backups may be generated within a 10 minute span of time.' - ); + $limit = config('backups.throttles.limit'); + $period = config('backups.throttles.period'); + if ($period > 0) { + $previous = $this->repository->getBackupsGeneratedDuringTimespan($server->id, $period); + if ($previous->count() >= $limit) { + throw new TooManyRequestsHttpException( + CarbonImmutable::now()->diffInSeconds($previous->last()->created_at->addMinutes(10)), + sprintf('Only %d backups may be generated within a %d second span of time.', $limit, $period) + ); + } } // Check if the server has reached or exceeded it's backup limit diff --git a/config/backups.php b/config/backups.php index a309a9ee6..21bddfe7b 100644 --- a/config/backups.php +++ b/config/backups.php @@ -16,6 +16,16 @@ return [ // to 6 hours. To disable this feature, set the value to `0`. 'prune_age' => env('BACKUP_PRUNE_AGE', 360), + // Defines the backup creation throttle limits for users. In this default example, we allow + // a user to create two (successful or pending) backups per 10 minutes. Even if they delete + // a backup it will be included in the throttle count. + // + // Set the period to "0" to disable this throttle. The period is defined in seconds. + 'throttles' => [ + 'limit' => env('BACKUP_THROTTLE_LIMIT', 2), + 'period' => env('BACKUP_THROTTLE_PERIOD', 600), + ], + 'disks' => [ // There is no configuration for the local disk for Wings. That configuration // is determined by the Daemon configuration, and not the Panel.