github.com/ethxdao/go-ethereum@v0.0.0-20221218102228-5ae34a9cc189/cmd/devp2p/discv4cmd.go (about)

     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  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/ethxdao/go-ethereum/cmd/devp2p/internal/v4test"
    26  	"github.com/ethxdao/go-ethereum/common"
    27  	"github.com/ethxdao/go-ethereum/crypto"
    28  	"github.com/ethxdao/go-ethereum/internal/flags"
    29  	"github.com/ethxdao/go-ethereum/p2p/discover"
    30  	"github.com/ethxdao/go-ethereum/p2p/enode"
    31  	"github.com/ethxdao/go-ethereum/params"
    32  )
    33  
    34  var (
    35  	discv4Command = &cli.Command{
    36  		Name:  "discv4",
    37  		Usage: "Node Discovery v4 tools",
    38  		Subcommands: []*cli.Command{
    39  			discv4PingCommand,
    40  			discv4RequestRecordCommand,
    41  			discv4ResolveCommand,
    42  			discv4ResolveJSONCommand,
    43  			discv4CrawlCommand,
    44  			discv4TestCommand,
    45  		},
    46  	}
    47  	discv4PingCommand = &cli.Command{
    48  		Name:      "ping",
    49  		Usage:     "Sends ping to a node",
    50  		Action:    discv4Ping,
    51  		ArgsUsage: "<node>",
    52  		Flags:     v4NodeFlags,
    53  	}
    54  	discv4RequestRecordCommand = &cli.Command{
    55  		Name:      "requestenr",
    56  		Usage:     "Requests a node record using EIP-868 enrRequest",
    57  		Action:    discv4RequestRecord,
    58  		ArgsUsage: "<node>",
    59  		Flags:     v4NodeFlags,
    60  	}
    61  	discv4ResolveCommand = &cli.Command{
    62  		Name:      "resolve",
    63  		Usage:     "Finds a node in the DHT",
    64  		Action:    discv4Resolve,
    65  		ArgsUsage: "<node>",
    66  		Flags:     v4NodeFlags,
    67  	}
    68  	discv4ResolveJSONCommand = &cli.Command{
    69  		Name:      "resolve-json",
    70  		Usage:     "Re-resolves nodes in a nodes.json file",
    71  		Action:    discv4ResolveJSON,
    72  		Flags:     v4NodeFlags,
    73  		ArgsUsage: "<nodes.json file>",
    74  	}
    75  	discv4CrawlCommand = &cli.Command{
    76  		Name:   "crawl",
    77  		Usage:  "Updates a nodes.json file with random nodes found in the DHT",
    78  		Action: discv4Crawl,
    79  		Flags:  flags.Merge(v4NodeFlags, []cli.Flag{crawlTimeoutFlag}),
    80  	}
    81  	discv4TestCommand = &cli.Command{
    82  		Name:   "test",
    83  		Usage:  "Runs tests against a node",
    84  		Action: discv4Test,
    85  		Flags: []cli.Flag{
    86  			remoteEnodeFlag,
    87  			testPatternFlag,
    88  			testTAPFlag,
    89  			testListen1Flag,
    90  			testListen2Flag,
    91  		},
    92  	}
    93  )
    94  
    95  var (
    96  	bootnodesFlag = &cli.StringFlag{
    97  		Name:  "bootnodes",
    98  		Usage: "Comma separated nodes used for bootstrapping",
    99  	}
   100  	nodekeyFlag = &cli.StringFlag{
   101  		Name:  "nodekey",
   102  		Usage: "Hex-encoded node key",
   103  	}
   104  	nodedbFlag = &cli.StringFlag{
   105  		Name:  "nodedb",
   106  		Usage: "Nodes database location",
   107  	}
   108  	listenAddrFlag = &cli.StringFlag{
   109  		Name:  "addr",
   110  		Usage: "Listening address",
   111  	}
   112  	crawlTimeoutFlag = &cli.DurationFlag{
   113  		Name:  "timeout",
   114  		Usage: "Time limit for the crawl.",
   115  		Value: 30 * time.Minute,
   116  	}
   117  	remoteEnodeFlag = &cli.StringFlag{
   118  		Name:    "remote",
   119  		Usage:   "Enode of the remote node under test",
   120  		EnvVars: []string{"REMOTE_ENODE"},
   121  	}
   122  )
   123  
   124  var v4NodeFlags = []cli.Flag{
   125  	bootnodesFlag,
   126  	nodekeyFlag,
   127  	nodedbFlag,
   128  	listenAddrFlag,
   129  }
   130  
   131  func discv4Ping(ctx *cli.Context) error {
   132  	n := getNodeArg(ctx)
   133  	disc := startV4(ctx)
   134  	defer disc.Close()
   135  
   136  	start := time.Now()
   137  	if err := disc.Ping(n); err != nil {
   138  		return fmt.Errorf("node didn't respond: %v", err)
   139  	}
   140  	fmt.Printf("node responded to ping (RTT %v).\n", time.Since(start))
   141  	return nil
   142  }
   143  
   144  func discv4RequestRecord(ctx *cli.Context) error {
   145  	n := getNodeArg(ctx)
   146  	disc := startV4(ctx)
   147  	defer disc.Close()
   148  
   149  	respN, err := disc.RequestENR(n)
   150  	if err != nil {
   151  		return fmt.Errorf("can't retrieve record: %v", err)
   152  	}
   153  	fmt.Println(respN.String())
   154  	return nil
   155  }
   156  
   157  func discv4Resolve(ctx *cli.Context) error {
   158  	n := getNodeArg(ctx)
   159  	disc := startV4(ctx)
   160  	defer disc.Close()
   161  
   162  	fmt.Println(disc.Resolve(n).String())
   163  	return nil
   164  }
   165  
   166  func discv4ResolveJSON(ctx *cli.Context) error {
   167  	if ctx.NArg() < 1 {
   168  		return fmt.Errorf("need nodes file as argument")
   169  	}
   170  	nodesFile := ctx.Args().Get(0)
   171  	inputSet := make(nodeSet)
   172  	if common.FileExist(nodesFile) {
   173  		inputSet = loadNodesJSON(nodesFile)
   174  	}
   175  
   176  	// Add extra nodes from command line arguments.
   177  	var nodeargs []*enode.Node
   178  	for i := 1; i < ctx.NArg(); i++ {
   179  		n, err := parseNode(ctx.Args().Get(i))
   180  		if err != nil {
   181  			exit(err)
   182  		}
   183  		nodeargs = append(nodeargs, n)
   184  	}
   185  
   186  	// Run the crawler.
   187  	disc := startV4(ctx)
   188  	defer disc.Close()
   189  	c := newCrawler(inputSet, disc, enode.IterNodes(nodeargs))
   190  	c.revalidateInterval = 0
   191  	output := c.run(0)
   192  	writeNodesJSON(nodesFile, output)
   193  	return nil
   194  }
   195  
   196  func discv4Crawl(ctx *cli.Context) error {
   197  	if ctx.NArg() < 1 {
   198  		return fmt.Errorf("need nodes file as argument")
   199  	}
   200  	nodesFile := ctx.Args().First()
   201  	var inputSet nodeSet
   202  	if common.FileExist(nodesFile) {
   203  		inputSet = loadNodesJSON(nodesFile)
   204  	}
   205  
   206  	disc := startV4(ctx)
   207  	defer disc.Close()
   208  	c := newCrawler(inputSet, disc, disc.RandomNodes())
   209  	c.revalidateInterval = 10 * time.Minute
   210  	output := c.run(ctx.Duration(crawlTimeoutFlag.Name))
   211  	writeNodesJSON(nodesFile, output)
   212  	return nil
   213  }
   214  
   215  // discv4Test runs the protocol test suite.
   216  func discv4Test(ctx *cli.Context) error {
   217  	// Configure test package globals.
   218  	if !ctx.IsSet(remoteEnodeFlag.Name) {
   219  		return fmt.Errorf("Missing -%v", remoteEnodeFlag.Name)
   220  	}
   221  	v4test.Remote = ctx.String(remoteEnodeFlag.Name)
   222  	v4test.Listen1 = ctx.String(testListen1Flag.Name)
   223  	v4test.Listen2 = ctx.String(testListen2Flag.Name)
   224  	return runTests(ctx, v4test.AllTests)
   225  }
   226  
   227  // startV4 starts an ephemeral discovery V4 node.
   228  func startV4(ctx *cli.Context) *discover.UDPv4 {
   229  	ln, config := makeDiscoveryConfig(ctx)
   230  	socket := listen(ln, ctx.String(listenAddrFlag.Name))
   231  	disc, err := discover.ListenV4(socket, ln, config)
   232  	if err != nil {
   233  		exit(err)
   234  	}
   235  	return disc
   236  }
   237  
   238  func makeDiscoveryConfig(ctx *cli.Context) (*enode.LocalNode, discover.Config) {
   239  	var cfg discover.Config
   240  
   241  	if ctx.IsSet(nodekeyFlag.Name) {
   242  		key, err := crypto.HexToECDSA(ctx.String(nodekeyFlag.Name))
   243  		if err != nil {
   244  			exit(fmt.Errorf("-%s: %v", nodekeyFlag.Name, err))
   245  		}
   246  		cfg.PrivateKey = key
   247  	} else {
   248  		cfg.PrivateKey, _ = crypto.GenerateKey()
   249  	}
   250  
   251  	if commandHasFlag(ctx, bootnodesFlag) {
   252  		bn, err := parseBootnodes(ctx)
   253  		if err != nil {
   254  			exit(err)
   255  		}
   256  		cfg.Bootnodes = bn
   257  	}
   258  
   259  	dbpath := ctx.String(nodedbFlag.Name)
   260  	db, err := enode.OpenDB(dbpath)
   261  	if err != nil {
   262  		exit(err)
   263  	}
   264  	ln := enode.NewLocalNode(db, cfg.PrivateKey)
   265  	return ln, cfg
   266  }
   267  
   268  func listen(ln *enode.LocalNode, addr string) *net.UDPConn {
   269  	if addr == "" {
   270  		addr = "0.0.0.0:0"
   271  	}
   272  	socket, err := net.ListenPacket("udp4", addr)
   273  	if err != nil {
   274  		exit(err)
   275  	}
   276  	usocket := socket.(*net.UDPConn)
   277  	uaddr := socket.LocalAddr().(*net.UDPAddr)
   278  	if uaddr.IP.IsUnspecified() {
   279  		ln.SetFallbackIP(net.IP{127, 0, 0, 1})
   280  	} else {
   281  		ln.SetFallbackIP(uaddr.IP)
   282  	}
   283  	ln.SetFallbackUDP(uaddr.Port)
   284  	return usocket
   285  }
   286  
   287  func parseBootnodes(ctx *cli.Context) ([]*enode.Node, error) {
   288  	s := params.RinkebyBootnodes
   289  	if ctx.IsSet(bootnodesFlag.Name) {
   290  		input := ctx.String(bootnodesFlag.Name)
   291  		if input == "" {
   292  			return nil, nil
   293  		}
   294  		s = strings.Split(input, ",")
   295  	}
   296  	nodes := make([]*enode.Node, len(s))
   297  	var err error
   298  	for i, record := range s {
   299  		nodes[i], err = parseNode(record)
   300  		if err != nil {
   301  			return nil, fmt.Errorf("invalid bootstrap node: %v", err)
   302  		}
   303  	}
   304  	return nodes, nil
   305  }