From e23f5fbcf6ed5031d6aa4bfb95df799351bde51b Mon Sep 17 00:00:00 2001 From: Asaf Gartner Date: Sat, 28 Aug 2021 15:52:40 +0300 Subject: [PATCH] Fixed ip handling --- src/website/requesthandling.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/website/requesthandling.go b/src/website/requesthandling.go index a1cefe6c..b12de9d7 100644 --- a/src/website/requesthandling.go +++ b/src/website/requesthandling.go @@ -149,6 +149,9 @@ func (c *RequestContext) FullUrl() string { return scheme + c.Req.Host + c.Req.URL.String() } +// NOTE(asaf): Assumes port is present (it should be for RemoteAddr according to the docs) +var ipRegex = regexp.MustCompile(`^(\[(?P[^\]]+)\]:\d+)|((?P[^:]+):\d+)$`) + func (c *RequestContext) GetIP() *net.IPNet { ipString := "" @@ -168,6 +171,18 @@ func (c *RequestContext) GetIP() *net.IPNet { if ipString == "" { ipString = c.Req.RemoteAddr + if ipString != "" { + matches := ipRegex.FindStringSubmatch(ipString) + if matches != nil { + v4 := matches[ipRegex.SubexpIndex("addrv4")] + v6 := matches[ipRegex.SubexpIndex("addrv6")] + if v4 != "" { + ipString = v4 + } else { + ipString = v6 + } + } + } } if ipString != "" {