blob: f86d5b51da45593fca75c6f18db2509401a8134c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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)
}
}
|