github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/go-ethereum/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/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/crypto"
    27  	"github.com/ethereum/go-ethereum/p2p/discover"
    28  	"github.com/ethereum/go-ethereum/p2p/enode"
    29  	"github.com/ethereum/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  		},
    43  	}
    44  	discv4PingCommand = cli.Command{
    45  		Name:      "ping",
    46  		Usage:     "Sends ping to a node",
    47  		Action:    discv4Ping,
    48  		ArgsUsage: "<node>",
    49  	}
    50  	discv4RequestRecordCommand = cli.Command{
    51  		Name:      "requestenr",
    52  		Usage:     "Requests a node record using EIP-868 enrRequest",
    53  		Action:    discv4RequestRecord,
    54  		ArgsUsage: "<node>",
    55  	}
    56  	discv4ResolveCommand = cli.Command{
    57  		Name:      "resolve",
    58  		Usage:     "Finds a node in the DHT",
    59  		Action:    discv4Resolve,
    60  		ArgsUsage: "<node>",
    61  		Flags:     []cli.Flag{bootnodesFlag},
    62  	}
    63  	discv4ResolveJSONCommand = cli.Command{
    64  		Name:      "resolve-json",
    65  		Usage:     "Re-resolves nodes in a nodes.json file",
    66  		Action:    discv4ResolveJSON,
    67  		Flags:     []cli.Flag{bootnodesFlag},
    68  		ArgsUsage: "<nodes.json file>",
    69  	}
    70  )
    71  
    72  var bootnodesFlag = cli.StringFlag{
    73  	Name:  "bootnodes",
    74  	Usage: "Comma separated nodes used for bootstrapping",
    75  }
    76  
    77  func discv4Ping(ctx *cli.Context) error {
    78  	n := getNodeArg(ctx)
    79  	disc := startV4(ctx)
    80  	defer disc.Close()
    81  
    82  	start := time.Now()
    83  	if err := disc.Ping(n); err != nil {
    84  		return fmt.Errorf("node didn't respond: %v", err)
    85  	}
    86  	fmt.Printf("node responded to ping (RTT %v).\n", time.Since(start))
    87  	return nil
    88  }
    89  
    90  func discv4RequestRecord(ctx *cli.Context) error {
    91  	n := getNodeArg(ctx)
    92  	disc := startV4(ctx)
    93  	defer disc.Close()
    94  
    95  	respN, err := disc.RequestENR(n)
    96  	if err != nil {
    97  		return fmt.Errorf("can't retrieve record: %v", err)
    98  	}
    99  	fmt.Println(respN.String())
   100  	return nil
   101  }
   102  
   103  func discv4Resolve(ctx *cli.Context) error {
   104  	n := getNodeArg(ctx)
   105  	disc := startV4(ctx)
   106  	defer disc.Close()
   107  
   108  	fmt.Println(disc.Resolve(n).String())
   109  	return nil
   110  }
   111  
   112  func discv4ResolveJSON(ctx *cli.Context) error {
   113  	if ctx.NArg() < 1 {
   114  		return fmt.Errorf("need nodes file as argument")
   115  	}
   116  	disc := startV4(ctx)
   117  	defer disc.Close()
   118  	file := ctx.Args().Get(0)
   119  
   120  	// Load existing nodes in file.
   121  	var nodes []*enode.Node
   122  	if common.FileExist(file) {
   123  		nodes = loadNodesJSON(file).nodes()
   124  	}
   125  	// Add nodes from command line arguments.
   126  	for i := 1; i < ctx.NArg(); i++ {
   127  		n, err := parseNode(ctx.Args().Get(i))
   128  		if err != nil {
   129  			exit(err)
   130  		}
   131  		nodes = append(nodes, n)
   132  	}
   133  
   134  	result := make(nodeSet, len(nodes))
   135  	for _, n := range nodes {
   136  		n = disc.Resolve(n)
   137  		result[n.ID()] = nodeJSON{Seq: n.Seq(), N: n}
   138  	}
   139  	writeNodesJSON(file, result)
   140  	return nil
   141  }
   142  
   143  func parseBootnodes(ctx *cli.Context) ([]*enode.Node, error) {
   144  	s := params.RinkebyBootnodes
   145  	if ctx.IsSet(bootnodesFlag.Name) {
   146  		s = strings.Split(ctx.String(bootnodesFlag.Name), ",")
   147  	}
   148  	nodes := make([]*enode.Node, len(s))
   149  	var err error
   150  	for i, record := range s {
   151  		nodes[i], err = parseNode(record)
   152  		if err != nil {
   153  			return nil, fmt.Errorf("invalid bootstrap node: %v", err)
   154  		}
   155  	}
   156  	return nodes, nil
   157  }
   158  
   159  // startV4 starts an ephemeral discovery V4 node.
   160  func startV4(ctx *cli.Context) *discover.UDPv4 {
   161  	socket, ln, cfg, err := listen()
   162  	if err != nil {
   163  		exit(err)
   164  	}
   165  	if commandHasFlag(ctx, bootnodesFlag) {
   166  		bn, err := parseBootnodes(ctx)
   167  		if err != nil {
   168  			exit(err)
   169  		}
   170  		cfg.Bootnodes = bn
   171  	}
   172  	disc, err := discover.ListenV4(socket, ln, cfg)
   173  	if err != nil {
   174  		exit(err)
   175  	}
   176  	return disc
   177  }
   178  
   179  func listen() (*net.UDPConn, *enode.LocalNode, discover.Config, error) {
   180  	var cfg discover.Config
   181  	cfg.PrivateKey, _ = crypto.GenerateKey()
   182  	db, _ := enode.OpenDB("")
   183  	ln := enode.NewLocalNode(db, cfg.PrivateKey)
   184  
   185  	socket, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IP{0, 0, 0, 0}})
   186  	if err != nil {
   187  		db.Close()
   188  		return nil, nil, cfg, err
   189  	}
   190  	addr := socket.LocalAddr().(*net.UDPAddr)
   191  	ln.SetFallbackIP(net.IP{127, 0, 0, 1})
   192  	ln.SetFallbackUDP(addr.Port)
   193  	return socket, ln, cfg, nil
   194  }