github.com/TykTechnologies/tyk/request
No package summary is available.
Package
Files: 1. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.
Functions
func RealIP
RealIP takes a request object, and returns the real Client IP address.
func RealIP(r *http.Request) string {
if contextIp := r.Context().Value("remote_addr"); contextIp != nil {
return contextIp.(string)
}
if realIPVal := r.Header.Get(header.XRealIP); realIPVal != "" {
if realIP := net.ParseIP(realIPVal); realIP != nil {
return realIP.String()
}
}
if fw := r.Header.Get(header.XForwardFor); fw != "" {
// X-Forwarded-For has no port
if i := strings.IndexByte(fw, ','); i >= 0 {
fw = fw[:i]
}
if fwIP := net.ParseIP(fw); fwIP != nil {
return fwIP.String()
}
}
// From net/http.Request.RemoteAddr:
// The HTTP server in this package sets RemoteAddr to an
// "IP:port" address before invoking a handler.
// So we can ignore the case of the port missing.
host, _, _ := net.SplitHostPort(r.RemoteAddr)
return host
}
Cognitive complexity: 12, Cyclomatic complexity: 7
Tests
Files: 1. Third party imports: 0. Imports from organisation: 0. Tests: 1. Benchmarks: 4.
Vars
var ipHeaderTests = []struct {
remoteAddr string
key string
value string
expected string
comment string
}{
{remoteAddr: "10.0.1.4:8080", key: "X-Real-IP", value: "10.0.0.1", expected: "10.0.0.1", comment: "X-Real-IP"},
{remoteAddr: "10.0.1.4:8080", key: "X-Forwarded-For", value: "10.0.0.2", expected: "10.0.0.2", comment: "X-Forwarded-For (single)"},
{remoteAddr: "10.0.1.4:8080", key: "X-Forwarded-For", value: "10.0.0.3, 10.0.0.2, 10.0.0.1", expected: "10.0.0.3", comment: "X-Forwarded-For (multiple)"},
{remoteAddr: "10.0.1.4:8080", expected: "10.0.1.4", comment: "RemoteAddr"},
{remoteAddr: "10.0.1.4:8080", key: "X-Forwarded-For", value: "bob", expected: "10.0.1.4", comment: "invalid X-Forwarded-For"},
{remoteAddr: "10.0.1.4:8080", key: "X-Real-IP", value: "bob", expected: "10.0.1.4", comment: "invalid X-Real-IP"},
}