Skip to content

Commit 24ea53c

Browse files
feat(settings): Add a test button to check if settings are ok
1 parent ac3d3f0 commit 24ea53c

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

inc/admin/init.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,27 @@ function pruneBouncerCacheInAdminPage()
104104
}
105105
}
106106

107+
function testBouncerConnexionInAdminPage($ip)
108+
{
109+
try {
110+
$settings = getDatabaseSettings();
111+
$bouncer = getBouncerInstance($settings);
112+
$remediation = $bouncer->getRemediationForIp($ip);
113+
$message = __("Bouncing has been successfully tested for IP: $ip. Result is: $remediation.");
114+
115+
AdminNotice::displaySuccess($message);
116+
} catch (BouncerException $e) {
117+
getCrowdSecLoggerInstance()->error('', [
118+
'type' => 'WP_EXCEPTION_WHILE_TESTING_CONNECTION',
119+
'message' => $e->getMessage(),
120+
'code' => $e->getCode(),
121+
'file' => $e->getFile(),
122+
'line' => $e->getLine(),
123+
]);
124+
AdminNotice::displayError('Technical error while testing bouncer connection: '.$e->getMessage());
125+
}
126+
}
127+
107128
// ACTIONS
108129
add_action('admin_post_crowdsec_clear_cache', function () {
109130
if (
@@ -132,6 +153,16 @@ function pruneBouncerCacheInAdminPage()
132153
header("Location: {$_SERVER['HTTP_REFERER']}");
133154
exit(0);
134155
});
156+
add_action('admin_post_crowdsec_test_connection', function () {
157+
if (
158+
!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'crowdsec_test_connection')) {
159+
die('This link expired.');
160+
}
161+
$ip = isset($_POST['crowdsec_test_connection_ip']) ? $_POST['crowdsec_test_connection_ip'] : $_SERVER['REMOTE_ADDR'];
162+
testBouncerConnexionInAdminPage($ip);
163+
header("Location: {$_SERVER['HTTP_REFERER']}");
164+
exit(0);
165+
});
135166

136167
// THEME
137168
add_action('admin_enqueue_scripts', function () {

inc/admin/settings.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ function adminSettings()
2525
/************************************
2626
** Section "Bouncing refinements" **
2727
***********************************/
28-
2928
add_settings_section('crowdsec_admin_boucing', 'Bouncing', function () {
3029
echo 'Refine bouncing according to your needs.';
3130
}, 'crowdsec_settings');

inc/templates/settings.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@
1212
submit_button();
1313
?>
1414
</form>
15+
<h2><?php echo __("Test your settings");?></h2>
16+
<p><?php echo __("Here you can check if your saved settings are correct.");?></p>
17+
<p><?php echo __("Click the 'Test bouncing' button and the bouncer will try to get the remediation for the following IP:");?></p>
18+
<form action="admin-post.php" method="post" id="crowdsec_action_test_connection">
19+
<input type="hidden" name="action" value="crowdsec_test_connection"/>
20+
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce('crowdsec_test_connection'); ?>"/>
21+
<input type="text" name="crowdsec_test_connection_ip" value="<?php echo $_SERVER['REMOTE_ADDR'];?>"/>
22+
<?php
23+
submit_button('Test bouncing', 'secondary');
24+
?>
25+
</form>
1526
<p>
1627
Feel free to ask any questions about this plugin, make your suggestions or raise issues on the <a href="https://wordpress.org/support/plugin/crowdsec/">plugin support page</a> or directly on <a href="https://github.com/crowdsecurity/cs-wordpress-bouncer/issues/new">Github</a>.
1728
</p>

0 commit comments

Comments
 (0)