github.com/core-coin/go-core/v2@v2.1.9/cmd/devp2p/discv4cmd.go (about) 1 // Copyright 2019 by the Authors 2 // This file is part of go-core. 3 // 4 // go-core 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-core 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-core. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 crand "crypto/rand" 21 "fmt" 22 "net" 23 "strings" 24 "time" 25 26 "gopkg.in/urfave/cli.v1" 27 28 "github.com/core-coin/go-core/v2/cmd/devp2p/internal/v4test" 29 "github.com/core-coin/go-core/v2/common" 30 "github.com/core-coin/go-core/v2/crypto" 31 "github.com/core-coin/go-core/v2/p2p/discover" 32 "github.com/core-coin/go-core/v2/p2p/enode" 33 "github.com/core-coin/go-core/v2/params" 34 ) 35 36 var ( 37 discv4Command = cli.Command{ 38 Name: "discv4", 39 Usage: "Node Discovery v4 tools", 40 Subcommands: []cli.Command{ 41 discv4PingCommand, 42 discv4RequestRecordCommand, 43 discv4ResolveCommand, 44 discv4ResolveJSONCommand, 45 discv4CrawlCommand, 46 discv4TestCommand, 47 }, 48 } 49 discv4PingCommand = cli.Command{ 50 Name: "ping", 51 Usage: "Sends ping to a node", 52 Action: discv4Ping, 53 ArgsUsage: "<node>", 54 } 55 discv4RequestRecordCommand = cli.Command{ 56 Name: "requestenr", 57 Usage: "Requests a node record using CIP-868 enrRequest", 58 Action: discv4RequestRecord, 59 ArgsUsage: "<node>", 60 } 61 discv4ResolveCommand = cli.Command{ 62 Name: "resolve", 63 Usage: "Finds a node in the DHT", 64 Action: discv4Resolve, 65 ArgsUsage: "<node>", 66 Flags: []cli.Flag{bootnodesFlag}, 67 } 68 discv4ResolveJSONCommand = cli.Command{ 69 Name: "resolve-json", 70 Usage: "Re-resolves nodes in a nodes.json file", 71 Action: discv4ResolveJSON, 72 Flags: []cli.Flag{bootnodesFlag}, 73 ArgsUsage: "<nodes.json file>", 74 } 75 discv4CrawlCommand = cli.Command{ 76 Name: "crawl", 77 Usage: "Updates a nodes.json file with random nodes found in the DHT", 78 Action: discv4Crawl, 79 Flags: []cli.Flag{bootnodesFlag, crawlTimeoutFlag}, 80 } 81 discv4TestCommand = cli.Command{ 82 Name: "test", 83 Usage: "Runs tests against a node", 84 Action: discv4Test, 85 Flags: []cli.Flag{ 86 remoteEnodeFlag, 87 testPatternFlag, 88 testTAPFlag, 89 testListen1Flag, 90 testListen2Flag, 91 }, 92 } 93 ) 94 95 var ( 96 bootnodesFlag = cli.StringFlag{ 97 Name: "bootnodes", 98 Usage: "Comma separated nodes used for bootstrapping", 99 } 100 nodekeyFlag = cli.StringFlag{ 101 Name: "nodekey", 102 Usage: "Hex-encoded node key", 103 } 104 nodedbFlag = cli.StringFlag{ 105 Name: "nodedb", 106 Usage: "Nodes database location", 107 } 108 listenAddrFlag = cli.StringFlag{ 109 Name: "addr", 110 Usage: "Listening address", 111 } 112 crawlTimeoutFlag = cli.DurationFlag{ 113 Name: "timeout", 114 Usage: "Time limit for the crawl.", 115 Value: 30 * time.Minute, 116 } 117 remoteEnodeFlag = cli.StringFlag{ 118 Name: "remote", 119 Usage: "Enode of the remote node under test", 120 EnvVar: "REMOTE_ENODE", 121 } 122 ) 123 124 func discv4Ping(ctx *cli.Context) error { 125 n := getNodeArg(ctx) 126 disc := startV4(ctx) 127 defer disc.Close() 128 129 start := time.Now() 130 if err := disc.Ping(n); err != nil { 131 return fmt.Errorf("node didn't respond: %v", err) 132 } 133 fmt.Printf("node responded to ping (RTT %v).\n", time.Since(start)) 134 return nil 135 } 136 137 func discv4RequestRecord(ctx *cli.Context) error { 138 n := getNodeArg(ctx) 139 disc := startV4(ctx) 140 defer disc.Close() 141 142 respN, err := disc.RequestENR(n) 143 if err != nil { 144 return fmt.Errorf("can't retrieve record: %v", err) 145 } 146 fmt.Println(respN.String()) 147 return nil 148 } 149 150 func discv4Resolve(ctx *cli.Context) error { 151 n := getNodeArg(ctx) 152 disc := startV4(ctx) 153 defer disc.Close() 154 155 fmt.Println(disc.Resolve(n).String()) 156 return nil 157 } 158 159 func discv4ResolveJSON(ctx *cli.Context) error { 160 if ctx.NArg() < 1 { 161 return fmt.Errorf("need nodes file as argument") 162 } 163 nodesFile := ctx.Args().Get(0) 164 inputSet := make(nodeSet) 165 if common.FileExist(nodesFile) { 166 inputSet = loadNodesJSON(nodesFile) 167 } 168 169 // Add extra nodes from command line arguments. 170 var nodeargs []*enode.Node 171 for i := 1; i < ctx.NArg(); i++ { 172 n, err := parseNode(ctx.Args().Get(i)) 173 if err != nil { 174 exit(err) 175 } 176 nodeargs = append(nodeargs, n) 177 } 178 179 // Run the crawler. 180 disc := startV4(ctx) 181 defer disc.Close() 182 c := newCrawler(inputSet, disc, enode.IterNodes(nodeargs)) 183 c.revalidateInterval = 0 184 output := c.run(0) 185 writeNodesJSON(nodesFile, output) 186 return nil 187 } 188 189 func discv4Crawl(ctx *cli.Context) error { 190 if ctx.NArg() < 1 { 191 return fmt.Errorf("need nodes file as argument") 192 } 193 nodesFile := ctx.Args().First() 194 var inputSet nodeSet 195 if common.FileExist(nodesFile) { 196 inputSet = loadNodesJSON(nodesFile) 197 } 198 199 disc := startV4(ctx) 200 defer disc.Close() 201 c := newCrawler(inputSet, disc, disc.RandomNodes()) 202 c.revalidateInterval = 10 * time.Minute 203 output := c.run(ctx.Duration(crawlTimeoutFlag.Name)) 204 writeNodesJSON(nodesFile, output) 205 return nil 206 } 207 208 // discv4Test runs the protocol test suite. 209 func discv4Test(ctx *cli.Context) error { 210 // Configure test package globals. 211 if !ctx.IsSet(remoteEnodeFlag.Name) { 212 return fmt.Errorf("Missing -%v", remoteEnodeFlag.Name) 213 } 214 v4test.Remote = ctx.String(remoteEnodeFlag.Name) 215 v4test.Listen1 = ctx.String(testListen1Flag.Name) 216 v4test.Listen2 = ctx.String(testListen2Flag.Name) 217 return runTests(ctx, v4test.AllTests) 218 } 219 220 // startV4 starts an ephemeral discovery V4 node. 221 func startV4(ctx *cli.Context) *discover.UDPv4 { 222 ln, config := makeDiscoveryConfig(ctx) 223 socket := listen(ln, ctx.String(listenAddrFlag.Name)) 224 disc, err := discover.ListenV4(socket, ln, config) 225 if err != nil { 226 exit(err) 227 } 228 return disc 229 } 230 231 func makeDiscoveryConfig(ctx *cli.Context) (*enode.LocalNode, discover.Config) { 232 var cfg discover.Config 233 234 if ctx.IsSet(nodekeyFlag.Name) { 235 key, err := crypto.UnmarshalPrivateKeyHex(ctx.String(nodekeyFlag.Name)) 236 if err != nil { 237 exit(fmt.Errorf("-%s: %v", nodekeyFlag.Name, err)) 238 } 239 cfg.PrivateKey = key 240 } else { 241 cfg.PrivateKey, _ = crypto.GenerateKey(crand.Reader) 242 } 243 244 if commandHasFlag(ctx, bootnodesFlag) { 245 bn, err := parseBootnodes(ctx) 246 if err != nil { 247 exit(err) 248 } 249 cfg.Bootnodes = bn 250 } 251 252 dbpath := ctx.String(nodedbFlag.Name) 253 db, err := enode.OpenDB(dbpath) 254 if err != nil { 255 exit(err) 256 } 257 ln := enode.NewLocalNode(db, cfg.PrivateKey) 258 return ln, cfg 259 } 260 261 func listen(ln *enode.LocalNode, addr string) *net.UDPConn { 262 if addr == "" { 263 addr = "0.0.0.0:0" 264 } 265 socket, err := net.ListenPacket("udp4", addr) 266 if err != nil { 267 exit(err) 268 } 269 usocket := socket.(*net.UDPConn) 270 uaddr := socket.LocalAddr().(*net.UDPAddr) 271 if uaddr.IP.IsUnspecified() { 272 ln.SetFallbackIP(net.IP{127, 0, 0, 1}) 273 } else { 274 ln.SetFallbackIP(uaddr.IP) 275 } 276 ln.SetFallbackUDP(uaddr.Port) 277 return usocket 278 } 279 280 func parseBootnodes(ctx *cli.Context) ([]*enode.Node, error) { 281 s := params.DevinBootnodes 282 if ctx.IsSet(bootnodesFlag.Name) { 283 input := ctx.String(bootnodesFlag.Name) 284 if input == "" { 285 return nil, nil 286 } 287 s = strings.Split(input, ",") 288 } 289 nodes := make([]*enode.Node, len(s)) 290 var err error 291 for i, record := range s { 292 nodes[i], err = parseNode(record) 293 if err != nil { 294 return nil, fmt.Errorf("invalid bootstrap node: %v", err) 295 } 296 } 297 return nodes, nil 298 }