clientip

package
v0.87.2 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package clientip reads the client address out of X-Forwarded-For the one way that survives an appending proxy: from the right.

A proxy that appends (nginx $proxy_add_x_forwarded_for, httputil.ReverseProxy, most CDNs) leaves whatever the client sent on the left and writes the address it observed on the right. The leftmost entry is therefore client-supplied, and keying a rate limit or an access log on it lets one client rotate it per request or name a third party. Walking from the right and skipping the operator's own proxy tiers lands on the first hop no trusted proxy vouches past: the address the outermost trusted proxy saw.

core/middleware.RateLimit, framework/ratelimit.ClientIP and battery/log share this walk so the rule has one implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Forwarded

func Forwarded(h http.Header, trusted Proxies) (net.IP, bool)

Forwarded walks every X-Forwarded-For line in h as one comma-separated list, from the right, skipping hops in trusted, and returns the first hop that is not trusted. It reports false when the header is absent, when every hop is trusted, or when the walk meets an entry that is not an IP before it finds an untrusted one: a trusted proxy never writes junk, so junk there means the list cannot be read and the caller falls back to the TCP peer.

The caller decides whether the immediate peer may speak for the header at all; Forwarded only reads it.

func ParseHop

func ParseHop(s string) net.IP

ParseHop parses one address as an IP, accepting a trailing port ("203.0.113.7:4711", "[2001:db8::1]:443") as some proxies append it. It returns nil for anything else.

Types

type Proxies

type Proxies struct {
	// contains filtered or unexported fields
}

Proxies is a parsed trusted-proxy list. Bare IPs match exactly; CIDR entries match by containment. The zero value trusts nothing.

func ParseProxies

func ParseProxies(entries []string) Proxies

ParseProxies parses entries as IPs or CIDRs. Entries that are neither are dropped, so a typo trusts nothing rather than everything.

func (Proxies) Contains

func (p Proxies) Contains(ip net.IP) bool

Contains reports whether ip is one of the trusted proxies.

func (Proxies) ContainsAddr

func (p Proxies) ContainsAddr(addr string) bool

ContainsAddr is Contains for a textual address, with or without a port.

func (Proxies) Empty

func (p Proxies) Empty() bool

Empty reports whether no proxy is trusted.

Jump to

Keyboard shortcuts

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