11<?php
22
3- require_once __DIR__ .'/constants.php ' ;
43
54use CrowdSecBouncer \AbstractBounce ;
65use CrowdSecBouncer \Bouncer ;
7- use CrowdSecBouncer \Constants ;
8- use CrowdSecBouncer \IBounce ;
96use CrowdSecBouncer \BouncerException ;
7+ require_once __DIR__ . '/Constants.php ' ;
108
119/**
1210 * The class that apply a bounce.
@@ -23,13 +21,6 @@ class Bounce extends AbstractBounce
2321 public function init (array $ crowdSecConfig , array $ forcedConfigs = []): Bouncer
2422 {
2523 $ finalConfigs = array_merge ($ crowdSecConfig , $ forcedConfigs );
26- $ crowdsecRandomLogFolder = $ finalConfigs ['crowdsec_random_log_folder ' ];
27- crowdsecDefineConstants ($ crowdsecRandomLogFolder );
28- $ this ->setDebug ($ finalConfigs ['crowdsec_debug_mode ' ]??false );
29- $ this ->setDisplayErrors ($ finalConfigs ['crowdsec_display_errors ' ] ?? false );
30- $ this ->initLogger ();
31-
32-
3324
3425 return $ this ->getBouncerInstance ($ finalConfigs );
3526 }
@@ -49,20 +40,17 @@ protected function specialcharsDecodeEntQuotes(string $value)
4940 */
5041 public function getBouncerInstance (array $ settings , bool $ forceReload = false ): Bouncer
5142 {
52- $ crowdSecLogPath = CROWDSEC_LOG_PATH ;
53- $ crowdSecDebugLogPath = CROWDSEC_DEBUG_LOG_PATH ;
54- $ this ->logger = getStandaloneCrowdSecLoggerInstance ($ crowdSecLogPath , $ this ->debug , $ crowdSecDebugLogPath );
5543 $ this ->settings = $ settings ;
5644
5745 $ configs = [
5846 // LAPI connection
5947 'api_key ' => $ this ->escape ($ this ->getStringSettings ('crowdsec_api_key ' )),
6048 'api_url ' => $ this ->escape ($ this ->getStringSettings ('crowdsec_api_url ' )),
61- 'api_user_agent ' => CROWDSEC_BOUNCER_USER_AGENT ,
62- 'api_timeout ' => CrowdSecBouncer \ Constants::API_TIMEOUT ,
49+ 'api_user_agent ' => Constants:: CROWDSEC_BOUNCER_USER_AGENT ,
50+ 'api_timeout ' => Constants::API_TIMEOUT ,
6351 // Debug
6452 'debug_mode ' => $ this ->getBoolSettings ('crowdsec_debug_mode ' ),
65- 'log_directory_path ' => CROWDSEC_LOG_BASE_PATH ,
53+ 'log_directory_path ' => Constants:: CROWDSEC_LOG_BASE_PATH ,
6654 'forced_test_ip ' => $ this ->getStringSettings ('crowdsec_forced_test_ip ' ),
6755 'forced_test_forwarded_ip ' => $ this ->getStringSettings ('crowdsec_forced_test_forwarded_ip ' ),
6856 'display_errors ' => $ this ->getBoolSettings ('crowdsec_display_errors ' ),
@@ -73,7 +61,7 @@ public function getBouncerInstance(array $settings, bool $forceReload = false):
7361 // Cache settings
7462 'stream_mode ' => $ this ->getBoolSettings ('crowdsec_stream_mode ' ),
7563 'cache_system ' => $ this ->escape ($ this ->getStringSettings ('crowdsec_cache_system ' )),
76- 'fs_cache_path ' => CROWDSEC_CACHE_PATH ,
64+ 'fs_cache_path ' => Constants:: CROWDSEC_CACHE_PATH ,
7765 'redis_dsn ' => $ this ->escape ($ this ->getStringSettings ('crowdsec_redis_dsn ' )),
7866 'memcached_dsn ' => $ this ->escape ($ this ->getStringSettings ('crowdsec_memcached_dsn ' )),
7967 'clean_ip_cache_duration ' => $ this ->getIntegerSettings ('crowdsec_clean_ip_cache_duration ' )?:Constants::CACHE_EXPIRATION_FOR_CLEAN_IP ,
@@ -89,7 +77,7 @@ public function getBouncerInstance(array $settings, bool $forceReload = false):
8977 'save_result ' => $ this ->getBoolSettings ('crowdsec_geolocation_save_result ' ),
9078 'maxmind ' => [
9179 'database_type ' => $ this ->getStringSettings ('crowdsec_geolocation_maxmind_database_type ' )?:Constants::MAXMIND_COUNTRY ,
92- 'database_path ' => CROWDSEC_BOUNCER_GEOLOCATION_DIR . '/ ' .ltrim ($ this ->getStringSettings ('crowdsec_geolocation_maxmind_database_path ' ), '/ ' ),
80+ 'database_path ' => Constants:: CROWDSEC_BOUNCER_GEOLOCATION_DIR . '/ ' .ltrim ($ this ->getStringSettings ('crowdsec_geolocation_maxmind_database_path ' ), '/ ' ),
9381 ]
9482 ]
9583 ];
@@ -222,6 +210,10 @@ public function getPostedVariable(string $name): ?string
222210 */
223211 public function shouldBounceCurrentIp (): bool
224212 {
213+ // We should not bounce when headers already sent
214+ if (headers_sent ()) {
215+ return false ;
216+ }
225217 // Don't bounce favicon calls.
226218 if ('/favicon.ico ' === $ _SERVER ['REQUEST_URI ' ]) {
227219 return false ;
@@ -309,10 +301,6 @@ public function sendResponse(?string $body, int $statusCode = 200): void
309301
310302 public function safelyBounce (array $ configs ): bool
311303 {
312- if (headers_sent ()) {
313- // We should not bounce when headers already sent
314- return false ;
315- }
316304 // If there is any technical problem while bouncing, don't block the user. Bypass boucing and log the error.
317305 set_error_handler (function ($ errno , $ errstr ) {
318306 throw new BouncerException ("$ errstr (Error level: $ errno) " );
@@ -325,8 +313,12 @@ public function safelyBounce(array $configs): bool
325313 }elseif ($ configs ['crowdsec_bouncing_level ' ] === 'flex_boucing ' ){
326314 $ configs ['crowdsec_bouncing_level ' ] = Constants::BOUNCING_LEVEL_FLEX ;
327315 }
328- $ this ->init ($ configs );
329- $ this ->run ();
316+ $ this ->settings = $ configs ;
317+ $ this ->initLogger ($ configs );
318+ if ($ this ->shouldBounceCurrentIp ()) {
319+ $ this ->init ($ configs );
320+ $ this ->run ();
321+ }
330322 $ result = true ;
331323 } catch (\Exception $ e ) {
332324 if (!$ this ->logger ){
@@ -344,7 +336,7 @@ public function safelyBounce(array $configs): bool
344336 'line ' => $ e ->getLine (),
345337 ]);
346338 }
347- if ($ this -> displayErrors ) {
339+ if (! empty ( $ configs [ ' display_errors ' ]) ) {
348340 throw $ e ;
349341 }
350342 }
@@ -364,40 +356,27 @@ public function isConfigValid(): bool
364356 $ apiUrl = $ this ->escape ($ this ->getStringSettings ('crowdsec_api_url ' ));
365357 if (empty ($ apiUrl )) {
366358 $ issues ['errors ' ][] = [
367- 'type ' => 'INCORRECT_API_URL ' ,
368- 'message ' => 'Bouncer enabled but no API URL provided ' ,
369- ];
359+ 'type ' => 'INCORRECT_API_URL ' ,
360+ 'message ' => 'Bouncer enabled but no API URL provided ' ,
361+ ];
370362 }
371363
372364 $ apiKey = $ this ->escape ($ this ->getStringSettings ('crowdsec_api_key ' ));
373365 if (empty ($ apiKey )) {
374366 $ issues ['errors ' ][] = [
375- 'type ' => 'INCORRECT_API_KEY ' ,
376- 'message ' => 'Bouncer enabled but no API key provided ' ,
377- ];
378- }
379-
380- try {
381- $ cacheSystem = $ this ->escape ($ this ->getStringSettings ('crowdsec_cache_system ' ));
382- $ memcachedDsn = $ this ->escape ($ this ->getStringSettings ('crowdsec_memcached_dsn ' ));
383- $ redisDsn = $ this ->escape ($ this ->getStringSettings ('crowdsec_redis_dsn ' ));
384- $ fsCachePath = CROWDSEC_CACHE_PATH ;
385- getCacheAdapterInstanceStandalone ($ cacheSystem , $ memcachedDsn , $ redisDsn , $ fsCachePath );
386- } catch (BouncerException $ e ) {
387- $ issues ['errors ' ][] = [
388- 'type ' => 'CACHE_CONFIG_ERROR ' ,
389- 'message ' => $ e ->getMessage (),
390- ];
367+ 'type ' => 'INCORRECT_API_KEY ' ,
368+ 'message ' => 'Bouncer enabled but no API key provided ' ,
369+ ];
391370 }
392371 }
393372
394373 return !count ($ issues ['errors ' ]) && !count ($ issues ['warnings ' ]);
395374 }
396375
397- public function initLogger (): void
376+ public function initLogger (array $ configs ): void
398377 {
399- $ crowdSecLogPath = CROWDSEC_LOG_PATH ;
400- $ crowdSecDebugLogPath = CROWDSEC_DEBUG_LOG_PATH ;
401- $ this -> logger = getStandaloneCrowdSecLoggerInstance ( $ crowdSecLogPath , $ this -> debug , $ crowdSecDebugLogPath );
378+ $ isDebug = ! empty ( $ configs [ ' crowdsec_debug_mode ' ]) ;
379+ $ this -> logger = getStandaloneCrowdSecLoggerInstance (Constants:: CROWDSEC_LOG_PATH , $ isDebug , Constants:: CROWDSEC_DEBUG_LOG_PATH ) ;
380+
402381 }
403382}
0 commit comments