Documentation
¶
Overview ¶
Package clientip resolves the originating client IP for requests relayed by trusted server-side forwarders, which assert the visitor IP alongside a shared secret
Index ¶
Constants ¶
const ( // ForwardedClientIPHeader carries the originating visitor IP asserted by a trusted forwarding proxy ForwardedClientIPHeader = "X-Openlane-Client-IP" // ForwarderSecretHeader carries the shared secret a trusted forwarding proxy presents ForwarderSecretHeader = "X-Openlane-Forwarder-Secret" //nolint:gosec // header name, not a credential // HeaderCFConnectingIP is the visitor IP header Cloudflare sets on every proxied request HeaderCFConnectingIP = "CF-Connecting-IP" // HeaderTrueClientIP is the visitor IP header Cloudflare sets when True-Client-IP is enabled (Enterprise) HeaderTrueClientIP = "True-Client-IP" // SourceRemoteAddr names the socket peer as an entry in a header precedence list SourceRemoteAddr = "RemoteAddr" )
Variables ¶
var DefaultHeaders = []string{HeaderCFConnectingIP, HeaderTrueClientIP, SourceRemoteAddr}
DefaultHeaders is the header precedence used to derive the originating client IP prefers CF-Connecting-IP, then True-Client-IP, falls back to the RemoteAddr
Functions ¶
func Extractor ¶
func Extractor(opts Options) echo.IPExtractor
Extractor returns an echo IPExtractor backed by Resolve, so c.RealIP() (request logs, audit records) resolves the client IP the same way as the rate limiter
func Resolve ¶
Resolve returns the originating client IP: the trusted forwarder's asserted IP when its secret matches, otherwise the first non-empty source in the header precedence list. It returns an empty string when no source yields a value
func TrustedForwardedIP ¶
TrustedForwardedIP returns the client IP asserted in ForwardedClientIPHeader when the request has the forwarder secret in ForwarderSecretHeader it returns an empty string when no secret is configured, the secret does not match, or the asserted value is not a valid IP
Types ¶
type Options ¶
type Options struct {
// ForwarderSecret is the shared secret trusted forwarders present; empty disables ForwardedClientIPHeader
ForwarderSecret string
// Headers is the precedence list consulted after the trusted forwarder header; empty uses DefaultHeaders
Headers []string
// ForwardedIndexFromBehind selects which X-Forwarded-For entry is used, counted from the last one
ForwardedIndexFromBehind int
}
Options controls how the client IP is resolved