diff --git a/config/user-monitoring.php b/config/user-monitoring.php index 0517a48..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 */ @@ -100,6 +100,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..5a0143a 100644 --- a/src/Traits/Actionable.php +++ b/src/Traits/Actionable.php @@ -73,10 +73,20 @@ private static function insertActionMonitoring(mixed $model, string $actionType) 'browser_name' => $detector->getBrowser(), 'platform' => $detector->getDevice(), 'device' => $detector->getDevice(), - '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(); + } }