github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/cmd/bootnode/main.go (about) 1 package main 2 3 import ( 4 "crypto/ecdsa" 5 "flag" 6 "fmt" 7 "os" 8 // "compress/gzip" 9 // "fmt" 10 "io" 11 // "os" 12 // "os/signal" 13 "runtime" 14 // "strings" 15 16 "github.com/ethereum/go-ethereum/crypto" 17 "github.com/sixexorg/magnetic-ring/log" 18 19 "github.com/sixexorg/magnetic-ring/p2pserver/discover" 20 "github.com/ethereum/go-ethereum/p2p/nat" 21 "github.com/ethereum/go-ethereum/p2p/netutil" 22 "github.com/sixexorg/magnetic-ring/p2pserver/common" 23 ) 24 25 // Fatalf formats a message to standard error and exits the program. 26 // The message is also printed to standard output if standard error 27 // is redirected to a different file. 28 func Fatalf(format string, args ...interface{}) { 29 w := io.MultiWriter(os.Stdout, os.Stderr) 30 if runtime.GOOS == "windows" { 31 // The SameFile check below doesn't work on Windows. 32 // stdout is unlikely to get redirected though, so just print there. 33 w = os.Stdout 34 } else { 35 outf, _ := os.Stdout.Stat() 36 errf, _ := os.Stderr.Stat() 37 if outf != nil && errf != nil && os.SameFile(outf, errf) { 38 w = os.Stderr 39 } 40 } 41 fmt.Fprintf(w, "Fatal: "+format+"\n", args...) 42 os.Exit(1) 43 } 44 45 func main() { 46 log.InitMagneticLog("./log") 47 var ( 48 listenAddr = flag.String("addr", "0.0.0.0:30303", "listen address") 49 genKey = flag.String("genkey", "", "generate a node key") 50 writeAddr = flag.Bool("writeaddress", false, "write out the node's pubkey hash and quit") 51 nodeKeyFile = flag.String("nodekey", "", "private key filename") 52 nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)") 53 natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)") 54 netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)") 55 verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)") 56 vmodule = flag.String("vmodule", "", "log verbosity pattern") 57 58 nodeKey *ecdsa.PrivateKey 59 err error 60 ) 61 flag.Parse() 62 63 common.InitP2PVariable() 64 65 glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false))) 66 glogger.Verbosity(log.Lvl(*verbosity)) 67 glogger.Vmodule(*vmodule) 68 log.Root().SetHandler(glogger) 69 70 natm, err := nat.Parse(*natdesc) 71 if err != nil { 72 Fatalf("-nat: %v", err) 73 } 74 switch { 75 case *genKey != "": 76 nodeKey, err = crypto.GenerateKey() 77 if err != nil { 78 Fatalf("could not generate key: %v", err) 79 } 80 if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil { 81 Fatalf("%v", err) 82 } 83 return 84 case *nodeKeyFile == "" && *nodeKeyHex == "": 85 Fatalf("Use -nodekey or -nodekeyhex to specify a private key") 86 case *nodeKeyFile != "" && *nodeKeyHex != "": 87 Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive") 88 case *nodeKeyFile != "": 89 if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil { 90 Fatalf("-nodekey: %v", err) 91 } 92 case *nodeKeyHex != "": 93 if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil { 94 Fatalf("-nodekeyhex: %v", err) 95 } 96 } 97 98 if *writeAddr { 99 fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey)) 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 Fatalf("-netrestrict: %v", err) 108 } 109 } 110 111 if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList,nil,nil,nil,true,common.StellarNodeID); err != nil { 112 Fatalf("%v", err) 113 } 114 115 select {} 116 }