This commit is contained in:
Matthieu Volat 2017-09-05 08:34:46 +00:00 committed by GitHub
commit cd2db8bbcc
4 changed files with 12 additions and 8 deletions

View file

@ -171,13 +171,15 @@ func parseComplete(rawurl string) (*Node, error) {
if id, err = HexID(u.User.String()); err != nil { if id, err = HexID(u.User.String()); err != nil {
return nil, fmt.Errorf("invalid node ID (%v)", err) return nil, fmt.Errorf("invalid node ID (%v)", err)
} }
// Parse the IP address. // Parse the host.
host, port, err := net.SplitHostPort(u.Host) host, port, err := net.SplitHostPort(u.Host)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid host: %v", err) return nil, fmt.Errorf("invalid host: %v", err)
} }
if ip = net.ParseIP(host); ip == nil { if addrs, err := net.LookupIP(host); err == nil {
return nil, errors.New("invalid IP address") ip = addrs[0] // Take first address
} else {
return nil, errors.New("could not resolve host")
} }
// Ensure the IP is 4 bytes long for IPv4 addresses. // Ensure the IP is 4 bytes long for IPv4 addresses.
if ipv4 := ip.To4(); ipv4 != nil { if ipv4 := ip.To4(); ipv4 != nil {

View file

@ -68,7 +68,7 @@ var parseNodeTests = []struct {
// Complete nodes with IP address. // Complete nodes with IP address.
{ {
rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@hostname:3", rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@hostname:3",
wantError: `invalid IP address`, wantError: `could not resolve host`,
}, },
{ {
rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:foo", rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:foo",

View file

@ -179,13 +179,15 @@ func parseComplete(rawurl string) (*Node, error) {
if id, err = HexID(u.User.String()); err != nil { if id, err = HexID(u.User.String()); err != nil {
return nil, fmt.Errorf("invalid node ID (%v)", err) return nil, fmt.Errorf("invalid node ID (%v)", err)
} }
// Parse the IP address. // Parse the host.
host, port, err := net.SplitHostPort(u.Host) host, port, err := net.SplitHostPort(u.Host)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid host: %v", err) return nil, fmt.Errorf("invalid host: %v", err)
} }
if ip = net.ParseIP(host); ip == nil { if addrs, err := net.LookupIP(host); err == nil {
return nil, errors.New("invalid IP address") ip = addrs[0] // Take first address
} else {
return nil, errors.New("could not resolve host")
} }
// Ensure the IP is 4 bytes long for IPv4 addresses. // Ensure the IP is 4 bytes long for IPv4 addresses.
if ipv4 := ip.To4(); ipv4 != nil { if ipv4 := ip.To4(); ipv4 != nil {

View file

@ -68,7 +68,7 @@ var parseNodeTests = []struct {
// Complete nodes with IP address. // Complete nodes with IP address.
{ {
rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@hostname:3", rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@hostname:3",
wantError: `invalid IP address`, wantError: `could not resolve host`,
}, },
{ {
rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:foo", rawurl: "enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@127.0.0.1:foo",