netutil

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package netutil derives the real client IP behind an optional set of trusted reverse proxies.

X-Forwarded-For is attacker-controlled: any client can send it. It may only be believed when the direct connection peer is itself a proxy we operate. ClientIP therefore honours XFF exclusively when the peer is in the configured trusted set, and otherwise returns the peer address. The same logic feeds both the rate-limit interceptor (per-IP buckets) and the plain-HTTP middleware, so a spoofed header can never dodge a limit.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TrustedProxies

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

TrustedProxies is the set of CIDR networks whose X-Forwarded-For headers are believed. The zero value (and a nil pointer) trusts no proxy, so ClientIP always returns the direct peer — the safe default for a directly-exposed server.

func ParseTrustedProxies

func ParseTrustedProxies(cidrs []string) (*TrustedProxies, error)

ParseTrustedProxies builds a TrustedProxies from CIDR strings (e.g. "10.0.0.0/8", "127.0.0.1/32", "::1/128"). A bare IP is accepted and treated as a single-host network. An empty slice yields a set that trusts nothing.

func (*TrustedProxies) ClientIP

func (t *TrustedProxies) ClientIP(remoteAddr, xff string) string

ClientIP returns the caller's IP for a request whose direct peer is remoteAddr ("host:port" or bare host) and whose X-Forwarded-For header is xff (possibly empty, possibly a comma-separated chain).

When the peer is not a trusted proxy, xff is ignored entirely and the peer host is returned. When the peer is trusted, the chain is walked from the right (nearest hop first) and the first address that is not itself a trusted proxy is returned — the real client just before it entered our proxy tier. If every hop is trusted, the left-most parseable entry wins; if none parses, the peer host is returned.

func (*TrustedProxies) Trusts

func (t *TrustedProxies) Trusts(ip string) bool

Trusts reports whether ip (an address string) is within the trusted set.

Jump to

Keyboard shortcuts

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