diff --git a/dns.go b/dns.go index 6cd3a2f..52981b6 100644 --- a/dns.go +++ b/dns.go @@ -38,7 +38,7 @@ func Resolve(name string, defaultPort int) ([]net.TCPAddr, error) { } // The hostname might not be there (if it's an address) - if url.Hostname() != "" { + if url.Hostname() != "" && url.Hostname() != "::" { name = url.Hostname() } diff --git a/go.mod b/go.mod index a3ad88f..b425fc8 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,5 @@ module github.com/emitter-io/address + +go 1.16 + +require github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..acb88a4 --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/ipaddr.go b/ipaddr.go index 0e6dc8c..157f50e 100644 --- a/ipaddr.go +++ b/ipaddr.go @@ -30,24 +30,40 @@ func Parse(addr string, defaultPort int) (*net.TCPAddr, error) { // Convenience: set private address if strings.Contains(addr, "private") { private := GetPrivateOrDefault(Loopback) - addr = strings.Replace(addr, "private", private.String(), 1) + if private.IP.To4() == nil && strings.Contains(addr, ":") { + addr = strings.Replace(addr, "private", fmt.Sprintf("[%v]", private.String()), 1) + } else { + addr = strings.Replace(addr, "private", private.String(), 1) + } } // Convenience: set public address if strings.Contains(addr, "external") { external := GetExternalOrDefault(Loopback) - addr = strings.Replace(addr, "external", external.String(), 1) + if external.IP.To4() == nil && strings.Contains(addr, ":") { + addr = strings.Replace(addr, "external", fmt.Sprintf("[%v]", external.String()), 1) + } else { + addr = strings.Replace(addr, "external", external.String(), 1) + } } // Convenience: set public address if strings.Contains(addr, "public") { public := GetPublicOrDefault(Loopback) - addr = strings.Replace(addr, "public", public.String(), 1) + if public.IP.To4() == nil && strings.Contains(addr, ":") { + addr = strings.Replace(addr, "public", fmt.Sprintf("[%v]", public.String()), 1) + } else { + addr = strings.Replace(addr, "public", public.String(), 1) + } } // If we have only an IP address, use the default port if ip := net.ParseIP(addr); ip != nil { - addr = fmt.Sprintf("%s:%d", ip, defaultPort) + if ip != nil && ip.To4() == nil { + addr = fmt.Sprintf("[%s]:%d", ip, defaultPort) + } else { + addr = fmt.Sprintf("%s:%d", ip, defaultPort) + } } // Resolve the address