github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/cmd/bootnode/main.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  // bootnode runs a bootstrap node for the Ethereum Discovery Protocol.
    19  package main
    20  
    21  import (
    22  	"crypto/ecdsa"
    23  	"flag"
    24  	"fmt"
    25  	"net"
    26  	"os"
    27  
    28  	"github.com/AigarNetwork/aigar/cmd/utils"
    29  	"github.com/AigarNetwork/aigar/crypto"
    30  	"github.com/AigarNetwork/aigar/log"
    31  	"github.com/AigarNetwork/aigar/p2p/discover"
    32  	"github.com/AigarNetwork/aigar/p2p/discv5"
    33  	"github.com/AigarNetwork/aigar/p2p/enode"
    34  	"github.com/AigarNetwork/aigar/p2p/nat"
    35  	"github.com/AigarNetwork/aigar/p2p/netutil"
    36  )
    37  
    38  func main() {
    39  	var (
    40  		listenAddr  = flag.String("addr", ":30301", "listen address")
    41  		genKey      = flag.String("genkey", "", "generate a node key")
    42  		writeAddr   = flag.Bool("writeaddress", false, "write out the node's public key and quit")
    43  		nodeKeyFile = flag.String("nodekey", "", "private key filename")
    44  		nodeKeyHex  = flag.String("nodekeyhex", "", "private key as hex (for testing)")
    45  		natdesc     = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
    46  		netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
    47  		runv5       = flag.Bool("v5", false, "run a v5 topic discovery bootnode")
    48  		verbosity   = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)")
    49  		vmodule     = flag.String("vmodule", "", "log verbosity pattern")
    50  
    51  		nodeKey *ecdsa.PrivateKey
    52  		err     error
    53  	)
    54  	flag.Parse()
    55  
    56  	glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
    57  	glogger.Verbosity(log.Lvl(*verbosity))
    58  	glogger.Vmodule(*vmodule)
    59  	log.Root().SetHandler(glogger)
    60  
    61  	natm, err := nat.Parse(*natdesc)
    62  	if err != nil {
    63  		utils.Fatalf("-nat: %v", err)
    64  	}
    65  	switch {
    66  	case *genKey != "":
    67  		nodeKey, err = crypto.GenerateKey()
    68  		if err != nil {
    69  			utils.Fatalf("could not generate key: %v", err)
    70  		}
    71  		if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
    72  			utils.Fatalf("%v", err)
    73  		}
    74  		if !*writeAddr {
    75  			return
    76  		}
    77  	case *nodeKeyFile == "" && *nodeKeyHex == "":
    78  		utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
    79  	case *nodeKeyFile != "" && *nodeKeyHex != "":
    80  		utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
    81  	case *nodeKeyFile != "":
    82  		if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
    83  			utils.Fatalf("-nodekey: %v", err)
    84  		}
    85  	case *nodeKeyHex != "":
    86  		if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
    87  			utils.Fatalf("-nodekeyhex: %v", err)
    88  		}
    89  	}
    90  
    91  	if *writeAddr {
    92  		fmt.Printf("%x\n", crypto.FromECDSAPub(&nodeKey.PublicKey)[1:])
    93  		os.Exit(0)
    94  	}
    95  
    96  	var restrictList *netutil.Netlist
    97  	if *netrestrict != "" {
    98  		restrictList, err = netutil.ParseNetlist(*netrestrict)
    99  		if err != nil {
   100  			utils.Fatalf("-netrestrict: %v", err)
   101  		}
   102  	}
   103  
   104  	addr, err := net.ResolveUDPAddr("udp", *listenAddr)
   105  	if err != nil {
   106  		utils.Fatalf("-ResolveUDPAddr: %v", err)
   107  	}
   108  	conn, err := net.ListenUDP("udp", addr)
   109  	if err != nil {
   110  		utils.Fatalf("-ListenUDP: %v", err)
   111  	}
   112  
   113  	realaddr := conn.LocalAddr().(*net.UDPAddr)
   114  	if natm != nil {
   115  		if !realaddr.IP.IsLoopback() {
   116  			go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery")
   117  		}
   118  		if ext, err := natm.ExternalIP(); err == nil {
   119  			realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
   120  		}
   121  	}
   122  
   123  	printNotice(&nodeKey.PublicKey, *realaddr)
   124  
   125  	if *runv5 {
   126  		if _, err := discv5.ListenUDP(nodeKey, conn, "", restrictList); err != nil {
   127  			utils.Fatalf("%v", err)
   128  		}
   129  	} else {
   130  		db, _ := enode.OpenDB("")
   131  		ln := enode.NewLocalNode(db, nodeKey)
   132  		cfg := discover.Config{
   133  			PrivateKey:  nodeKey,
   134  			NetRestrict: restrictList,
   135  		}
   136  		if _, err := discover.ListenUDP(conn, ln, cfg); err != nil {
   137  			utils.Fatalf("%v", err)
   138  		}
   139  	}
   140  
   141  	select {}
   142  }
   143  
   144  func printNotice(nodeKey *ecdsa.PublicKey, addr net.UDPAddr) {
   145  	if addr.IP.IsUnspecified() {
   146  		addr.IP = net.IP{127, 0, 0, 1}
   147  	}
   148  	n := enode.NewV4(nodeKey, addr.IP, 0, addr.Port)
   149  	fmt.Println(n.URLv4())
   150  	fmt.Println("Note: you're using cmd/bootnode, a developer tool.")
   151  	fmt.Println("We recommend using a regular node as bootstrap node for production deployments.")
   152  }