urlv4.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 (
  31. incompleteNodeURL = regexp.MustCompile("(?i)^(?:enode://)?([0-9a-f]+)$")
  32. lookupIPFunc = net.LookupIP
  33. )
  34. // MustParseV4 parses a node URL. It panics if the URL is not valid.
  35. func MustParseV4(rawurl string) *Node {
  36. n, err := ParseV4(rawurl)
  37. if err != nil {
  38. panic("invalid node URL: " + err.Error())
  39. }
  40. return n
  41. }
  42. // ParseV4 parses a node URL.
  43. //
  44. // There are two basic forms of node URLs:
  45. //
  46. // - incomplete nodes, which only have the public key (node ID)
  47. // - complete nodes, which contain the public key and IP/Port information
  48. //
  49. // For incomplete nodes, the designator must look like one of these
  50. //
  51. // enode://<hex node id>
  52. // <hex node id>
  53. //
  54. // For complete nodes, the node ID is encoded in the username portion
  55. // of the URL, separated from the host by an @ sign. The hostname can
  56. // only be given as an IP address or using DNS domain name.
  57. // The port in the host name section is the TCP listening port. If the
  58. // TCP and UDP (discovery) ports differ, the UDP port is specified as
  59. // query parameter "discport".
  60. //
  61. // In the following example, the node URL describes
  62. // a node with IP address 10.3.58.6, TCP listening port 30303
  63. // and UDP discovery port 30301.
  64. //
  65. // enode://<hex node id>@10.3.58.6:30303?discport=30301
  66. func ParseV4(rawurl string) (*Node, error) {
  67. if m := incompleteNodeURL.FindStringSubmatch(rawurl); m != nil {
  68. id, err := parsePubkey(m[1])
  69. if err != nil {
  70. return nil, fmt.Errorf("invalid public key (%v)", err)
  71. }
  72. return NewV4(id, nil, 0, 0), nil
  73. }
  74. return parseComplete(rawurl)
  75. }
  76. // NewV4 creates a node from discovery v4 node information. The record
  77. // contained in the node has a zero-length signature.
  78. func NewV4(pubkey *ecdsa.PublicKey, ip net.IP, tcp, udp int) *Node {
  79. var r enr.Record
  80. if len(ip) > 0 {
  81. r.Set(enr.IP(ip))
  82. }
  83. return newV4(pubkey, r, tcp, udp)
  84. }
  85. // broken out from `func NewV4` (above) same in upstream go-ethereum, but taken out
  86. // to avoid code duplication b/t NewV4 and NewV4Hostname
  87. func newV4(pubkey *ecdsa.PublicKey, r enr.Record, tcp, udp int) *Node {
  88. if udp != 0 {
  89. r.Set(enr.UDP(udp))
  90. }
  91. if tcp != 0 {
  92. r.Set(enr.TCP(tcp))
  93. }
  94. signV4Compat(&r, pubkey)
  95. n, err := New(v4CompatID{}, &r)
  96. if err != nil {
  97. panic(err)
  98. }
  99. return n
  100. }
  101. // isNewV4 returns true for nodes created by NewV4.
  102. func isNewV4(n *Node) bool {
  103. var k s256raw
  104. return n.r.IdentityScheme() == "" && n.r.Load(&k) == nil && len(n.r.Signature()) == 0
  105. }
  106. // Quorum
  107. // NewV4Hostname creates a node from discovery v4 node information. The record
  108. // contained in the node has a zero-length signature. It sets the hostname or ip
  109. // of the node depends on hostname context
  110. func NewV4Hostname(pubkey *ecdsa.PublicKey, hostname string, tcp, udp, raftPort int) *Node {
  111. var r enr.Record
  112. if ip := net.ParseIP(hostname); ip == nil {
  113. r.Set(enr.Hostname(hostname))
  114. } else {
  115. r.Set(enr.IP(ip))
  116. }
  117. if raftPort != 0 {
  118. r.Set(enr.RaftPort(raftPort))
  119. }
  120. return newV4(pubkey, r, tcp, udp)
  121. }
  122. // End-Quorum
  123. func parseComplete(rawurl string) (*Node, error) {
  124. var (
  125. id *ecdsa.PublicKey
  126. ip net.IP
  127. tcpPort, udpPort uint64
  128. )
  129. u, err := url.Parse(rawurl)
  130. if err != nil {
  131. return nil, err
  132. }
  133. if u.Scheme != "enode" {
  134. return nil, errors.New("invalid URL scheme, want \"enode\"")
  135. }
  136. // Parse the Node ID from the user portion.
  137. if u.User == nil {
  138. return nil, errors.New("does not contain node ID")
  139. }
  140. if id, err = parsePubkey(u.User.String()); err != nil {
  141. return nil, fmt.Errorf("invalid public key (%v)", err)
  142. }
  143. qv := u.Query()
  144. // Parse the IP address.
  145. ips, err := net.LookupIP(u.Hostname())
  146. if err != nil {
  147. // Quorum: if IP look up fail don't return error for raft url
  148. if qv.Get("raftport") == "" {
  149. return nil, err
  150. }
  151. } else {
  152. ip = ips[0]
  153. // Ensure the IP is 4 bytes long for IPv4 addresses.
  154. if ipv4 := ip.To4(); ipv4 != nil {
  155. ip = ipv4
  156. }
  157. }
  158. // Parse the port numbers.
  159. if tcpPort, err = strconv.ParseUint(u.Port(), 10, 16); err != nil {
  160. return nil, errors.New("invalid port")
  161. }
  162. udpPort = tcpPort
  163. if qv.Get("discport") != "" {
  164. udpPort, err = strconv.ParseUint(qv.Get("discport"), 10, 16)
  165. if err != nil {
  166. return nil, errors.New("invalid discport in query")
  167. }
  168. }
  169. // Quorum
  170. if qv.Get("raftport") != "" {
  171. raftPort, err := strconv.ParseUint(qv.Get("raftport"), 10, 16)
  172. if err != nil {
  173. return nil, errors.New("invalid raftport in query")
  174. }
  175. if u.Hostname() == "" {
  176. return nil, errors.New("empty hostname in raft url")
  177. }
  178. return NewV4Hostname(id, u.Hostname(), int(tcpPort), int(udpPort), int(raftPort)), nil
  179. }
  180. // End-Quorum
  181. return NewV4(id, ip, int(tcpPort), int(udpPort)), nil
  182. }
  183. func HexPubkey(h string) (*ecdsa.PublicKey, error) {
  184. k, err := parsePubkey(h)
  185. if err != nil {
  186. return nil, err
  187. }
  188. return k, err
  189. }
  190. // parsePubkey parses a hex-encoded secp256k1 public key.
  191. func parsePubkey(in string) (*ecdsa.PublicKey, error) {
  192. b, err := hex.DecodeString(in)
  193. if err != nil {
  194. return nil, err
  195. } else if len(b) != 64 {
  196. return nil, fmt.Errorf("wrong length, want %d hex chars", 128)
  197. }
  198. b = append([]byte{0x4}, b...)
  199. return crypto.UnmarshalPubkey(b)
  200. }
  201. // used by Quorum RAFT - to derive enodeID
  202. func (n *Node) EnodeID() string {
  203. var (
  204. scheme enr.ID
  205. nodeid string
  206. key ecdsa.PublicKey
  207. )
  208. n.Load(&scheme)
  209. n.Load((*Secp256k1)(&key))
  210. switch {
  211. case scheme == "v4" || key != ecdsa.PublicKey{}:
  212. nodeid = fmt.Sprintf("%x", crypto.FromECDSAPub(&key)[1:])
  213. default:
  214. nodeid = fmt.Sprintf("%s.%x", scheme, n.id[:])
  215. }
  216. return nodeid
  217. }
  218. func (n *Node) URLv4() string {
  219. var (
  220. scheme enr.ID
  221. nodeid string
  222. key ecdsa.PublicKey
  223. )
  224. n.Load(&scheme)
  225. n.Load((*Secp256k1)(&key))
  226. switch {
  227. case scheme == "v4" || key != ecdsa.PublicKey{}:
  228. nodeid = fmt.Sprintf("%x", crypto.FromECDSAPub(&key)[1:])
  229. default:
  230. nodeid = fmt.Sprintf("%s.%x", scheme, n.id[:])
  231. }
  232. u := url.URL{Scheme: "enode"}
  233. if n.Incomplete() {
  234. u.Host = nodeid
  235. } else {
  236. u.User = url.User(nodeid)
  237. if n.Host() != "" && net.ParseIP(n.Host()) == nil {
  238. // Quorum
  239. u.Host = net.JoinHostPort(n.Host(), strconv.Itoa(n.TCP()))
  240. } else {
  241. addr := net.TCPAddr{IP: n.IP(), Port: n.TCP()}
  242. u.Host = addr.String()
  243. }
  244. if n.UDP() != n.TCP() {
  245. u.RawQuery = "discport=" + strconv.Itoa(n.UDP())
  246. }
  247. // Quorum
  248. if n.HasRaftPort() {
  249. raftQuery := "raftport=" + strconv.Itoa(n.RaftPort())
  250. if len(u.RawQuery) > 0 {
  251. u.RawQuery = u.RawQuery + "&" + raftQuery
  252. } else {
  253. u.RawQuery = raftQuery
  254. }
  255. }
  256. }
  257. return u.String()
  258. }
  259. // PubkeyToIDV4 derives the v4 node address from the given public key.
  260. func PubkeyToIDV4(key *ecdsa.PublicKey) ID {
  261. e := make([]byte, 64)
  262. math.ReadBits(key.X, e[:len(e)/2])
  263. math.ReadBits(key.Y, e[len(e)/2:])
  264. return ID(crypto.Keccak256Hash(e))
  265. }