Documentation
¶
Overview ¶
Package realip provides functions to validate proxy forwarding headers to determine the client IP address or forward trusted header values.
Index ¶
- Constants
- type Option
- type RealIP
- func (r *RealIP) FromRequest(req *http.Request) string
- func (r *RealIP) HeaderFromRequest(req *http.Request) string
- func (r *RealIP) SetHeaders(headers []string)
- func (r *RealIP) SetProxyCnt(cnt int)
- func (r *RealIP) SetTrustedPeers(peers []netip.Prefix)
- func (r *RealIP) SetTrustedProxies(proxies []netip.Prefix)
Constants ¶
const ( XRealIP = "X-Real-IP" XForwardedFor = "X-Forwarded-For" TrueClientIP = "True-Client-IP" )
Common HTTP request headers.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(r *RealIP)
Option is a function that configures RealIP settings.
func WithHeaders ¶
WithHeaders sets which HTTP headers to check for the real client IP. Common headers include X-Forwarded-For, X-Real-IP, and True-Client-IP. The headers are checked in order; the first matching header is used.
func WithProxyCnt ¶
WithProxyCnt sets the exact number of proxies in the chain. If you know there are exactly N proxies between the client and your server, use this to skip those N IPs from the X-Forwarded-For header and get the real client IP.
func WithTrustedPeers ¶
WithTrustedPeers sets the IP ranges that are allowed to set client IP headers. This is a security-critical setting. Only requests from trusted peers (proxies) will have their headers checked. Other requests will use the remote address.
func WithTrustedProxies ¶
WithTrustedProxies sets the IP ranges of internal proxies in a chain. When you have multiple proxies, this helps identify which IPs in the X-Forwarded-For header are proxies so they can be skipped to find the real client IP.
type RealIP ¶
type RealIP struct {
// contains filtered or unexported fields
}
RealIP holds settings for extracting the client IP from an HTTP request.
func (*RealIP) FromRequest ¶
FromRequest gets the client IP from the request.
func (*RealIP) HeaderFromRequest ¶ added in v0.0.2
HeaderFromRequest validates the request's proxy chain and returns the header value if all security checks pass. Unlike FromRequest, this returns the raw header value rather than extracting an IP address, making it suitable for passing the header downstream to other services.
Returns an empty string if: - No trusted peers are configured (secure default) - The immediate peer is not trusted - The configured headers are not present in the request
func (*RealIP) SetHeaders ¶
SetHeaders updates the headers to check for the real client IP. This allows changing configuration after RealIP has been created.
func (*RealIP) SetProxyCnt ¶
SetProxyCnt updates the exact number of proxies in the chain. This allows changing configuration after RealIP has been created.
func (*RealIP) SetTrustedPeers ¶
SetTrustedPeers updates the IP ranges that are allowed to set client IP headers. This allows changing configuration after RealIP has been created.
func (*RealIP) SetTrustedProxies ¶
SetTrustedProxies updates the IP ranges of internal proxies. This allows changing configuration after RealIP has been created.