p2p/enode: allow DNS names in enode URLs (#18524)

This commit is contained in:
alexwang
2019-08-22 20:23:40 +08:00
committed by Felix Lange
parent 4d358b9fc0
commit b90cdbaa79
2 changed files with 10 additions and 8 deletions

View File

@ -125,15 +125,17 @@ func parseComplete(rawurl string) (*Node, error) {
return nil, fmt.Errorf("invalid public key (%v)", err)
}
// Parse the IP address.
host, port, err := net.SplitHostPort(u.Host)
ips, err := net.LookupIP(u.Hostname())
if err != nil {
return nil, fmt.Errorf("invalid host: %v", err)
return nil, err
}
if ip = net.ParseIP(host); ip == nil {
return nil, errors.New("invalid IP address")
ip = ips[0]
// Ensure the IP is 4 bytes long for IPv4 addresses.
if ipv4 := ip.To4(); ipv4 != nil {
ip = ipv4
}
// Parse the port numbers.
if tcpPort, err = strconv.ParseUint(port, 10, 16); err != nil {
if tcpPort, err = strconv.ParseUint(u.Port(), 10, 16); err != nil {
return nil, errors.New("invalid port")
}
udpPort = tcpPort