Help us shape the future of Go! Take the 2026 Go Developer Survey

clientip

package
v0.6.7 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package clientip answers "where did this request come from".

It is one short function, in a package of its own, because getting it wrong is silent. A server that believes X-Forwarded-For unconditionally lets any caller name their own address, and every limit keyed on one becomes decorative — the requests still arrive, the counter still counts, and it counts a different fictional client each time. A server that never believes it sees only the load balancer, so one address carries everybody and the limit is either off or on for the whole internet.

The answer is neither: believe the header exactly when the immediate peer is a proxy the application named. That has to be the same answer everywhere it is asked, which is why this is not a method on whichever handler needed it first.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Of

func Of(r *http.Request, trusted []netip.Prefix) netip.Addr

Of returns the address the request came from.

trusted are the networks whose X-Forwarded-For may be believed. Empty trusts none of them, which is the right default and the wrong setting behind a load balancer — see Parse for the one that is not a choice.

The zero netip.Addr is a peer that could not be parsed at all, which happens on a Unix socket and in tests. Callers should treat it as "no address" rather than as one, since every request with no address would otherwise share a budget.

func Parse

func Parse(remote string) netip.Addr

Parse reads an address out of a host:port, or out of a bare host.

IPv4-in-IPv6 is unmapped, so that ::ffff:198.51.100.7 and 198.51.100.7 are one address rather than two budgets.

func String

func String(r *http.Request, trusted []netip.Prefix) string

String is Of rendered for a log or a rate-limit key, and "" for no address.

Types

This section is empty.

Jump to

Keyboard shortcuts

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