Add proxy IP headers to Telegram notifications
All checks were successful
Tests / Laravel tests (pull_request) Successful in 12m3s

This commit is contained in:
2026-06-04 00:30:44 +02:00
parent 03b06632f9
commit da4bdc6bf4
6 changed files with 96 additions and 19 deletions

View File

@@ -7,16 +7,21 @@ use Illuminate\Support\Facades\Http;
class TelegramNotificationService
{
/**
* @param array<string, string|null> $ipHeaders
*/
public function notifyContactMessage(
string $name,
string $message,
string $ip,
string $userAgent,
?string $email = null,
?string $phone = null
?string $phone = null,
array $ipHeaders = []
): void {
$email ??= '-';
$phone ??= '-';
$ipHeaderText = $this->formatIpHeaders($ipHeaders);
$text = <<<TEXT
Nieuw contactbericht ontvangen
@@ -25,6 +30,7 @@ Naam: {$name}
Email: {$email}
Telefoon: {$phone}
IP: {$ip}
{$ipHeaderText}
User Agent: {$userAgent}
Bericht:
@@ -36,16 +42,21 @@ TEXT;
$this->send($text);
}
public function notifyPersonaliaClick(Personalia $personalia, ?string $ip, ?string $userAgent): void
/**
* @param array<string, string|null> $ipHeaders
*/
public function notifyPersonaliaClick(Personalia $personalia, ?string $ip, ?string $userAgent, array $ipHeaders = []): void
{
$ip ??= '-';
$userAgent ??= '-';
$ipHeaderText = $this->formatIpHeaders($ipHeaders);
$message = <<<TEXT
Persoonlijke gegevens bekeken
Naam: {$personalia->value}
IP: {$ip}
{$ipHeaderText}
User Agent: {$userAgent}
Tijdstip: {now()->format('d-m-Y H:i')}
@@ -56,11 +67,13 @@ TEXT;
/**
* @param array<string, mixed> $visit
* @param array<string, string|null> $ipHeaders
*/
public function notifyPageVisit(array $visit, ?string $ip, ?string $userAgent, ?string $acceptLanguage): void
public function notifyPageVisit(array $visit, ?string $ip, ?string $userAgent, ?string $acceptLanguage, array $ipHeaders = []): void
{
$screen = $this->formatDimensions($visit['screen'] ?? null);
$viewport = $this->formatDimensions($visit['viewport'] ?? null);
$ipHeaderText = $this->formatIpHeaders($ipHeaders);
$message = <<<TEXT
Pagina bezocht
@@ -72,6 +85,7 @@ Referrer: {$this->value($visit['referrer'] ?? null)}
Visitor ID: {$this->value($visit['visitor_id'] ?? null)}
IP: {$this->value($ip)}
{$ipHeaderText}
User Agent: {$this->value($userAgent)}
Accept-Language: {$this->value($acceptLanguage)}
Browser taal: {$this->value($visit['language'] ?? null)}
@@ -108,6 +122,23 @@ TEXT;
]);
}
/**
* @param array<string, string|null> $ipHeaders
*/
protected function formatIpHeaders(array $ipHeaders): string
{
$headers = [
'CF-Connecting-IP',
'X-Forwarded-For',
'X-Real-IP',
'Forwarded',
];
return collect($headers)
->map(fn (string $header): string => "{$header}: {$this->value($ipHeaders[$header] ?? null)}")
->implode("\n");
}
protected function value(mixed $value): string
{
if (is_bool($value)) {