clientip

package
v0.6.1-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 4 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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 New added in v0.5.0

func New(o Options) (*Resolver, error)

New returns a Resolver configured by o.

func NewResolver

func NewResolver(trustedProxies []string) (*Resolver, error)

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

func (r *Resolver) Resolve(peer netip.Addr, h http.Header) netip.Addr

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.

func (*Resolver) Trusts

func (r *Resolver) Trusts(addr netip.Addr) bool

Trusts reports whether addr is a trusted proxy, whose forwarding headers may be relied on and passed on.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL