44
55use ErrorException ;
66use Exception ;
7+ use IPLib \Factory ;
78use Symfony \Component \Cache \Adapter \AbstractAdapter ;
89use Symfony \Component \Cache \Adapter \MemcachedAdapter ;
910use Symfony \Component \Cache \Adapter \PhpFilesAdapter ;
@@ -44,7 +45,25 @@ public function init(array $configs, array $forcedConfigs = []): Bouncer
4445 $ this ->session_name = session_name ('crowdsec ' );
4546 session_start ();
4647 }
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+ }
4765 $ this ->settings = $ configs ;
66+
4867 if (\is_array ($ forcedConfigs )) {
4968 $ this ->settings = array_merge ($ this ->settings , $ forcedConfigs );
5069 }
@@ -384,14 +403,13 @@ public function sendResponse(?string $body, int $statusCode = 200): void
384403 public function safelyBounce (array $ configs ): bool
385404 {
386405 $ result = false ;
406+ set_error_handler (function ($ errno , $ errstr ) {
407+ throw new BouncerException ("$ errstr (Error level: $ errno) " );
408+ });
387409 try {
388- set_error_handler (function ($ errno , $ errstr ) {
389- throw new BouncerException ("$ errstr (Error level: $ errno) " );
390- });
391410 $ this ->init ($ configs );
392411 $ this ->run ();
393412 $ result = true ;
394- restore_error_handler ();
395413 } catch (Exception $ e ) {
396414 $ this ->logger ->error ('' , [
397415 'type ' => 'EXCEPTION_WHILE_BOUNCING ' ,
@@ -409,6 +427,7 @@ public function safelyBounce(array $configs): bool
409427 session_name ($ this ->session_name );
410428 }
411429 }
430+ restore_error_handler ();
412431
413432 return $ result ;
414433 }
0 commit comments