Skip to content

Commit bdc34d4

Browse files
committed
Check if the request ip is forwarded or using cloudflare
1 parent 52fb752 commit bdc34d4

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/App/Http/Traits/ActivityLogger.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function activity($description = null, $details = null)
3232
if (Crawler::isCrawler()) {
3333
$userType = trans('LaravelLogger::laravel-logger.userTypes.crawler');
3434
if (is_null($description)) {
35-
$description = $userType.' '.trans('LaravelLogger::laravel-logger.verbTypes.crawled').' '.Request::fullUrl();
35+
$description = $userType . ' ' . trans('LaravelLogger::laravel-logger.verbTypes.crawled') . ' ' . Request::fullUrl();
3636
}
3737
}
3838

@@ -57,7 +57,15 @@ public static function activity($description = null, $details = null)
5757
break;
5858
}
5959

60-
$description = $verb.' '.Request::path();
60+
$description = $verb . ' ' . Request::path();
61+
}
62+
63+
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
64+
$ip = $_SERVER["HTTP_CF_CONNECTING_IP"];
65+
} elseif (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
66+
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
67+
} else {
68+
$ip = Request::ip();
6169
}
6270

6371
$data = [
@@ -66,7 +74,7 @@ public static function activity($description = null, $details = null)
6674
'userType' => $userType,
6775
'userId' => $userId,
6876
'route' => Request::fullUrl(),
69-
'ipAddress' => Request::ip(),
77+
'ipAddress' => $ip,
7078
'userAgent' => Request::header('user-agent'),
7179
'locale' => Request::header('accept-language'),
7280
'referer' => Request::header('referer'),
@@ -78,7 +86,7 @@ public static function activity($description = null, $details = null)
7886
if ($validator->fails()) {
7987
$errors = self::prepareErrorMessage($validator->errors(), $data);
8088
if (config('LaravelLogger.logDBActivityLogFailuresToFile')) {
81-
Log::error('Failed to record activity event. Failed Validation: '.$errors);
89+
Log::error('Failed to record activity event. Failed Validation: ' . $errors);
8290
}
8391
} else {
8492
self::storeActivity($data);

0 commit comments

Comments
 (0)