discv4cmd.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "fmt"
  19. "net"
  20. "strings"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/p2p/discover"
  25. "github.com/ethereum/go-ethereum/p2p/enode"
  26. "github.com/ethereum/go-ethereum/params"
  27. "gopkg.in/urfave/cli.v1"
  28. )
  29. var (
  30. discv4Command = cli.Command{
  31. Name: "discv4",
  32. Usage: "Node Discovery v4 tools",
  33. Subcommands: []cli.Command{
  34. discv4PingCommand,
  35. discv4RequestRecordCommand,
  36. discv4ResolveCommand,
  37. discv4ResolveJSONCommand,
  38. },
  39. }
  40. discv4PingCommand = cli.Command{
  41. Name: "ping",
  42. Usage: "Sends ping to a node",
  43. Action: discv4Ping,
  44. ArgsUsage: "<node>",
  45. }
  46. discv4RequestRecordCommand = cli.Command{
  47. Name: "requestenr",
  48. Usage: "Requests a node record using EIP-868 enrRequest",
  49. Action: discv4RequestRecord,
  50. ArgsUsage: "<node>",
  51. }
  52. discv4ResolveCommand = cli.Command{
  53. Name: "resolve",
  54. Usage: "Finds a node in the DHT",
  55. Action: discv4Resolve,
  56. ArgsUsage: "<node>",
  57. Flags: []cli.Flag{bootnodesFlag},
  58. }
  59. discv4ResolveJSONCommand = cli.Command{
  60. Name: "resolve-json",
  61. Usage: "Re-resolves nodes in a nodes.json file",
  62. Action: discv4ResolveJSON,
  63. Flags: []cli.Flag{bootnodesFlag},
  64. ArgsUsage: "<nodes.json file>",
  65. }
  66. )
  67. var bootnodesFlag = cli.StringFlag{
  68. Name: "bootnodes",
  69. Usage: "Comma separated nodes used for bootstrapping",
  70. }
  71. func discv4Ping(ctx *cli.Context) error {
  72. n := getNodeArg(ctx)
  73. disc := startV4(ctx)
  74. defer disc.Close()
  75. start := time.Now()
  76. if err := disc.Ping(n); err != nil {
  77. return fmt.Errorf("node didn't respond: %v", err)
  78. }
  79. fmt.Printf("node responded to ping (RTT %v).\n", time.Since(start))
  80. return nil
  81. }
  82. func discv4RequestRecord(ctx *cli.Context) error {
  83. n := getNodeArg(ctx)
  84. disc := startV4(ctx)
  85. defer disc.Close()
  86. respN, err := disc.RequestENR(n)
  87. if err != nil {
  88. return fmt.Errorf("can't retrieve record: %v", err)
  89. }
  90. fmt.Println(respN.String())
  91. return nil
  92. }
  93. func discv4Resolve(ctx *cli.Context) error {
  94. n := getNodeArg(ctx)
  95. disc := startV4(ctx)
  96. defer disc.Close()
  97. fmt.Println(disc.Resolve(n).String())
  98. return nil
  99. }
  100. func discv4ResolveJSON(ctx *cli.Context) error {
  101. if ctx.NArg() < 1 {
  102. return fmt.Errorf("need nodes file as argument")
  103. }
  104. disc := startV4(ctx)
  105. defer disc.Close()
  106. file := ctx.Args().Get(0)
  107. // Load existing nodes in file.
  108. var nodes []*enode.Node
  109. if common.FileExist(file) {
  110. nodes = loadNodesJSON(file).nodes()
  111. }
  112. // Add nodes from command line arguments.
  113. for i := 1; i < ctx.NArg(); i++ {
  114. n, err := parseNode(ctx.Args().Get(i))
  115. if err != nil {
  116. exit(err)
  117. }
  118. nodes = append(nodes, n)
  119. }
  120. result := make(nodeSet, len(nodes))
  121. for _, n := range nodes {
  122. n = disc.Resolve(n)
  123. result[n.ID()] = nodeJSON{Seq: n.Seq(), N: n}
  124. }
  125. writeNodesJSON(file, result)
  126. return nil
  127. }
  128. func parseBootnodes(ctx *cli.Context) ([]*enode.Node, error) {
  129. s := params.RinkebyBootnodes
  130. if ctx.IsSet(bootnodesFlag.Name) {
  131. s = strings.Split(ctx.String(bootnodesFlag.Name), ",")
  132. }
  133. nodes := make([]*enode.Node, len(s))
  134. var err error
  135. for i, record := range s {
  136. nodes[i], err = parseNode(record)
  137. if err != nil {
  138. return nil, fmt.Errorf("invalid bootstrap node: %v", err)
  139. }
  140. }
  141. return nodes, nil
  142. }
  143. // startV4 starts an ephemeral discovery V4 node.
  144. func startV4(ctx *cli.Context) *discover.UDPv4 {
  145. socket, ln, cfg, err := listen()
  146. if err != nil {
  147. exit(err)
  148. }
  149. if commandHasFlag(ctx, bootnodesFlag) {
  150. bn, err := parseBootnodes(ctx)
  151. if err != nil {
  152. exit(err)
  153. }
  154. cfg.Bootnodes = bn
  155. }
  156. disc, err := discover.ListenV4(socket, ln, cfg)
  157. if err != nil {
  158. exit(err)
  159. }
  160. return disc
  161. }
  162. func listen() (*net.UDPConn, *enode.LocalNode, discover.Config, error) {
  163. var cfg discover.Config
  164. cfg.PrivateKey, _ = crypto.GenerateKey()
  165. db, _ := enode.OpenDB("")
  166. ln := enode.NewLocalNode(db, cfg.PrivateKey)
  167. socket, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IP{0, 0, 0, 0}})
  168. if err != nil {
  169. db.Close()
  170. return nil, nil, cfg, err
  171. }
  172. addr := socket.LocalAddr().(*net.UDPAddr)
  173. ln.SetFallbackIP(net.IP{127, 0, 0, 1})
  174. ln.SetFallbackUDP(addr.Port)
  175. return socket, ln, cfg, nil
  176. }