Documentation
¶
Overview ¶
Package realip extracts a real IP address from the request.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get returns real IP from the given request. It checks headers in the following priority order:
- X-Real-IP - trusted proxy (nginx/reproxy) sets this to actual client
- CF-Connecting-IP - Cloudflare's header for original client
- X-Forwarded-For - leftmost public IP (original client in CDN chain)
- RemoteAddr - fallback for direct connections
Only public IPs are accepted from headers; private/loopback/link-local IPs are skipped.
func GetTrusted ¶ added in v1.26.0
func GetTrusted(r *http.Request, trustedProxies []*net.IPNet, opts ...TrustedOption) (string, error)
GetTrusted returns the client IP of the request, taking it from the forwarding headers only when the direct peer, the address in RemoteAddr, is one of the trusted proxies. Any other peer is known by its own address, whatever headers it sent.
From a trusted peer, X-Forwarded-For is read right to left, all its header lines as one list: each entry that is a trusted proxy is skipped and the first other entry is the client. If every entry is trusted, the leftmost one is. An empty or malformed entry ends the walk at the peer, so a client cannot lead it past such an entry to a forged one. Without the header the peer's own address is used, so a trusted proxy has to append to X-Forwarded-For, or the caller has to use PreferXRealIP: behind a proxy that sends no X-Forwarded-For every request gets the proxy's address, and an OnlyFrom or Profiler rule naming it, such as 127.0.0.1, admits every client. A proxy whose own peer is inside the trusted ranges, such as the gateway of its Docker network, makes the walk pass that address to whatever the client put in front: trust only the proxy's address, or use PreferXRealIP. X-Real-IP is read only with PreferXRealIP, and CF-Connecting-IP is never read: a proxy can pass either on from any client.
The address returned for the peer is bare, without the port. An error means RemoteAddr holds no IP. With no trusted proxies every request gets its peer's address.
func ParseTrustedProxies ¶ added in v1.26.0
ParseTrustedProxies parses a list of trusted proxies for GetTrusted. Each entry is a CIDR, such as 172.16.0.0/12, or a bare IP, which stands for that one host. Blank entries are skipped; a malformed entry is an error, so a typo cannot quietly leave a proxy untrusted. A list of blank entries only gives nil.
func TrustsAnyPeer ¶ added in v1.26.0
TrustsAnyPeer reports whether the list holds a catch-all network, one that covers every IPv4 or every IPv6 address, such as 0.0.0.0/0, ::/0 or the v4-mapped ::ffff:0.0.0.0/96. Such a network trusts the forwarding headers of every client and so lets any client choose its address.
Types ¶
type TrustedOption ¶ added in v1.26.0
type TrustedOption func(*trustedOpts)
TrustedOption changes how GetTrusted reads the headers of a trusted peer
func PreferXRealIP ¶ added in v1.26.0
func PreferXRealIP() TrustedOption
PreferXRealIP makes GetTrusted take X-Real-IP from a trusted peer ahead of X-Forwarded-For, as sent, when the request carries exactly one such header and it is an IP address, private addresses and those of trusted proxies included. Use it only when every trusted proxy sets X-Real-IP itself and replaces the client's, as nginx does with proxy_set_header X-Real-IP $remote_addr. A proxy that passes the client's X-Real-IP on, as net/http/httputil.ReverseProxy does, lets any client choose its address, loopback included. It suits a proxy whose own peer address is inside the trusted ranges, such as the gateway of a Docker network: X-Forwarded-For alone would walk past that address to whatever the client put in front.