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