clientip

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package clientip resolves the client address of a request from the connection peer and, only when that peer is a trusted proxy, from the forwarding headers it set. Every rate limit and session record keys on the value this package produces, so a client can never choose its own address by sending a header.

Index

Constants

View Source
const KeywordNone = "none"

KeywordNone trusts no proxy at all: the peer address is always the client.

View Source
const KeywordPrivate = "private"

KeywordPrivate expands to the loopback, RFC 1918, link-local, and unique-local ranges, which covers a reverse proxy on the same host or on a container network. It is the default because that is how most instances are deployed, and a default of "none" would put every user behind such a proxy into one shared bucket. The trade is that any peer on the private network is believed when it forwards an address; an operator whose instance is reached directly through a NAT hop that presents a private peer address without rewriting headers should set "none" or list the real proxy explicitly.

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context) string

FromContext returns the client address stored by the middleware, or "" when the request did not pass through it.

func Middleware

func Middleware(resolver *Resolver) echo.MiddlewareFunc

Middleware resolves the client address once per request and stores it in the request context, where both API transports and the file server read it.

func WithClientIP

func WithClientIP(ctx context.Context, ip string) context.Context

WithClientIP stores the resolved client address in ctx.

Types

type Resolver

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

Resolver decides which address identifies the client of a request.

func ParseTrustedProxies

func ParseTrustedProxies(values []string) (*Resolver, error)

ParseTrustedProxies turns the configured list into a resolver. Each entry is a CIDR, a single address, or one of the keywords. An empty list means "private". Entries may themselves be comma-separated.

func (*Resolver) Resolve

func (r *Resolver) Resolve(remoteAddr string, header http.Header) string

Resolve returns the client address for a request with the given peer address (host:port or bare host) and headers. It always returns a usable string: an unparseable peer is returned as-is so that the limiter still has a key.

func (*Resolver) Trusts

func (r *Resolver) Trusts(addr netip.Addr) bool

Trusts reports whether addr is a configured proxy.

Jump to

Keyboard shortcuts

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