Documentation
¶
Overview ¶
Package clientip resolves the real client address of a request that may have passed through load balancers or proxies.
A WAF that keys rate limits and IP lists on the wrong address is worse than none, so forwarding headers are only trusted when the request arrives from a configured trusted proxy, and X-Forwarded-For is walked right to left.
Index ¶
Constants ¶
const ( HeaderXForwardedFor = "X-Forwarded-For" // HeaderForwarded is RFC 7239's Forwarded, its for= parameters. HeaderForwarded = "Forwarded" )
The headers a Resolver reads the forwarding chain from.
const MaxTrustedHops = 10
MaxTrustedHops bounds Options.TrustedHops.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶ added in v0.5.0
type Options struct {
// TrustedProxies are CIDR prefixes (or plain addresses) of proxies whose
// forwarding header is trusted.
TrustedProxies []string
// TrustedHops is how many entries of the forwarding header, from the
// right, are proxies whatever their address (load balancers whose
// addresses aren't fixed). It applies only to a request from a trusted
// peer, before the address checks.
TrustedHops int
// Header is HeaderXForwardedFor (the default, "") or HeaderForwarded,
// case-insensitively.
Header string
}
Options configure a Resolver.
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver resolves client IPs. The zero value trusts no proxies and always returns the peer address.
func NewResolver ¶
NewResolver returns a Resolver that trusts forwarding headers set by peers in the given CIDR prefixes (for example the cloud load balancer ranges). Plain addresses are accepted and treated as single-host prefixes.
func (*Resolver) Resolve ¶
Resolve returns the client address for a request received from peer.
If peer is not a trusted proxy, peer is the client. Otherwise the forwarding chain (X-Forwarded-For, or Forwarded's for= parameters) is walked from right to left, skipping the first TrustedHops entries and then trusted proxies; the first other address is the client. If every hop is trusted, the leftmost valid address is returned. An entry may carry a port ("198.51.100.7:4711", "[2001:db8::1]:4711"). A malformed entry (or, in Forwarded, one without for= or with an obfuscated or "unknown" node) stops the walk, and the last valid address seen is returned.