github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/cmd/bootnode/_main_old.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 "os" 24 25 "github.com/ethereum/go-ethereum/cmd/utils" 26 "github.com/ethereum/go-ethereum/crypto" 27 "github.com/ethereum/go-ethereum/log" 28 "github.com/ethereum/go-ethereum/p2p/discover" 29 "github.com/ethereum/go-ethereum/p2p/discv5" 30 "github.com/ethereum/go-ethereum/p2p/nat" 31 "github.com/ethereum/go-ethereum/p2p/netutil" 32 ) 33 34 func main() { 35 var ( 36 listenAddr = flag.String("addr", ":30301", "listen address") 37 genKey = flag.String("genkey", "", "generate a node key") 38 writeAddr = flag.Bool("writeaddress", false, "write out the node's pubkey hash and quit") 39 nodeKeyFile = flag.String("nodekey", "", "private key filename") 40 nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)") 41 natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)") 42 netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)") 43 runv5 = flag.Bool("v5", false, "run a v5 topic discovery bootnode") 44 verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)") 45 vmodule = flag.String("vmodule", "", "log verbosity pattern") 46 47 nodeKey *ecdsa.PrivateKey 48 err error 49 ) 50 flag.Parse() 51 52 glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false))) 53 glogger.Verbosity(log.Lvl(*verbosity)) 54 glogger.Vmodule(*vmodule) 55 log.Root().SetHandler(glogger) 56 57 natm, err := nat.Parse(*natdesc) 58 if err != nil { 59 utils.Fatalf("-nat: %v", err) 60 } 61 switch { 62 case *genKey != "": 63 nodeKey, err = crypto.GenerateKey() 64 if err != nil { 65 utils.Fatalf("could not generate key: %v", err) 66 } 67 if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil { 68 utils.Fatalf("%v", err) 69 } 70 return 71 case *nodeKeyFile == "" && *nodeKeyHex == "": 72 utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key") 73 case *nodeKeyFile != "" && *nodeKeyHex != "": 74 utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive") 75 case *nodeKeyFile != "": 76 if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil { 77 utils.Fatalf("-nodekey: %v", err) 78 } 79 case *nodeKeyHex != "": 80 if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil { 81 utils.Fatalf("-nodekeyhex: %v", err) 82 } 83 } 84 85 if *writeAddr { 86 //fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey)) 87 os.Exit(0) 88 } 89 90 var restrictList *netutil.Netlist 91 if *netrestrict != "" { 92 restrictList, err = netutil.ParseNetlist(*netrestrict) 93 if err != nil { 94 utils.Fatalf("-netrestrict: %v", err) 95 } 96 } 97 98 if *runv5 { 99 if _, err := discv5.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil { 100 utils.Fatalf("%v", err) 101 } 102 } else { 103 if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil { 104 utils.Fatalf("%v", err) 105 } 106 } 107 108 select {} 109 }