Skip to content

Commit

Permalink
netutil: consistently format ipv6 addresses
Browse files Browse the repository at this point in the history
This formats ipv6 addresses to ensure they can be compared safely

Signed-off-by: kidsan <8798449+Kidsan@users.noreply.github.com>
  • Loading branch information
Kidsan committed Jan 20, 2023
1 parent ee566c4 commit ce24ec3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
22 changes: 21 additions & 1 deletion pkg/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/url"
"reflect"
"sort"
"strconv"
"time"

"go.etcd.io/etcd/client/pkg/v3/types"
Expand Down Expand Up @@ -103,7 +104,26 @@ func resolveURL(ctx context.Context, lg *zap.Logger, u url.URL) (string, error)
)
return "", err
}
if host == "localhost" || net.ParseIP(host) != nil {
if net.ParseIP(host) != nil {
p, err := strconv.Atoi(u.Port())
if err != nil {
lg.Warn(
"failed to parse URL Port while resolving URL",
zap.String("url", u.String()),
zap.String("host", u.Host),
zap.String("port", u.Port()),
zap.Error(err),
)
return "", err

}
addr := &net.TCPAddr{
IP: net.ParseIP(host),
Port: p,
}
return addr.String(), nil
}
if host == "localhost" {
return "", nil
}
for ctx.Err() == nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/netutil/netutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ func TestURLStringsEqual(t *testing.T) {
"http://host1:8080",
"http://host2:8080",
}, errOnResolve},
{
urlsA: []string{"https://[2a0c:eb00:0:f7:0e5f:7a4b:72e8:fa3a]:2380"},
urlsB: []string{"https://[2a0c:eb00:0:f7:e5f:7a4b:72e8:fa3a]:2380"},
resolver: resolveTCPAddrDefault,
},
}
for idx, c := range cases {
t.Logf("TestURLStringsEqual, case #%d", idx)
Expand Down

0 comments on commit ce24ec3

Please sign in to comment.