Skip to content

Commit de96197

Browse files
committed
add cache expiration for bad ip
1 parent edeb3ff commit de96197

File tree

6 files changed

+25
-6
lines changed

6 files changed

+25
-6
lines changed

docs/configuration-reference.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
// Optional. Handle unknown remediations as. Select from 'bypass' (minimum remediation), 'captcha' or 'ban' (maximum remediation). Defaults to 'captcha'.
2424
'fallback_remediation'=> 'captcha',
2525

26-
// Optional. Set the duration we keep in cache the fact that an IP is clean. In seconds. Defaults to 60 (1 minute).
27-
'cache_expiration_for_clean_ip'=> '60',
26+
// Optional. Set the duration we keep in cache the fact that an IP is clean. In seconds. Defaults to 5.
27+
'cache_expiration_for_clean_ip'=> '5',
28+
29+
// Optional. Set the duration we keep in cache the fact that an IP is bad. In seconds. Defaults to 20.
30+
'cache_expiration_for_bad_ip'=> '20',
2831
]
2932
$cacheAdapter = (...)
3033
$bouncer = new Bouncer();

examples/live-mode/full-example-live-mode.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
'api_timeout' => 1,
5050
'live_mode' => true,
5151
'max_remediation_level' => 'ban',
52-
'cache_expiration_for_clean_ip' => 1200
52+
'cache_expiration_for_clean_ip' => 2,
53+
'cache_expiration_for_bad_ip' => 30
5354
], $cacheAdapter,);
5455

5556
// Ask remediation to LAPI

src/ApiCache.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class ApiCache
3434
/** @var int */
3535
private $cacheExpirationForCleanIp;
3636

37+
/** @var int */
38+
private $cacheExpirationForBadIp;
39+
3740
/** @var ApiClient */
3841
private $apiClient;
3942

@@ -56,12 +59,14 @@ public function configure(
5659
int $timeout,
5760
string $userAgent,
5861
string $apiKey,
59-
int $cacheExpirationForCleanIp
62+
int $cacheExpirationForCleanIp,
63+
int $cacheExpirationForBadIp
6064
): void {
6165
$adapter = $adapter ?: new FilesystemAdapter();
6266
$this->adapter = $adapter;
6367
$this->liveMode = $liveMode;
6468
$this->cacheExpirationForCleanIp = $cacheExpirationForCleanIp;
69+
$this->cacheExpirationForBadIp = $cacheExpirationForBadIp;
6570
$cacheConfigItem = $this->adapter->getItem('cacheConfig');
6671
$cacheConfig = $cacheConfigItem->get();
6772
$warmedUp = (is_array($cacheConfig) && isset($cacheConfig['warmed_up']) && $cacheConfig['warmed_up'] === true);
@@ -71,6 +76,7 @@ public function configure(
7176
'adapter' => get_class($adapter),
7277
'mode' => ($liveMode ? 'live' : 'stream'),
7378
'exp_clean_ips' => $cacheExpirationForCleanIp,
79+
'exp_bad_ips' => $cacheExpirationForBadIp,
7480
'warmed_up' => ($this->warmedUp ? 'true' : 'false'),
7581
]);
7682
$this->apiClient->configure($apiUrl, $timeout, $userAgent, $apiKey);
@@ -218,9 +224,11 @@ private function formatRemediationFromDecision(?array $decision): array
218224
return [Constants::REMEDIATION_BYPASS, time() + $this->cacheExpirationForCleanIp, 0];
219225
}
220226

227+
$duration = min($this->cacheExpirationForBadIp, self::parseDurationToSeconds($decision['duration']));
228+
221229
return [
222230
$decision['type'], // ex: ban, captcha
223-
time() + self::parseDurationToSeconds($decision['duration']), // expiration timestamp
231+
time() + $duration, // expiration timestamp
224232
$decision['id'],
225233
];
226234
}

src/Bouncer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public function configure(array $config, AbstractAdapter $cacheAdapter = null):
6969
$this->config['api_timeout'],
7070
$this->config['api_user_agent'],
7171
$this->config['api_key'],
72-
$this->config['cache_expiration_for_clean_ip']
72+
$this->config['cache_expiration_for_clean_ip'],
73+
$this->config['cache_expiration_for_bad_ip']
7374
);
7475
}
7576

src/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function getConfigTreeBuilder()
4444
->integerNode('cache_expiration_for_clean_ip')
4545
->defaultValue(Constants::CACHE_EXPIRATION_FOR_CLEAN_IP)
4646
->end()
47+
->integerNode('cache_expiration_for_bad_ip')
48+
->defaultValue(Constants::CACHE_EXPIRATION_FOR_BAD_IP)
49+
->end()
4750
->end();
4851

4952
// TODO P2 add "live_mode_max_cache_duration" to avoid manually cache clear in this use case:

src/Constants.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class Constants
2727
/** @var int The duration we keep a clean IP in cache 5s */
2828
public const CACHE_EXPIRATION_FOR_CLEAN_IP = 5;
2929

30+
/** @var int The duration we keep a bad IP in cache 20s */
31+
public const CACHE_EXPIRATION_FOR_BAD_IP = 20;
32+
3033
/** @var string The ban remediation */
3134
public const REMEDIATION_BAN = 'ban';
3235

0 commit comments

Comments
 (0)