-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptive_backoff_simulation.php
More file actions
59 lines (46 loc) · 1.62 KB
/
adaptive_backoff_simulation.php
File metadata and controls
59 lines (46 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* @copyright ©2025 Maatify.dev
* @Library maatify/security-guard
* @Project maatify:security-guard
* @author Mohamed Abdulalim (megyptm) <mohamed@maatify.dev>
* @since 2025-12-11 01:36
* @see https://www.maatify.dev Maatify.dev
* @link https://github.com/Maatify/security-guard view Project on GitHub
* @note Distributed in the hope that it will be useful - WITHOUT WARRANTY.
*/
declare(strict_types=1);
/**
* PRO Example 3:
* Adaptive backoff simulation (increasing penalty windows).
*/
use Maatify\SecurityGuard\DTO\LoginAttemptDTO;
$guard = require __DIR__ . '/../bootstrap.php';
$config = $guard->getConfig();
$ip = "172.30.15.50";
$subject = "api_login";
echo "\n===== ADAPTIVE BACKOFF SIMULATION =====\n\n";
for ($i = 1; $i <= 6; $i++) {
$attempt = LoginAttemptDTO::now(
ip : $ip,
subject : $subject,
resetAfter: $config->windowSeconds(),
userAgent : "API-CLI",
context : ['iteration' => $i]
);
$result = $guard->handleAttempt($attempt, false);
echo "Attempt {$i} → failure count = {$result}\n";
if ($guard->isBlocked($ip, $subject)) {
$remaining = $guard->getRemainingBlockSeconds($ip, $subject);
echo "🚫 AUTO BLOCKED — remaining: {$remaining} sec\n";
break;
}
// Show adaptive backoff concept
if ($config->backoffEnabled()) {
$delay = min(
$config->initialBackoffSeconds() * ($config->backoffMultiplier() ** ($i - 1)),
$config->maxBackoffSeconds()
);
echo "⏳ Backoff delay suggestion: {$delay} sec\n";
}
}