Skip to content

Commit 1f21eac

Browse files
feat(*): Add config to disable bouncing feature and a config for curl
1 parent 8836e49 commit 1f21eac

24 files changed

+1040
-70
lines changed

.github/workflows/end-to-end-auto-prepend-test-suite.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ jobs:
7676
sudo chmod -R 777 ${{ github.workspace }}/wp-content
7777
sudo chmod -R 777 ${{ github.workspace }}/my-own-modules
7878
79+
- name: Some DEBUG information
80+
run: |
81+
ddev --version
82+
ddev exec php -v
83+
ddev exec -s crowdsec crowdsec -version
84+
7985
- name: Install WordPress ${{ matrix.wp-version }} with PHP ${{ matrix.php-version }}
8086
run: |
8187
wget https://wordpress.org/wordpress-${{ matrix.wp-version }}.tar.gz

.github/workflows/end-to-end-test-suite.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ jobs:
7676
sudo chmod -R 777 ${{ github.workspace }}/wp-content
7777
sudo chmod -R 777 ${{ github.workspace }}/my-own-modules
7878
79+
- name: Some DEBUG information
80+
run: |
81+
ddev --version
82+
ddev exec php -v
83+
ddev exec -s crowdsec crowdsec -version
84+
7985
- name: Install WordPress ${{ matrix.wp-version }} with PHP ${{ matrix.php-version }}
8086
run: |
8187
wget https://wordpress.org/wordpress-${{ matrix.wp-version }}.tar.gz

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.26.0",
22+
"crowdsec/bouncer": "0.27.0",
2323
"symfony/polyfill-mbstring": "1.20.0",
2424
"symfony/service-contracts": "2.4.1"
2525
},

composer.lock

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

docs/USER_GUIDE.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ also be able to test your settings.
7272

7373
***
7474

75+
`Bouncing feature → Disable bouncing feature`
76+
77+
Until you have configured all the required parameters, you should disable the bouncing feature.
78+
79+
***
80+
81+
7582
`Connection details → LAPI URL`
7683

7784
Url to join your CrowdSec LAPI.
@@ -82,6 +89,16 @@ Url to join your CrowdSec LAPI.
8289

8390
Key generated by the cscli command.
8491

92+
93+
***
94+
95+
`Connection details → Use cURL to call LAPI`
96+
97+
By default, `file_get_contents` method is used to call LAPI. This method requires to have enabled the option
98+
`allow_url_fopen`.
99+
Here, you can choose to use `cURL` requests instead. Beware that in this case, you need to have php `cURL` extension
100+
installed and enabled on your system.
101+
85102
***
86103

87104
`Bouncing → Bouncing level`
34.9 KB
Loading

inc/Bounce.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
2+
declare(strict_types=1);
33

44
use CrowdSecBouncer\AbstractBounce;
55
use CrowdSecBouncer\Bouncer;
@@ -18,11 +18,10 @@
1818
*/
1919
class Bounce extends AbstractBounce
2020
{
21-
public function init(array $crowdSecConfig, array $forcedConfigs = []): Bouncer
21+
public function init(array $configs): Bouncer
2222
{
23-
$finalConfigs = array_merge($crowdSecConfig, $forcedConfigs);
24-
25-
return $this->getBouncerInstance($finalConfigs);
23+
$this->settings = $configs;
24+
return $this->getBouncerInstance($this->settings);
2625
}
2726

2827
protected function escape(string $value)
@@ -38,14 +37,15 @@ protected function specialcharsDecodeEntQuotes(string $value)
3837
/**
3938
* @return Bouncer get the bouncer instance
4039
*/
41-
public function getBouncerInstance(array $settings, bool $forceReload = false): Bouncer
40+
public function getBouncerInstance(array $settings): Bouncer
4241
{
43-
$this->settings = $settings;
42+
$this->settings = array_merge($this->settings, $settings);
4443

4544
$configs = [
4645
// LAPI connection
4746
'api_key' => $this->escape($this->getStringSettings('crowdsec_api_key')),
4847
'api_url' => $this->escape($this->getStringSettings('crowdsec_api_url')),
48+
'use_curl' => $this->getBoolSettings('crowdsec_use_curl'),
4949
'api_user_agent' => Constants::CROWDSEC_BOUNCER_USER_AGENT,
5050
'api_timeout' => Constants::API_TIMEOUT,
5151
// Debug
@@ -82,7 +82,7 @@ public function getBouncerInstance(array $settings, bool $forceReload = false):
8282
]
8383
];
8484

85-
$this->bouncer = getBouncerInstanceStandalone($configs, $forceReload);
85+
$this->bouncer = getBouncerInstanceStandalone($configs);
8686

8787
return $this->bouncer;
8888
}
@@ -210,6 +210,19 @@ public function getPostedVariable(string $name): ?string
210210
*/
211211
public function shouldBounceCurrentIp(): bool
212212
{
213+
$shouldNotBounce = $this->getBoolSettings('crowdsec_bouncer_disabled');
214+
if ($shouldNotBounce) {
215+
if($this->logger){
216+
$this->logger->warning('', [
217+
'type' => 'WP_CONFIG_BOUNCER_DISABLED',
218+
'message' => 'Will not bounce because bouncing is disabled.',
219+
]);
220+
}
221+
222+
return false;
223+
}
224+
225+
213226
// We should not bounce when headers already sent
214227
if (headers_sent()) {
215228
return false;

inc/admin/advanced-settings.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function adminAdvancedSettings()
7777
$settings = getDatabaseSettings();
7878
$oldDsn = $settings['redis_dsn'] ?? '';
7979
$settings['redis_dsn'] = $input;
80-
$bouncer = getBouncerInstance($settings, true);
80+
$bouncer = getBouncerInstance($settings);
8181
$bouncer->testConnection();
8282
} catch (Exception $e) {
8383
$message = __('There was an error while testing new DSN ('.$input.')');
@@ -98,7 +98,7 @@ function adminAdvancedSettings()
9898
$settings = getDatabaseSettings();
9999
$oldDsn = $settings['memcached_dsn'] ?? '';
100100
$settings['memcached_dsn'] = $input;
101-
$bouncer = getBouncerInstance($settings, true);
101+
$bouncer = getBouncerInstance($settings);
102102
$bouncer->testConnection();
103103
} catch (Exception $e) {
104104
$message = __('There was an error while testing new DSN ('.$input.')');
@@ -142,7 +142,7 @@ function adminAdvancedSettings()
142142
try {
143143
// Reload bouncer instance with the new cache system and so test if dsn is correct.
144144
$settings['cache_system'] = $input;
145-
$bouncer = getBouncerInstance($settings, true);
145+
$bouncer = getBouncerInstance($settings);
146146
$bouncer->testConnection();
147147
} catch (BouncerException $e) {
148148
$message = __('There was an error while testing new cache ('.$input.')');
@@ -155,7 +155,7 @@ function adminAdvancedSettings()
155155
}
156156

157157
try {
158-
if ((bool) get_option('crowdsec_stream_mode') && !$error) {
158+
if (get_option('crowdsec_stream_mode') && !$error) {
159159
// system
160160
$result = $bouncer->warmBlocklistCacheUp();
161161
$count = $result['count'];

inc/admin/settings.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33

44
function adminSettings()
55
{
6+
/**********************************
7+
** Section "Bouncer feature" **
8+
*********************************/
9+
add_settings_section('crowdsec_admin_general', 'Bouncing feature', function () {
10+
}, 'crowdsec_settings');
11+
12+
// Field "Disable bouncing"
13+
addFieldCheckbox('crowdsec_bouncer_disabled', 'Disable bouncing feature', 'crowdsec_plugin_settings',
14+
'crowdsec_settings', 'crowdsec_admin_general', function () {}, function () {}, '<p>If checked, the bouncer will not be instantiated at all.
15+
<br>This should be checked until you have configured all the required parameters.</p>');
16+
17+
618
/**********************************
719
** Section "Connection details" **
820
*********************************/
@@ -21,6 +33,11 @@ function adminSettings()
2133
return $input;
2234
}, '<p>Generated with the cscli command, ex: <em>cscli bouncers add wordpress-bouncer</em></p>', 'Your bouncer key', 'width: 280px;', 'text');
2335

36+
// Field "Use cURL"
37+
addFieldCheckbox('crowdsec_use_curl', 'Use cURL to call LAPI', 'crowdsec_plugin_settings',
38+
'crowdsec_settings', 'crowdsec_admin_connection', function () {}, function () {}, '<p>If checked, calls to LAPI will be done with <i>cURL</i> (be sure to have <i>cURL</i> enabled on your system before enabling).
39+
<br>If not checked, calls are done with <i>file_get_contents</i> method (<i>allow_url_fopen</i> is required for this).</p>');
40+
2441
/************************************
2542
** Section "Bouncing refinements" **
2643
***********************************/
@@ -60,5 +77,5 @@ function adminSettings()
6077
}, function () {
6178
// Stream mode just deactivated.
6279
unscheduleBlocklistRefresh();
63-
}, '<p>If enabled, the wp-admin is not bounced, only the public website</p><p><strong>Important note:</strong> the login page is a common page to both sections. If you want to bounce it, you have to disable "Public website only".</p>');
80+
}, '<p>If checked, the wp-admin is not bounced, only the public website</p><p><strong>Important note:</strong> the login page is a common page to both sections. If you want to bounce it, you have to disable "Public website only".</p>');
6481
}

inc/bouncer-instance-standalone.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,8 @@ function getStandaloneCrowdSecLoggerInstance(string $crowdsecLogPath, bool $debu
3838

3939
$crowdSecBouncer = null;
4040

41-
function getBouncerInstanceStandalone(array $configs, bool $forceReload = false): Bouncer
41+
function getBouncerInstanceStandalone(array $configs): Bouncer
4242
{
43-
// Singleton for this function
44-
global $crowdSecBouncer;
45-
if (!$forceReload && $crowdSecBouncer) {
46-
return $crowdSecBouncer;
47-
}
48-
4943
// Init Bouncer instance
5044
$bouncingLevel = $configs['bouncing_level'];
5145
switch ($bouncingLevel) {

0 commit comments

Comments
 (0)