From 3fef4b4a2826eb2084eaa9f7ada007645bc0c337 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 22 Mar 2020 10:48:27 +0100 Subject: [PATCH] Ensured non-obfuscated IP address is passed to event listener which geolocates it --- .../Core/src/EventDispatcher/LocateShortUrlVisit.php | 11 ++++++----- module/Core/src/EventDispatcher/ShortUrlVisited.php | 11 +++++++++-- module/Core/src/Service/VisitsTracker.php | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/module/Core/src/EventDispatcher/LocateShortUrlVisit.php b/module/Core/src/EventDispatcher/LocateShortUrlVisit.php index a0f8d033..6abbe02b 100644 --- a/module/Core/src/EventDispatcher/LocateShortUrlVisit.php +++ b/module/Core/src/EventDispatcher/LocateShortUrlVisit.php @@ -53,7 +53,7 @@ class LocateShortUrlVisit } if ($this->downloadOrUpdateGeoLiteDb($visitId)) { - $this->locateVisit($visitId, $visit); + $this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit); } $this->eventDispatcher->dispatch(new VisitLocated($visitId)); @@ -80,12 +80,13 @@ class LocateShortUrlVisit return true; } - private function locateVisit(string $visitId, Visit $visit): void + private function locateVisit(string $visitId, ?string $originalIpAddress, Visit $visit): void { + $isLocatable = $originalIpAddress !== null || $visit->isLocatable(); + $addr = $originalIpAddress ?? $visit->getRemoteAddr(); + try { - $location = $visit->isLocatable() - ? $this->ipLocationResolver->resolveIpLocation($visit->getRemoteAddr()) - : Location::emptyInstance(); + $location = $isLocatable ? $this->ipLocationResolver->resolveIpLocation($addr) : Location::emptyInstance(); $visit->locate(new VisitLocation($location)); $this->em->flush(); diff --git a/module/Core/src/EventDispatcher/ShortUrlVisited.php b/module/Core/src/EventDispatcher/ShortUrlVisited.php index 1f0b5b5c..c33f805a 100644 --- a/module/Core/src/EventDispatcher/ShortUrlVisited.php +++ b/module/Core/src/EventDispatcher/ShortUrlVisited.php @@ -9,10 +9,12 @@ use JsonSerializable; final class ShortUrlVisited implements JsonSerializable { private string $visitId; + private ?string $originalIpAddress; - public function __construct(string $visitId) + public function __construct(string $visitId, ?string $originalIpAddress = null) { $this->visitId = $visitId; + $this->originalIpAddress = $originalIpAddress; } public function visitId(): string @@ -20,8 +22,13 @@ final class ShortUrlVisited implements JsonSerializable return $this->visitId; } + public function originalIpAddress(): ?string + { + return $this->originalIpAddress; + } + public function jsonSerialize(): array { - return ['visitId' => $this->visitId]; + return ['visitId' => $this->visitId, 'originalIpAddress' => $this->originalIpAddress]; } } diff --git a/module/Core/src/Service/VisitsTracker.php b/module/Core/src/Service/VisitsTracker.php index 54abe319..f477681a 100644 --- a/module/Core/src/Service/VisitsTracker.php +++ b/module/Core/src/Service/VisitsTracker.php @@ -39,7 +39,7 @@ class VisitsTracker implements VisitsTrackerInterface $this->em->persist($visit); $this->em->flush(); - $this->eventDispatcher->dispatch(new ShortUrlVisited($visit->getId())); + $this->eventDispatcher->dispatch(new ShortUrlVisited($visit->getId(), $visitor->getRemoteAddress())); } /**