github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/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  	"log"
    25  	"os"
    26  
    27  	"github.com/ethereumproject/go-ethereum/crypto"
    28  	"github.com/ethereumproject/go-ethereum/logger/glog"
    29  	"github.com/ethereumproject/go-ethereum/p2p/discover"
    30  	"github.com/ethereumproject/go-ethereum/p2p/nat"
    31  )
    32  
    33  // Version is the application revision identifier. It can be set with the linker
    34  // as in: go build -ldflags "-X main.Version="`git describe --tags`
    35  var Version = "unknown"
    36  
    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  	versionFlag = flag.Bool("version", false, "Prints the revision identifier and exit immediatily.")
    44  )
    45  
    46  // onlyDoGenKey exits 0 if successful.
    47  // It does the -genkey flag feature and that is all.
    48  func onlyDoGenKey() {
    49  	key, err := crypto.GenerateKey()
    50  	if err != nil {
    51  		log.Fatalf("could not generate key: %s", err)
    52  	}
    53  	f, e := os.Create(*genKey)
    54  	defer f.Close()
    55  	if e != nil {
    56  		log.Fatalf("coult not open genkey file: %v", e)
    57  	}
    58  	if _, err := crypto.WriteECDSAKey(f, key); err != nil {
    59  		log.Fatal(err)
    60  	}
    61  	os.Exit(0)
    62  }
    63  
    64  func main() {
    65  	flag.Var(glog.GetVerbosity(), "verbosity", "log verbosity (0-9)")
    66  	flag.Var(glog.GetVModule(), "vmodule", "log verbosity pattern")
    67  	glog.SetToStderr(true)
    68  	flag.Parse()
    69  
    70  	if *versionFlag {
    71  		fmt.Println("bootnode version", Version)
    72  		os.Exit(0)
    73  	}
    74  
    75  	if *genKey != "" {
    76  		// exits 0 if successful
    77  		onlyDoGenKey()
    78  	}
    79  
    80  	natm, err := nat.Parse(*natdesc)
    81  	if err != nil {
    82  		log.Fatalf("nat: %s", err)
    83  	}
    84  
    85  	var nodeKey *ecdsa.PrivateKey
    86  	switch {
    87  	case *nodeKeyFile == "" && *nodeKeyHex == "":
    88  		log.Fatal("Use -nodekey or -nodekeyhex to specify a private key")
    89  	case *nodeKeyFile != "" && *nodeKeyHex != "":
    90  		log.Fatal("Options -nodekey and -nodekeyhex are mutually exclusive")
    91  	case *nodeKeyFile != "":
    92  		f, err := os.Open(*nodeKeyFile)
    93  		if err != nil {
    94  			log.Fatalf("error opening node key file: %v", err)
    95  		}
    96  		nodeKey, err = crypto.LoadECDSA(f)
    97  		if err := f.Close(); err != nil {
    98  			log.Fatalf("error closing key file: %v", err)
    99  		}
   100  		if err != nil {
   101  			log.Fatalf("nodekey: %s", err)
   102  		}
   103  	case *nodeKeyHex != "":
   104  		var err error
   105  		nodeKey, err = crypto.HexToECDSA(*nodeKeyHex)
   106  		if err != nil {
   107  			log.Fatalf("nodekeyhex: %s", err)
   108  		}
   109  	}
   110  
   111  	if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, ""); err != nil {
   112  		log.Fatal(err)
   113  	}
   114  	select {}
   115  }