cmd/anubis: set X-Real-Ip based on X-Forwarded-For (#63)
This triggers a SHAME release[0]. [0]: https://pridever.org/
This commit is contained in:
parent
a9777a3126
commit
07e6695430
6 changed files with 27 additions and 1 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/TecharoHQ/anubis"
|
||||
"github.com/sebest/xff"
|
||||
)
|
||||
|
||||
// UnchangingCache sets the Cache-Control header to cache a response for 1 year if
|
||||
|
@ -33,3 +34,17 @@ func DefaultXRealIP(defaultIP string, next http.Handler) http.Handler {
|
|||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// XForwardedForToXRealIP sets the X-Real-Ip header based on the contents
|
||||
// of the X-Forwarded-For header.
|
||||
func XForwardedForToXRealIP(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if xffHeader := r.Header.Get("X-Forwarded-For"); r.Header.Get("X-Real-Ip") == "" && xffHeader != "" {
|
||||
ip := xff.Parse(xffHeader)
|
||||
slog.Debug("setting x-real-ip", "val", ip)
|
||||
r.Header.Set("X-Real-Ip", ip)
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue