Skip to content

Commit

Permalink
fix(libs/utils): Use valid ip4 address (#2394)
Browse files Browse the repository at this point in the history
Fixes an issue where `localhost` was not accepted as a valid
`--gateway.addr`. Uses net.ResolveIPAddr instead.

Found by @tuxcanfly
  • Loading branch information
renaynay authored Jun 27, 2023
1 parent f79651c commit 637072e
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions libs/utils/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ func ValidateAddr(addr string) (string, error) {
return addr, err
}

if ip := net.ParseIP(addr); ip == nil {
addrs, err := net.LookupHost(addr)
if err != nil {
return addr, fmt.Errorf("could not resolve %v: %w", addr, err)
}
if len(addrs) == 0 {
return addr, fmt.Errorf("no IP addresses found for DNS record: %v", addr)
}
addr = addrs[0]
ip := net.ParseIP(addr)
if ip != nil {
return addr, nil
}
return addr, nil

resolved, err := net.ResolveIPAddr("ip4", addr)
if err != nil {
return addr, err
}
return resolved.String(), nil
}

0 comments on commit 637072e

Please sign in to comment.