discv5cmd.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "time"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/p2p/discover"
  22. "gopkg.in/urfave/cli.v1"
  23. )
  24. var (
  25. discv5Command = cli.Command{
  26. Name: "discv5",
  27. Usage: "Node Discovery v5 tools",
  28. Subcommands: []cli.Command{
  29. discv5PingCommand,
  30. discv5ResolveCommand,
  31. discv5CrawlCommand,
  32. discv5ListenCommand,
  33. },
  34. }
  35. discv5PingCommand = cli.Command{
  36. Name: "ping",
  37. Usage: "Sends ping to a node",
  38. Action: discv5Ping,
  39. }
  40. discv5ResolveCommand = cli.Command{
  41. Name: "resolve",
  42. Usage: "Finds a node in the DHT",
  43. Action: discv5Resolve,
  44. Flags: []cli.Flag{bootnodesFlag},
  45. }
  46. discv5CrawlCommand = cli.Command{
  47. Name: "crawl",
  48. Usage: "Updates a nodes.json file with random nodes found in the DHT",
  49. Action: discv5Crawl,
  50. Flags: []cli.Flag{bootnodesFlag, crawlTimeoutFlag},
  51. }
  52. discv5ListenCommand = cli.Command{
  53. Name: "listen",
  54. Usage: "Runs a node",
  55. Action: discv5Listen,
  56. Flags: []cli.Flag{
  57. bootnodesFlag,
  58. nodekeyFlag,
  59. nodedbFlag,
  60. listenAddrFlag,
  61. },
  62. }
  63. )
  64. func discv5Ping(ctx *cli.Context) error {
  65. n := getNodeArg(ctx)
  66. disc := startV5(ctx)
  67. defer disc.Close()
  68. fmt.Println(disc.Ping(n))
  69. return nil
  70. }
  71. func discv5Resolve(ctx *cli.Context) error {
  72. n := getNodeArg(ctx)
  73. disc := startV5(ctx)
  74. defer disc.Close()
  75. fmt.Println(disc.Resolve(n))
  76. return nil
  77. }
  78. func discv5Crawl(ctx *cli.Context) error {
  79. if ctx.NArg() < 1 {
  80. return fmt.Errorf("need nodes file as argument")
  81. }
  82. nodesFile := ctx.Args().First()
  83. var inputSet nodeSet
  84. if common.FileExist(nodesFile) {
  85. inputSet = loadNodesJSON(nodesFile)
  86. }
  87. disc := startV5(ctx)
  88. defer disc.Close()
  89. c := newCrawler(inputSet, disc, disc.RandomNodes())
  90. c.revalidateInterval = 10 * time.Minute
  91. output := c.run(ctx.Duration(crawlTimeoutFlag.Name))
  92. writeNodesJSON(nodesFile, output)
  93. return nil
  94. }
  95. func discv5Listen(ctx *cli.Context) error {
  96. disc := startV5(ctx)
  97. defer disc.Close()
  98. fmt.Println(disc.Self())
  99. select {}
  100. }
  101. // startV5 starts an ephemeral discovery v5 node.
  102. func startV5(ctx *cli.Context) *discover.UDPv5 {
  103. ln, config := makeDiscoveryConfig(ctx)
  104. socket := listen(ln, ctx.String(listenAddrFlag.Name))
  105. disc, err := discover.ListenV5(socket, ln, config)
  106. if err != nil {
  107. exit(err)
  108. }
  109. return disc
  110. }