5
5
require_once __DIR__ .'/templates/captcha.php ' ;
6
6
require_once __DIR__ .'/templates/access-forbidden.php ' ;
7
7
8
+ use Bramus \Monolog \Formatter \ColoredLineFormatter ;
9
+ use Exception ;
8
10
use IPLib \Factory ;
9
11
use Monolog \Formatter \LineFormatter ;
10
12
use Monolog \Handler \RotatingFileHandler ;
@@ -50,6 +52,7 @@ protected function getArraySettings(string $name): array
50
52
51
53
/**
52
54
* Run a bounce.
55
+ * @throws Exception
53
56
*/
54
57
public function run (
55
58
): void {
@@ -86,12 +89,15 @@ protected function initLoggerHelper($logDirectoryPath, $loggerName): void
86
89
$ debugLogPath = $ logDirectoryPath .'/debug.log ' ;
87
90
$ debugFileHandler = new RotatingFileHandler ($ debugLogPath , 0 , Logger::DEBUG );
88
91
if (class_exists ('\Bramus\Monolog\Formatter\ColoredLineFormatter ' )) {
89
- $ debugFileHandler ->setFormatter (new \ Bramus \ Monolog \ Formatter \ ColoredLineFormatter (null , "[%datetime%] %message% %context% \n" , 'H:i:s ' ));
92
+ $ debugFileHandler ->setFormatter (new ColoredLineFormatter (null , "[%datetime%] %message% %context% \n" , 'H:i:s ' ));
90
93
$ this ->logger ->pushHandler ($ debugFileHandler );
91
94
}
92
95
}
93
96
}
94
97
98
+ /**
99
+ * @throws Exception
100
+ */
95
101
protected function bounceCurrentIp ()
96
102
{
97
103
$ ip = $ this ->getRemoteIp ();
@@ -116,7 +122,7 @@ protected function bounceCurrentIp()
116
122
$ this ->getBouncerInstance ();
117
123
$ remediation = $ this ->bouncer ->getRemediationForIp ($ ip );
118
124
$ this ->handleRemediation ($ remediation , $ ip );
119
- } catch (\ Exception $ e ) {
125
+ } catch (Exception $ e ) {
120
126
$ this ->logger ->warning ('' , [
121
127
'type ' => 'UNKNOWN_EXCEPTION_WHILE_BOUNCING ' ,
122
128
'ip ' => $ ip ,
@@ -133,7 +139,7 @@ protected function bounceCurrentIp()
133
139
134
140
protected function shouldTrustXforwardedFor (string $ ip ): bool
135
141
{
136
- $ comparableAddress = Factory::addressFromString ($ ip )->getComparableString ();
142
+ $ comparableAddress = Factory::parseAddressString ($ ip, 3 )->getComparableString ();
137
143
if (null === $ comparableAddress ) {
138
144
$ this ->logger ->warning ('' , [
139
145
'type ' => 'INVALID_INPUT_IP ' ,
@@ -198,7 +204,7 @@ protected function handleCaptchaResolutionForm(string $ip)
198
204
}
199
205
200
206
// Handle image refresh.
201
- if (null !== $ this ->getPostedVariable ('refresh ' ) && (bool ) ( int ) $ this ->getPostedVariable ('refresh ' )) {
207
+ if (null !== $ this ->getPostedVariable ('refresh ' ) && (int ) $ this ->getPostedVariable ('refresh ' )) {
202
208
// Generate new captcha image for the user
203
209
$ this ->storeNewCaptchaCoupleInSession ();
204
210
$ this ->setSessionVariable ('crowdsec_captcha_resolution_failed ' , false );
@@ -213,7 +219,7 @@ protected function handleCaptchaResolutionForm(string $ip)
213
219
$ this ->getSessionVariable ('crowdsec_captcha_phrase_to_guess ' ),
214
220
$ this ->getPostedVariable ('phrase ' ),
215
221
$ ip )) {
216
- // User has correctly fill the captcha
222
+ // User has correctly filled the captcha
217
223
$ this ->setSessionVariable ('crowdsec_captcha_has_to_be_resolved ' , false );
218
224
$ this ->unsetSessionVariable ('crowdsec_captcha_phrase_to_guess ' );
219
225
$ this ->unsetSessionVariable ('crowdsec_captcha_inline_image ' );
@@ -235,7 +241,7 @@ protected function handleCaptchaRemediation($ip)
235
241
$ this ->handleCaptchaResolutionForm ($ ip );
236
242
237
243
if (null === $ this ->getSessionVariable ('crowdsec_captcha_has_to_be_resolved ' )) {
238
- // Setup the first captcha remediation.
244
+ // Set up the first captcha remediation.
239
245
240
246
$ this ->storeNewCaptchaCoupleInSession ();
241
247
$ this ->setSessionVariable ('crowdsec_captcha_has_to_be_resolved ' , true );
@@ -257,16 +263,14 @@ protected function handleRemediation(string $remediation, string $ip)
257
263
$ this ->clearCaptchaSessionContext ();
258
264
}
259
265
switch ($ remediation ) {
260
- case Constants::REMEDIATION_BYPASS :
261
- return ;
262
266
case Constants::REMEDIATION_CAPTCHA :
263
267
$ this ->handleCaptchaRemediation ($ ip );
264
268
break ;
265
269
case Constants::REMEDIATION_BAN :
266
270
$ this ->handleBanRemediation ();
267
271
break ;
272
+ case Constants::REMEDIATION_BYPASS :
268
273
default :
269
- return ;
270
274
}
271
275
}
272
276
}
0 commit comments