Skip to content

Commit 6531cf9

Browse files
committed
fix cache key
1 parent 61e852d commit 6531cf9

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

src/ApiCache.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ private function saveRemediations(array $decisions): array
294294

295295
if ('Ip' === $decision['scope']) {
296296
$address = Factory::addressFromString($decision['value']);
297-
$this->addRemediationToCacheItem(Bouncer::formatIpAsCacheKey($address), $type, $exp, $id);
297+
$this->addRemediationToCacheItem($address->toString(), $type, $exp, $id);
298298
} elseif ('Range' === $decision['scope']) {
299299
$range = Subnet::fromString($decision['value']);
300300

@@ -310,11 +310,11 @@ private function saveRemediations(array $decisions): array
310310

311311
$comparableEndAddress = $range->getComparableEndString();
312312
$address = $range->getStartAddress();
313-
$this->addRemediationToCacheItem(Bouncer::formatIpAsCacheKey($address), $type, $exp, $id);
313+
$this->addRemediationToCacheItem($address->toString(), $type, $exp, $id);
314314
$ipCount = 1;
315315
do {
316316
$address = $address->getNextAddress();
317-
$this->addRemediationToCacheItem(Bouncer::formatIpAsCacheKey($address), $type, $exp, $id);
317+
$this->addRemediationToCacheItem($address->toString(), $type, $exp, $id);
318318
++$ipCount;
319319
if ($ipCount >= 1000) {
320320
throw new BouncerException("Unable to store the decision ${$decision['id']}, the IP range: ${$decision['value']} is too large and can cause storage problem. Decision ignored.");
@@ -333,7 +333,7 @@ private function removeRemediations(array $decisions): array
333333
foreach ($decisions as $decision) {
334334
if ('Ip' === $decision['scope']) {
335335
$address = Factory::addressFromString($decision['value']);
336-
if (!$this->removeDecisionFromRemediationItem(Bouncer::formatIpAsCacheKey($address), $decision['id'])) {
336+
if (!$this->removeDecisionFromRemediationItem($address->toString(), $decision['id'])) {
337337
$this->logger->debug('', ['type' => 'DECISION_TO_REMOVE_NOT_FOUND_IN_CACHE', 'decision' => $decision['id']]);
338338
} else {
339339
$this->logger->debug('', [
@@ -356,14 +356,14 @@ private function removeRemediations(array $decisions): array
356356

357357
$comparableEndAddress = $range->getComparableEndString();
358358
$address = $range->getStartAddress();
359-
if (!$this->removeDecisionFromRemediationItem(Bouncer::formatIpAsCacheKey($address), $decision['id'])) {
359+
if (!$this->removeDecisionFromRemediationItem($address->toString(), $decision['id'])) {
360360
$this->logger->debug('', ['type' => 'DECISION_TO_REMOVE_NOT_FOUND_IN_CACHE', 'decision' => $decision['id']]);
361361
}
362362
$ipCount = 1;
363363
$success = true;
364364
do {
365365
$address = $address->getNextAddress();
366-
if (!$this->removeDecisionFromRemediationItem(Bouncer::formatIpAsCacheKey($address), $decision['id'])) {
366+
if (!$this->removeDecisionFromRemediationItem($address->toString(), $decision['id'])) {
367367
$success = false;
368368
}
369369
++$ipCount;
@@ -532,7 +532,7 @@ private function hit(string $ip): string
532532
*/
533533
public function get(AddressInterface $address): string
534534
{
535-
$cacheKey = Bouncer::formatIpAsCacheKey($address);
535+
$cacheKey = $address->toString();
536536
$this->logger->debug('', ['type' => 'START_IP_CHECK', 'ip' => $cacheKey]);
537537
if (!$this->liveMode && !$this->warmedUp) {
538538
throw new BouncerException('CrowdSec Bouncer configured in "stream" mode. Please warm the cache up before trying to access it.');

src/Bouncer.php

-14
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
use Gregwar\Captcha\CaptchaBuilder;
99
use Gregwar\Captcha\PhraseBuilder;
10-
use IPLib\Address\AddressInterface;
11-
use IPLib\Address\Type;
1210
use IPLib\Factory;
1311
use Monolog\Handler\NullHandler;
1412
use Monolog\Logger;
@@ -93,18 +91,6 @@ private function capRemediationLevel(string $remediation): string
9391
return $remediation;
9492
}
9593

96-
/**
97-
* Format the input IP address as a cache key.
98-
*/
99-
public static function formatIpAsCacheKey(AddressInterface $ip): string
100-
{
101-
if (Type::T_IPv6 === $ip->getAddressType) {
102-
return implode(':', \array_slice($ip->toString(true), 0, 4));
103-
}
104-
105-
return $ip->toString();
106-
}
107-
10894
/**
10995
* Get the remediation for the specified IP. This method use the cache layer.
11096
* In live mode, when no remediation was found in cache,

0 commit comments

Comments
 (0)