fix: rate limiting X-Real-IP from the real peer
This commit is contained in:
1 parent
9b47685002
commit
0e050c36ff
11 files changed
+439
-8
No files matched your search
@@ -0,0 +1,44 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func TrustedProxyRealIP(trustedProxies []string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
realIP := r.Header.Get("X-Real-IP")
|
||||
if realIP != "" && isTrustedProxy(r.RemoteAddr, trustedProxies) {
|
||||
r.RemoteAddr = withRewrittenHost(r.RemoteAddr, realIP)
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func isTrustedProxy(remoteAddr string, trustedProxies []string) bool {
|
||||
host := hostOf(remoteAddr)
|
||||
for _, trusted := range trustedProxies {
|
||||
if host == trusted {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func hostOf(remoteAddr string) string {
|
||||
host, _, err := net.SplitHostPort(remoteAddr)
|
||||
if err != nil {
|
||||
return remoteAddr
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
func withRewrittenHost(remoteAddr string, newHost string) string {
|
||||
_, port, err := net.SplitHostPort(remoteAddr)
|
||||
if err != nil {
|
||||
return newHost
|
||||
}
|
||||
return net.JoinHostPort(newHost, port)
|
||||
}
|
||||
Reference in new issue
Block a user