From 4f7cc3c57e0ee4b45014d6380313165fc4b312ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Sun, 31 Jan 2021 19:00:42 +0100 Subject: [PATCH] fix: make logging middleware respect proxy headers --- middlewares/logging.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/middlewares/logging.go b/middlewares/logging.go index dc614d1..2237067 100644 --- a/middlewares/logging.go +++ b/middlewares/logging.go @@ -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