clientip

package
v0.4.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 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

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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 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 X-Forwarded-For chain is walked from right to left, skipping trusted proxies; the first untrusted address is the client. If every hop is trusted, the leftmost valid address is returned. A malformed entry stops the walk, and the last valid address seen is returned.

The Forwarded header (RFC 7239) is not consulted yet.

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