github.com/isti4github/eth-ecc@v0.0.0-20201227085832-c337f2d99319/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  	"sort"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/Onther-Tech/go-ethereum/crypto"
    27  	"github.com/Onther-Tech/go-ethereum/p2p/discover"
    28  	"github.com/Onther-Tech/go-ethereum/p2p/enode"
    29  	"github.com/Onther-Tech/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  		},
    42  	}
    43  	discv4PingCommand = cli.Command{
    44  		Name:   "ping",
    45  		Usage:  "Sends ping to a node",
    46  		Action: discv4Ping,
    47  	}
    48  	discv4RequestRecordCommand = cli.Command{
    49  		Name:   "requestenr",
    50  		Usage:  "Requests a node record using EIP-868 enrRequest",
    51  		Action: discv4RequestRecord,
    52  	}
    53  	discv4ResolveCommand = cli.Command{
    54  		Name:   "resolve",
    55  		Usage:  "Finds a node in the DHT",
    56  		Action: discv4Resolve,
    57  		Flags:  []cli.Flag{bootnodesFlag},
    58  	}
    59  )
    60  
    61  var bootnodesFlag = cli.StringFlag{
    62  	Name:  "bootnodes",
    63  	Usage: "Comma separated nodes used for bootstrapping",
    64  }
    65  
    66  func discv4Ping(ctx *cli.Context) error {
    67  	n, disc, err := getNodeArgAndStartV4(ctx)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	defer disc.Close()
    72  
    73  	start := time.Now()
    74  	if err := disc.Ping(n); err != nil {
    75  		return fmt.Errorf("node didn't respond: %v", err)
    76  	}
    77  	fmt.Printf("node responded to ping (RTT %v).\n", time.Since(start))
    78  	return nil
    79  }
    80  
    81  func discv4RequestRecord(ctx *cli.Context) error {
    82  	n, disc, err := getNodeArgAndStartV4(ctx)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	defer disc.Close()
    87  
    88  	respN, err := disc.RequestENR(n)
    89  	if err != nil {
    90  		return fmt.Errorf("can't retrieve record: %v", err)
    91  	}
    92  	fmt.Println(respN.String())
    93  	return nil
    94  }
    95  
    96  func discv4Resolve(ctx *cli.Context) error {
    97  	n, disc, err := getNodeArgAndStartV4(ctx)
    98  	if err != nil {
    99  		return err
   100  	}
   101  	defer disc.Close()
   102  
   103  	fmt.Println(disc.Resolve(n).String())
   104  	return nil
   105  }
   106  
   107  func getNodeArgAndStartV4(ctx *cli.Context) (*enode.Node, *discover.UDPv4, error) {
   108  	if ctx.NArg() != 1 {
   109  		return nil, nil, fmt.Errorf("missing node as command-line argument")
   110  	}
   111  	n, err := parseNode(ctx.Args()[0])
   112  	if err != nil {
   113  		return nil, nil, err
   114  	}
   115  	var bootnodes []*enode.Node
   116  	if commandHasFlag(ctx, bootnodesFlag) {
   117  		bootnodes, err = parseBootnodes(ctx)
   118  		if err != nil {
   119  			return nil, nil, err
   120  		}
   121  	}
   122  	disc, err := startV4(bootnodes)
   123  	return n, disc, err
   124  }
   125  
   126  func parseBootnodes(ctx *cli.Context) ([]*enode.Node, error) {
   127  	s := params.RinkebyBootnodes
   128  	if ctx.IsSet(bootnodesFlag.Name) {
   129  		s = strings.Split(ctx.String(bootnodesFlag.Name), ",")
   130  	}
   131  	nodes := make([]*enode.Node, len(s))
   132  	var err error
   133  	for i, record := range s {
   134  		nodes[i], err = parseNode(record)
   135  		if err != nil {
   136  			return nil, fmt.Errorf("invalid bootstrap node: %v", err)
   137  		}
   138  	}
   139  	return nodes, nil
   140  }
   141  
   142  // commandHasFlag returns true if the current command supports the given flag.
   143  func commandHasFlag(ctx *cli.Context, flag cli.Flag) bool {
   144  	flags := ctx.FlagNames()
   145  	sort.Strings(flags)
   146  	i := sort.SearchStrings(flags, flag.GetName())
   147  	return i != len(flags) && flags[i] == flag.GetName()
   148  }
   149  
   150  // startV4 starts an ephemeral discovery V4 node.
   151  func startV4(bootnodes []*enode.Node) (*discover.UDPv4, error) {
   152  	var cfg discover.Config
   153  	cfg.Bootnodes = bootnodes
   154  	cfg.PrivateKey, _ = crypto.GenerateKey()
   155  	db, _ := enode.OpenDB("")
   156  	ln := enode.NewLocalNode(db, cfg.PrivateKey)
   157  
   158  	socket, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IP{0, 0, 0, 0}})
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  	addr := socket.LocalAddr().(*net.UDPAddr)
   163  	ln.SetFallbackIP(net.IP{127, 0, 0, 1})
   164  	ln.SetFallbackUDP(addr.Port)
   165  	return discover.ListenUDP(socket, ln, cfg)
   166  }