p2p/enode: improve IPv6 support, add ENR text representation (#19663)
* p2p/enr: add entries for for IPv4/IPv6 separation This adds entry types for "ip6", "udp6", "tcp6" keys. The IP type stays around because removing it would break a lot of code and force everyone to care about the distinction. * p2p/enode: track IPv4 and IPv6 address separately LocalNode predicts the local node's UDP endpoint and updates the record. This change makes it predict IPv4 and IPv6 endpoints separately since they can now be in the record at the same time. * p2p/enode: implement base64 text format * all: switch to enode.Parse(...) This allows passing base64-encoded node records to all the places that previously accepted enode:// URLs. The URL format is still supported. * cmd/bootnode, p2p: log node URL instead of ENR ...and return the base64 record in NodeInfo.
This commit is contained in:
@ -18,6 +18,7 @@ package enode
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
@ -27,8 +28,11 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
)
|
||||
|
||||
var errMissingPrefix = errors.New("missing 'enr:' prefix for base64-encoded record")
|
||||
|
||||
// Node represents a host on the network.
|
||||
type Node struct {
|
||||
r enr.Record
|
||||
@ -48,6 +52,34 @@ func New(validSchemes enr.IdentityScheme, r *enr.Record) (*Node, error) {
|
||||
return node, nil
|
||||
}
|
||||
|
||||
// MustParse parses a node record or enode:// URL. It panics if the input is invalid.
|
||||
func MustParse(rawurl string) *Node {
|
||||
n, err := Parse(ValidSchemes, rawurl)
|
||||
if err != nil {
|
||||
panic("invalid node: " + err.Error())
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// Parse decodes and verifies a base64-encoded node record.
|
||||
func Parse(validSchemes enr.IdentityScheme, input string) (*Node, error) {
|
||||
if strings.HasPrefix(input, "enode://") {
|
||||
return ParseV4(input)
|
||||
}
|
||||
if !strings.HasPrefix(input, "enr:") {
|
||||
return nil, errMissingPrefix
|
||||
}
|
||||
bin, err := base64.RawURLEncoding.DecodeString(input[4:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var r enr.Record
|
||||
if err := rlp.DecodeBytes(bin, &r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return New(validSchemes, &r)
|
||||
}
|
||||
|
||||
// ID returns the node identifier.
|
||||
func (n *Node) ID() ID {
|
||||
return n.id
|
||||
@ -68,11 +100,19 @@ func (n *Node) Load(k enr.Entry) error {
|
||||
return n.r.Load(k)
|
||||
}
|
||||
|
||||
// IP returns the IP address of the node.
|
||||
// IP returns the IP address of the node. This prefers IPv4 addresses.
|
||||
func (n *Node) IP() net.IP {
|
||||
var ip net.IP
|
||||
n.Load((*enr.IP)(&ip))
|
||||
return ip
|
||||
var (
|
||||
ip4 enr.IPv4
|
||||
ip6 enr.IPv6
|
||||
)
|
||||
if n.Load(&ip4) == nil {
|
||||
return net.IP(ip4)
|
||||
}
|
||||
if n.Load(&ip6) == nil {
|
||||
return net.IP(ip6)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UDP returns the UDP port of the node.
|
||||
@ -105,10 +145,11 @@ func (n *Node) Record() *enr.Record {
|
||||
return &cpy
|
||||
}
|
||||
|
||||
// checks whether n is a valid complete node.
|
||||
// ValidateComplete checks whether n has a valid IP and UDP port.
|
||||
// Deprecated: don't use this method.
|
||||
func (n *Node) ValidateComplete() error {
|
||||
if n.Incomplete() {
|
||||
return errors.New("incomplete node")
|
||||
return errors.New("missing IP address")
|
||||
}
|
||||
if n.UDP() == 0 {
|
||||
return errors.New("missing UDP port")
|
||||
@ -122,20 +163,24 @@ func (n *Node) ValidateComplete() error {
|
||||
return n.Load(&key)
|
||||
}
|
||||
|
||||
// The string representation of a Node is a URL.
|
||||
// Please see ParseNode for a description of the format.
|
||||
// String returns the text representation of the record.
|
||||
func (n *Node) String() string {
|
||||
return n.v4URL()
|
||||
if isNewV4(n) {
|
||||
return n.URLv4() // backwards-compatibility glue for NewV4 nodes
|
||||
}
|
||||
enc, _ := rlp.EncodeToBytes(&n.r) // always succeeds because record is valid
|
||||
b64 := base64.RawURLEncoding.EncodeToString(enc)
|
||||
return "enr:" + b64
|
||||
}
|
||||
|
||||
// MarshalText implements encoding.TextMarshaler.
|
||||
func (n *Node) MarshalText() ([]byte, error) {
|
||||
return []byte(n.v4URL()), nil
|
||||
return []byte(n.String()), nil
|
||||
}
|
||||
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
func (n *Node) UnmarshalText(text []byte) error {
|
||||
dec, err := ParseV4(string(text))
|
||||
dec, err := Parse(ValidSchemes, string(text))
|
||||
if err == nil {
|
||||
*n = *dec
|
||||
}
|
||||
|
Reference in New Issue
Block a user