Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Enum/ResourceLimit.php
10287 views
1
<?php
2
3
namespace Pterodactyl\Enum;
4
5
use Illuminate\Http\Request;
6
use Webmozart\Assert\Assert;
7
use Pterodactyl\Models\Server;
8
use Illuminate\Cache\RateLimiting\Limit;
9
use Illuminate\Support\Facades\RateLimiter;
10
use Illuminate\Routing\Middleware\ThrottleRequests;
11
12
/**
13
* A basic resource throttler for individual servers. This is applied in addition
14
* to existing rate limits and allows the code to slow down speedy users that might
15
* be creating resources a little too quickly for comfort. This throttle generally
16
* only applies to creation flows, and not general view/edit/delete flows.
17
*/
18
enum ResourceLimit
19
{
20
case Allocation;
21
case Backup;
22
case Database;
23
case Schedule;
24
case Subuser;
25
case Websocket;
26
case FilePull;
27
28
public function throttleKey(): string
29
{
30
return mb_strtolower("api.client:server-resource:{$this->name}");
31
}
32
33
/**
34
* Returns a middleware that will throttle the specific resource by server. This
35
* throttle applies to any user making changes to that resource on the specific
36
* server, it is NOT per-user.
37
*/
38
public function middleware(): string
39
{
40
return ThrottleRequests::using($this->throttleKey());
41
}
42
43
public function limit(): Limit
44
{
45
return match($this) {
46
self::Backup => Limit::perMinutes(15, 3),
47
self::Database => Limit::perMinute(2),
48
self::FilePull => Limit::perMinutes(10, 5),
49
self::Subuser => Limit::perMinutes(15, 10),
50
self::Websocket => Limit::perMinute(5),
51
default => Limit::perMinute(2),
52
};
53
}
54
55
public static function boot(): void
56
{
57
foreach (self::cases() as $case) {
58
RateLimiter::for($case->throttleKey(), function (Request $request) use ($case) {
59
Assert::isInstanceOf($server = $request->route()->parameter('server'), Server::class);
60
61
return $case->limit()->by($server->uuid);
62
});
63
}
64
}
65
}
66
67