From b65680a2d5f624ed528d36f8406f31535555828a Mon Sep 17 00:00:00 2001 From: Didip Kerabat Date: Sat, 18 Nov 2017 14:44:42 -0800 Subject: [PATCH] =?UTF-8?q?Apparently,=20it=E2=80=99s=20possible=20to=20re?= =?UTF-8?q?ceive=20ipv6=20without=20port.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libstring/libstring.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libstring/libstring.go b/libstring/libstring.go index 0105372..fef29af 100644 --- a/libstring/libstring.go +++ b/libstring/libstring.go @@ -24,7 +24,12 @@ func RemoteIP(ipLookups []string, forwardedForIndexFromBehind int, r *http.Reque for _, lookup := range ipLookups { if lookup == "RemoteAddr" { - ip, _, _ := net.SplitHostPort(r.RemoteAddr) + // 1. Cover the basic use cases for both ipv4 and ipv6 + ip, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + // 2. Upon error, just return the remote addr. + return r.RemoteAddr + } return ip } if lookup == "X-Forwarded-For" && forwardedFor != "" {