4
4
5
5
use ErrorException ;
6
6
use Exception ;
7
+ use IPLib \Factory ;
7
8
use Symfony \Component \Cache \Adapter \AbstractAdapter ;
8
9
use Symfony \Component \Cache \Adapter \MemcachedAdapter ;
9
10
use Symfony \Component \Cache \Adapter \PhpFilesAdapter ;
@@ -44,7 +45,25 @@ public function init(array $configs, array $forcedConfigs = []): Bouncer
44
45
$ this ->session_name = session_name ('crowdsec ' );
45
46
session_start ();
46
47
}
48
+ // Convert array of string to array of array with comparable IPs
49
+ if (\is_array (($ configs ['trust_ip_forward_array ' ]))) {
50
+ $ forwardConfigs = $ configs ['trust_ip_forward_array ' ];
51
+ $ finalForwardConfigs = [];
52
+ foreach ($ forwardConfigs as $ forwardConfig ) {
53
+ if (\is_string ($ forwardConfig )) {
54
+ $ parsedString = Factory::parseAddressString ($ forwardConfig , 3 );
55
+ if (!empty ($ parsedString )) {
56
+ $ comparableValue = $ parsedString ->getComparableString ();
57
+ $ finalForwardConfigs [] = [$ comparableValue , $ comparableValue ];
58
+ }
59
+ } elseif (\is_array ($ forwardConfig )) {
60
+ $ finalForwardConfigs [] = $ forwardConfig ;
61
+ }
62
+ }
63
+ $ configs ['trust_ip_forward_array ' ] = $ finalForwardConfigs ;
64
+ }
47
65
$ this ->settings = $ configs ;
66
+
48
67
if (\is_array ($ forcedConfigs )) {
49
68
$ this ->settings = array_merge ($ this ->settings , $ forcedConfigs );
50
69
}
@@ -384,14 +403,13 @@ public function sendResponse(?string $body, int $statusCode = 200): void
384
403
public function safelyBounce (array $ configs ): bool
385
404
{
386
405
$ result = false ;
406
+ set_error_handler (function ($ errno , $ errstr ) {
407
+ throw new BouncerException ("$ errstr (Error level: $ errno) " );
408
+ });
387
409
try {
388
- set_error_handler (function ($ errno , $ errstr ) {
389
- throw new BouncerException ("$ errstr (Error level: $ errno) " );
390
- });
391
410
$ this ->init ($ configs );
392
411
$ this ->run ();
393
412
$ result = true ;
394
- restore_error_handler ();
395
413
} catch (Exception $ e ) {
396
414
$ this ->logger ->error ('' , [
397
415
'type ' => 'EXCEPTION_WHILE_BOUNCING ' ,
@@ -409,6 +427,7 @@ public function safelyBounce(array $configs): bool
409
427
session_name ($ this ->session_name );
410
428
}
411
429
}
430
+ restore_error_handler ();
412
431
413
432
return $ result ;
414
433
}
0 commit comments