Skip to content

Commit

Permalink
fix: make logging middleware respect proxy headers
Browse files Browse the repository at this point in the history
  • Loading branch information
muety committed Jan 31, 2021
1 parent c6139e5 commit 4f7cc3c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion middlewares/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4f7cc3c

Please sign in to comment.