Skip to content

Commit

Permalink
Handle IPv6 on Linux (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlewis authored Jun 18, 2021
1 parent 9bde75a commit 3c15880
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module github.com/emitter-io/address

go 1.16

require github.com/stretchr/testify v1.7.0
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
24 changes: 20 additions & 4 deletions ipaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3c15880

Please sign in to comment.