Make sure IpGeolocationMiddleware skips localhost

This commit is contained in:
Alejandro Celaya
2024-11-19 09:08:04 +01:00
parent 052c9e76a1
commit fa08014226
3 changed files with 13 additions and 4 deletions

View File

@@ -9,6 +9,7 @@ use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\Util\IpAddress;
use Shlinkio\Shlink\Core\Config\Options\TrackingOptions;
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface;
@@ -46,7 +47,9 @@ readonly class IpGeolocationMiddleware implements MiddlewareInterface
private function geolocateIpAddress(string|null $ipAddress): Location
{
try {
return $ipAddress === null ? Location::empty() : $this->ipLocationResolver->resolveIpLocation($ipAddress);
return $ipAddress === null || $ipAddress === IpAddress::LOCALHOST
? Location::empty()
: $this->ipLocationResolver->resolveIpLocation($ipAddress);
} catch (WrongIpException $e) {
$this->logger->warning('Tried to locate IP address, but it seems to be wrong. {e}', ['e' => $e]);
return Location::empty();

View File

@@ -54,7 +54,7 @@ readonly class VisitLocator implements VisitLocatorInterface
}
// If the IP address is non-locatable, locate it as empty to prevent next processes to pick it again
$location = Location::emptyInstance();
$location = Location::empty();
}
$this->locateVisit($visit, VisitLocation::fromGeolocation($location), $helper);