clientip

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package clientip resolves the real client IP of an HTTP request. X-Forwarded-For is honored ONLY when the direct peer is a configured trusted proxy, so a client cannot spoof XFF to forge its address for the login guard or the rate limiter.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Key

func Key(ip string) string

Key normalizes an IP to a rate-limit / lockout key: an IPv4 address maps to itself (/32), an IPv6 address maps to its /64 network — so an attacker cannot rotate addresses within a single (typically /64) allocation to mint unbounded distinct keys and evade per-IP limits or the auto-block.

func ParseCIDRs

func ParseCIDRs(s string) []*net.IPNet

ParseCIDRs parses a comma-separated CIDR list, skipping blank/invalid entries.

Types

type Resolver

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

Resolver derives a request's client IP against a set of trusted-proxy CIDRs.

func NewResolver

func NewResolver(cidrs string) *Resolver

NewResolver builds a Resolver from a comma-separated CIDR list (env KEN_TRUSTED_PROXIES). An empty/blank list means "no proxy" — RemoteAddr is always used and XFF is ignored.

func (*Resolver) IP

func (r *Resolver) IP(req *http.Request) string

IP returns the request's client IP. When the direct peer is a trusted proxy the rightmost X-Forwarded-For hop that is a valid IP and not itself a trusted proxy is used (across all XFF header lines); otherwise RemoteAddr's host is returned and XFF is ignored.

func (*Resolver) TrustedPeer

func (r *Resolver) TrustedPeer(req *http.Request) bool

TrustedPeer reports whether the request's direct peer (RemoteAddr) is one of the configured trusted proxies — i.e. whether this request's X-Forwarded-* headers may be believed. With no trusted proxies configured it is always false.

Jump to

Keyboard shortcuts

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