Skip to content

Commit

Permalink
Merge pull request #49 from mahdibashirpour/fix/ip-reverse-proxy
Browse files Browse the repository at this point in the history
[1.x] Fix IP address retrieval when using reverse proxy
  • Loading branch information
milwad-dev authored Dec 16, 2024
2 parents 3e3812c + 5047306 commit f721646
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
16 changes: 12 additions & 4 deletions config/user-monitoring.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
/*
Expand All @@ -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',
],
Expand Down Expand Up @@ -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
*/
Expand All @@ -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'
],

/*
Expand Down
12 changes: 11 additions & 1 deletion src/Traits/Actionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit f721646

Please sign in to comment.