Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetClientIP ¶
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 ¶
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 ¶
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.