github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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/atheioschain/go-atheios/cmd/utils"
    27  	"github.com/atheioschain/go-atheios/crypto"
    28  	"github.com/atheioschain/go-atheios/logger/glog"
    29  	"github.com/atheioschain/go-atheios/p2p/discover"
    30  	"github.com/atheioschain/go-atheios/p2p/discv5"
    31  	"github.com/atheioschain/go-atheios/p2p/nat"
    32  	"github.com/atheioschain/go-atheios/p2p/netutil"
    33  )
    34  
    35  func main() {
    36  	var (
    37  		listenAddr  = flag.String("addr", ":30697", "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  
    46  		nodeKey *ecdsa.PrivateKey
    47  		err     error
    48  	)
    49  	flag.Var(glog.GetVerbosity(), "verbosity", "log verbosity (0-9)")
    50  	flag.Var(glog.GetVModule(), "vmodule", "log verbosity pattern")
    51  	glog.SetToStderr(true)
    52  	flag.Parse()
    53  
    54  	natm, err := nat.Parse(*natdesc)
    55  	if err != nil {
    56  		utils.Fatalf("-nat: %v", err)
    57  	}
    58  	switch {
    59  	case *genKey != "":
    60  		nodeKey, err = crypto.GenerateKey()
    61  		if err != nil {
    62  			utils.Fatalf("could not generate key: %v", err)
    63  		}
    64  		if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
    65  			utils.Fatalf("%v", err)
    66  		}
    67  	case *nodeKeyFile == "" && *nodeKeyHex == "":
    68  		utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
    69  	case *nodeKeyFile != "" && *nodeKeyHex != "":
    70  		utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
    71  	case *nodeKeyFile != "":
    72  		if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
    73  			utils.Fatalf("-nodekey: %v", err)
    74  		}
    75  	case *nodeKeyHex != "":
    76  		if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
    77  			utils.Fatalf("-nodekeyhex: %v", err)
    78  		}
    79  	}
    80  
    81  	if *writeAddr {
    82  		fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey))
    83  		os.Exit(0)
    84  	}
    85  
    86  	var restrictList *netutil.Netlist
    87  	if *netrestrict != "" {
    88  		restrictList, err = netutil.ParseNetlist(*netrestrict)
    89  		if err != nil {
    90  			utils.Fatalf("-netrestrict: %v", err)
    91  		}
    92  	}
    93  
    94  	if *runv5 {
    95  		if _, err := discv5.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
    96  			utils.Fatalf("%v", err)
    97  		}
    98  	} else {
    99  		if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
   100  			utils.Fatalf("%v", err)
   101  		}
   102  	}
   103  
   104  	select {}
   105  }