trustedip

package
v1.71.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: AGPL-3.0 Imports: 3 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetClientIP

func GetClientIP(l List, r *http.Request) string

GetClientIP gets the IP of the client from the 'Forwarded', 'X-Forwarded-For', or 'X-Real-Ip' headers if r.RemoteAddr is a trusted IP and returning it from the first header which are checked in that specific order. If the IP isn't trusted then it returns r.RemoteAddr. It panics if r is nil.

If an address contains the port, it's stripped. Addresses with ports are accepted in r.RemoteAddr and 'Forwarded' header.

NOTE: it doesn't check that the IP value get from wherever source is a well formatted IP v4 nor v6, because it's expected that it's. It also expect that request has been created from a trusted source (e.g. http.Server).

func GetIPFromHeaders

func GetIPFromHeaders(headers http.Header) (string, bool)

GetIPFromHeaders gets the IP of the client from the first exiting header in this order: 'Forwarded', 'X-Forwarded-For', or 'X-Real-Ip'. It returns the IP and true if the any of the headers exists, otherwise false.

The 'for' field of the 'Forwarded' may contain the IP with a port, as defined in the RFC 7239. When the header contains the IP with a port, the port is striped, so only the IP is returned.

NOTE: it doesn't check that the IP value get from wherever source is a well formatted IP v4 nor v6; an invalid formatted IP will return an undefined result.

Types

type List

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

List is a list of trusted IPs for conveniently verifying if an IP is trusted.

func NewList

func NewList(ips ...string) List

NewList creates a new List which trusts the passed ips.

NOTE: ips are not checked to be well formatted and their values are what they kept in the list.

func NewListTrustAll

func NewListTrustAll() List

NewListTrustAll creates a new List which trusts any IP.

func NewListUntrustAll

func NewListUntrustAll() List

NewListUntrustAll creates a new List which doesn't trust in any IP.

func (List) IsTrusted

func (l List) IsTrusted(ip string) bool

IsTrusted returns true if ip is trusted, otherwise false.

Jump to

Keyboard shortcuts

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