Skip to content

Commit

Permalink
Merge pull request pterodactyl#2 from pterodactyl/develop
Browse files Browse the repository at this point in the history
Update From Upstream
  • Loading branch information
alliraine authored Aug 29, 2020
2 parents d3a544a + d735909 commit f54d4f9
Show file tree
Hide file tree
Showing 130 changed files with 1,907 additions and 816 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ RUN cp docker/default.conf /etc/nginx/conf.d/default.conf \
&& cat docker/www.conf > /usr/local/etc/php-fpm.d/www.conf \
&& rm /usr/local/etc/php-fpm.d/www.conf.default \
&& cat docker/supervisord.conf > /etc/supervisord.conf \
&& echo "* * * * * /usr/bin/php /app/artisan schedule:run >> /dev/null 2>&1" >> /var/spool/cron/crontabs/root \
&& echo "* * * * * /usr/local/bin/php /app/artisan schedule:run >> /dev/null 2>&1" >> /var/spool/cron/crontabs/root \
&& sed -i s/ssl_session_cache/#ssl_session_cache/g /etc/nginx/nginx.conf \
&& mkdir -p /var/run/php /var/run/nginx

EXPOSE 80 443

ENTRYPOINT ["/bin/ash", "docker/entrypoint.sh"]

CMD [ "supervisord", "-n", "-c", "/etc/supervisord.conf" ]
CMD [ "supervisord", "-n", "-c", "/etc/supervisord.conf" ]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ in becoming a sponsor?](https://github.com/sponsors/DaneEveritt)
> DedicatedMC provides Raw Power hosting at affordable pricing, making sure to never compromise on your performance
> and giving you the best performance money can buy.
#### [Skynode](https://www.skynode.pro/)
> Skynode provides blazing fast game servers along with a top notch user experience. Whatever our clients are looking
> for, we're able to provide it!
#### [XCORE-SERVER.de](https://xcore-server.de)
> XCORE-SERVER.de offers High-End Servers for hosting and gaming since 2012. Fast, excellent and well known for eSports Gaming.
## Support & Documentation
Support for using Pterodactyl can be found on our [Documentation Website](https://pterodactyl.io/project/introduction.html), [Guides Website](https://pterodactyl.io/community/about.html), or via our [Discord Chat](https://discord.gg/QRDZvVm).
Expand Down
51 changes: 51 additions & 0 deletions app/Console/Commands/Maintenance/PruneOrphanedBackupsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Pterodactyl\Console\Commands\Maintenance;

use Carbon\CarbonImmutable;
use InvalidArgumentException;
use Illuminate\Console\Command;
use Pterodactyl\Repositories\Eloquent\BackupRepository;

class PruneOrphanedBackupsCommand extends Command
{
/**
* @var string
*/
protected $signature = 'p:maintenance:prune-backups {--since-minutes=30}';

/**
* @var string
*/
protected $description = 'Marks all backups that have not completed in the last "n" minutes as being failed.';

/**
* @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository
*/
public function handle(BackupRepository $repository)
{
$since = $this->option('since-minutes');
if (! is_digit($since)) {
throw new InvalidArgumentException('The --since-minutes option must be a valid numeric digit.');
}

$query = $repository->getBuilder()
->whereNull('completed_at')
->whereDate('created_at', '<=', CarbonImmutable::now()->subMinutes($since));

$count = $query->count();
if (! $count) {
$this->info('There are no orphaned backups to be marked as failed.');

return;
}

$this->warn("Marking {$count} backups that have not been marked as completed in the last {$since} minutes as failed.");

$query->update([
'is_successful' => false,
'completed_at' => CarbonImmutable::now(),
'updated_at' => CarbonImmutable::now(),
]);
}
}
9 changes: 9 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ protected function commands()
*/
protected function schedule(Schedule $schedule)
{
// Execute scheduled commands for servers every minute, as if there was a normal cron running.
$schedule->command('p:schedule:process')->everyMinute()->withoutOverlapping();

// Every 30 minutes, run the backup pruning command so that any abandoned backups can be removed
// from the UI view for the server.
$schedule->command('p:maintenance:prune-backups', [
'--since-minutes' => '30',
])->everyThirtyMinutes();

// Every day cleanup any internal backups of service files.
$schedule->command('p:maintenance:clean-service-backups')->daily();
}
}
12 changes: 0 additions & 12 deletions app/Contracts/Repository/ServerRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@ public function findWithVariables(int $id): Server;
*/
public function getPrimaryAllocation(Server $server, bool $refresh = false): Server;

/**
* Return all of the server variables possible and default to the variable
* default if there is no value defined for the specific server requested.
*
* @param int $id
* @param bool $returnAsObject
* @return array|object
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getVariablesWithValues(int $id, bool $returnAsObject = false);

/**
* Return enough data to be used for the creation of a server via the daemon.
*
Expand Down
26 changes: 20 additions & 6 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,21 @@ public function invalidJson($request, ValidationException $exception)
return [str_replace('.', '_', $field) => $cleaned];
})->toArray();

$errors = collect($exception->errors())->map(function ($errors, $field) use ($codes) {
$errors = collect($exception->errors())->map(function ($errors, $field) use ($codes, $exception) {
$response = [];
foreach ($errors as $key => $error) {
$response[] = [
'code' => str_replace(self::PTERODACTYL_RULE_STRING, 'p_', array_get(
$meta = [
'source_field' => $field,
'rule' => str_replace(self::PTERODACTYL_RULE_STRING, 'p_', array_get(
$codes, str_replace('.', '_', $field) . '.' . $key
)),
'detail' => $error,
'source' => ['field' => $field],
];

$converted = self::convertToArray($exception)['errors'][0];
$converted['detail'] = $error;
$converted['meta'] = is_array($converted['meta']) ? array_merge($converted['meta'], $meta) : $meta;

$response[] = $converted;
}

return $response;
Expand All @@ -209,10 +214,19 @@ public static function convertToArray(Throwable $exception, array $override = []
{
$error = [
'code' => class_basename($exception),
'status' => method_exists($exception, 'getStatusCode') ? strval($exception->getStatusCode()) : '500',
'status' => method_exists($exception, 'getStatusCode')
? strval($exception->getStatusCode())
: ($exception instanceof ValidationException ? '422' : '500'),
'detail' => 'An error was encountered while processing this request.',
];

if ($exception instanceof ModelNotFoundException || $exception->getPrevious() instanceof ModelNotFoundException) {
// Show a nicer error message compared to the standard "No query results for model"
// response that is normally returned. If we are in debug mode this will get overwritten
// with a more specific error message to help narrow down things.
$error['detail'] = 'The requested resource could not be found on the server.';
}

if (config('app.debug')) {
$error = array_merge($error, [
'detail' => $exception->getMessage(),
Expand Down
15 changes: 12 additions & 3 deletions app/Http/Controllers/Admin/Servers/ServerViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Contracts\View\Factory;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Servers\EnvironmentService;
use Pterodactyl\Repositories\Eloquent\NestRepository;
use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Eloquent\MountRepository;
Expand Down Expand Up @@ -56,6 +57,11 @@ class ServerViewController extends Controller
*/
private $nodeRepository;

/**
* @var \Pterodactyl\Services\Servers\EnvironmentService
*/
private $environmentService;

/**
* ServerViewController constructor.
*
Expand All @@ -66,6 +72,7 @@ class ServerViewController extends Controller
* @param \Pterodactyl\Repositories\Eloquent\NestRepository $nestRepository
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
* @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService
*/
public function __construct(
Factory $view,
Expand All @@ -74,7 +81,8 @@ public function __construct(
MountRepository $mountRepository,
NestRepository $nestRepository,
NodeRepository $nodeRepository,
ServerRepository $repository
ServerRepository $repository,
EnvironmentService $environmentService
) {
$this->view = $view;
$this->databaseHostRepository = $databaseHostRepository;
Expand All @@ -83,6 +91,7 @@ public function __construct(
$this->nestRepository = $nestRepository;
$this->nodeRepository = $nodeRepository;
$this->repository = $repository;
$this->environmentService = $environmentService;
}

/**
Expand Down Expand Up @@ -138,12 +147,12 @@ public function build(Request $request, Server $server)
*/
public function startup(Request $request, Server $server)
{
$parameters = $this->repository->getVariablesWithValues($server->id, true);
$nests = $this->nestRepository->getWithEggs();
$variables = $this->environmentService->handle($server);

$this->plainInject([
'server' => $server,
'server_variables' => $parameters->data,
'server_variables' => $variables,
'nests' => $nests->map(function (Nest $item) {
return array_merge($item->toArray(), [
'eggs' => $item->eggs->keyBy('id')->toArray(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function __invoke(DownloadBackupRequest $request, Server $server, Backup
throw new BadRequestHttpException;
}

return JsonResponse::create([
return new JsonResponse([
'object' => 'signed_url',
'attributes' => [
'url' => $url,
Expand Down
73 changes: 73 additions & 0 deletions app/Http/Controllers/Api/Client/Servers/FileUploadController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Pterodactyl\Http\Controllers\Api\Client\Servers;

use Carbon\CarbonImmutable;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Services\Nodes\NodeJWTService;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\UploadFileRequest;

class FileUploadController extends ClientApiController
{
/**
* @var \Pterodactyl\Services\Nodes\NodeJWTService
*/
private $jwtService;

/**
* FileUploadController constructor.
*
* @param \Pterodactyl\Services\Nodes\NodeJWTService $jwtService
*/
public function __construct(
NodeJWTService $jwtService
) {
parent::__construct();

$this->jwtService = $jwtService;
}

/**
* Returns a url where files can be uploaded to.
*
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Files\UploadFileRequest $request
* @param \Pterodactyl\Models\Server $server
*
* @return \Illuminate\Http\JsonResponse
*/
public function __invoke(UploadFileRequest $request, Server $server)
{
return new JsonResponse([
'object' => 'signed_url',
'attributes' => [
'url' => $this->getUploadUrl($server, $request->user()),
],
]);
}

/**
* Returns a url where files can be uploaded to.
*
* @param \Pterodactyl\Models\Server $server
* @param \Pterodactyl\Models\User $user
* @return string
*/
protected function getUploadUrl(Server $server, User $user)
{
$token = $this->jwtService
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
->setClaims([
'server_uuid' => $server->uuid,
])
->handle($server->node, $user->id . $server->uuid);

return sprintf(
'%s/upload/file?token=%s',
$server->node->getConnectionAddress(),
$token->__toString()
);
}
}
Loading

0 comments on commit f54d4f9

Please sign in to comment.