Skip to content

Commit 1651580

Browse files
feat(*): Update mlocati/ip-lib dependency and remove deprecated methods
1 parent 7d4aae2 commit 1651580

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"symfony/cache": "^5.2",
3535
"monolog/monolog": "^1.17 || ^2.1",
3636
"gregwar/captcha": "^1.1",
37-
"mlocati/ip-lib": "^1.14",
37+
"mlocati/ip-lib": "^1.18",
3838
"geoip2/geoip2": "^2.12.2"
3939
},
4040
"require-dev": {

src/AbstractBounce.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
require_once __DIR__.'/templates/captcha.php';
66
require_once __DIR__.'/templates/access-forbidden.php';
77

8+
use Bramus\Monolog\Formatter\ColoredLineFormatter;
9+
use Exception;
810
use IPLib\Factory;
911
use Monolog\Formatter\LineFormatter;
1012
use Monolog\Handler\RotatingFileHandler;
@@ -50,6 +52,7 @@ protected function getArraySettings(string $name): array
5052

5153
/**
5254
* Run a bounce.
55+
* @throws Exception
5356
*/
5457
public function run(
5558
): void {
@@ -86,12 +89,15 @@ protected function initLoggerHelper($logDirectoryPath, $loggerName): void
8689
$debugLogPath = $logDirectoryPath.'/debug.log';
8790
$debugFileHandler = new RotatingFileHandler($debugLogPath, 0, Logger::DEBUG);
8891
if (class_exists('\Bramus\Monolog\Formatter\ColoredLineFormatter')) {
89-
$debugFileHandler->setFormatter(new \Bramus\Monolog\Formatter\ColoredLineFormatter(null, "[%datetime%] %message% %context%\n", 'H:i:s'));
92+
$debugFileHandler->setFormatter(new ColoredLineFormatter(null, "[%datetime%] %message% %context%\n", 'H:i:s'));
9093
$this->logger->pushHandler($debugFileHandler);
9194
}
9295
}
9396
}
9497

98+
/**
99+
* @throws Exception
100+
*/
95101
protected function bounceCurrentIp()
96102
{
97103
$ip = $this->getRemoteIp();
@@ -116,7 +122,7 @@ protected function bounceCurrentIp()
116122
$this->getBouncerInstance();
117123
$remediation = $this->bouncer->getRemediationForIp($ip);
118124
$this->handleRemediation($remediation, $ip);
119-
} catch (\Exception $e) {
125+
} catch (Exception $e) {
120126
$this->logger->warning('', [
121127
'type' => 'UNKNOWN_EXCEPTION_WHILE_BOUNCING',
122128
'ip' => $ip,
@@ -133,7 +139,7 @@ protected function bounceCurrentIp()
133139

134140
protected function shouldTrustXforwardedFor(string $ip): bool
135141
{
136-
$comparableAddress = Factory::addressFromString($ip)->getComparableString();
142+
$comparableAddress = Factory::parseAddressString($ip, 3)->getComparableString();
137143
if (null === $comparableAddress) {
138144
$this->logger->warning('', [
139145
'type' => 'INVALID_INPUT_IP',
@@ -198,7 +204,7 @@ protected function handleCaptchaResolutionForm(string $ip)
198204
}
199205

200206
// Handle image refresh.
201-
if (null !== $this->getPostedVariable('refresh') && (bool) (int) $this->getPostedVariable('refresh')) {
207+
if (null !== $this->getPostedVariable('refresh') && (int) $this->getPostedVariable('refresh')) {
202208
// Generate new captcha image for the user
203209
$this->storeNewCaptchaCoupleInSession();
204210
$this->setSessionVariable('crowdsec_captcha_resolution_failed', false);
@@ -213,7 +219,7 @@ protected function handleCaptchaResolutionForm(string $ip)
213219
$this->getSessionVariable('crowdsec_captcha_phrase_to_guess'),
214220
$this->getPostedVariable('phrase'),
215221
$ip)) {
216-
// User has correctly fill the captcha
222+
// User has correctly filled the captcha
217223
$this->setSessionVariable('crowdsec_captcha_has_to_be_resolved', false);
218224
$this->unsetSessionVariable('crowdsec_captcha_phrase_to_guess');
219225
$this->unsetSessionVariable('crowdsec_captcha_inline_image');
@@ -235,7 +241,7 @@ protected function handleCaptchaRemediation($ip)
235241
$this->handleCaptchaResolutionForm($ip);
236242

237243
if (null === $this->getSessionVariable('crowdsec_captcha_has_to_be_resolved')) {
238-
// Setup the first captcha remediation.
244+
// Set up the first captcha remediation.
239245

240246
$this->storeNewCaptchaCoupleInSession();
241247
$this->setSessionVariable('crowdsec_captcha_has_to_be_resolved', true);
@@ -257,16 +263,14 @@ protected function handleRemediation(string $remediation, string $ip)
257263
$this->clearCaptchaSessionContext();
258264
}
259265
switch ($remediation) {
260-
case Constants::REMEDIATION_BYPASS:
261-
return;
262266
case Constants::REMEDIATION_CAPTCHA:
263267
$this->handleCaptchaRemediation($ip);
264268
break;
265269
case Constants::REMEDIATION_BAN:
266270
$this->handleBanRemediation();
267271
break;
272+
case Constants::REMEDIATION_BYPASS:
268273
default:
269-
return;
270274
}
271275
}
272276
}

src/ApiCache.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ private function saveRemediations(array $decisions): array
384384
$id = $remediation[2];
385385

386386
if (Constants::SCOPE_IP === $decision['scope']) {
387-
$address = Factory::addressFromString($decision['value']);
387+
$address = Factory::parseAddressString($decision['value'], 3);
388388
if (null === $address) {
389389
$this->logger->warning('', [
390390
'type' => 'INVALID_IP_TO_ADD_FROM_REMEDIATION',
@@ -395,7 +395,7 @@ private function saveRemediations(array $decisions): array
395395
$cacheKey = $this->getCacheKey($decision['scope'], $address->toString());
396396
$this->addRemediationToCacheItem($cacheKey, $type, $exp, $id);
397397
} elseif (Constants::SCOPE_RANGE === $decision['scope']) {
398-
$range = Subnet::fromString($decision['value']);
398+
$range = Subnet::parseString($decision['value']);
399399

400400
$addressType = $range->getAddressType();
401401
$isIpv6 = (Type::T_IPv6 === $addressType);
@@ -438,7 +438,7 @@ private function removeRemediations(array $decisions): array
438438
$count = 0;
439439
foreach ($decisions as $decision) {
440440
if (Constants::SCOPE_IP === $decision['scope']) {
441-
$address = Factory::addressFromString($decision['value']);
441+
$address = Factory::parseAddressString($decision['value'], 3);
442442
if (null === $address) {
443443
$this->logger->warning('', [
444444
'type' => 'INVALID_IP_TO_REMOVE_FROM_REMEDIATION',
@@ -457,7 +457,7 @@ private function removeRemediations(array $decisions): array
457457
]);
458458
}
459459
} elseif (Constants::SCOPE_RANGE === $decision['scope']) {
460-
$range = Subnet::fromString($decision['value']);
460+
$range = Subnet::parseString($decision['value']);
461461

462462
$addressType = $range->getAddressType();
463463
$isIpv6 = (Type::T_IPv6 === $addressType);

src/Bouncer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use CrowdSecBouncer\Fixes\Gregwar\Captcha\CaptchaBuilder;
99
use Gregwar\Captcha\PhraseBuilder;
1010
use IPLib\Factory;
11+
use IPLib\ParseStringFlag;
1112
use Monolog\Handler\NullHandler;
1213
use Monolog\Logger;
1314
use Psr\Log\LoggerInterface;
@@ -109,7 +110,7 @@ private function capRemediationLevel(string $remediation): string
109110
*/
110111
public function getRemediationForIp(string $ip): string
111112
{
112-
$address = Factory::addressFromString($ip, false);
113+
$address = Factory::parseAddressString($ip, ParseStringFlag::MAY_INCLUDE_ZONEID);
113114
if (null === $address) {
114115
throw new BouncerException("IP $ip format is invalid.");
115116
}

0 commit comments

Comments
 (0)