github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/go-ethereum/cmd/bootnode/main.go (about) 1 // Copyright 2015 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 // bootnode runs a bootstrap node for the Ethereum Discovery Protocol. 18 package main 19 20 import ( 21 "crypto/ecdsa" 22 "flag" 23 "fmt" 24 "net" 25 "os" 26 27 "github.com/ethereum/go-ethereum/cmd/utils" 28 "github.com/ethereum/go-ethereum/crypto" 29 "github.com/ethereum/go-ethereum/log" 30 "github.com/ethereum/go-ethereum/p2p/discover" 31 "github.com/ethereum/go-ethereum/p2p/discv5" 32 "github.com/ethereum/go-ethereum/p2p/enode" 33 "github.com/ethereum/go-ethereum/p2p/nat" 34 "github.com/ethereum/go-ethereum/p2p/netutil" 35 ) 36 37 func main() { 38 var ( 39 listenAddr = flag.String("addr", ":30301", "listen address") 40 genKey = flag.String("genkey", "", "generate a node key") 41 writeAddr = flag.Bool("writeaddress", false, "write out the node's public key and quit") 42 nodeKeyFile = flag.String("nodekey", "", "private key filename") 43 nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)") 44 natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)") 45 netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)") 46 runv5 = flag.Bool("v5", false, "run a v5 topic discovery bootnode") 47 verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)") 48 vmodule = flag.String("vmodule", "", "log verbosity pattern") 49 50 nodeKey *ecdsa.PrivateKey 51 err error 52 ) 53 flag.Parse() 54 55 glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false))) 56 glogger.Verbosity(log.Lvl(*verbosity)) 57 glogger.Vmodule(*vmodule) 58 log.Root().SetHandler(glogger) 59 60 natm, err := nat.Parse(*natdesc) 61 if err != nil { 62 utils.Fatalf("-nat: %v", err) 63 } 64 switch { 65 case *genKey != "": 66 nodeKey, err = crypto.GenerateKey() 67 if err != nil { 68 utils.Fatalf("could not generate key: %v", err) 69 } 70 if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil { 71 utils.Fatalf("%v", err) 72 } 73 return 74 case *nodeKeyFile == "" && *nodeKeyHex == "": 75 utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key") 76 case *nodeKeyFile != "" && *nodeKeyHex != "": 77 utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive") 78 case *nodeKeyFile != "": 79 if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil { 80 utils.Fatalf("-nodekey: %v", err) 81 } 82 case *nodeKeyHex != "": 83 if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil { 84 utils.Fatalf("-nodekeyhex: %v", err) 85 } 86 } 87 88 if *writeAddr { 89 fmt.Printf("%x\n", crypto.FromECDSAPub(&nodeKey.PublicKey)[1:]) 90 os.Exit(0) 91 } 92 93 var restrictList *netutil.Netlist 94 if *netrestrict != "" { 95 restrictList, err = netutil.ParseNetlist(*netrestrict) 96 if err != nil { 97 utils.Fatalf("-netrestrict: %v", err) 98 } 99 } 100 101 addr, err := net.ResolveUDPAddr("udp", *listenAddr) 102 if err != nil { 103 utils.Fatalf("-ResolveUDPAddr: %v", err) 104 } 105 conn, err := net.ListenUDP("udp", addr) 106 if err != nil { 107 utils.Fatalf("-ListenUDP: %v", err) 108 } 109 110 realaddr := conn.LocalAddr().(*net.UDPAddr) 111 if natm != nil { 112 if !realaddr.IP.IsLoopback() { 113 go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery") 114 } 115 if ext, err := natm.ExternalIP(); err == nil { 116 realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port} 117 } 118 } 119 120 printNotice(&nodeKey.PublicKey, *realaddr) 121 122 if *runv5 { 123 if _, err := discv5.ListenUDP(nodeKey, conn, "", restrictList); err != nil { 124 utils.Fatalf("%v", err) 125 } 126 } else { 127 db, _ := enode.OpenDB("") 128 ln := enode.NewLocalNode(db, nodeKey) 129 cfg := discover.Config{ 130 PrivateKey: nodeKey, 131 NetRestrict: restrictList, 132 } 133 if _, err := discover.ListenUDP(conn, ln, cfg); err != nil { 134 utils.Fatalf("%v", err) 135 } 136 } 137 138 select {} 139 } 140 141 func printNotice(nodeKey *ecdsa.PublicKey, addr net.UDPAddr) { 142 if addr.IP.IsUnspecified() { 143 addr.IP = net.IP{127, 0, 0, 1} 144 } 145 n := enode.NewV4(nodeKey, addr.IP, 0, addr.Port) 146 fmt.Println(n.URLv4()) 147 fmt.Println("Note: you're using cmd/bootnode, a developer tool.") 148 fmt.Println("We recommend using a regular node as bootstrap node for production deployments.") 149 }