Merge branch 'develop' into feature/react-admin

This commit is contained in:
Matthew Penner 2021-03-21 15:49:41 -06:00
commit 49de31bf4c
24 changed files with 169 additions and 32 deletions

View file

@ -6,4 +6,11 @@ use Pterodactyl\Exceptions\DisplayException;
class TwoFactorAuthenticationTokenInvalid extends DisplayException
{
/**
* TwoFactorAuthenticationTokenInvalid constructor.
*/
public function __construct()
{
parent::__construct('The provided two-factor authentication token was not valid.');
}
}

View file

@ -195,7 +195,7 @@ class BackupController extends ClientApiController
// actions against it via the Panel API.
$server->update(['status' => Server::STATUS_RESTORING_BACKUP]);
$this->repository->setServer($server)->restore($backup, $url ?? null, $request->input('truncate') === 'true');
$this->repository->setServer($server)->restore($backup, $url ?? null, $request->input('truncate'));
});
return $this->returnNoContent();

View file

@ -2,7 +2,9 @@
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Carbon\Carbon;
use Pterodactyl\Models\Server;
use Illuminate\Cache\Repository;
use Pterodactyl\Transformers\Api\Client\StatsTransformer;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
@ -10,27 +12,35 @@ use Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest;
class ResourceUtilizationController extends ClientApiController
{
private Repository $cache;
private DaemonServerRepository $repository;
/**
* ResourceUtilizationController constructor.
*/
public function __construct(DaemonServerRepository $repository)
public function __construct(Repository $cache, DaemonServerRepository $repository)
{
parent::__construct();
$this->cache = $cache;
$this->repository = $repository;
}
/**
* Return the current resource utilization for a server.
* Return the current resource utilization for a server. This value is cached for up to
* 20 seconds at a time to ensure that repeated requests to this endpoint do not cause
* a flood of unnecessary API calls.
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function __invoke(GetServerRequest $request, Server $server): array
{
$stats = $this->repository->setServer($server)->getDetails();
$stats = $this->cache
->tags(['resources'])
->remember($server->uuid, Carbon::now()->addSeconds(20), function () use ($server) {
return $this->repository->setServer($server)->getDetails();
});
return $this->fractal->item($stats)
->transformWith($this->getTransformer(StatsTransformer::class))

View file

@ -57,7 +57,14 @@ class TwoFactorController extends ClientApiController
/**
* Updates a user's account to have two-factor enabled.
*
* @return \Illuminate\Http\JsonResponse
*
* @throws \Throwable
* @throws \Illuminate\Validation\ValidationException
* @throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException
* @throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException
* @throws \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException
* @throws \Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid
*/
public function store(Request $request): JsonResponse
{

View file

@ -122,13 +122,14 @@ class Schedule extends Model
* Returns the schedule's execution crontab entry as a string.
*
* @return \Carbon\CarbonImmutable
* @throws \Exception
*/
public function getNextRunDate()
{
$formatted = sprintf('%s %s %s %s %s', $this->cron_minute, $this->cron_hour, $this->cron_day_of_month, $this->cron_month, $this->cron_day_of_week);
return CarbonImmutable::createFromTimestamp(
CronExpression::factory($formatted)->getNextRunDate()->getTimestamp()
(new CronExpression($formatted))->getNextRunDate()->getTimestamp()
);
}

View file

@ -5,10 +5,12 @@ namespace Pterodactyl\Services\Servers;
use Illuminate\Support\Arr;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Allocation;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class BuildModificationService
{
@ -78,10 +80,18 @@ class BuildModificationService
$updateData = $this->structureService->handle($server);
// Because Wings always fetches an updated configuration from the Panel when booting
// a server this type of exception can be safely "ignored" and just written to the logs.
// Ideally this request succeedes so we can apply resource modifications on the fly
// but if it fails it isn't the end of the world.
if (!empty($updateData['build'])) {
$this->daemonServerRepository->setServer($server)->update([
'build' => $updateData['build'],
]);
try {
$this->daemonServerRepository->setServer($server)->update([
'build' => $updateData['build'],
]);
} catch (DaemonConnectionException $exception) {
Log::warning($exception, ['server_id' => $server->id]);
}
}
$this->connection->commit();

View file

@ -74,7 +74,7 @@ class ToggleTwoFactorService
$isValidToken = $this->google2FA->verifyKey($secret, $token, config()->get('pterodactyl.auth.2fa.window'));
if (!$isValidToken) {
throw new TwoFactorAuthenticationTokenInvalid('The token provided is not valid.');
throw new TwoFactorAuthenticationTokenInvalid();
}
return $this->connection->transaction(function () use ($user, $toggleState) {
@ -94,6 +94,9 @@ class ToggleTwoFactorService
$inserts[] = [
'user_id' => $user->id,
'token' => password_hash($token, PASSWORD_DEFAULT),
// insert() won't actually set the time on the models, so make sure we do this
// manually here.
'created_at' => Carbon::now(),
];
$tokens[] = $token;