clientip

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: May 15, 2025 License: LGPL-2.1 Imports: 3 Imported by: 0

Documentation

Overview

clientip remote-client-ip determination which handles complex issues around the X-Forwarded-For header, as described here: https://adam-p.ca/blog/2022/03/x-forwarded-for/

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlattenDelimitedInputs added in v0.3.0

func FlattenDelimitedInputs(input []string, sep string) []string

FlattenDelimitedInputs processes a slice of multiple delimited-value strings by splitting them on the delimiter, trimming whitespace, removing empty strings, and removing duplicates while preserving the original order. An empty separator results in a split between every utf-8 character. The result is a single slice of strings. For example, given: ["1.1.1.1, "2.2.2.2, 3.3.3.3, 4.4.4.4", "4.4.4.4, 5.5.5.5"] return: ["1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4", "5.5.5.5"]

func GetClientIP

func GetClientIP(req *http.Request, trustXFF bool, trustedProxies *TrustedProxies, trustedHeader string) string

GetClientIP determines the real client IP address from an HTTP request, following best practices for X-Forwarded-For handling.

  • If trustedHeader is true, we use it
  • If trustXFF is false, use the direct remote address.
  • If trustXFF is true and trustedProxies is non-empty, walk through the X-Forwarded-For header from right to left, skipping over any IPs in trustedProxies, and return the first untrusted IP.
  • If trustXFF is true and trustedProxies is nil or empty, return the first IP in X-Forwarded-For (from the left).
  • If no suitable IP is found, fall back to RemoteAddr.
  • The remote address's host part is extracted with net.SplitHostPort if possible.

Types

type TrustedProxies

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

TrustedProxies holds a list of trusted proxy networks.

func NewTrustedProxies

func NewTrustedProxies(cidrList string) *TrustedProxies

NewTrustedProxies parses a comma-separated list of CIDR blocks and returns a TrustedProxies struct.

func (*TrustedProxies) IsTrusted

func (tp *TrustedProxies) IsTrusted(ip string) bool

IsTrusted returns true if ip is in any of the trusted proxy networks.

func (*TrustedProxies) String added in v0.3.1

func (tp *TrustedProxies) String() string

Jump to

Keyboard shortcuts

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