github.com/ylsGit/go-ethereum@v1.6.5/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  		return
    72  	case *nodeKeyFile == "" && *nodeKeyHex == "":
    73  		utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
    74  	case *nodeKeyFile != "" && *nodeKeyHex != "":
    75  		utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
    76  	case *nodeKeyFile != "":
    77  		if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
    78  			utils.Fatalf("-nodekey: %v", err)
    79  		}
    80  	case *nodeKeyHex != "":
    81  		if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
    82  			utils.Fatalf("-nodekeyhex: %v", err)
    83  		}
    84  	}
    85  
    86  	if *writeAddr {
    87  		fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey))
    88  		os.Exit(0)
    89  	}
    90  
    91  	var restrictList *netutil.Netlist
    92  	if *netrestrict != "" {
    93  		restrictList, err = netutil.ParseNetlist(*netrestrict)
    94  		if err != nil {
    95  			utils.Fatalf("-netrestrict: %v", err)
    96  		}
    97  	}
    98  
    99  	if *runv5 {
   100  		if _, err := discv5.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
   101  			utils.Fatalf("%v", err)
   102  		}
   103  	} else {
   104  		if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
   105  			utils.Fatalf("%v", err)
   106  		}
   107  	}
   108  
   109  	select {}
   110  }