clientaddr

package
v0.274.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 9 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 clientaddr is the ONE reading of "which address is this request from". Every per-address limit, every address forwarded to GoTrue (X-Muster-Client-IP), every audit row and log line that names a client takes it from here (Of), in muster-server and in muster-preview alike.

🔴 THE DEFAULT TRUSTS NOTHING. The answer is the TCP peer of the connection, whatever the request's headers say, on every listener. A forwarding header (CF-Connecting-IP, X-Forwarded-For, X-Real-IP) is something any client can type, and a plain reverse proxy passes most of them through unchanged, so a header is believed ONLY when the TCP peer is inside a configured trusted range (MUSTER_TRUSTED_PROXIES) — and then only the one header the operator named (MUSTER_CLIENT_IP_HEADER).

🔴 X-Forwarded-For IS READ FROM THE RIGHT. Each proxy APPENDS the address it received the request from, so the right-most entries are the ones written by proxies, and the left-most is whatever the client sent. The client is the first entry, walking from the right, that is NOT a trusted proxy. The left-most entry is used only when every entry is trusted.

⚠ THAT IS SAFE ONLY BEHIND A PROXY THAT APPENDS. A proxy that forwards the client's X-Forwarded-For untouched (nginx with a bare proxy_pass) leaves a client-typed value right-most, and no reading of the header can undo that: such a proxy must name the client in a header it OVERWRITES (X-Real-IP) or append it ($proxy_add_x_forwarded_for). docs/configuration.md has the recipes.

The package is a leaf: the standard library only.

Index

Constants

View Source
const (
	HeaderCFConnectingIP = "CF-Connecting-IP"
	HeaderXForwardedFor  = "X-Forwarded-For"
	HeaderXRealIP        = "X-Real-IP"
)

The headers MUSTER_CLIENT_IP_HEADER may name, in their canonical spelling.

View Source
const Unknown = "unknown"

Unknown is the address of a request whose TCP peer does not parse (never a real network connection; a test's hand-made RemoteAddr).

Variables

This section is empty.

Functions

func Of

func Of(r *http.Request) string

Of is r's client address: the one Middleware resolved, or — for a request that did not pass through it — the TCP peer (the strict default; never a header).

Types

type Config

type Config struct {
	// TrustedProxies is the comma-separated CIDR list (a bare address is a
	// single host). Empty trusts nothing.
	TrustedProxies string
	// Header is the header a trusted peer names the client in: one of the
	// Header* constants, case-insensitive. Empty with TrustedProxies empty.
	Header string

	TrustedVar, HeaderVar string
}

Config is what New reads. The two *Var names are only for messages, so the same code serves muster-server (MUSTER_*) and muster-preview (PREVIEW_*).

type Policy

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

Policy decides a request's client address. A nil *Policy is the strict default: the TCP peer, always.

func New

func New(c Config) (*Policy, error)

New parses c. Both settings or neither: a header with no trusted range would never be read, and a trusted range with no header would believe nothing, and either is a configuration that silently does not do what it says.

func (*Policy) Describe

func (p *Policy) Describe() string

Describe is the boot log's line for the active mode.

func (*Policy) Header

func (p *Policy) Header() string

Header is the header a trusted peer is believed in; "" when none is.

func (*Policy) Middleware

func (p *Policy) Middleware(next http.Handler) http.Handler

Middleware resolves each request's client address once, with p, and carries it on the request for Of.

func (*Policy) Resolve

func (p *Policy) Resolve(r *http.Request) string

Resolve is the client address of r: the TCP peer, unless the peer is a trusted proxy, in which case the configured header's client (validated; the peer when the header is absent or does not parse). Always a canonical IP string, or Unknown.

func (*Policy) Trusted

func (p *Policy) Trusted() []string

Trusted is the trusted ranges, as written back out.

func (*Policy) WithLog

func (p *Policy) WithLog(logf func(format string, args ...any)) *Policy

WithLog returns a copy of p that logs, through logf, forwarding headers arriving from an untrusted peer and a trusted peer sending no usable header, each at most once a minute. A nil p gives the strict default with logging.

Jump to

Keyboard shortcuts

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