github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/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" 31 "github.com/ethereum/go-ethereum/p2p/discover" 32 "github.com/ethereum/go-ethereum/p2p/discv5" 33 "github.com/ethereum/go-ethereum/p2p/enode" 34 "github.com/ethereum/go-ethereum/p2p/nat" 35 "github.com/ethereum/go-ethereum/p2p/netutil" 36 ) 37 38 func main() { 39 var ( 40 listenAddr = flag.String("addr", ":30301", "listen address") 41 genKey = flag.String("genkey", "", "generate a node key") 42 writeAddr = flag.Bool("writeaddress", false, "write out the node's public key and quit") 43 nodeKeyFile = flag.String("nodekey", "", "private key filename") 44 nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)") 45 natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)") 46 netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)") 47 runv4 = flag.Bool("v4", true, "run a v4 topic discovery bootnode") 48 runv5 = flag.Bool("v5", false, "run a v5 topic discovery bootnode") 49 verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)") 50 vmodule = flag.String("vmodule", "", "log verbosity pattern") 51 networkId = flag.Uint64("networkid", 0, "network ID") 52 pingIPFromPacket = flag.Bool("ping-ip-from-packet", false, "Has the discovery protocol use the IP address given by a ping packet") 53 54 nodeKey *ecdsa.PrivateKey 55 err error 56 ) 57 flag.Parse() 58 59 glogger := log.NewGlogHandler(log.StreamHandler(os.Stdout, log.JSONFormat())) 60 glogger.Verbosity(log.Lvl(*verbosity)) 61 glogger.Vmodule(*vmodule) 62 log.Root().SetHandler(glogger) 63 64 natm, err := nat.Parse(*natdesc) 65 if err != nil { 66 utils.Fatalf("-nat: %v", err) 67 } 68 switch { 69 case !*runv4 && !*runv5: 70 utils.Fatalf("Must specify at least one discovery protocol (v4 or v5)") 71 case *genKey != "": 72 nodeKey, err = crypto.GenerateKey() 73 if err != nil { 74 utils.Fatalf("could not generate key: %v", err) 75 } 76 if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil { 77 utils.Fatalf("%v", err) 78 } 79 return 80 case *nodeKeyFile == "" && *nodeKeyHex == "": 81 utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key") 82 case *nodeKeyFile != "" && *nodeKeyHex != "": 83 utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive") 84 case *nodeKeyFile != "": 85 if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil { 86 utils.Fatalf("-nodekey: %v", err) 87 } 88 case *nodeKeyHex != "": 89 if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil { 90 utils.Fatalf("-nodekeyhex: %v", err) 91 } 92 case *networkId == 0: 93 utils.Fatalf("--networkid must be set to non zero number") 94 case *runv5: 95 utils.Fatalf("Celo networks currently do not support discovery v5") 96 } 97 98 if *writeAddr { 99 fmt.Printf("%x\n", crypto.FromECDSAPub(&nodeKey.PublicKey)[1:]) 100 os.Exit(0) 101 } 102 103 var restrictList *netutil.Netlist 104 if *netrestrict != "" { 105 restrictList, err = netutil.ParseNetlist(*netrestrict) 106 if err != nil { 107 utils.Fatalf("-netrestrict: %v", err) 108 } 109 } 110 111 addr, err := net.ResolveUDPAddr("udp", *listenAddr) 112 if err != nil { 113 utils.Fatalf("-ResolveUDPAddr: %v", err) 114 } 115 conn, err := net.ListenUDP("udp", addr) 116 if err != nil { 117 utils.Fatalf("-ListenUDP: %v", err) 118 } 119 120 realaddr := conn.LocalAddr().(*net.UDPAddr) 121 if natm != nil { 122 if !realaddr.IP.IsLoopback() { 123 go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery") 124 } 125 // TODO: react to external IP changes over time. 126 if ext, err := natm.ExternalIP(); err == nil { 127 realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port} 128 } 129 } 130 131 // If v4 and v5 are both enabled, a packet is first tried as v4 132 // and then v5 if v4 decoding fails, following the same pattern as full 133 // nodes that use v4 and v5: 134 // https://github.com/celo-org/celo-blockchain/blob/7fbd6f3574f1c1c1e657c152fc63fb771adab3af/p2p/server.go#L588 135 var unhandled chan discover.ReadPacket 136 var sconn *p2p.SharedUDPConn 137 if *runv4 { 138 if *runv5 { 139 unhandled = make(chan discover.ReadPacket, 100) 140 sconn = &p2p.SharedUDPConn{conn, unhandled} 141 } 142 db, _ := enode.OpenDB("") 143 ln := enode.NewLocalNode(db, nodeKey, *networkId) 144 cfg := discover.Config{ 145 PrivateKey: nodeKey, 146 NetRestrict: restrictList, 147 PingIPFromPacket: *pingIPFromPacket, 148 Unhandled: unhandled, 149 } 150 if _, err := discover.ListenUDP(conn, ln, cfg); err != nil { 151 utils.Fatalf("%v", err) 152 } 153 } 154 155 if *runv5 { 156 var err error 157 if sconn != nil { 158 _, err = discv5.ListenUDP(nodeKey, sconn, "", restrictList) 159 } else { 160 _, err = discv5.ListenUDP(nodeKey, conn, "", restrictList) 161 } 162 if err != nil { 163 utils.Fatalf("%v", err) 164 } 165 } 166 167 select {} 168 }