github.com/devfans/go-ethereum@v1.5.10-0.20170326212234-7419d0c38291/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  	"os"
    25  
    26  	"github.com/ethereum/go-ethereum/cmd/utils"
    27  	"github.com/ethereum/go-ethereum/crypto"
    28  	"github.com/ethereum/go-ethereum/log"
    29  	"github.com/ethereum/go-ethereum/p2p/discover"
    30  	"github.com/ethereum/go-ethereum/p2p/discv5"
    31  	"github.com/ethereum/go-ethereum/p2p/nat"
    32  	"github.com/ethereum/go-ethereum/p2p/netutil"
    33  )
    34  
    35  func main() {
    36  	var (
    37  		listenAddr  = flag.String("addr", ":30301", "listen address")
    38  		genKey      = flag.String("genkey", "", "generate a node key")
    39  		writeAddr   = flag.Bool("writeaddress", false, "write out the node's pubkey hash 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  		netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
    44  		runv5       = flag.Bool("v5", false, "run a v5 topic discovery bootnode")
    45  		verbosity   = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)")
    46  		vmodule     = flag.String("vmodule", "", "log verbosity pattern")
    47  
    48  		nodeKey *ecdsa.PrivateKey
    49  		err     error
    50  	)
    51  	flag.Parse()
    52  
    53  	glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
    54  	glogger.Verbosity(log.Lvl(*verbosity))
    55  	glogger.Vmodule(*vmodule)
    56  	log.Root().SetHandler(glogger)
    57  
    58  	natm, err := nat.Parse(*natdesc)
    59  	if err != nil {
    60  		utils.Fatalf("-nat: %v", err)
    61  	}
    62  	switch {
    63  	case *genKey != "":
    64  		nodeKey, err = crypto.GenerateKey()
    65  		if err != nil {
    66  			utils.Fatalf("could not generate key: %v", err)
    67  		}
    68  		if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
    69  			utils.Fatalf("%v", err)
    70  		}
    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  }