Files
traefik-botfilter/util.go
T
2026-01-13 14:16:00 +08:00

29 lines
727 B
Go

package botfilter
import (
"net"
"net/http"
"net/netip"
"net/url"
pathpkg "path"
"strings"
)
func clientIP(r *http.Request, cfg *compiledConfig) netip.Addr {
remote := parseRemoteAddress(r.RemoteAddr)
if cfg.clientIPHeader == "" || !addressInPrefixes(remote, cfg.trustedProxies) {
return remote
}
// A trusted reverse proxy must overwrite (not append to an untrusted
// client-provided value) this header. The left-most valid value is the
// original client in the conventional X-Forwarded-For representation.
for _, value := range strings.Split(r.Header.Get(cfg.clientIPHeader), ",") {
if addr, err := netip.ParseAddr(strings.TrimSpace(value)); err == nil {
return addr.Unmap()
}
}
return remote
}