diff options
Diffstat (limited to 'http/request/client_ip.go')
-rw-r--r-- | http/request/client_ip.go | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/http/request/client_ip.go b/http/request/client_ip.go index 22086081..83d3a577 100644 --- a/http/request/client_ip.go +++ b/http/request/client_ip.go @@ -10,15 +10,7 @@ import ( "strings" ) -func dropIPv6zone(address string) string { - i := strings.IndexByte(address, '%') - if i != -1 { - address = address[:i] - } - return address -} - -// FindClientIP returns client real IP address. +// FindClientIP returns the client real IP address based on trusted Reverse-Proxy HTTP headers. func FindClientIP(r *http.Request) string { headers := []string{"X-Forwarded-For", "X-Real-Ip"} for _, header := range headers { @@ -36,6 +28,11 @@ func FindClientIP(r *http.Request) string { } // Fallback to TCP/IP source IP address. + return FindRemoteIP(r) +} + +// FindRemoteIP returns remote client IP address. +func FindRemoteIP(r *http.Request) string { remoteIP, _, err := net.SplitHostPort(r.RemoteAddr) if err != nil { remoteIP = r.RemoteAddr @@ -49,3 +46,11 @@ func FindClientIP(r *http.Request) string { return remoteIP } + +func dropIPv6zone(address string) string { + i := strings.IndexByte(address, '%') + if i != -1 { + address = address[:i] + } + return address +} |