httpip

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package httpip resolves the real client IP of an HTTP request behind explicitly trusted proxies, and provides net/http middleware.

Forwarding headers are honored only when the direct peer (RemoteAddr) is a trusted proxy. X-Forwarded-For is walked right to left, skipping trusted hops; the first untrusted hop is the client. Never trust the leftmost X-Forwarded-For entry blindly — any client can set it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context) (netip.Addr, bool)

FromContext returns the client IP stored by Middleware.

func WithClientIP added in v0.5.0

func WithClientIP(ctx context.Context, a netip.Addr) context.Context

WithClientIP returns ctx carrying a, readable with FromContext.

Types

type Resolver

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

Resolver extracts client IPs. Immutable after New; safe for concurrent use.

func New

func New(trustedProxies ...netip.Prefix) *Resolver

New returns a Resolver trusting the given proxy networks. With none, forwarding headers are ignored and RemoteAddr is used.

func (*Resolver) ClientIP

func (res *Resolver) ClientIP(r *http.Request) (netip.Addr, error)

ClientIP returns the normalized client address of r.

func (*Resolver) Middleware

func (res *Resolver) Middleware(next http.Handler) http.Handler

Middleware stores the client IP in the request context. Requests whose IP cannot be resolved pass through without one.

func (*Resolver) Resolve added in v0.5.0

func (res *Resolver) Resolve(remoteAddr string, xff []string, xRealIP string) (netip.Addr, error)

Resolve is the transport-agnostic core of ClientIP, for servers that do not use net/http (e.g. fasthttp/Fiber). remoteAddr is the direct peer ("ip:port" or bare IP); xff holds every X-Forwarded-For header value.

func (*Resolver) Restrict

func (res *Resolver) Restrict(acl *ipx.ACL, next http.Handler) http.Handler

Restrict answers 403 unless acl allows the client IP.

Jump to

Keyboard shortcuts

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