Merge pull request #1269 from acelaya-forks/feature/replace-ip-lib

Feature/replace ip lib
This commit is contained in:
Alejandro Celaya 2021-12-12 17:21:08 +01:00 committed by GitHub
commit 14d3493db8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 25 deletions

View File

@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).
## [Unreleased] ## [2.10.0] - 2021-12-12
### Added ### Added
* [#1163](https://github.com/shlinkio/shlink/issues/1163) Allowed setting not-found redirects for default domain in the same way it's done for any other domain. * [#1163](https://github.com/shlinkio/shlink/issues/1163) Allowed setting not-found redirects for default domain in the same way it's done for any other domain.
@ -34,6 +34,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
* [#844](https://github.com/shlinkio/shlink/issues/844) Added mutation checks to API tests. * [#844](https://github.com/shlinkio/shlink/issues/844) Added mutation checks to API tests.
* [#1218](https://github.com/shlinkio/shlink/issues/1218) Updated to symfony/mercure 0.6. * [#1218](https://github.com/shlinkio/shlink/issues/1218) Updated to symfony/mercure 0.6.
* [#1223](https://github.com/shlinkio/shlink/issues/1223) Updated to phpstan 1.0. * [#1223](https://github.com/shlinkio/shlink/issues/1223) Updated to phpstan 1.0.
* [#1258](https://github.com/shlinkio/shlink/issues/1258) Updated to Symfony 6 components, except symfony/console.
* Added `domain` field to `DeleteShortUrlException` exception. * Added `domain` field to `DeleteShortUrlException` exception.
### Deprecated ### Deprecated

View File

@ -38,6 +38,7 @@
"mezzio/mezzio-fastroute": "^3.3", "mezzio/mezzio-fastroute": "^3.3",
"mezzio/mezzio-problem-details": "^1.5", "mezzio/mezzio-problem-details": "^1.5",
"mezzio/mezzio-swoole": "^3.5", "mezzio/mezzio-swoole": "^3.5",
"mlocati/ip-lib": "^1.17",
"monolog/monolog": "^2.3", "monolog/monolog": "^2.3",
"nikolaposa/monolog-factory": "^3.1", "nikolaposa/monolog-factory": "^3.1",
"ocramius/proxy-manager": "^2.11", "ocramius/proxy-manager": "^2.11",
@ -47,14 +48,13 @@
"predis/predis": "^1.1", "predis/predis": "^1.1",
"pugx/shortid-php": "^1.0", "pugx/shortid-php": "^1.0",
"ramsey/uuid": "^4.2", "ramsey/uuid": "^4.2",
"rlanvin/php-ip": "dev-master#6b3a785 as 3.0", "shlinkio/shlink-common": "^4.2",
"shlinkio/shlink-common": "dev-main#e7fdff3 as 4.2",
"shlinkio/shlink-config": "^1.4", "shlinkio/shlink-config": "^1.4",
"shlinkio/shlink-event-dispatcher": "dev-main#3925299 as 2.3", "shlinkio/shlink-event-dispatcher": "^2.3",
"shlinkio/shlink-importer": "dev-main#d099072 as 2.5", "shlinkio/shlink-importer": "^2.5",
"shlinkio/shlink-installer": "^6.3", "shlinkio/shlink-installer": "^6.3",
"shlinkio/shlink-ip-geolocation": "^2.2", "shlinkio/shlink-ip-geolocation": "^2.2",
"symfony/console": "^6.0 || ^5.4", "symfony/console": "^5.4",
"symfony/filesystem": "^6.0 || ^5.4", "symfony/filesystem": "^6.0 || ^5.4",
"symfony/lock": "^6.0 || ^5.4", "symfony/lock": "^6.0 || ^5.4",
"symfony/mercure": "^0.6", "symfony/mercure": "^0.6",

View File

@ -5,9 +5,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit; namespace Shlinkio\Shlink\Core\Visit;
use Fig\Http\Message\RequestMethodInterface; use Fig\Http\Message\RequestMethodInterface;
use InvalidArgumentException; use IPLib\Address\IPv4;
use IPLib\Factory;
use IPLib\Range\RangeInterface;
use Mezzio\Router\Middleware\ImplicitHeadMiddleware; use Mezzio\Router\Middleware\ImplicitHeadMiddleware;
use PhpIP\IP;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory; use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\ShortUrl;
@ -73,9 +74,8 @@ class RequestTracker implements RequestTrackerInterface, RequestMethodInterface
return false; return false;
} }
try { $ip = IPv4::parseString($remoteAddr);
$ip = IP::create($remoteAddr); if ($ip === null) {
} catch (InvalidArgumentException) {
return false; return false;
} }
@ -83,24 +83,23 @@ class RequestTracker implements RequestTrackerInterface, RequestMethodInterface
$disableTrackingFrom = $this->trackingOptions->disableTrackingFrom(); $disableTrackingFrom = $this->trackingOptions->disableTrackingFrom();
return some($disableTrackingFrom, function (string $value) use ($ip, $remoteAddrParts): bool { return some($disableTrackingFrom, function (string $value) use ($ip, $remoteAddrParts): bool {
try { $range = match (true) {
return match (true) { str_contains($value, '*') => $this->parseValueWithWildcards($value, $remoteAddrParts),
str_contains($value, '*') => $ip->matches($this->parseValueWithWildcards($value, $remoteAddrParts)), default => Factory::parseRangeString($value),
str_contains($value, '/') => $ip->isIn($value), };
default => $ip->matches($value),
}; return $range !== null && $ip->matches($range);
} catch (InvalidArgumentException) {
return false;
}
}); });
} }
private function parseValueWithWildcards(string $value, array $remoteAddrParts): string private function parseValueWithWildcards(string $value, array $remoteAddrParts): ?RangeInterface
{ {
// Replace wildcard parts with the corresponding ones from the remote address // Replace wildcard parts with the corresponding ones from the remote address
return implode('.', map( return Factory::parseRangeString(
explode('.', $value), implode('.', map(
fn (string $part, int $index) => $part === '*' ? $remoteAddrParts[$index] : $part, explode('.', $value),
)); fn (string $part, int $index) => $part === '*' ? $remoteAddrParts[$index] : $part,
)),
);
} }
} }