summaryrefslogtreecommitdiff
path: root/util/ip.go
diff options
context:
space:
mode:
Diffstat (limited to 'util/ip.go')
-rw-r--r--util/ip.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/util/ip.go b/util/ip.go
new file mode 100644
index 0000000..f86d5b5
--- /dev/null
+++ b/util/ip.go
@@ -0,0 +1,24 @@
1package util
2
3import (
4 "net"
5 "regexp"
6)
7
8// Parses an IPv4 or IPv6 address with an optional port on the end. Returns
9// match groups for the addresses. The first match is the IPv6 address and the
10// second the IPv4 address.
11var ipRegexp = regexp.MustCompile(`(?:\[([0-9a-f:]+)\]|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(?::\d+)?`)
12
13func ParseIP(s string) net.IP {
14 ips := ipRegexp.FindStringSubmatch(s)
15 if ips == nil {
16 return nil
17 }
18
19 if v6, v4 := ips[1], ips[2]; v6 != "" {
20 return net.ParseIP(v6)
21 } else {
22 return net.ParseIP(v4)
23 }
24}