Skip to content

Commit

Permalink
Issue #3483186: Add some metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Oct 29, 2024
1 parent bec10db commit 1865d8c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/install/turnstile_protect.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ rate_limit: true
window: 86400
threshold: 20
max_challenges: 5
history_enabled: false
3 changes: 3 additions & 0 deletions config/schema/turnstile_protect.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ turnstile_protect.settings:
max_challenges:
type: integer
label: 'The maximum times the client can be challenged in a session before sending 429 responses'
history_enabled:
type: boolean
label: 'Save a daily snapshot of the flood information on your site'
8 changes: 8 additions & 0 deletions src/Form/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#min' => 1,
];

$form['history_enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable History'),
'#description' => $this->t('Enable or disable history tracking in Turnstile Protect.'),
'#default_value' => $config->get('history_enabled'),
];

return parent::buildForm($form, $form_state);
}

Expand All @@ -152,6 +159,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->set('threshold', (int) $form_state->getValue('threshold'))
->set('window', (int) $form_state->getValue('window'))
->set('max_challenges', (int) $form_state->getValue('max_challenges'))
->set('history_enabled', (bool) $form_state->getValue('history_enabled'))
->save();

parent::submitForm($form, $form_state);
Expand Down
55 changes: 55 additions & 0 deletions turnstile_protect.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* @file
* Contains turnstile_protect.install.
*/

use Drupal\Core\Database\Database;

/**
* Implements hook_schema().
*/
function turnstile_protect_schema() {
$schema['turnstile_protect_history'] = [
'description' => 'Stores the history of requests by IP range.',
'fields' => [
'timestamp' => [
'description' => 'The Unix timestamp of the record.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'ip_range' => [
'description' => 'The IP range being tracked.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
'requests' => [
'description' => 'The number of requests by the IP range.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
],
'indexes' => [
'ip_range' => ['ip_range'],
],
];

return $schema;
}

/**
* Adds the turnstile_protect_history table.
*/
function turnstile_protect_update_10001() {
$schema = Database::getConnection()->schema();
if (!$schema->tableExists('turnstile_protect_history')) {
// Create the table using the schema definition from hook_schema().
$schema_definition = turnstile_protect_schema();
$schema->createTable('turnstile_protect_history', $schema_definition['turnstile_protect_history']);
}

}
27 changes: 27 additions & 0 deletions turnstile_protect.module
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,30 @@ function turnstile_protect_captcha_alter(&$captcha, $info) {
$captcha['form']['turnstile_widget']['#markup'] = str_replace('<div', '<div data-callback="turnstileProtectAutoSubmit"', $captcha['form']['turnstile_widget']['#markup']);
$captcha['form']['#attached']['library'][] = 'turnstile_protect/challenge';
}

/**
* Implements hook_cron().
*/
function turnstile_protect_cron() {
$config = \Drupal::config('turnstile_protect.settings');
if (!$config->get('history_enabled')) {
return;
}

$threshold_timestamp = \Drupal::time()->getRequestTime() - $config->get('window');
// add some padding to prevent missing a whole day of metrics
$threshold_timestamp -= 300;
$result = \Drupal::database()->query("SELECT MAX(timestamp) FROM {turnstile_protect_history}")->fetchField();
if ($result && $result > $threshold_timestamp) {
return;
}

\Drupal::database()->query("INSERT INTO {turnstile_protect_history} (`timestamp`, ip_range, requests)
SELECT UNIX_TIMESTAMP(), identifier, COUNT(*)
FROM {flood}
WHERE `event` = :event
GROUP BY identifier
ORDER BY COUNT(*) DESC", [
':event' => 'turnstile_protect_rate_limit',
]);
}

0 comments on commit 1865d8c

Please sign in to comment.