1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-07-15 02:04:17 +02:00

[#282] added the "real" user ip to the logs

This commit is contained in:
Gani Georgiev
2022-08-18 15:27:45 +03:00
parent 9bf66be870
commit cfaff31d97
19 changed files with 241 additions and 146 deletions

View File

@ -252,7 +252,8 @@ func ActivityLogger(app core.App) echo.MiddlewareFunc {
Method: strings.ToLower(httpRequest.Method),
Status: status,
Auth: requestAuth,
Ip: httpRequest.RemoteAddr,
UserIp: realUserIp(httpRequest),
RemoteIp: httpRequest.RemoteAddr,
Referer: httpRequest.Referer(),
UserAgent: httpRequest.UserAgent(),
Meta: meta,
@ -297,3 +298,23 @@ func ActivityLogger(app core.App) echo.MiddlewareFunc {
}
}
}
// Returns the "real" user IP from common proxy headers
// (fallback to [r.RemoteAddr]).
//
// The returned IP shouldn't be trusted if not behind a trusted reverse proxy!
func realUserIp(r *http.Request) string {
ipHeaders := []string{
"CF-Connecting-IP",
"X-Forwarded-For",
"X-Real-Ip",
}
for _, header := range ipHeaders {
if ip := r.Header.Get(header); ip != "" {
return ip
}
}
return r.RemoteAddr
}