From 07872ef4cafab8b7c975936057c779799bb655ff Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Sat, 25 May 2019 03:38:36 -0400 Subject: checking for X-Real-IP header sometimes used by nginx when forwarding requests to getwtxt --- http.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/http.go b/http.go index 98e0cb5..fcce0a7 100644 --- a/http.go +++ b/http.go @@ -10,7 +10,7 @@ import ( // Attaches a request's IP address to the request's context. // If getwtxt is behind a reverse proxy, get the last entry -// in the X-Forwarded-For HTTP header as the user IP. +// in the X-Forwarded-For or X-Real-IP HTTP header as the user IP. func newCtxUserIP(ctx context.Context, r *http.Request) context.Context { base := strings.Split(r.RemoteAddr, ":") @@ -22,6 +22,12 @@ func newCtxUserIP(ctx context.Context, r *http.Request) context.Context { uip = base[0] } + if _, ok := r.Header["X-Real-IP"]; ok { + proxied := r.Header["X-Real-IP"] + base = strings.Split(proxied[len(proxied)-1], ":") + uip = base[0] + } + return context.WithValue(ctx, ctxKey, uip) } -- cgit 1.4.1-2-gfad0