diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-05-24 01:37:34 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-05-24 02:11:57 -0400 |
commit | dbc93a011f442d09a18b7ae4ab34a8cfe64431dc (patch) | |
tree | 493dcd5a3639ef00b8f92c391727ddad7bdd2e71 | |
parent | 6cf0e264b121b754bf8006914c23d3e45f9d0415 (diff) | |
download | getwtxt-dbc93a011f442d09a18b7ae4ab34a8cfe64431dc.tar.gz |
checking for X-Forwarded-For header when adding user IP to context
-rw-r--r-- | http.go | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/http.go b/http.go index 8ba0442..98e0cb5 100644 --- a/http.go +++ b/http.go @@ -8,12 +8,20 @@ import ( "strings" ) -// Attaches a request's IP address to the request's context +// 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. func newCtxUserIP(ctx context.Context, r *http.Request) context.Context { base := strings.Split(r.RemoteAddr, ":") uip := base[0] + if _, ok := r.Header["X-Forwarded-For"]; ok { + proxied := r.Header["X-Forwarded-For"] + base = strings.Split(proxied[len(proxied)-1], ":") + uip = base[0] + } + return context.WithValue(ctx, ctxKey, uip) } |