| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- // Copyright 2019 The go-ethereum Authors
- // This file is part of go-ethereum.
- //
- // go-ethereum is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // go-ethereum is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
- package main
- import (
- "fmt"
- "net"
- "strings"
- "time"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/p2p/discover"
- "github.com/ethereum/go-ethereum/p2p/enode"
- "github.com/ethereum/go-ethereum/params"
- "gopkg.in/urfave/cli.v1"
- )
- var (
- discv4Command = cli.Command{
- Name: "discv4",
- Usage: "Node Discovery v4 tools",
- Subcommands: []cli.Command{
- discv4PingCommand,
- discv4RequestRecordCommand,
- discv4ResolveCommand,
- discv4ResolveJSONCommand,
- },
- }
- discv4PingCommand = cli.Command{
- Name: "ping",
- Usage: "Sends ping to a node",
- Action: discv4Ping,
- ArgsUsage: "<node>",
- }
- discv4RequestRecordCommand = cli.Command{
- Name: "requestenr",
- Usage: "Requests a node record using EIP-868 enrRequest",
- Action: discv4RequestRecord,
- ArgsUsage: "<node>",
- }
- discv4ResolveCommand = cli.Command{
- Name: "resolve",
- Usage: "Finds a node in the DHT",
- Action: discv4Resolve,
- ArgsUsage: "<node>",
- Flags: []cli.Flag{bootnodesFlag},
- }
- discv4ResolveJSONCommand = cli.Command{
- Name: "resolve-json",
- Usage: "Re-resolves nodes in a nodes.json file",
- Action: discv4ResolveJSON,
- Flags: []cli.Flag{bootnodesFlag},
- ArgsUsage: "<nodes.json file>",
- }
- )
- var bootnodesFlag = cli.StringFlag{
- Name: "bootnodes",
- Usage: "Comma separated nodes used for bootstrapping",
- }
- func discv4Ping(ctx *cli.Context) error {
- n := getNodeArg(ctx)
- disc := startV4(ctx)
- defer disc.Close()
- start := time.Now()
- if err := disc.Ping(n); err != nil {
- return fmt.Errorf("node didn't respond: %v", err)
- }
- fmt.Printf("node responded to ping (RTT %v).\n", time.Since(start))
- return nil
- }
- func discv4RequestRecord(ctx *cli.Context) error {
- n := getNodeArg(ctx)
- disc := startV4(ctx)
- defer disc.Close()
- respN, err := disc.RequestENR(n)
- if err != nil {
- return fmt.Errorf("can't retrieve record: %v", err)
- }
- fmt.Println(respN.String())
- return nil
- }
- func discv4Resolve(ctx *cli.Context) error {
- n := getNodeArg(ctx)
- disc := startV4(ctx)
- defer disc.Close()
- fmt.Println(disc.Resolve(n).String())
- return nil
- }
- func discv4ResolveJSON(ctx *cli.Context) error {
- if ctx.NArg() < 1 {
- return fmt.Errorf("need nodes file as argument")
- }
- disc := startV4(ctx)
- defer disc.Close()
- file := ctx.Args().Get(0)
- // Load existing nodes in file.
- var nodes []*enode.Node
- if common.FileExist(file) {
- nodes = loadNodesJSON(file).nodes()
- }
- // Add nodes from command line arguments.
- for i := 1; i < ctx.NArg(); i++ {
- n, err := parseNode(ctx.Args().Get(i))
- if err != nil {
- exit(err)
- }
- nodes = append(nodes, n)
- }
- result := make(nodeSet, len(nodes))
- for _, n := range nodes {
- n = disc.Resolve(n)
- result[n.ID()] = nodeJSON{Seq: n.Seq(), N: n}
- }
- writeNodesJSON(file, result)
- return nil
- }
- func parseBootnodes(ctx *cli.Context) ([]*enode.Node, error) {
- s := params.RinkebyBootnodes
- if ctx.IsSet(bootnodesFlag.Name) {
- s = strings.Split(ctx.String(bootnodesFlag.Name), ",")
- }
- nodes := make([]*enode.Node, len(s))
- var err error
- for i, record := range s {
- nodes[i], err = parseNode(record)
- if err != nil {
- return nil, fmt.Errorf("invalid bootstrap node: %v", err)
- }
- }
- return nodes, nil
- }
- // startV4 starts an ephemeral discovery V4 node.
- func startV4(ctx *cli.Context) *discover.UDPv4 {
- socket, ln, cfg, err := listen()
- if err != nil {
- exit(err)
- }
- if commandHasFlag(ctx, bootnodesFlag) {
- bn, err := parseBootnodes(ctx)
- if err != nil {
- exit(err)
- }
- cfg.Bootnodes = bn
- }
- disc, err := discover.ListenV4(socket, ln, cfg)
- if err != nil {
- exit(err)
- }
- return disc
- }
- func listen() (*net.UDPConn, *enode.LocalNode, discover.Config, error) {
- var cfg discover.Config
- cfg.PrivateKey, _ = crypto.GenerateKey()
- db, _ := enode.OpenDB("")
- ln := enode.NewLocalNode(db, cfg.PrivateKey)
- socket, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IP{0, 0, 0, 0}})
- if err != nil {
- db.Close()
- return nil, nil, cfg, err
- }
- addr := socket.LocalAddr().(*net.UDPAddr)
- ln.SetFallbackIP(net.IP{127, 0, 0, 1})
- ln.SetFallbackUDP(addr.Port)
- return socket, ln, cfg, nil
- }
|