Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Providers/RouteServiceProvider.php
10277 views
1
<?php
2
3
namespace Pterodactyl\Providers;
4
5
use Illuminate\Http\Request;
6
use Pterodactyl\Models\Database;
7
use Pterodactyl\Enum\ResourceLimit;
8
use Illuminate\Support\Facades\Route;
9
use Illuminate\Cache\RateLimiting\Limit;
10
use Illuminate\Support\Facades\RateLimiter;
11
use Pterodactyl\Http\Middleware\TrimStrings;
12
use Pterodactyl\Http\Middleware\AdminAuthenticate;
13
use Pterodactyl\Http\Middleware\RequireTwoFactorAuthentication;
14
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
15
16
class RouteServiceProvider extends ServiceProvider
17
{
18
protected const FILE_PATH_REGEX = '/^\/api\/client\/servers\/([a-z0-9-]{36})\/files(\/?$|\/(.)*$)/i';
19
20
/**
21
* Define your route model bindings, pattern filters, etc.
22
*/
23
public function boot(): void
24
{
25
$this->configureRateLimiting();
26
27
// Disable trimming string values when requesting file information — it isn't helpful
28
// and messes up the ability to actually open a directory that ends with a space.
29
TrimStrings::skipWhen(function (Request $request) {
30
return preg_match(self::FILE_PATH_REGEX, $request->getPathInfo()) === 1;
31
});
32
33
// This is needed to make use of the "resolveRouteBinding" functionality in the
34
// model. Without it you'll never trigger that logic flow thus resulting in a 404
35
// error because we request databases with a HashID, and not with a normal ID.
36
Route::model('database', Database::class);
37
38
$this->routes(function () {
39
Route::middleware('web')->group(function () {
40
Route::middleware(['auth.session', RequireTwoFactorAuthentication::class])
41
->group(base_path('routes/base.php'));
42
43
Route::middleware(['auth.session', RequireTwoFactorAuthentication::class, AdminAuthenticate::class])
44
->prefix('/admin')
45
->group(base_path('routes/admin.php'));
46
47
Route::middleware('guest')->prefix('/auth')->group(base_path('routes/auth.php'));
48
});
49
50
Route::middleware(['api', RequireTwoFactorAuthentication::class])->group(function () {
51
Route::middleware(['application-api', 'throttle:api.application'])
52
->prefix('/api/application')
53
->scopeBindings()
54
->group(base_path('routes/api-application.php'));
55
56
Route::middleware(['client-api', 'throttle:api.client'])
57
->prefix('/api/client')
58
->scopeBindings()
59
->group(base_path('routes/api-client.php'));
60
});
61
62
Route::middleware('daemon')
63
->prefix('/api/remote')
64
->scopeBindings()
65
->group(base_path('routes/api-remote.php'));
66
});
67
}
68
69
/**
70
* Configure the rate limiters for the application.
71
*/
72
protected function configureRateLimiting(): void
73
{
74
// Authentication rate limiting. For login and checkpoint endpoints we'll apply
75
// a limit of 10 requests per minute, for the forgot password endpoint apply a
76
// limit of two per minute for the requester so that there is less ability to
77
// trigger email spam.
78
RateLimiter::for('authentication', function (Request $request) {
79
if ($request->route()->named('auth.post.forgot-password')) {
80
return Limit::perMinute(2)->by($request->ip());
81
}
82
83
return Limit::perMinute(10);
84
});
85
86
// Configure the throttles for both the application and client APIs below.
87
// This is configurable per-instance in "config/http.php". By default this
88
// limiter will be tied to the specific request user, and falls back to the
89
// request IP if there is no request user present for the key.
90
//
91
// This means that an authenticated API user cannot use IP switching to get
92
// around the limits.
93
RateLimiter::for('api.client', function (Request $request) {
94
$key = optional($request->user())->uuid ?: $request->ip();
95
96
return Limit::perMinutes(
97
config('http.rate_limit.client_period'),
98
config('http.rate_limit.client')
99
)->by($key);
100
});
101
102
RateLimiter::for('api.application', function (Request $request) {
103
$key = optional($request->user())->uuid ?: $request->ip();
104
105
return Limit::perMinutes(
106
config('http.rate_limit.application_period'),
107
config('http.rate_limit.application')
108
)->by($key);
109
});
110
111
ResourceLimit::boot();
112
}
113
}
114
115