urlv4.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. package enode
  17. import (
  18. "crypto/ecdsa"
  19. "encoding/hex"
  20. "errors"
  21. "fmt"
  22. "net"
  23. "net/url"
  24. "regexp"
  25. "strconv"
  26. "github.com/ethereum/go-ethereum/common/math"
  27. "github.com/ethereum/go-ethereum/crypto"
  28. "github.com/ethereum/go-ethereum/p2p/enr"
  29. )
  30. var incompleteNodeURL = regexp.MustCompile("(?i)^(?:enode://)?([0-9a-f]+)$")
  31. // MustParseV4 parses a node URL. It panics if the URL is not valid.
  32. func MustParseV4(rawurl string) *Node {
  33. n, err := ParseV4(rawurl)
  34. if err != nil {
  35. panic("invalid node URL: " + err.Error())
  36. }
  37. return n
  38. }
  39. // ParseV4 parses a node URL.
  40. //
  41. // There are two basic forms of node URLs:
  42. //
  43. // - incomplete nodes, which only have the public key (node ID)
  44. // - complete nodes, which contain the public key and IP/Port information
  45. //
  46. // For incomplete nodes, the designator must look like one of these
  47. //
  48. // enode://<hex node id>
  49. // <hex node id>
  50. //
  51. // For complete nodes, the node ID is encoded in the username portion
  52. // of the URL, separated from the host by an @ sign. The hostname can
  53. // only be given as an IP address, DNS domain names are not allowed.
  54. // The port in the host name section is the TCP listening port. If the
  55. // TCP and UDP (discovery) ports differ, the UDP port is specified as
  56. // query parameter "discport".
  57. //
  58. // In the following example, the node URL describes
  59. // a node with IP address 10.3.58.6, TCP listening port 30303
  60. // and UDP discovery port 30301.
  61. //
  62. // enode://<hex node id>@10.3.58.6:30303?discport=30301
  63. func ParseV4(rawurl string) (*Node, error) {
  64. if m := incompleteNodeURL.FindStringSubmatch(rawurl); m != nil {
  65. id, err := parsePubkey(m[1])
  66. if err != nil {
  67. return nil, fmt.Errorf("invalid public key (%v)", err)
  68. }
  69. return NewV4(id, nil, 0, 0), nil
  70. }
  71. return parseComplete(rawurl)
  72. }
  73. // NewV4 creates a node from discovery v4 node information. The record
  74. // contained in the node has a zero-length signature.
  75. func NewV4(pubkey *ecdsa.PublicKey, ip net.IP, tcp, udp int) *Node {
  76. var r enr.Record
  77. if len(ip) > 0 {
  78. r.Set(enr.IP(ip))
  79. }
  80. if udp != 0 {
  81. r.Set(enr.UDP(udp))
  82. }
  83. if tcp != 0 {
  84. r.Set(enr.TCP(tcp))
  85. }
  86. signV4Compat(&r, pubkey)
  87. n, err := New(v4CompatID{}, &r)
  88. if err != nil {
  89. panic(err)
  90. }
  91. return n
  92. }
  93. // isNewV4 returns true for nodes created by NewV4.
  94. func isNewV4(n *Node) bool {
  95. var k s256raw
  96. return n.r.IdentityScheme() == "" && n.r.Load(&k) == nil && len(n.r.Signature()) == 0
  97. }
  98. func parseComplete(rawurl string) (*Node, error) {
  99. var (
  100. id *ecdsa.PublicKey
  101. ip net.IP
  102. tcpPort, udpPort uint64
  103. )
  104. u, err := url.Parse(rawurl)
  105. if err != nil {
  106. return nil, err
  107. }
  108. if u.Scheme != "enode" {
  109. return nil, errors.New("invalid URL scheme, want \"enode\"")
  110. }
  111. // Parse the Node ID from the user portion.
  112. if u.User == nil {
  113. return nil, errors.New("does not contain node ID")
  114. }
  115. if id, err = parsePubkey(u.User.String()); err != nil {
  116. return nil, fmt.Errorf("invalid public key (%v)", err)
  117. }
  118. // Parse the IP address.
  119. host, port, err := net.SplitHostPort(u.Host)
  120. if err != nil {
  121. return nil, fmt.Errorf("invalid host: %v", err)
  122. }
  123. if ip = net.ParseIP(host); ip == nil {
  124. return nil, errors.New("invalid IP address")
  125. }
  126. // Parse the port numbers.
  127. if tcpPort, err = strconv.ParseUint(port, 10, 16); err != nil {
  128. return nil, errors.New("invalid port")
  129. }
  130. udpPort = tcpPort
  131. qv := u.Query()
  132. if qv.Get("discport") != "" {
  133. udpPort, err = strconv.ParseUint(qv.Get("discport"), 10, 16)
  134. if err != nil {
  135. return nil, errors.New("invalid discport in query")
  136. }
  137. }
  138. return NewV4(id, ip, int(tcpPort), int(udpPort)), nil
  139. }
  140. // parsePubkey parses a hex-encoded secp256k1 public key.
  141. func parsePubkey(in string) (*ecdsa.PublicKey, error) {
  142. b, err := hex.DecodeString(in)
  143. if err != nil {
  144. return nil, err
  145. } else if len(b) != 64 {
  146. return nil, fmt.Errorf("wrong length, want %d hex chars", 128)
  147. }
  148. b = append([]byte{0x4}, b...)
  149. return crypto.UnmarshalPubkey(b)
  150. }
  151. func (n *Node) URLv4() string {
  152. var (
  153. scheme enr.ID
  154. nodeid string
  155. key ecdsa.PublicKey
  156. )
  157. n.Load(&scheme)
  158. n.Load((*Secp256k1)(&key))
  159. switch {
  160. case scheme == "v4" || key != ecdsa.PublicKey{}:
  161. nodeid = fmt.Sprintf("%x", crypto.FromECDSAPub(&key)[1:])
  162. default:
  163. nodeid = fmt.Sprintf("%s.%x", scheme, n.id[:])
  164. }
  165. u := url.URL{Scheme: "enode"}
  166. if n.Incomplete() {
  167. u.Host = nodeid
  168. } else {
  169. addr := net.TCPAddr{IP: n.IP(), Port: n.TCP()}
  170. u.User = url.User(nodeid)
  171. u.Host = addr.String()
  172. if n.UDP() != n.TCP() {
  173. u.RawQuery = "discport=" + strconv.Itoa(n.UDP())
  174. }
  175. }
  176. return u.String()
  177. }
  178. // PubkeyToIDV4 derives the v4 node address from the given public key.
  179. func PubkeyToIDV4(key *ecdsa.PublicKey) ID {
  180. e := make([]byte, 64)
  181. math.ReadBits(key.X, e[:len(e)/2])
  182. math.ReadBits(key.Y, e[len(e)/2:])
  183. return ID(crypto.Keccak256Hash(e))
  184. }