Documentation
¶
Overview ¶
Package clientip resolves the address associated with an HTTP request. Direct uses only the connection peer; Proxies and Cloudflare accept headers only from explicitly trusted peers. These resolvers return addresses, not rate-limit buckets, and do not authenticate a proxy's tenant or zone.
For example, use clientip.Proxies(netip.MustParsePrefix("10.0.0.0/8")) when an isolated reverse proxy in that network appends X-Forwarded-For:
request, _ := http.NewRequest(http.MethodGet, "/", nil)
request.RemoteAddr = "10.0.0.1:443"
request.Header.Set("X-Forwarded-For", "192.0.2.1")
address := clientip.Proxies(netip.MustParsePrefix("10.0.0.0/8"))(request)
fmt.Println(address) // 192.0.2.1
Trust only networks whose members cannot send forged forwarding headers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cloudflare ¶
Cloudflare returns a resolver that accepts exactly one bare IP in CF-Connecting-IP only when the connection peer belongs to the embedded Cloudflare ranges. Otherwise it returns the direct peer, normalizing parsed addresses by unmapping IPv4 and removing IPv6 zones. It never falls back to another header. The snapshot can become stale and requires releases to update; an intervening proxy is not covered.
Cloudflare's shared ranges do not authenticate your zone: for direct connections, protect the origin with zone-specific authenticated origin pulls. Cloudflare Tunnel needs a custom resolver that trusts its connector; this resolver does not identify Tunnel traffic. Workers and Pseudo IPv4 settings can change the header's meaning.
Example:
request, _ := http.NewRequest(http.MethodGet, "/", nil)
request.RemoteAddr = "173.245.48.1:443"
request.Header.Set("CF-Connecting-IP", "192.0.2.1")
fmt.Println(clientip.Cloudflare()(request)) // 192.0.2.1
Example ¶
ExampleCloudflare resolves the client only for a Cloudflare connection peer.
package main
import (
"fmt"
"net/http/httptest"
"arkoren.dev/core/http/clientip"
)
func main() {
request := httptest.NewRequest("GET", "/", nil)
request.RemoteAddr = "173.245.48.1:443"
request.Header.Set("CF-Connecting-IP", "192.0.2.1")
fmt.Println(clientip.Cloudflare()(request))
}
Output: 192.0.2.1
func Direct ¶
Direct returns a resolver that extracts the host from RemoteAddr, or returns RemoteAddr unchanged when it is not a host:port pair. It ignores all headers and preserves the host's spelling and IPv6 zone, matching auth's default.
Example:
request, _ := http.NewRequest(http.MethodGet, "/", nil) request.RemoteAddr = "192.0.2.1:443" fmt.Println(clientip.Direct()(request)) // 192.0.2.1
Example ¶
ExampleDirect shows that the connection host, not a forwarding header, wins.
package main
import (
"fmt"
"net/http/httptest"
"arkoren.dev/core/http/clientip"
)
func main() {
request := httptest.NewRequest("GET", "/", nil)
request.RemoteAddr = "192.0.2.1:443"
fmt.Println(clientip.Direct()(request))
}
Output: 192.0.2.1
func Proxies ¶
Proxies returns a resolver for proxies that append X-Forwarded-For. It copies trusted and ignores invalid prefixes; no addresses are trusted implicitly. After checking the connection peer, it walks all header lines right to left, stopping at the first untrusted address or at the nearest trusted hop when an entry is malformed. IP literals only are accepted, not IP:port entries. Parsed addresses are unmapped and IPv6 zones removed; an unparseable peer is returned as Direct would return it. IPv6 addresses are not bucketed. Prefixes are not unmapped: an IPv4-mapped IPv6 prefix cannot match an unmapped IPv4 address, so configure the corresponding IPv4 prefix instead.
Trust only isolated proxies that append or replace the header. In particular, trusting a network accessible to arbitrary clients permits spoofing.
Example:
request, _ := http.NewRequest(http.MethodGet, "/", nil)
request.RemoteAddr = "10.0.0.1:443"
request.Header.Set("X-Forwarded-For", "192.0.2.1")
resolve := clientip.Proxies(netip.MustParsePrefix("10.0.0.0/8"))
fmt.Println(resolve(request)) // 192.0.2.1
Example ¶
ExampleProxies resolves a client through an explicitly trusted proxy.
package main
import (
"fmt"
"net/http/httptest"
"net/netip"
"arkoren.dev/core/http/clientip"
)
func main() {
request := httptest.NewRequest("GET", "/", nil)
request.RemoteAddr = "10.0.0.1:443"
request.Header.Set("X-Forwarded-For", "192.0.2.1")
resolve := clientip.Proxies(netip.MustParsePrefix("10.0.0.0/8"))
fmt.Println(resolve(request))
}
Output: 192.0.2.1
Types ¶
This section is empty.