github.com/guiltylotus/go-ethereum@v1.9.7/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 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 crawlTimeoutFlag = cli.DurationFlag{ 85 Name: "timeout", 86 Usage: "Time limit for the crawl.", 87 Value: 30 * time.Minute, 88 } 89 ) 90 91 func discv4Ping(ctx *cli.Context) error { 92 n := getNodeArg(ctx) 93 disc := startV4(ctx) 94 defer disc.Close() 95 96 start := time.Now() 97 if err := disc.Ping(n); err != nil { 98 return fmt.Errorf("node didn't respond: %v", err) 99 } 100 fmt.Printf("node responded to ping (RTT %v).\n", time.Since(start)) 101 return nil 102 } 103 104 func discv4RequestRecord(ctx *cli.Context) error { 105 n := getNodeArg(ctx) 106 disc := startV4(ctx) 107 defer disc.Close() 108 109 respN, err := disc.RequestENR(n) 110 if err != nil { 111 return fmt.Errorf("can't retrieve record: %v", err) 112 } 113 fmt.Println(respN.String()) 114 return nil 115 } 116 117 func discv4Resolve(ctx *cli.Context) error { 118 n := getNodeArg(ctx) 119 disc := startV4(ctx) 120 defer disc.Close() 121 122 fmt.Println(disc.Resolve(n).String()) 123 return nil 124 } 125 126 func discv4ResolveJSON(ctx *cli.Context) error { 127 if ctx.NArg() < 1 { 128 return fmt.Errorf("need nodes file as argument") 129 } 130 nodesFile := ctx.Args().Get(0) 131 inputSet := make(nodeSet) 132 if common.FileExist(nodesFile) { 133 inputSet = loadNodesJSON(nodesFile) 134 } 135 136 // Add extra nodes from command line arguments. 137 var nodeargs []*enode.Node 138 for i := 1; i < ctx.NArg(); i++ { 139 n, err := parseNode(ctx.Args().Get(i)) 140 if err != nil { 141 exit(err) 142 } 143 nodeargs = append(nodeargs, n) 144 } 145 146 // Run the crawler. 147 disc := startV4(ctx) 148 defer disc.Close() 149 c := newCrawler(inputSet, disc, enode.IterNodes(nodeargs)) 150 c.revalidateInterval = 0 151 output := c.run(0) 152 writeNodesJSON(nodesFile, output) 153 return nil 154 } 155 156 func discv4Crawl(ctx *cli.Context) error { 157 if ctx.NArg() < 1 { 158 return fmt.Errorf("need nodes file as argument") 159 } 160 nodesFile := ctx.Args().First() 161 var inputSet nodeSet 162 if common.FileExist(nodesFile) { 163 inputSet = loadNodesJSON(nodesFile) 164 } 165 166 disc := startV4(ctx) 167 defer disc.Close() 168 c := newCrawler(inputSet, disc, disc.RandomNodes()) 169 c.revalidateInterval = 10 * time.Minute 170 output := c.run(ctx.Duration(crawlTimeoutFlag.Name)) 171 writeNodesJSON(nodesFile, output) 172 return nil 173 } 174 175 func parseBootnodes(ctx *cli.Context) ([]*enode.Node, error) { 176 s := params.RinkebyBootnodes 177 if ctx.IsSet(bootnodesFlag.Name) { 178 s = strings.Split(ctx.String(bootnodesFlag.Name), ",") 179 } 180 nodes := make([]*enode.Node, len(s)) 181 var err error 182 for i, record := range s { 183 nodes[i], err = parseNode(record) 184 if err != nil { 185 return nil, fmt.Errorf("invalid bootstrap node: %v", err) 186 } 187 } 188 return nodes, nil 189 } 190 191 // startV4 starts an ephemeral discovery V4 node. 192 func startV4(ctx *cli.Context) *discover.UDPv4 { 193 socket, ln, cfg, err := listen() 194 if err != nil { 195 exit(err) 196 } 197 if commandHasFlag(ctx, bootnodesFlag) { 198 bn, err := parseBootnodes(ctx) 199 if err != nil { 200 exit(err) 201 } 202 cfg.Bootnodes = bn 203 } 204 disc, err := discover.ListenV4(socket, ln, cfg) 205 if err != nil { 206 exit(err) 207 } 208 return disc 209 } 210 211 func listen() (*net.UDPConn, *enode.LocalNode, discover.Config, error) { 212 var cfg discover.Config 213 cfg.PrivateKey, _ = crypto.GenerateKey() 214 db, _ := enode.OpenDB("") 215 ln := enode.NewLocalNode(db, cfg.PrivateKey) 216 217 socket, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IP{0, 0, 0, 0}}) 218 if err != nil { 219 db.Close() 220 return nil, nil, cfg, err 221 } 222 addr := socket.LocalAddr().(*net.UDPAddr) 223 ln.SetFallbackIP(net.IP{127, 0, 0, 1}) 224 ln.SetFallbackUDP(addr.Port) 225 return socket, ln, cfg, nil 226 }