2019-06-07 08:29:16 -05:00
|
|
|
// 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"
|
2020-07-07 07:37:33 -05:00
|
|
|
"os"
|
2019-06-07 08:29:16 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-07-07 07:37:33 -05:00
|
|
|
"github.com/ethereum/go-ethereum/cmd/devp2p/internal/v4test"
|
2019-09-25 04:38:13 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-06-07 08:29:16 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-07-07 07:37:33 -05:00
|
|
|
"github.com/ethereum/go-ethereum/internal/utesting"
|
2019-06-07 08:29:16 -05:00
|
|
|
"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,
|
2019-09-25 04:38:13 -05:00
|
|
|
discv4ResolveJSONCommand,
|
2019-10-29 10:08:57 -05:00
|
|
|
discv4CrawlCommand,
|
2020-07-07 07:37:33 -05:00
|
|
|
discv4TestCommand,
|
2019-06-07 08:29:16 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
discv4PingCommand = cli.Command{
|
2019-09-25 04:38:13 -05:00
|
|
|
Name: "ping",
|
|
|
|
Usage: "Sends ping to a node",
|
|
|
|
Action: discv4Ping,
|
|
|
|
ArgsUsage: "<node>",
|
2019-06-07 08:29:16 -05:00
|
|
|
}
|
|
|
|
discv4RequestRecordCommand = cli.Command{
|
2019-09-25 04:38:13 -05:00
|
|
|
Name: "requestenr",
|
|
|
|
Usage: "Requests a node record using EIP-868 enrRequest",
|
|
|
|
Action: discv4RequestRecord,
|
|
|
|
ArgsUsage: "<node>",
|
2019-06-07 08:29:16 -05:00
|
|
|
}
|
|
|
|
discv4ResolveCommand = cli.Command{
|
2019-09-25 04:38:13 -05:00
|
|
|
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>",
|
2019-06-07 08:29:16 -05:00
|
|
|
}
|
2019-10-29 10:08:57 -05:00
|
|
|
discv4CrawlCommand = cli.Command{
|
|
|
|
Name: "crawl",
|
|
|
|
Usage: "Updates a nodes.json file with random nodes found in the DHT",
|
|
|
|
Action: discv4Crawl,
|
|
|
|
Flags: []cli.Flag{bootnodesFlag, crawlTimeoutFlag},
|
|
|
|
}
|
2020-07-07 07:37:33 -05:00
|
|
|
discv4TestCommand = cli.Command{
|
|
|
|
Name: "test",
|
|
|
|
Usage: "Runs tests against a node",
|
|
|
|
Action: discv4Test,
|
|
|
|
Flags: []cli.Flag{remoteEnodeFlag, testPatternFlag, testListen1Flag, testListen2Flag},
|
|
|
|
}
|
2019-06-07 08:29:16 -05:00
|
|
|
)
|
|
|
|
|
2019-10-29 10:08:57 -05:00
|
|
|
var (
|
|
|
|
bootnodesFlag = cli.StringFlag{
|
|
|
|
Name: "bootnodes",
|
|
|
|
Usage: "Comma separated nodes used for bootstrapping",
|
|
|
|
}
|
2020-04-08 02:57:23 -05:00
|
|
|
nodekeyFlag = cli.StringFlag{
|
|
|
|
Name: "nodekey",
|
|
|
|
Usage: "Hex-encoded node key",
|
|
|
|
}
|
|
|
|
nodedbFlag = cli.StringFlag{
|
|
|
|
Name: "nodedb",
|
|
|
|
Usage: "Nodes database location",
|
|
|
|
}
|
|
|
|
listenAddrFlag = cli.StringFlag{
|
|
|
|
Name: "addr",
|
|
|
|
Usage: "Listening address",
|
|
|
|
}
|
2019-10-29 10:08:57 -05:00
|
|
|
crawlTimeoutFlag = cli.DurationFlag{
|
|
|
|
Name: "timeout",
|
|
|
|
Usage: "Time limit for the crawl.",
|
|
|
|
Value: 30 * time.Minute,
|
|
|
|
}
|
2020-07-07 07:37:33 -05:00
|
|
|
remoteEnodeFlag = cli.StringFlag{
|
|
|
|
Name: "remote",
|
|
|
|
Usage: "Enode of the remote node under test",
|
|
|
|
EnvVar: "REMOTE_ENODE",
|
|
|
|
}
|
|
|
|
testPatternFlag = cli.StringFlag{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "Pattern of test suite(s) to run",
|
|
|
|
}
|
|
|
|
testListen1Flag = cli.StringFlag{
|
|
|
|
Name: "listen1",
|
|
|
|
Usage: "IP address of the first tester",
|
|
|
|
Value: v4test.Listen1,
|
|
|
|
}
|
|
|
|
testListen2Flag = cli.StringFlag{
|
|
|
|
Name: "listen2",
|
|
|
|
Usage: "IP address of the second tester",
|
|
|
|
Value: v4test.Listen2,
|
|
|
|
}
|
2019-10-29 10:08:57 -05:00
|
|
|
)
|
2019-06-07 08:29:16 -05:00
|
|
|
|
|
|
|
func discv4Ping(ctx *cli.Context) error {
|
2019-09-25 04:38:13 -05:00
|
|
|
n := getNodeArg(ctx)
|
|
|
|
disc := startV4(ctx)
|
2019-06-07 08:29:16 -05:00
|
|
|
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 {
|
2019-09-25 04:38:13 -05:00
|
|
|
n := getNodeArg(ctx)
|
|
|
|
disc := startV4(ctx)
|
2019-06-07 08:29:16 -05:00
|
|
|
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 {
|
2019-09-25 04:38:13 -05:00
|
|
|
n := getNodeArg(ctx)
|
|
|
|
disc := startV4(ctx)
|
2019-06-07 08:29:16 -05:00
|
|
|
defer disc.Close()
|
|
|
|
|
|
|
|
fmt.Println(disc.Resolve(n).String())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-25 04:38:13 -05:00
|
|
|
func discv4ResolveJSON(ctx *cli.Context) error {
|
|
|
|
if ctx.NArg() < 1 {
|
|
|
|
return fmt.Errorf("need nodes file as argument")
|
2019-06-07 08:29:16 -05:00
|
|
|
}
|
2019-10-29 10:08:57 -05:00
|
|
|
nodesFile := ctx.Args().Get(0)
|
|
|
|
inputSet := make(nodeSet)
|
|
|
|
if common.FileExist(nodesFile) {
|
|
|
|
inputSet = loadNodesJSON(nodesFile)
|
2019-06-07 08:29:16 -05:00
|
|
|
}
|
2019-10-29 10:08:57 -05:00
|
|
|
|
|
|
|
// Add extra nodes from command line arguments.
|
|
|
|
var nodeargs []*enode.Node
|
2019-09-25 04:38:13 -05:00
|
|
|
for i := 1; i < ctx.NArg(); i++ {
|
|
|
|
n, err := parseNode(ctx.Args().Get(i))
|
2019-06-07 08:29:16 -05:00
|
|
|
if err != nil {
|
2019-09-25 04:38:13 -05:00
|
|
|
exit(err)
|
2019-06-07 08:29:16 -05:00
|
|
|
}
|
2019-10-29 10:08:57 -05:00
|
|
|
nodeargs = append(nodeargs, n)
|
2019-09-25 04:38:13 -05:00
|
|
|
}
|
|
|
|
|
2019-10-29 10:08:57 -05:00
|
|
|
// Run the crawler.
|
|
|
|
disc := startV4(ctx)
|
|
|
|
defer disc.Close()
|
|
|
|
c := newCrawler(inputSet, disc, enode.IterNodes(nodeargs))
|
|
|
|
c.revalidateInterval = 0
|
|
|
|
output := c.run(0)
|
|
|
|
writeNodesJSON(nodesFile, output)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func discv4Crawl(ctx *cli.Context) error {
|
|
|
|
if ctx.NArg() < 1 {
|
|
|
|
return fmt.Errorf("need nodes file as argument")
|
|
|
|
}
|
|
|
|
nodesFile := ctx.Args().First()
|
|
|
|
var inputSet nodeSet
|
|
|
|
if common.FileExist(nodesFile) {
|
|
|
|
inputSet = loadNodesJSON(nodesFile)
|
2019-06-07 08:29:16 -05:00
|
|
|
}
|
2019-10-29 10:08:57 -05:00
|
|
|
|
|
|
|
disc := startV4(ctx)
|
|
|
|
defer disc.Close()
|
|
|
|
c := newCrawler(inputSet, disc, disc.RandomNodes())
|
|
|
|
c.revalidateInterval = 10 * time.Minute
|
|
|
|
output := c.run(ctx.Duration(crawlTimeoutFlag.Name))
|
|
|
|
writeNodesJSON(nodesFile, output)
|
2019-09-25 04:38:13 -05:00
|
|
|
return nil
|
2019-06-07 08:29:16 -05:00
|
|
|
}
|
|
|
|
|
2020-07-07 07:37:33 -05:00
|
|
|
func discv4Test(ctx *cli.Context) error {
|
|
|
|
// Configure test package globals.
|
|
|
|
if !ctx.IsSet(remoteEnodeFlag.Name) {
|
|
|
|
return fmt.Errorf("Missing -%v", remoteEnodeFlag.Name)
|
|
|
|
}
|
|
|
|
v4test.Remote = ctx.String(remoteEnodeFlag.Name)
|
|
|
|
v4test.Listen1 = ctx.String(testListen1Flag.Name)
|
|
|
|
v4test.Listen2 = ctx.String(testListen2Flag.Name)
|
|
|
|
|
|
|
|
// Filter and run test cases.
|
|
|
|
tests := v4test.AllTests
|
|
|
|
if ctx.IsSet(testPatternFlag.Name) {
|
|
|
|
tests = utesting.MatchTests(tests, ctx.String(testPatternFlag.Name))
|
|
|
|
}
|
|
|
|
results := utesting.RunTests(tests, os.Stdout)
|
|
|
|
if fails := utesting.CountFailures(results); fails > 0 {
|
|
|
|
return fmt.Errorf("%v/%v tests passed.", len(tests)-fails, len(tests))
|
|
|
|
}
|
|
|
|
fmt.Printf("%v/%v passed\n", len(tests), len(tests))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-25 04:38:13 -05:00
|
|
|
// startV4 starts an ephemeral discovery V4 node.
|
|
|
|
func startV4(ctx *cli.Context) *discover.UDPv4 {
|
2020-04-08 02:57:23 -05:00
|
|
|
ln, config := makeDiscoveryConfig(ctx)
|
|
|
|
socket := listen(ln, ctx.String(listenAddrFlag.Name))
|
|
|
|
disc, err := discover.ListenV4(socket, ln, config)
|
2019-09-25 04:38:13 -05:00
|
|
|
if err != nil {
|
|
|
|
exit(err)
|
|
|
|
}
|
2020-04-08 02:57:23 -05:00
|
|
|
return disc
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeDiscoveryConfig(ctx *cli.Context) (*enode.LocalNode, discover.Config) {
|
|
|
|
var cfg discover.Config
|
|
|
|
|
|
|
|
if ctx.IsSet(nodekeyFlag.Name) {
|
|
|
|
key, err := crypto.HexToECDSA(ctx.String(nodekeyFlag.Name))
|
|
|
|
if err != nil {
|
|
|
|
exit(fmt.Errorf("-%s: %v", nodekeyFlag.Name, err))
|
|
|
|
}
|
|
|
|
cfg.PrivateKey = key
|
|
|
|
} else {
|
|
|
|
cfg.PrivateKey, _ = crypto.GenerateKey()
|
|
|
|
}
|
|
|
|
|
2019-09-25 04:38:13 -05:00
|
|
|
if commandHasFlag(ctx, bootnodesFlag) {
|
|
|
|
bn, err := parseBootnodes(ctx)
|
|
|
|
if err != nil {
|
|
|
|
exit(err)
|
|
|
|
}
|
|
|
|
cfg.Bootnodes = bn
|
|
|
|
}
|
2020-04-08 02:57:23 -05:00
|
|
|
|
|
|
|
dbpath := ctx.String(nodedbFlag.Name)
|
|
|
|
db, err := enode.OpenDB(dbpath)
|
2019-09-25 04:38:13 -05:00
|
|
|
if err != nil {
|
|
|
|
exit(err)
|
|
|
|
}
|
2019-06-07 08:29:16 -05:00
|
|
|
ln := enode.NewLocalNode(db, cfg.PrivateKey)
|
2020-04-08 02:57:23 -05:00
|
|
|
return ln, cfg
|
|
|
|
}
|
2019-06-07 08:29:16 -05:00
|
|
|
|
2020-04-08 02:57:23 -05:00
|
|
|
func listen(ln *enode.LocalNode, addr string) *net.UDPConn {
|
|
|
|
if addr == "" {
|
|
|
|
addr = "0.0.0.0:0"
|
|
|
|
}
|
|
|
|
socket, err := net.ListenPacket("udp4", addr)
|
2019-06-07 08:29:16 -05:00
|
|
|
if err != nil {
|
2020-04-08 02:57:23 -05:00
|
|
|
exit(err)
|
2019-06-07 08:29:16 -05:00
|
|
|
}
|
2020-04-08 02:57:23 -05:00
|
|
|
usocket := socket.(*net.UDPConn)
|
|
|
|
uaddr := socket.LocalAddr().(*net.UDPAddr)
|
2019-06-07 08:29:16 -05:00
|
|
|
ln.SetFallbackIP(net.IP{127, 0, 0, 1})
|
2020-04-08 02:57:23 -05:00
|
|
|
ln.SetFallbackUDP(uaddr.Port)
|
|
|
|
return usocket
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-06-07 08:29:16 -05:00
|
|
|
}
|