clientip

package
v0.3.0-rc.53 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package clientip extracts the client's real IP from a request.

X-Forwarded-For is trusted only when it comes from a configured proxy, and the first untrusted address is taken scanning from right to left: the rightmost entry was added by the proxy nearest to us, while the leftmost can be anything the client chooses to write (for example private addresses added by a corporate network proxy). This matches Caddy's trusted_proxies_strict.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Proxied

func Proxied(req *http.Request) bool

Proxied reports whether the request came through a proxy: it carries any X-Forwarded-* header or one of the headers above, or it is HTTP/1.0 (nginx's default proxy_pass uses HTTP/1.0 for upstream fetches and does not add X-Forwarded-For). The client may also write these headers itself, so they are only ever used to refuse, never to allow.

func Remote

func Remote(req *http.Request) netip.Addr

Remote parses RemoteAddr (the TCP source address).

Types

type Resolver

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

Resolver resolves client IP addresses and supports atomic updates to its trusted proxy prefixes.

func New

func New(trusted []netip.Prefix, useXRealIP bool) *Resolver

New creates a Resolver. When trusted is empty no proxy is trusted and the TCP source address is used directly.

func (*Resolver) ClientIP

func (r *Resolver) ClientIP(req *http.Request) (client, remote netip.Addr)

ClientIP returns the client IP and the TCP source address.

func (*Resolver) Forwarded

func (r *Resolver) Forwarded(req *http.Request) (client netip.Addr, ok bool)

Forwarded returns the client address passed on by a trusted proxy: the TCP peer is a trusted proxy and the address was actually taken from X-Real-IP (with use_x_real_ip) or X-Forwarded-For. Otherwise ok is false.

func (*Resolver) SetTrusted

func (r *Resolver) SetTrusted(trusted []netip.Prefix)

SetTrusted atomically replaces the trusted proxy ranges.

func (*Resolver) Trusted

func (r *Resolver) Trusted(a netip.Addr) bool

Trusted reports whether a is a trusted proxy.

Jump to

Keyboard shortcuts

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