From 269295715f2bee97254ccfae1fe20985cdf91ed7 Mon Sep 17 00:00:00 2001 From: mahdi bashirpour <50519818+mahdibashirpour@users.noreply.github.com> Date: Mon, 16 Dec 2024 13:06:16 +0330 Subject: [PATCH 1/3] Fix IP address retrieval when using reverse proxy --- config/user-monitoring.php | 9 ++++++++- src/Traits/Actionable.php | 6 +++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/config/user-monitoring.php b/config/user-monitoring.php index 0517a48..50e1772 100644 --- a/config/user-monitoring.php +++ b/config/user-monitoring.php @@ -88,7 +88,6 @@ */ 'action_monitoring' => [ 'table' => 'actions_monitoring', - /* * Monitor actions. * @@ -100,6 +99,14 @@ 'on_read' => true, 'on_restore' => false, 'on_replicate' => false, + + /** + * Determines if the application should use reverse proxy headers to fetch the real client IP + * If set to true, it will try to get the IP from the specified header (X-Real-IP or X-Forwarded-For) + * This is useful when using reverse proxies like Nginx or Cloudflare. + */ + 'use_reverse_proxy_ip' => false, + 'real_ip_header' => 'X-Forwarded-For' ], /* diff --git a/src/Traits/Actionable.php b/src/Traits/Actionable.php index 9b08d5b..4ffebe1 100644 --- a/src/Traits/Actionable.php +++ b/src/Traits/Actionable.php @@ -73,7 +73,11 @@ private static function insertActionMonitoring(mixed $model, string $actionType) 'browser_name' => $detector->getBrowser(), 'platform' => $detector->getDevice(), 'device' => $detector->getDevice(), - 'ip' => request()->ip(), + 'ip' => config('user-monitoring.use_reverse_proxy_ip') + ? request()->header(config('user-monitoring.real_ip_header')) + ?: request()->ip() + ?: request()->ip() + : request()->ip(),, 'page' => request()->url(), 'created_at' => now(), 'updated_at' => now(), From 9f2f6739689b6e382b7b88b7679a1032aef351f3 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi <98118400+milwad-dev@users.noreply.github.com> Date: Mon, 16 Dec 2024 13:19:13 +0330 Subject: [PATCH 2/3] Update Actionable.php --- src/Traits/Actionable.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Traits/Actionable.php b/src/Traits/Actionable.php index 4ffebe1..5a0143a 100644 --- a/src/Traits/Actionable.php +++ b/src/Traits/Actionable.php @@ -73,14 +73,20 @@ private static function insertActionMonitoring(mixed $model, string $actionType) 'browser_name' => $detector->getBrowser(), 'platform' => $detector->getDevice(), 'device' => $detector->getDevice(), - 'ip' => config('user-monitoring.use_reverse_proxy_ip') - ? request()->header(config('user-monitoring.real_ip_header')) - ?: request()->ip() - ?: request()->ip() - : request()->ip(),, + 'ip' => $this->getRealIP(), 'page' => request()->url(), 'created_at' => now(), 'updated_at' => now(), ]); } + + /** + * Get real ip. + */ + private function getRealIP(): string + { + return config('user-monitoring.use_reverse_proxy_ip') + ? request()->header(config('user-monitoring.real_ip_header')) ?: request()->ip() ?: request()->ip() + : request()->ip(); + } } From 5047306ffc0c5097a81ecfc8ac172b66d423bf77 Mon Sep 17 00:00:00 2001 From: Milwad Khosravi <98118400+milwad-dev@users.noreply.github.com> Date: Mon, 16 Dec 2024 13:20:03 +0330 Subject: [PATCH 3/3] Update user-monitoring.php --- config/user-monitoring.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/config/user-monitoring.php b/config/user-monitoring.php index 50e1772..0090b06 100644 --- a/config/user-monitoring.php +++ b/config/user-monitoring.php @@ -13,7 +13,7 @@ /* * User properties. * - * You can customize the user guard, table, foreign key, and ... . + * You can customize the user guard, table, foreign key, and ... */ 'user' => [ /* @@ -39,12 +39,12 @@ /* * If you are using uuid or ulid you can change it for the type of foreign_key. * - * When you are using ulid or uuid, you need to add related traits into the models. + * When using ulid or uuid, you need to add related traits into the models. */ 'foreign_key_type' => 'id', // uuid, ulid, id /* - * If you want to display custom username, you can create your own attribute in User and change this value. + * If you want to display a custom username, you can create your attribute in User and change this value. */ 'display_attribute' => 'name', ], @@ -76,7 +76,7 @@ /* * If you want to delete visit rows after some days, you can change this to 360 for example, - * but you don't like to delete rows you can change it to 0. + * but if you don't like to delete rows you can change it to 0. * * For this feature you need Task-Scheduling => https://laravel.com/docs/10.x/scheduling */ @@ -88,6 +88,7 @@ */ 'action_monitoring' => [ 'table' => 'actions_monitoring', + /* * Monitor actions. *