Skip to content

Commit f721646

Browse files
authored
Merge pull request #49 from mahdibashirpour/fix/ip-reverse-proxy
[1.x] Fix IP address retrieval when using reverse proxy
2 parents 3e3812c + 5047306 commit f721646

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

config/user-monitoring.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/*
1414
* User properties.
1515
*
16-
* You can customize the user guard, table, foreign key, and ... .
16+
* You can customize the user guard, table, foreign key, and ...
1717
*/
1818
'user' => [
1919
/*
@@ -39,12 +39,12 @@
3939
/*
4040
* If you are using uuid or ulid you can change it for the type of foreign_key.
4141
*
42-
* When you are using ulid or uuid, you need to add related traits into the models.
42+
* When using ulid or uuid, you need to add related traits into the models.
4343
*/
4444
'foreign_key_type' => 'id', // uuid, ulid, id
4545

4646
/*
47-
* If you want to display custom username, you can create your own attribute in User and change this value.
47+
* If you want to display a custom username, you can create your attribute in User and change this value.
4848
*/
4949
'display_attribute' => 'name',
5050
],
@@ -76,7 +76,7 @@
7676

7777
/*
7878
* If you want to delete visit rows after some days, you can change this to 360 for example,
79-
* but you don't like to delete rows you can change it to 0.
79+
* but if you don't like to delete rows you can change it to 0.
8080
*
8181
* For this feature you need Task-Scheduling => https://laravel.com/docs/10.x/scheduling
8282
*/
@@ -100,6 +100,14 @@
100100
'on_read' => true,
101101
'on_restore' => false,
102102
'on_replicate' => false,
103+
104+
/**
105+
* Determines if the application should use reverse proxy headers to fetch the real client IP
106+
* If set to true, it will try to get the IP from the specified header (X-Real-IP or X-Forwarded-For)
107+
* This is useful when using reverse proxies like Nginx or Cloudflare.
108+
*/
109+
'use_reverse_proxy_ip' => false,
110+
'real_ip_header' => 'X-Forwarded-For'
103111
],
104112

105113
/*

src/Traits/Actionable.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,20 @@ private static function insertActionMonitoring(mixed $model, string $actionType)
7373
'browser_name' => $detector->getBrowser(),
7474
'platform' => $detector->getDevice(),
7575
'device' => $detector->getDevice(),
76-
'ip' => request()->ip(),
76+
'ip' => $this->getRealIP(),
7777
'page' => request()->url(),
7878
'created_at' => now(),
7979
'updated_at' => now(),
8080
]);
8181
}
82+
83+
/**
84+
* Get real ip.
85+
*/
86+
private function getRealIP(): string
87+
{
88+
return config('user-monitoring.use_reverse_proxy_ip')
89+
? request()->header(config('user-monitoring.real_ip_header')) ?: request()->ip() ?: request()->ip()
90+
: request()->ip();
91+
}
8292
}

0 commit comments

Comments
 (0)