Skip to content

Commit 06b9192

Browse files
committed
fix various bug
1 parent 20412e8 commit 06b9192

File tree

6 files changed

+68
-48
lines changed

6 files changed

+68
-48
lines changed

docker-compose.yml

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ services:
1010
- redis
1111
- memcached
1212
environment:
13-
- WORDPRESS_DB_PASSWORD=super_secret_password
14-
- WORDPRESS_DEBUG=1
15-
#- WORDPRESS_CONFIG_EXTRA=define('DISABLE_WP_CRON', true);
13+
WORDPRESS_DB_PASSWORD: super_secret_password
14+
WORDPRESS_DEBUG: 1
15+
WORDPRESS_CONFIG_EXTRA: |
16+
define('WP_CRON_LOCK_TIMEOUT', 1);
1617
# more here https://hub.docker.com/_/wordpress
1718
ports:
1819
- "80:80"
@@ -29,9 +30,10 @@ services:
2930
- redis
3031
- memcached
3132
environment:
32-
- WORDPRESS_DB_PASSWORD=super_secret_password
33-
- WORDPRESS_DEBUG=1
34-
#- WORDPRESS_CONFIG_EXTRA=define('DISABLE_WP_CRON', true);
33+
WORDPRESS_DB_PASSWORD: super_secret_password
34+
WORDPRESS_DEBUG: 1
35+
WORDPRESS_CONFIG_EXTRA: |
36+
define('WP_CRON_LOCK_TIMEOUT', 1);
3537
# more here https://hub.docker.com/_/wordpress
3638
ports:
3739
- "80:80"
@@ -48,9 +50,10 @@ services:
4850
- redis
4951
- memcached
5052
environment:
51-
- WORDPRESS_DB_PASSWORD=super_secret_password
52-
- WORDPRESS_DEBUG=1
53-
#- WORDPRESS_CONFIG_EXTRA=define('DISABLE_WP_CRON', true);
53+
WORDPRESS_DB_PASSWORD: super_secret_password
54+
WORDPRESS_DEBUG: 1
55+
WORDPRESS_CONFIG_EXTRA: |
56+
define('WP_CRON_LOCK_TIMEOUT', 1);
5457
# more here https://hub.docker.com/_/wordpress
5558
ports:
5659
- "80:80"
@@ -67,9 +70,10 @@ services:
6770
- redis
6871
- memcached
6972
environment:
70-
- WORDPRESS_DB_PASSWORD=super_secret_password
71-
- WORDPRESS_DEBUG=1
72-
#- WORDPRESS_CONFIG_EXTRA=define('DISABLE_WP_CRON', true);
73+
WORDPRESS_DB_PASSWORD: super_secret_password
74+
WORDPRESS_DEBUG: 1
75+
WORDPRESS_CONFIG_EXTRA: |
76+
define('WP_CRON_LOCK_TIMEOUT', 1);
7377
# more here https://hub.docker.com/_/wordpress
7478
ports:
7579
- "80:80"
@@ -86,9 +90,10 @@ services:
8690
- redis
8791
- memcached
8892
environment:
89-
- WORDPRESS_DB_PASSWORD=super_secret_password
90-
- WORDPRESS_DEBUG=1
91-
#- WORDPRESS_CONFIG_EXTRA=define('DISABLE_WP_CRON', true);
93+
WORDPRESS_DB_PASSWORD: super_secret_password
94+
WORDPRESS_DEBUG: 1
95+
WORDPRESS_CONFIG_EXTRA: |
96+
define('WP_CRON_LOCK_TIMEOUT', 1);
9297
# more here https://hub.docker.com/_/wordpress
9398
ports:
9499
- "80:80"
@@ -105,9 +110,10 @@ services:
105110
- redis
106111
- memcached
107112
environment:
108-
- WORDPRESS_DB_PASSWORD=super_secret_password
109-
- WORDPRESS_DEBUG=1
110-
#- WORDPRESS_CONFIG_EXTRA=define('DISABLE_WP_CRON', true);
113+
WORDPRESS_DB_PASSWORD: super_secret_password
114+
WORDPRESS_DEBUG: 1
115+
WORDPRESS_CONFIG_EXTRA: |
116+
define('WP_CRON_LOCK_TIMEOUT', 1);
111117
# more here https://hub.docker.com/_/wordpress
112118
ports:
113119
- "80:80"
@@ -124,9 +130,10 @@ services:
124130
- redis
125131
- memcached
126132
environment:
127-
- WORDPRESS_DB_PASSWORD=super_secret_password
128-
- WORDPRESS_DEBUG=1
129-
#- WORDPRESS_CONFIG_EXTRA=define('DISABLE_WP_CRON', true);
133+
WORDPRESS_DB_PASSWORD: super_secret_password
134+
WORDPRESS_DEBUG: 1
135+
WORDPRESS_CONFIG_EXTRA: |
136+
define('WP_CRON_LOCK_TIMEOUT', 1);
130137
# more here https://hub.docker.com/_/wordpress
131138
ports:
132139
- "80:80"
@@ -143,9 +150,10 @@ services:
143150
- redis
144151
- memcached
145152
environment:
146-
- WORDPRESS_DB_PASSWORD=super_secret_password
147-
- WORDPRESS_DEBUG=1
148-
#- WORDPRESS_CONFIG_EXTRA=define('DISABLE_WP_CRON', true);
153+
WORDPRESS_DB_PASSWORD: super_secret_password
154+
WORDPRESS_DEBUG: 1
155+
WORDPRESS_CONFIG_EXTRA: |
156+
define('WP_CRON_LOCK_TIMEOUT', 1);
149157
# more here https://hub.docker.com/_/wordpress
150158
ports:
151159
- "80:80"

docs/contribute.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ docker-compose exec crowdsec cscli decisions delete --all
6161

6262
# View CrowdSec logs:
6363
docker-compose logs crowdsec
64-
6564
```
6665

6766
# WP Scan pass
@@ -125,6 +124,12 @@ docker-compose down && docker rmi wordpress-bouncer_wordpress<X.X>
125124
docker-compose run wordpress<X.X> bash
126125
```
127126

127+
### Display the plugin logs
128+
129+
```bash
130+
tail -f logs/*
131+
```
132+
128133
#### New feature
129134

130135
```bash
@@ -133,7 +138,7 @@ git commit # as much as necessary.
133138

134139
# Rename branch if necessary
135140
git branch -m <new-name>
136-
git push origin :<old-name> && git push origin <new-name>
141+
git push origin :<old-name> && git push -u origin <new-name>
137142

138143
# Create PR
139144
gh pr create --fill

inc/admin/advanced-settings.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,47 @@ function adminAdvancedSettings()
1515
// Field "crowdsec_stream_mode"
1616
addFieldCheckbox('crowdsec_stream_mode', 'Enable the "Stream" mode', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_stream_mode', function () {
1717
// Stream mode just activated.
18+
$bouncer = getBouncerInstance();
19+
$result = $bouncer->warmBlocklistCacheUp();
20+
$message = __('As the stream mode is enabled, the cache has just been warmed up, '.($result > 0 ? 'there are now '.$result.' decisions' : 'there is now '.$result.' decision').' in cache.');
21+
AdminNotice::displaySuccess($message);
1822
scheduleBlocklistRefresh();
1923
}, function () {
2024
// Stream mode just deactivated.
2125
unscheduleBlocklistRefresh();
2226
}, '
2327
<p>With the stream mode, every decision is retrieved in an asynchronous way. 3 advantages: <br>&nbsp;1) Inivisible latency when loading pages<br>&nbsp;2) The IP verifications works even if your CrowdSec is not reachable.<br>&nbsp;3) The API can never be overloaded by the WordPress traffic</p>
24-
<p>Note: This method has one limit: for maximum 60 seconds, all the new decisions may not be taken into account.</p>'.
28+
<p>Note: This method has one limit: all the decisions updates since the previous resync will not be taken in account until the next resync.</p>'.
2529
(get_option('crowdsec_stream_mode') ?
2630
'<p><input style="margin-right:10px" type="button" value="Refresh the cache now" class="button button-secondary button-small" onclick="document.getElementById(\'crowdsec_ation_refresh_cache\').submit();"></p>' :
2731
'<p><input style="margin-right:10px" type="button" disabled="disabled" value="Refresh the cache now" class="button button-secondary button-small"></p>'));
2832

2933
// Field "crowdsec_stream_mode_refresh_frequency"
3034
addFieldString('crowdsec_stream_mode_refresh_frequency', 'Resync decisions each<br>(stream mode only)', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_stream_mode', function ($input) {
3135
$input = (int) $input;
32-
if ($input < 60) {
33-
$input = 60;
34-
add_settings_error('Resync decisions each', 'crowdsec_error', 'The "Resync decisions each" value should be more than 60sec (WP_CRON_LOCK_TIMEOUT). We just reset the frequency to 60 seconds.');
36+
if ($input < 1) {
37+
$input = 1;
38+
add_settings_error('Resync decisions each', 'crowdsec_error', 'The "Resync decisions each" value should be more than 1sec (WP_CRON_LOCK_TIMEOUT). We just reset the frequency to 1 seconds.');
3539

3640
return $input;
3741
}
3842

3943
// Update wp-cron schedule.
4044
if ((bool) get_option('crowdsec_stream_mode')) {
45+
$bouncer = getBouncerInstance();
46+
$result = $bouncer->warmBlocklistCacheUp();
47+
$message = __('As the stream mode refresh duration changed, the cache has just been warmed up, '.($result > 0 ? 'there are now '.$result.' decisions' : 'there is now '.$result.' decision').' in cache.');
48+
AdminNotice::displaySuccess($message);
4149
scheduleBlocklistRefresh();
4250
}
4351

4452
return $input;
45-
}, ' seconds. <p>Our advice is 60 seconds (according to WP_CRON_LOCK_TIMEOUT).</p>', '...', 'width: 115px;', 'number');
53+
}, ' seconds. <p>Our advice is 60 seconds (as WordPress ignores durations under this value <a href="https://wordpress.stackexchange.com/questions/100104/better-handling-of-wp-cron-server-load-abuse" target="_blank">see WP_CRON_LOCK_TIMEOUT</a>).<br>'.
54+
' If you need a shorter delay between each resync, you can <strong>go down to 1 sec</strong>.<br>'.
55+
' But as mentionned is the WordPress Developer Documentation, you should considere hooking WP-Cron Into the System Task Scheduler'.
56+
' by yourself and reduce the WP_CRON_LOCK_TIMEOUT value to the same value as you set here. '.
57+
'<a href="https://developer.wordpress.org/plugins/cron/hooking-wp-cron-into-the-system-task-scheduler/" target="_blank">'.
58+
'Here is explained how</a>.</p>', '...', 'width: 115px;', 'number');
4659

4760
/*********************
4861
** Section "Cache" **
@@ -136,26 +149,26 @@ function adminAdvancedSettings()
136149
]);
137150

138151
// Field "crowdsec_clean_ip_cache_duration"
139-
addFieldString('crowdsec_clean_ip_cache_duration', 'Recheck clean IPs each', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_cache', function ($input) {
152+
addFieldString('crowdsec_clean_ip_cache_duration', 'Recheck clean IPs each<br>(live mode only)', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_cache', function ($input) {
140153
if ((int) $input <= 0) {
141154
add_settings_error('Recheck clean IPs each', 'crowdsec_error', 'Recheck clean IPs each: Minimum is 1 second.');
142155

143156
return '1';
144157
}
145158

146159
return $input;
147-
}, ' seconds. <p>The duration (in seconds) between re-asking LAPI about an already checked clean IP.<br>Minimum 1 second.', '...', 'width: 115px;', 'number');
160+
}, ' seconds. <p>The duration between re-asking LAPI about an already checked clean IP.<br>Minimum 1 second.<br> Note that this setting can not be apply in stream mode.', '...', 'width: 115px;', 'number', (bool) get_option('crowdsec_stream_mode'));
148161

149162
// Field "crowdsec_bad_ip_cache_duration"
150-
addFieldString('crowdsec_bad_ip_cache_duration', 'Recheck bad IPs each', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_cache', function ($input) {
163+
addFieldString('crowdsec_bad_ip_cache_duration', 'Recheck bad IPs each<br>(live mode only)', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_cache', function ($input) {
151164
if ((int) $input <= 0) {
152165
add_settings_error('Recheck bad IPs each', 'crowdsec_error', 'Recheck bad IPs each: Minimum is 1 second.');
153166

154167
return '1';
155168
}
156169

157170
return $input;
158-
}, ' seconds. <p>The duration (in seconds) between re-asking LAPI about an already checked bad IP.<br>Minimum 1 second.', '...', 'width: 115px;', 'number');
171+
}, ' seconds. <p>The duration between re-asking LAPI about an already checked bad IP.<br>Minimum 1 second.<br> Note that this setting can not be apply in stream mode.', '...', 'width: 115px;', 'number', (bool) get_option('crowdsec_stream_mode'));
159172

160173
/***************************
161174
** Section "Remediation" **
@@ -215,6 +228,7 @@ function convertInlineIpRangesToLongArray(string $inlineIpRanges): array
215228
$longIpBoundsList = array_merge($longIpBoundsList, [[$long, $long]]);
216229
}
217230
}
231+
218232
return $longIpBoundsList;
219233
}
220234

@@ -239,12 +253,6 @@ function convertInlineIpRangesToLongArray(string $inlineIpRanges): array
239253
'fill the IPs or IPs ranges here...', '');
240254

241255
// Field "crowdsec_hide_mentions"
242-
addFieldCheckbox('crowdsec_hide_mentions', 'Hide CrowdSec mentions', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_remediations', function () {
243-
// Stream mode just activated.
244-
scheduleBlocklistRefresh();
245-
}, function () {
246-
// Stream mode just deactivated.
247-
unscheduleBlocklistRefresh();
248-
}, '
256+
addFieldCheckbox('crowdsec_hide_mentions', 'Hide CrowdSec mentions', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_remediations', function () {}, function () {}, '
249257
<p>Enable if you want to hide CrowdSec mentions on the Ban and Captcha pages</p>');
250258
}

inc/admin/init.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
add_action('admin_notices', [new AdminNotice(), 'displayAdminNotice']);
99

1010
if (is_admin()) {
11-
1211
function wrapErrorMessage(string $errorMessage)
1312
{
1413
return "CrowdSec: $errorMessage";
@@ -162,7 +161,7 @@ function addFieldCheckbox(string $optionName, string $label, string $optionGroup
162161
]);
163162
}
164163

165-
function addFieldString(string $optionName, string $label, string $optionGroup, string $pageName, string $sectionName, callable $onChange, $descriptionHtml, $placeholder, $inputStyle, $inputType = 'text')
164+
function addFieldString(string $optionName, string $label, string $optionGroup, string $pageName, string $sectionName, callable $onChange, $descriptionHtml, $placeholder, $inputStyle, $inputType = 'text', $disabled = false)
166165
{
167166
register_setting($optionGroup, $optionName, function ($input) use ($onChange, $optionName) {
168167
$currentState = esc_attr($input);
@@ -175,11 +174,11 @@ function addFieldString(string $optionName, string $label, string $optionGroup,
175174

176175
return $currentState;
177176
});
178-
add_settings_field($optionName, $label, function ($args) use ($descriptionHtml, $optionName, $inputStyle, $inputType) {
177+
add_settings_field($optionName, $label, function ($args) use ($descriptionHtml, $optionName, $inputStyle, $inputType, $disabled) {
179178
$name = $args['label_for'];
180179
$placeholder = $args['placeholder'];
181180
$value = esc_attr(get_option($optionName));
182-
echo "<input style=\"$inputStyle\" type=\"$inputType\" class=\"regular-text\" name=\"$name\" value=\"$value\" placeholder=\"$placeholder\">$descriptionHtml";
181+
echo '<input '.($disabled ? 'disabled="disabled"' : '')." style=\"$inputStyle\" type=\"$inputType\" class=\"regular-text\" name=\"$name\" value=\"$value\" placeholder=\"$placeholder\">$descriptionHtml";
183182
}, $pageName, $sectionName, [
184183
'label_for' => $optionName,
185184
'placeholder' => $placeholder,

inc/admin/settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ function adminSettings()
6262
}, function () {
6363
// Stream mode just deactivated.
6464
unscheduleBlocklistRefresh();
65-
}, '<p>If enabled, this wp-admin is not bounced, only the public website.</p>');
65+
}, '<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>');
6666
}

inc/bounce-current-ip.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function safelyBounceCurrentIp()
170170
}
171171

172172
$everywhere = empty(get_option('crowdsec_public_website_only'));
173-
$shoudRun = ($everywhere || !is_admin());
173+
$shoudRun = ($everywhere || (!is_admin() && !in_array($GLOBALS['pagenow'], ['wp-login.php', 'wp-cron.php'])));
174174
if ($shoudRun && isBouncerConfigOk()) {
175175
bounceCurrentIp();
176176
}

0 commit comments

Comments
 (0)