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