Files

203 lines
7.3 KiB
PHP
Raw Permalink Normal View History

2015-12-06 13:58:49 -05:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2015-12-06 13:58:49 -05:00
namespace Pterodactyl\Providers;
2026-10-05 18:10:18 -04:00
use Carbon\CarbonImmutable;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
2021-01-23 12:09:16 -08:00
use Illuminate\Pagination\Paginator;
2026-10-11 02:28:28 -04:00
use Illuminate\Routing\Route as IlluminateRoute;
use Illuminate\Support\Facades\Cache;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
2017-09-24 22:34:30 -05:00
use Illuminate\Support\Facades\Schema;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\View;
2015-12-06 13:58:49 -05:00
use Illuminate\Support\ServiceProvider;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Str;
2026-10-11 13:45:59 -04:00
use Illuminate\Validation\Rules\Password;
2026-10-11 02:28:28 -04:00
use Knuckles\Scribe\Scribe;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Enum\ResourceLimit;
2026-10-06 13:13:39 +01:00
use Pterodactyl\Extensions\Illuminate\Auth\Passwords\PasswordBrokerManager;
2026-10-11 02:28:28 -04:00
use Pterodactyl\Extensions\Scribe\Support\PathParameterNames;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\ApiKey;
use Pterodactyl\Models\Backup;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\DatabaseHost;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Models\Location;
use Pterodactyl\Models\Mount;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Schedule;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Tag;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\User;
use Pterodactyl\Models\UserSSHKey;
use Pterodactyl\Services\Daemon\DaemonManager;
2026-10-08 17:40:00 -04:00
use Pterodactyl\Services\Servers\EnvironmentService;
2026-10-11 02:28:28 -04:00
use ReflectionFunctionAbstract;
2015-12-06 13:58:49 -05:00
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
2023-02-23 12:30:16 -07:00
public function boot(): void
2015-12-06 13:58:49 -05:00
{
2017-09-24 22:34:30 -05:00
Schema::defaultStringLength(191);
2026-10-11 13:45:59 -04:00
// The password policy every password rule applies (see CreateUser::passwordRules()).
Password::defaults(Password::min(8));
2017-09-24 22:34:30 -05:00
2026-10-05 18:10:18 -04:00
Route::model('database', Database::class);
$this->configureRateLimiting();
// Surface lazy loads, discarded fills, and missing-attribute reads as
// exceptions everywhere except production, where a missed spot should log a
// query rather than take the panel down.
Model::shouldBeStrict(! $this->app->isProduction());
// Refuse migrate:fresh and friends against the production database.
DB::prohibitDestructiveCommands($this->app->isProduction());
// All framework-created dates (model casts included) are immutable; in-place
// mutation of a shared timestamp is never something we want.
Date::use(CarbonImmutable::class);
View::share('appVersion', $this->versionData()['version']);
View::share('appIsGit', $this->versionData()['is_git']);
2021-01-23 12:09:16 -08:00
Paginator::useBootstrap();
// If the APP_URL value is set with https:// make sure we force it here. Theoretically
// this should just work with the proxy logic, but there are a lot of cases where it
// doesn't, and it triggers a lot of support requests, so lets just head it off here.
//
// @see https://github.com/pterodactyl/panel/issues/3623
2026-10-11 19:06:44 -04:00
if (Str::startsWith(config('app.url'), 'https://')) {
URL::forceScheme('https');
}
2026-10-11 02:28:28 -04:00
if ($this->app->runningConsoleCommand('scribe:generate')) {
Scribe::normalizeEndpointUrlUsing(
fn (string $url, IlluminateRoute $route, ReflectionFunctionAbstract $method): string => PathParameterNames::normalize($route, $method),
);
}
Relation::enforceMorphMap([
2026-10-05 18:10:18 -04:00
'allocation' => Allocation::class,
'api_key' => ApiKey::class,
'backup' => Backup::class,
'database' => Database::class,
'database_host' => DatabaseHost::class,
'egg' => Egg::class,
'egg_variable' => EggVariable::class,
'location' => Location::class,
'mount' => Mount::class,
'node' => Node::class,
'schedule' => Schedule::class,
'server' => Server::class,
'server_database' => Database::class,
'ssh_key' => UserSSHKey::class,
'tag' => Tag::class,
'task' => Task::class,
'user' => User::class,
]);
2015-12-06 13:58:49 -05:00
}
2017-12-30 16:33:00 -06:00
/**
* Register application service providers.
*/
2023-02-23 12:30:16 -07:00
public function register(): void
2017-12-30 16:33:00 -06:00
{
2026-10-05 18:10:18 -04:00
$this->app->singleton(DaemonManager::class);
2026-10-08 17:40:00 -04:00
// Keys registered through setEnvironmentKey() must survive to every consumer,
// so the service is shared rather than rebuilt on each resolve.
$this->app->singleton(EnvironmentService::class);
2026-10-06 13:13:39 +01:00
$this->app->extend('auth.password', fn (): PasswordBrokerManager => new PasswordBrokerManager($this->app));
2017-12-30 16:33:00 -06:00
// Only load the settings service provider if the environment
// is configured to allow it.
2026-10-05 18:10:18 -04:00
if (! config('pterodactyl.load_environment_only', false) && ! $this->app->environment('testing')) {
2017-12-30 16:33:00 -06:00
$this->app->register(SettingsServiceProvider::class);
}
}
/**
* Return version information for the footer.
2026-10-05 18:10:18 -04:00
*
* @return array{version: string, is_git: bool}
*/
2022-10-14 10:59:20 -06:00
protected function versionData(): array
{
2026-10-05 18:10:18 -04:00
return Cache::remember('git-version', 5, function (): array {
if (file_exists(base_path('.git/HEAD'))) {
2026-10-05 18:10:18 -04:00
$headContents = file_get_contents(base_path('.git/HEAD'));
if ($headContents !== false) {
$head = explode(' ', $headContents);
2017-07-23 17:55:38 -05:00
2026-10-05 18:10:18 -04:00
if (array_key_exists(1, $head)) {
$path = base_path('.git/'.mb_trim($head[1]));
}
2017-07-23 17:55:38 -05:00
}
}
if (isset($path) && file_exists($path)) {
2026-10-05 18:10:18 -04:00
$refContents = file_get_contents($path);
if ($refContents !== false) {
return [
'version' => mb_substr($refContents, 0, 8),
'is_git' => true,
];
}
}
return [
2026-10-05 18:10:18 -04:00
'version' => config()->string('app.version'),
'is_git' => false,
];
});
}
2026-10-05 18:10:18 -04:00
/**
* Rate limiters for authentication and the three APIs.
*/
private function configureRateLimiting(): void
{
RateLimiter::for('authentication', function (Request $request): Limit {
if ($request->route()?->named('auth.post.forgot-password')) {
return Limit::perMinute(2)->by($request->ip());
}
return Limit::perMinute(10)->by($request->ip());
});
RateLimiter::for('api.client', fn (Request $request): Limit => $this->apiLimit($request, 'client'));
RateLimiter::for('api.application', fn (Request $request): Limit => $this->apiLimit($request, 'application'));
RateLimiter::for('api.admin', fn (Request $request): Limit => $this->apiLimit($request, 'admin'));
ResourceLimit::boot();
}
private function apiLimit(Request $request, string $name): Limit
{
$user = $request->user();
$key = $user instanceof User ? $user->uuid : $request->ip();
2026-10-11 19:15:22 -04:00
return Limit::perMinutes(config()->integer("http.rate_limit.{$name}_period"), config()->integer("http.rate_limit.{$name}"))->by($key);
2026-10-05 18:10:18 -04:00
}
2015-12-06 13:58:49 -05:00
}