diff --git a/src/Admin/src/Service/AdminService.php b/src/Admin/src/Service/AdminService.php index e63f537..49342da 100644 --- a/src/Admin/src/Service/AdminService.php +++ b/src/Admin/src/Service/AdminService.php @@ -205,7 +205,7 @@ public function logAdminVisit(array $serverParams, string $name, string $status) $organization = $this->locationService->getOrganization($ipAddress)->getName(); $adminLogin = (new AdminLogin()) - ->setAdminIp($this->locationService->obfuscateIpAddress($ipAddress)) + ->setAdminIp(IpService::obfuscateIpAddress($ipAddress)) ->setContinent($continent) ->setCountry($country) ->setOrganization($organization) diff --git a/src/App/src/Service/IpService.php b/src/App/src/Service/IpService.php index fa694fe..53276c4 100644 --- a/src/App/src/Service/IpService.php +++ b/src/App/src/Service/IpService.php @@ -6,6 +6,9 @@ use function filter_var; use function getenv; +use function preg_replace_callback; +use function str_repeat; +use function strlen; use const FILTER_FLAG_IPV4; use const FILTER_FLAG_IPV6; @@ -53,4 +56,17 @@ public static function isPublicIp(string $ipAddress): bool FILTER_FLAG_NO_RES_RANGE ) === $ipAddress; } + + public static function obfuscateIpAddress(string $ipAddress, string $mask = 'x'): string + { + if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { + $pattern = '/\d+$/'; + } elseif (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { + $pattern = '/[a-z0-9]+$/i'; + } else { + return $ipAddress; + } + + return preg_replace_callback($pattern, fn (array $last) => str_repeat($mask, strlen($last[0])), $ipAddress); + } }