@@ -31,7 +31,7 @@ class Bouncer extends AbstractBouncer
3131 */
3232 public function __construct (array $ configs , LoggerInterface $ logger = null )
3333 {
34- $ this ->shouldNotBounceWpAdmin = (bool )($ configs ['crowdsec_public_website_only ' ]?? true );
34+ $ this ->shouldNotBounceWpAdmin = (bool )($ configs ['crowdsec_public_website_only ' ] ?? true );
3535 $ configs = $ this ->handleRawConfigs ($ configs );
3636 $ this ->logger = $ logger ?: new FileLog ($ configs , 'wordpress_bouncer ' );
3737 $ configs ['user_agent_version ' ] = Constants::VERSION ;
@@ -63,55 +63,114 @@ public function handleRawConfigs(array $rawConfigs): array
6363 return [
6464 // LAPI connection
6565 'api_key ' => $ this ->escape ((string )$ rawConfigs ['crowdsec_api_key ' ] ?? '' ),
66- 'auth_type ' => (string )($ rawConfigs ['crowdsec_auth_type ' ] ?? Constants::AUTH_KEY ),
67- 'tls_cert_path ' => Constants::TLS_DIR . '/ ' . ltrim ((string )$ rawConfigs ['crowdsec_tls_cert_path ' ] ?? '' , '/ ' ),
68- 'tls_key_path ' => Constants::TLS_DIR . '/ ' . ltrim ((string )$ rawConfigs ['crowdsec_tls_key_path ' ] ?? '' , '/ ' ),
69- 'tls_verify_peer ' => (bool )($ rawConfigs ['crowdsec_tls_verify_peer ' ] ?? false ),
66+ 'auth_type ' => (string )($ this ->handleRawConfig (
67+ $ rawConfigs ,
68+ 'crowdsec_auth_type ' ,
69+ Constants::AUTH_KEY
70+ )),
71+ 'tls_cert_path ' => Constants::TLS_DIR . '/ ' .
72+ ltrim (
73+ (string )($ this ->handleRawConfig (
74+ $ rawConfigs , 'crowdsec_tls_cert_path ' , '' )
75+ ),
76+ '/ '
77+ ),
78+ 'tls_key_path ' => Constants::TLS_DIR . '/ ' .
79+ ltrim (
80+ (string )($ this ->handleRawConfig (
81+ $ rawConfigs , 'crowdsec_tls_key_path ' , '' )
82+ ),
83+ '/ '
84+ ),
85+ 'tls_verify_peer ' => (bool )($ this ->handleRawConfig ($ rawConfigs , 'crowdsec_tls_verify_peer ' , false )),
7086 'tls_ca_cert_path ' => Constants::TLS_DIR . '/ ' .
71- ltrim ((string )$ rawConfigs ['crowdsec_tls_ca_cert_path ' ], '/ ' ),
87+ ltrim (
88+ (string )($ this ->handleRawConfig (
89+ $ rawConfigs , 'crowdsec_tls_ca_cert_path ' , '' )
90+ ),
91+ '/ '
92+ ),
7293 'api_url ' => $ this ->escape ((string )$ rawConfigs ['crowdsec_api_url ' ] ?? '' ),
73- 'use_curl ' => (bool )($ rawConfigs ['crowdsec_use_curl ' ] ?? false ),
74- 'api_timeout ' => (int )($ rawConfigs ['crowdsec_api_timeout ' ] ?? Constants::API_TIMEOUT ),
94+ 'use_curl ' => (bool )($ this ->handleRawConfig ($ rawConfigs , 'crowdsec_use_curl ' , false )),
95+ 'api_timeout ' => (int )($ this ->handleRawConfig (
96+ $ rawConfigs ,
97+ 'crowdsec_api_timeout ' ,
98+ Constants::API_TIMEOUT
99+ )),
75100 // Debug
76- 'debug_mode ' => (bool )($ rawConfigs[ 'crowdsec_debug_mode ' ] ?? false ),
77- 'disable_prod_log ' => (bool )($ rawConfigs[ 'crowdsec_disable_prod_log ' ] ?? false ),
101+ 'debug_mode ' => (bool )($ this -> handleRawConfig ( $ rawConfigs, 'crowdsec_debug_mode ' , false ) ),
102+ 'disable_prod_log ' => (bool )($ this -> handleRawConfig ( $ rawConfigs, 'crowdsec_disable_prod_log ' , false ) ),
78103 'log_directory_path ' => Constants::LOG_BASE_PATH ,
79104 'forced_test_ip ' => (string )($ rawConfigs ['crowdsec_forced_test_ip ' ] ?? '' ),
80105 'forced_test_forwarded_ip ' => (string )($ rawConfigs ['crowdsec_forced_test_forwarded_ip ' ] ?? '' ),
81- 'display_errors ' => (bool )($ rawConfigs[ 'crowdsec_display_errors ' ] ?? false ),
106+ 'display_errors ' => (bool )($ this -> handleRawConfig ( $ rawConfigs, 'crowdsec_display_errors ' , false ) ),
82107 // Bouncer
83- 'bouncing_level ' => (string )($ rawConfigs ['crowdsec_bouncing_level ' ] ?? Constants::BOUNCING_LEVEL_DISABLED ),
108+ 'bouncing_level ' => (string )($ this ->handleRawConfig (
109+ $ rawConfigs ,
110+ 'crowdsec_bouncing_level ' ,
111+ Constants::BOUNCING_LEVEL_DISABLED
112+ )),
84113 'trust_ip_forward_array ' => (array )($ rawConfigs ['crowdsec_trust_ip_forward_array ' ] ?? []),
85- 'fallback_remediation ' => (string )($ rawConfigs ['crowdsec_fallback_remediation ' ] ??
86- Constants::REMEDIATION_BYPASS ),
114+ 'fallback_remediation ' => (string )($ this ->handleRawConfig (
115+ $ rawConfigs ,
116+ 'crowdsec_fallback_remediation ' ,
117+ Constants::REMEDIATION_BYPASS
118+ )),
87119 // Cache settings
88- 'stream_mode ' => (bool )($ rawConfigs ['crowdsec_stream_mode ' ] ?? false ),
89- 'cache_system ' => $ this ->escape ((string )$ rawConfigs ['crowdsec_cache_system ' ] ?? Constants::CACHE_SYSTEM_PHPFS ),
120+ 'stream_mode ' => (bool )($ this ->handleRawConfig ($ rawConfigs , 'crowdsec_stream_mode ' , false )),
121+ 'cache_system ' => $ this ->escape ((string )($ this ->handleRawConfig (
122+ $ rawConfigs ,
123+ 'crowdsec_cache_system ' ,
124+ Constants::CACHE_SYSTEM_PHPFS
125+ ))),
90126 'fs_cache_path ' => Constants::CACHE_PATH ,
91127 'redis_dsn ' => $ this ->escape ((string )$ rawConfigs ['crowdsec_redis_dsn ' ] ?? '' ),
92128 'memcached_dsn ' => $ this ->escape ((string )$ rawConfigs ['crowdsec_memcached_dsn ' ] ?? '' ),
93- 'clean_ip_cache_duration ' => (int )($ rawConfigs ['crowdsec_clean_ip_cache_duration ' ] ??
94- Constants::CACHE_EXPIRATION_FOR_CLEAN_IP ),
95- 'bad_ip_cache_duration ' => (int )($ rawConfigs ['crowdsec_bad_ip_cache_duration ' ] ??
96- Constants::CACHE_EXPIRATION_FOR_BAD_IP ),
97- 'captcha_cache_duration ' => (int )($ rawConfigs ['crowdsec_captcha_cache_duration ' ] ??
98- Constants::CACHE_EXPIRATION_FOR_CAPTCHA ),
129+ 'clean_ip_cache_duration ' => (int )($ this ->handleRawConfig (
130+ $ rawConfigs ,
131+ 'crowdsec_clean_ip_cache_duration ' ,
132+ Constants::CACHE_EXPIRATION_FOR_CLEAN_IP
133+ )),
134+ 'bad_ip_cache_duration ' => (int )($ this ->handleRawConfig (
135+ $ rawConfigs ,
136+ 'crowdsec_bad_ip_cache_duration ' ,
137+ Constants::CACHE_EXPIRATION_FOR_BAD_IP
138+ )),
139+ 'captcha_cache_duration ' => (int )($ this ->handleRawConfig (
140+ $ rawConfigs ,
141+ 'crowdsec_captcha_cache_duration ' ,
142+ Constants::CACHE_EXPIRATION_FOR_CAPTCHA
143+ )),
99144 // Geolocation
100145 'geolocation ' => [
101- 'enabled ' => (bool )($ rawConfigs ['crowdsec_geolocation_enabled ' ] ?? false ),
102- 'type ' => (string )($ rawConfigs ['crowdsec_geolocation_type ' ] ?? Constants::GEOLOCATION_TYPE_MAXMIND ),
103- 'cache_duration ' => (int )($ rawConfigs ['crowdsec_geolocation_cache_duration ' ]
104- ?? Constants::CACHE_EXPIRATION_FOR_GEO ),
146+ 'enabled ' => (bool )($ this ->handleRawConfig ($ rawConfigs , 'crowdsec_geolocation_enabled ' , false )),
147+ 'type ' => (string )($ this ->handleRawConfig (
148+ $ rawConfigs ,
149+ 'crowdsec_geolocation_type ' ,
150+ Constants::GEOLOCATION_TYPE_MAXMIND
151+ )),
152+ 'cache_duration ' => (int )($ this ->handleRawConfig (
153+ $ rawConfigs ,
154+ 'crowdsec_geolocation_cache_duration ' ,
155+ Constants::CACHE_EXPIRATION_FOR_GEO
156+ )),
105157 'maxmind ' => [
106- 'database_type ' => (string )($ rawConfigs ['crowdsec_geolocation_maxmind_database_type ' ] ??
107- Constants::MAXMIND_COUNTRY ),
158+ 'database_type ' => (string )($ this ->handleRawConfig (
159+ $ rawConfigs ,
160+ 'crowdsec_geolocation_maxmind_database_type ' ,
161+ Constants::MAXMIND_COUNTRY )
162+ ),
108163 'database_path ' => Constants::GEOLOCATION_DIR . '/ ' .
109- ltrim ((string )($ rawConfigs ['crowdsec_geolocation_maxmind_database_path ' ] ?? '' ),
110- '/ ' ),
164+ ltrim (
165+ (string )($ this ->handleRawConfig (
166+ $ rawConfigs , 'crowdsec_geolocation_maxmind_database_path ' , '' )
167+ ),
168+ '/ '
169+ ),
111170 ]
112171 ],
113172 // Ban and Captcha walls
114- 'hide_mentions ' => (bool )($ rawConfigs[ 'crowdsec_hide_mentions ' ] ?? false ),
173+ 'hide_mentions ' => (bool )($ this -> handleRawConfig ( $ rawConfigs, 'crowdsec_hide_mentions ' , false ) ),
115174 'custom_css ' => $ this ->specialcharsDecodeEntQuotes (
116175 (string )($ rawConfigs ['crowdsec_theme_custom_css ' ] ?? '' )
117176 ),
@@ -191,6 +250,15 @@ public function handleRawConfigs(array $rawConfigs): array
191250 ];
192251 }
193252
253+ private function handleRawConfig (array $ rawConfigs , string $ key , $ defaultValue )
254+ {
255+ if (!empty ($ rawConfigs [$ key ])) {
256+ return $ rawConfigs [$ key ];
257+ }
258+
259+ return $ defaultValue ;
260+ }
261+
194262 /**
195263 * @return string Ex: "X-Forwarded-For"
196264 */
@@ -295,6 +363,7 @@ public function shouldBounceCurrentIp(): bool
295363 }
296364 }
297365 }
366+
298367 return true ;
299368 }
300369}
0 commit comments