Skip to content

Commit 8836e49

Browse files
feat(*): Use 0.26.0 version of crowdsec/bouncer lib
1 parent 5b95390 commit 8836e49

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1075
-1548
lines changed

.github/workflows/release.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
uses: actions/checkout@v2
4141

4242
- name: Check version ${{ env.VERSION_NUMBER }} consistency in files
43-
# Check crowdsec.php, readme.txt, inc/constants.php and CHANGELOG.md
43+
# Check crowdsec.php, readme.txt, inc/Constants.php and CHANGELOG.md
4444
run: |
4545
CURRENT_DATE=$(date +'%Y-%m-%d')
4646
CHANGELOG_VERSION=$(grep -o -E "## \[(.*)\] - $CURRENT_DATE" CHANGELOG.md | head -1 | sed 's/ //g')
@@ -84,14 +84,14 @@ jobs:
8484
echo "Stable tag in readme.txt: KO"
8585
exit 1
8686
fi
87-
CONSTANT_VERSION=$(grep -E "WordPress CrowdSec Bouncer/v(.*)" inc/constants.php | sed 's/[\x27(),/ ]//g')
87+
CONSTANT_VERSION=$(grep -E "WordPress CrowdSec Bouncer/v(.*)" inc/Constants.php | sed 's/[\x27(),/ ]//g')
8888
echo $CONSTANT_VERSION
89-
echo "defineCROWDSEC_BOUNCER_USER_AGENTWordPressCrowdSecBouncerv${{ env.VERSION_NUMBER }}"
90-
if [[ $CONSTANT_VERSION == "defineCROWDSEC_BOUNCER_USER_AGENTWordPressCrowdSecBouncerv${{ env.VERSION_NUMBER }};" ]]
89+
echo "publicconstCROWDSEC_BOUNCER_USER_AGENTWordPressCrowdSecBouncerv${{ env.VERSION_NUMBER }}"
90+
if [[ $CONSTANT_VERSION == "publicconstCROWDSEC_BOUNCER_USER_AGENTWordPressCrowdSecBouncerv${{ env.VERSION_NUMBER }};" ]]
9191
then
92-
echo "Version in inc/constants.php: OK"
92+
echo "Version in inc/Constants.php: OK"
9393
else
94-
echo "Version in inc/constants.php: KO"
94+
echo "Version in inc/Constants.php: KO"
9595
exit 1
9696
fi
9797

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}
2020
},
2121
"require": {
22-
"crowdsec/bouncer": "0.24.0",
22+
"crowdsec/bouncer": "0.26.0",
2323
"symfony/polyfill-mbstring": "1.20.0",
2424
"symfony/service-contracts": "2.4.1"
2525
},

composer.lock

Lines changed: 25 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crowdsec.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424

2525

2626
require_once __DIR__.'/inc/plugin-setup.php';
27-
require_once __DIR__.'/inc/constants.php';
28-
$crowdsecRandomLogFolder = get_option('crowdsec_random_log_folder') ?: '';
29-
crowdsecDefineConstants($crowdsecRandomLogFolder);
3027
require_once __DIR__.'/inc/scheduling.php';
3128
register_activation_hook(__FILE__, 'activate_crowdsec_plugin');
3229
register_deactivation_hook(__FILE__, 'deactivate_crowdsec_plugin');

inc/Bounce.php

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
<?php
22

3-
require_once __DIR__.'/constants.php';
43

54
use CrowdSecBouncer\AbstractBounce;
65
use CrowdSecBouncer\Bouncer;
7-
use CrowdSecBouncer\Constants;
8-
use CrowdSecBouncer\IBounce;
96
use 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
}

inc/Constants.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use CrowdSecBouncer\Constants as LibConstants;
6+
7+
/**
8+
* Every constant of the plugin are set here.
9+
*
10+
* @author CrowdSec team
11+
*
12+
* @see https://crowdsec.net CrowdSec Official Website
13+
*
14+
* @copyright Copyright (c) 2020+ CrowdSec
15+
* @license MIT License
16+
*/
17+
class Constants extends LibConstants
18+
{
19+
20+
public const CROWDSEC_LOG_BASE_PATH = __DIR__ . '/../logs/';
21+
public const CROWDSEC_LOG_PATH = __DIR__ . '/../logs/prod.log';
22+
public const CROWDSEC_DEBUG_LOG_PATH = __DIR__ . '/../logs/debug.log';
23+
public const CROWDSEC_CACHE_PATH = __DIR__ . '/../.cache';
24+
public const CROWDSEC_CONFIG_PATH = __DIR__ . '/standalone-settings.php';
25+
public const CROWDSEC_BOUNCER_USER_AGENT = 'WordPress CrowdSec Bouncer/v1.7.0';
26+
public const CROWDSEC_BOUNCER_GEOLOCATION_DIR = __DIR__ . '/../geolocation';
27+
28+
}

0 commit comments

Comments
 (0)