fix: make logging middleware respect proxy headers

This commit is contained in:
Ferdinand Mütsch 2021-01-31 19:00:42 +01:00
parent c6139e5366
commit 4f7cc3c57e
1 changed files with 12 additions and 1 deletions

View File

@ -39,10 +39,21 @@ func (lg *LoggingMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.URL.String(),
duration,
ww.BytesWritten(),
r.RemoteAddr,
readUserIP(r),
)
}
func readUserIP(r *http.Request) string {
ip := r.Header.Get("X-Real-Ip")
if ip == "" {
ip = r.Header.Get("X-Forwarded-For")
}
if ip == "" {
ip = r.RemoteAddr
}
return ip
}
// The below writer-wrapping code has been lifted from
// https://github.com/zenazn/goji/blob/master/web/middleware/logger.go - because
// it does exactly what is needed, and it's unlikely to change in any