github.com/ebceco/ebc@v1.8.19-0.20190309150932-8cb0b9e06484/p2p/enode/urlv4.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package enode 18 19 import ( 20 "crypto/ecdsa" 21 "encoding/hex" 22 "errors" 23 "fmt" 24 "net" 25 "net/url" 26 "regexp" 27 "strconv" 28 29 "github.com/ebceco/ebc/common/math" 30 "github.com/ebceco/ebc/crypto" 31 "github.com/ebceco/ebc/p2p/enr" 32 ) 33 34 var incompleteNodeURL = regexp.MustCompile("(?i)^(?:enode://)?([0-9a-f]+)$") 35 36 // MustParseV4 parses a node URL. It panics if the URL is not valid. 37 func MustParseV4(rawurl string) *Node { 38 n, err := ParseV4(rawurl) 39 if err != nil { 40 panic("invalid node URL: " + err.Error()) 41 } 42 return n 43 } 44 45 // ParseV4 parses a node URL. 46 // 47 // There are two basic forms of node URLs: 48 // 49 // - incomplete nodes, which only have the public key (node ID) 50 // - complete nodes, which contain the public key and IP/Port information 51 // 52 // For incomplete nodes, the designator must look like one of these 53 // 54 // enode://<hex node id> 55 // <hex node id> 56 // 57 // For complete nodes, the node ID is encoded in the username portion 58 // of the URL, separated from the host by an @ sign. The hostname can 59 // only be given as an IP address, DNS domain names are not allowed. 60 // The port in the host name section is the TCP listening port. If the 61 // TCP and UDP (discovery) ports differ, the UDP port is specified as 62 // query parameter "discport". 63 // 64 // In the following example, the node URL describes 65 // a node with IP address 10.3.58.6, TCP listening port 30303 66 // and UDP discovery port 30301. 67 // 68 // enode://<hex node id>@10.3.58.6:30303?discport=30301 69 func ParseV4(rawurl string) (*Node, error) { 70 if m := incompleteNodeURL.FindStringSubmatch(rawurl); m != nil { 71 id, err := parsePubkey(m[1]) 72 if err != nil { 73 return nil, fmt.Errorf("invalid node ID (%v)", err) 74 } 75 return NewV4(id, nil, 0, 0), nil 76 } 77 return parseComplete(rawurl) 78 } 79 80 // NewV4 creates a node from discovery v4 node information. The record 81 // contained in the node has a zero-length signature. 82 func NewV4(pubkey *ecdsa.PublicKey, ip net.IP, tcp, udp int) *Node { 83 var r enr.Record 84 if ip != nil { 85 r.Set(enr.IP(ip)) 86 } 87 if udp != 0 { 88 r.Set(enr.UDP(udp)) 89 } 90 if tcp != 0 { 91 r.Set(enr.TCP(tcp)) 92 } 93 signV4Compat(&r, pubkey) 94 n, err := New(v4CompatID{}, &r) 95 if err != nil { 96 panic(err) 97 } 98 return n 99 } 100 101 func parseComplete(rawurl string) (*Node, error) { 102 var ( 103 id *ecdsa.PublicKey 104 ip net.IP 105 tcpPort, udpPort uint64 106 ) 107 u, err := url.Parse(rawurl) 108 if err != nil { 109 return nil, err 110 } 111 if u.Scheme != "enode" { 112 return nil, errors.New("invalid URL scheme, want \"enode\"") 113 } 114 // Parse the Node ID from the user portion. 115 if u.User == nil { 116 return nil, errors.New("does not contain node ID") 117 } 118 if id, err = parsePubkey(u.User.String()); err != nil { 119 return nil, fmt.Errorf("invalid node ID (%v)", err) 120 } 121 // Parse the IP address. 122 host, port, err := net.SplitHostPort(u.Host) 123 if err != nil { 124 return nil, fmt.Errorf("invalid host: %v", err) 125 } 126 if ip = net.ParseIP(host); ip == nil { 127 return nil, errors.New("invalid IP address") 128 } 129 // Ensure the IP is 4 bytes long for IPv4 addresses. 130 if ipv4 := ip.To4(); ipv4 != nil { 131 ip = ipv4 132 } 133 // Parse the port numbers. 134 if tcpPort, err = strconv.ParseUint(port, 10, 16); err != nil { 135 return nil, errors.New("invalid port") 136 } 137 udpPort = tcpPort 138 qv := u.Query() 139 if qv.Get("discport") != "" { 140 udpPort, err = strconv.ParseUint(qv.Get("discport"), 10, 16) 141 if err != nil { 142 return nil, errors.New("invalid discport in query") 143 } 144 } 145 return NewV4(id, ip, int(tcpPort), int(udpPort)), nil 146 } 147 148 // parsePubkey parses a hex-encoded secp256k1 public key. 149 func parsePubkey(in string) (*ecdsa.PublicKey, error) { 150 b, err := hex.DecodeString(in) 151 if err != nil { 152 return nil, err 153 } else if len(b) != 64 { 154 return nil, fmt.Errorf("wrong length, want %d hex chars", 128) 155 } 156 b = append([]byte{0x4}, b...) 157 return crypto.UnmarshalPubkey(b) 158 } 159 160 func (n *Node) v4URL() string { 161 var ( 162 scheme enr.ID 163 nodeid string 164 key ecdsa.PublicKey 165 ) 166 n.Load(&scheme) 167 n.Load((*Secp256k1)(&key)) 168 switch { 169 case scheme == "v4" || key != ecdsa.PublicKey{}: 170 nodeid = fmt.Sprintf("%x", crypto.FromECDSAPub(&key)[1:]) 171 default: 172 nodeid = fmt.Sprintf("%s.%x", scheme, n.id[:]) 173 } 174 u := url.URL{Scheme: "enode"} 175 if n.Incomplete() { 176 u.Host = nodeid 177 } else { 178 addr := net.TCPAddr{IP: n.IP(), Port: n.TCP()} 179 u.User = url.User(nodeid) 180 u.Host = addr.String() 181 if n.UDP() != n.TCP() { 182 u.RawQuery = "discport=" + strconv.Itoa(n.UDP()) 183 } 184 } 185 return u.String() 186 } 187 188 // PubkeyToIDV4 derives the v4 node address from the given public key. 189 func PubkeyToIDV4(key *ecdsa.PublicKey) ID { 190 e := make([]byte, 64) 191 math.ReadBits(key.X, e[:len(e)/2]) 192 math.ReadBits(key.Y, e[len(e)/2:]) 193 return ID(crypto.Keccak256Hash(e)) 194 }