github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/bootnode/main.go (about) 1 /* 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 18 // Command bootnode runs a bootstrap node for the Discovery Protocol. 19 package main 20 21 import ( 22 "crypto/ecdsa" 23 "encoding/hex" 24 "flag" 25 "fmt" 26 "io/ioutil" 27 "log" 28 "os" 29 30 "github.com/jonasnick/go-ethereum/crypto" 31 "github.com/jonasnick/go-ethereum/logger" 32 "github.com/jonasnick/go-ethereum/p2p/discover" 33 "github.com/jonasnick/go-ethereum/p2p/nat" 34 ) 35 36 func main() { 37 var ( 38 listenAddr = flag.String("addr", ":30301", "listen address") 39 genKey = flag.String("genkey", "", "generate a node key and quit") 40 nodeKeyFile = flag.String("nodekey", "", "private key filename") 41 nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)") 42 natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)") 43 44 nodeKey *ecdsa.PrivateKey 45 err error 46 ) 47 flag.Parse() 48 logger.AddLogSystem(logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.DebugLevel)) 49 50 if *genKey != "" { 51 writeKey(*genKey) 52 os.Exit(0) 53 } 54 55 natm, err := nat.Parse(*natdesc) 56 if err != nil { 57 log.Fatalf("-nat: %v", err) 58 } 59 switch { 60 case *nodeKeyFile == "" && *nodeKeyHex == "": 61 log.Fatal("Use -nodekey or -nodekeyhex to specify a private key") 62 case *nodeKeyFile != "" && *nodeKeyHex != "": 63 log.Fatal("Options -nodekey and -nodekeyhex are mutually exclusive") 64 case *nodeKeyFile != "": 65 if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil { 66 log.Fatalf("-nodekey: %v", err) 67 } 68 case *nodeKeyHex != "": 69 if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil { 70 log.Fatalf("-nodekeyhex: %v", err) 71 } 72 } 73 74 if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm); err != nil { 75 log.Fatal(err) 76 } 77 select {} 78 } 79 80 func writeKey(target string) { 81 key, err := crypto.GenerateKey() 82 if err != nil { 83 log.Fatal("could not generate key: %v", err) 84 } 85 b := crypto.FromECDSA(key) 86 if target == "-" { 87 fmt.Println(hex.EncodeToString(b)) 88 } else { 89 if err := ioutil.WriteFile(target, b, 0600); err != nil { 90 log.Fatal("write error: ", err) 91 } 92 } 93 }