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 authored and ahrtr committed Jan 26, 2023
1 parent d2fc8db commit c5347cb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func resolveURL(ctx context.Context, lg *zap.Logger, u url.URL) (string, error)
)
return "", err
}
if host == "localhost" || net.ParseIP(host) != nil {
if host == "localhost" {
return "", nil
}
for ctx.Err() == nil {
Expand Down
29 changes: 20 additions & 9 deletions pkg/netutil/netutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,16 @@ func TestResolveTCPAddrs(t *testing.T) {
if err != nil {
return nil, err
}
if tt.hostMap[host] == "" {
return nil, errors.New("cannot resolve host")
}
i, err := strconv.Atoi(port)
if err != nil {
return nil, err
}
if ip := net.ParseIP(host); ip != nil {
return &net.TCPAddr{IP: ip, Port: i, Zone: ""}, nil
}
if tt.hostMap[host] == "" {
return nil, errors.New("cannot resolve host")
}
return &net.TCPAddr{IP: net.ParseIP(tt.hostMap[host]), Port: i, Zone: ""}, nil
}
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
Expand All @@ -152,17 +155,20 @@ func TestURLsEqual(t *testing.T) {
"second.com": "10.0.11.2",
}
resolveTCPAddr = func(ctx context.Context, addr string) (*net.TCPAddr, error) {
host, port, herr := net.SplitHostPort(addr)
if herr != nil {
return nil, herr
}
if _, ok := hostm[host]; !ok {
return nil, errors.New("cannot resolve host.")
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
i, err := strconv.Atoi(port)
if err != nil {
return nil, err
}
if ip := net.ParseIP(host); ip != nil {
return &net.TCPAddr{IP: ip, Port: i, Zone: ""}, nil
}
if hostm[host] == "" {
return nil, errors.New("cannot resolve host")
}
return &net.TCPAddr{IP: net.ParseIP(hostm[host]), Port: i, Zone: ""}, nil
}

Expand Down Expand Up @@ -330,6 +336,11 @@ func TestURLStringsEqual(t *testing.T) {
"http://host1:8080",
"http://host2:8080",
}, errOnResolve},
{
urlsA: []string{"https://[c262:266f:fa53:0ee6:966e:e3f0:d68f:b046]:2380"},
urlsB: []string{"https://[c262:266f:fa53:ee6:966e:e3f0:d68f:b046]:2380"},
resolver: resolveTCPAddrDefault,
},
}
for idx, c := range cases {
t.Logf("TestURLStringsEqual, case #%d", idx)
Expand Down

0 comments on commit c5347cb

Please sign in to comment.