From 075eeef37d3da850158eb3c15af33b6b76ab0d1c Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Mon, 10 Aug 2020 17:30:22 +0000 Subject: Fix request IP parsing --- util/ip.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/util/ip.go b/util/ip.go index f86d5b5..239973a 100644 --- a/util/ip.go +++ b/util/ip.go @@ -5,10 +5,14 @@ import ( "regexp" ) -// Parses an IPv4 or IPv6 address with an optional port on the end. Returns -// match groups for the addresses. The first match is the IPv6 address and the -// second the IPv4 address. -var ipRegexp = regexp.MustCompile(`(?:\[([0-9a-f:]+)\]|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(?::\d+)?`) +// Matches: +// - IPv4: 192.168.1.1 +// - IPv4 + Port: 192.168.1.1:2020 +// - IPv6 + Port: [fe80::551f:5aff:49cf:13e4]:2020 +// - IPv6: fe80::551f:5aff:49cf:13e4 +// +// Return Order: []string{matched, IPv4, IPv6 (port notation), IPv6 (raw notation), Port +var ipRegexp = regexp.MustCompile(`(?:((?:\d+\.){3}\d+)|\[([^\]]+)\]|([0-9a-f:]+))(?::(\d+))?`) func ParseIP(s string) net.IP { ips := ipRegexp.FindStringSubmatch(s) @@ -16,9 +20,13 @@ func ParseIP(s string) net.IP { return nil } - if v6, v4 := ips[1], ips[2]; v6 != "" { - return net.ParseIP(v6) + if ips[1] != "" { + return net.ParseIP(ips[1]) + } else if ips[2] != "" { + return net.ParseIP(ips[2]) + } else if ips[3] != "" { + return net.ParseIP(ips[3]) } else { - return net.ParseIP(v4) + return nil } } -- cgit v1.2.3