about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-25 03:38:36 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-25 03:38:36 -0400
commit07872ef4cafab8b7c975936057c779799bb655ff (patch)
treeb9f7306b1703b7be2b0b48c679b857b7b55bc0d0
parent55c8ffa2d8ef4509fd0f8b2035ec9c4af7e41b77 (diff)
downloadgetwtxt-07872ef4cafab8b7c975936057c779799bb655ff.tar.gz
checking for X-Real-IP header sometimes used by nginx when forwarding requests to getwtxt
-rw-r--r--http.go8
1 files changed, 7 insertions, 1 deletions
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)
 }