package util import ( "net" "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+)?`) func ParseIP(s string) net.IP { ips := ipRegexp.FindStringSubmatch(s) if ips == nil { return nil } if v6, v4 := ips[1], ips[2]; v6 != "" { return net.ParseIP(v6) } else { return net.ParseIP(v4) } }