1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHALCON-EXT package. |
5
|
|
|
* |
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
7
|
|
|
* <https://github.com/adhocore> |
8
|
|
|
* |
9
|
|
|
* Licensed under MIT license. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhalconExt\Http\Middleware; |
13
|
|
|
|
14
|
|
|
use Phalcon\Http\Request; |
15
|
|
|
use Phalcon\Http\Response; |
16
|
|
|
use PhalconExt\Http\BaseMiddleware; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* A request throttling middleware. |
20
|
|
|
* |
21
|
|
|
* @author Jitendra Adhikari <[email protected]> |
22
|
|
|
* @license MIT |
23
|
|
|
* |
24
|
|
|
* @link https://github.com/adhocore/phalcon-ext |
25
|
|
|
*/ |
26
|
|
|
class Throttle extends BaseMiddleware |
27
|
|
|
{ |
28
|
|
|
/** @var string */ |
29
|
|
|
protected $redis; |
30
|
|
|
|
31
|
|
|
protected $configKey = 'throttle'; |
32
|
|
|
|
33
|
|
|
protected $retryKey = ''; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get retry key that causes throttling. |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getRetryKey(): string |
41
|
|
|
{ |
42
|
|
|
return $this->retryKey; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Handle the throttle. |
47
|
|
|
* |
48
|
|
|
* @param Request $request |
49
|
|
|
* @param Response $response |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function before(Request $request, Response $response): bool |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if ('' === $this->retryKey = $this->findRetryKey($request)) { |
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->abort(429, 'Too many requests. Try again later.', [ |
60
|
|
|
'Retry-After' => \ceil($this->di('redis')->getTtl($this->retryKey) / 60), |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Find the redis key that contains hits counter which has exceeded threshold for throttle. |
66
|
|
|
* |
67
|
|
|
* @param Request $request |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
protected function findRetryKey(Request $request): string |
72
|
|
|
{ |
73
|
|
|
$retryKey = ''; |
74
|
|
|
$redis = $this->di('redis'); |
75
|
|
|
$baseKey = $this->getKey($request); |
76
|
|
|
|
77
|
|
|
foreach ($this->config['maxHits'] as $minutes => $maxHits) { |
78
|
|
|
$key = "$baseKey:$minutes"; |
79
|
|
|
|
80
|
|
|
if ($this->shouldThrottle($redis, $key, $minutes, $maxHits)) { |
81
|
|
|
$retryKey = $key; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $retryKey; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the unique key for this client. |
90
|
|
|
* |
91
|
|
|
* @param Request $request |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function getKey(Request $request): string |
96
|
|
|
{ |
97
|
|
|
$key = $request->getClientAddress(true); |
98
|
|
|
|
99
|
|
|
if ($this->config['checkUserAgent'] ?? false) { |
100
|
|
|
$key .= ':' . \md5($request->getUserAgent()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return ($this->config['prefix'] ?? '') . $key; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check if we should throttle. Update hits counter if not. |
108
|
|
|
* |
109
|
|
|
* @param Request $request |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
protected function shouldThrottle($redis, string $key, int $minutes, int $maxHits): bool |
114
|
|
|
{ |
115
|
|
|
$hits = $redis->get($key) ?: 0; |
116
|
|
|
$ttl = $hits ? $redis->getTtl($key) : $minutes * 60; |
117
|
|
|
|
118
|
|
|
if ($hits >= $maxHits) { |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$redis->save($key, $hits + 1, $ttl); |
123
|
|
|
|
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.