github.com/theQRL/go-zond@v0.1.1/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  	"net"
    25  	"os"
    26  	"time"
    27  
    28  	"github.com/theQRL/go-zond/cmd/utils"
    29  	"github.com/theQRL/go-zond/crypto"
    30  	"github.com/theQRL/go-zond/log"
    31  	"github.com/theQRL/go-zond/p2p/discover"
    32  	"github.com/theQRL/go-zond/p2p/enode"
    33  	"github.com/theQRL/go-zond/p2p/nat"
    34  	"github.com/theQRL/go-zond/p2p/netutil"
    35  )
    36  
    37  func main() {
    38  	var (
    39  		listenAddr  = flag.String("addr", ":30301", "listen address")
    40  		genKey      = flag.String("genkey", "", "generate a node key")
    41  		writeAddr   = flag.Bool("writeaddress", false, "write out the node's public key and quit")
    42  		nodeKeyFile = flag.String("nodekey", "", "private key filename")
    43  		nodeKeyHex  = flag.String("nodekeyhex", "", "private key as hex (for testing)")
    44  		natdesc     = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|pmp:<IP>|extip:<IP>)")
    45  		netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
    46  		runv5       = flag.Bool("v5", false, "run a v5 topic discovery bootnode")
    47  		verbosity   = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-5)")
    48  		vmodule     = flag.String("vmodule", "", "log verbosity pattern")
    49  
    50  		nodeKey *ecdsa.PrivateKey
    51  		err     error
    52  	)
    53  	flag.Parse()
    54  
    55  	glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
    56  	glogger.Verbosity(log.Lvl(*verbosity))
    57  	glogger.Vmodule(*vmodule)
    58  	log.Root().SetHandler(glogger)
    59  
    60  	natm, err := nat.Parse(*natdesc)
    61  	if err != nil {
    62  		utils.Fatalf("-nat: %v", err)
    63  	}
    64  	switch {
    65  	case *genKey != "":
    66  		nodeKey, err = crypto.GenerateKey()
    67  		if err != nil {
    68  			utils.Fatalf("could not generate key: %v", err)
    69  		}
    70  		if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
    71  			utils.Fatalf("%v", err)
    72  		}
    73  		if !*writeAddr {
    74  			return
    75  		}
    76  	case *nodeKeyFile == "" && *nodeKeyHex == "":
    77  		utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
    78  	case *nodeKeyFile != "" && *nodeKeyHex != "":
    79  		utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
    80  	case *nodeKeyFile != "":
    81  		if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
    82  			utils.Fatalf("-nodekey: %v", err)
    83  		}
    84  	case *nodeKeyHex != "":
    85  		if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
    86  			utils.Fatalf("-nodekeyhex: %v", err)
    87  		}
    88  	}
    89  
    90  	if *writeAddr {
    91  		fmt.Printf("%x\n", crypto.FromECDSAPub(&nodeKey.PublicKey)[1:])
    92  		os.Exit(0)
    93  	}
    94  
    95  	var restrictList *netutil.Netlist
    96  	if *netrestrict != "" {
    97  		restrictList, err = netutil.ParseNetlist(*netrestrict)
    98  		if err != nil {
    99  			utils.Fatalf("-netrestrict: %v", err)
   100  		}
   101  	}
   102  
   103  	addr, err := net.ResolveUDPAddr("udp", *listenAddr)
   104  	if err != nil {
   105  		utils.Fatalf("-ResolveUDPAddr: %v", err)
   106  	}
   107  	conn, err := net.ListenUDP("udp", addr)
   108  	if err != nil {
   109  		utils.Fatalf("-ListenUDP: %v", err)
   110  	}
   111  	defer conn.Close()
   112  
   113  	db, _ := enode.OpenDB("")
   114  	ln := enode.NewLocalNode(db, nodeKey)
   115  
   116  	listenerAddr := conn.LocalAddr().(*net.UDPAddr)
   117  	if natm != nil && !listenerAddr.IP.IsLoopback() {
   118  		natAddr := doPortMapping(natm, ln, listenerAddr)
   119  		if natAddr != nil {
   120  			listenerAddr = natAddr
   121  		}
   122  	}
   123  
   124  	printNotice(&nodeKey.PublicKey, *listenerAddr)
   125  	cfg := discover.Config{
   126  		PrivateKey:  nodeKey,
   127  		NetRestrict: restrictList,
   128  	}
   129  	if *runv5 {
   130  		if _, err := discover.ListenV5(conn, ln, cfg); err != nil {
   131  			utils.Fatalf("%v", err)
   132  		}
   133  	} else {
   134  		if _, err := discover.ListenUDP(conn, ln, cfg); err != nil {
   135  			utils.Fatalf("%v", err)
   136  		}
   137  	}
   138  
   139  	select {}
   140  }
   141  
   142  func printNotice(nodeKey *ecdsa.PublicKey, addr net.UDPAddr) {
   143  	if addr.IP.IsUnspecified() {
   144  		addr.IP = net.IP{127, 0, 0, 1}
   145  	}
   146  	n := enode.NewV4(nodeKey, addr.IP, 0, addr.Port)
   147  	fmt.Println(n.URLv4())
   148  	fmt.Println("Note: you're using cmd/bootnode, a developer tool.")
   149  	fmt.Println("We recommend using a regular node as bootstrap node for production deployments.")
   150  }
   151  
   152  func doPortMapping(natm nat.Interface, ln *enode.LocalNode, addr *net.UDPAddr) *net.UDPAddr {
   153  	const (
   154  		protocol = "udp"
   155  		name     = "ethereum discovery"
   156  	)
   157  	newLogger := func(external int, internal int) log.Logger {
   158  		return log.New("proto", protocol, "extport", external, "intport", internal, "interface", natm)
   159  	}
   160  
   161  	var (
   162  		intport    = addr.Port
   163  		extaddr    = &net.UDPAddr{IP: addr.IP, Port: addr.Port}
   164  		mapTimeout = nat.DefaultMapTimeout
   165  		log        = newLogger(addr.Port, intport)
   166  	)
   167  	addMapping := func() {
   168  		// Get the external address.
   169  		var err error
   170  		extaddr.IP, err = natm.ExternalIP()
   171  		if err != nil {
   172  			log.Debug("Couldn't get external IP", "err", err)
   173  			return
   174  		}
   175  		// Create the mapping.
   176  		p, err := natm.AddMapping(protocol, extaddr.Port, intport, name, mapTimeout)
   177  		if err != nil {
   178  			log.Debug("Couldn't add port mapping", "err", err)
   179  			return
   180  		}
   181  		if p != uint16(extaddr.Port) {
   182  			extaddr.Port = int(p)
   183  			log = newLogger(extaddr.Port, intport)
   184  			log.Info("NAT mapped alternative port")
   185  		} else {
   186  			log.Info("NAT mapped port")
   187  		}
   188  		// Update IP/port information of the local node.
   189  		ln.SetStaticIP(extaddr.IP)
   190  		ln.SetFallbackUDP(extaddr.Port)
   191  	}
   192  
   193  	// Perform mapping once, synchronously.
   194  	log.Info("Attempting port mapping")
   195  	addMapping()
   196  
   197  	// Refresh the mapping periodically.
   198  	go func() {
   199  		refresh := time.NewTimer(mapTimeout)
   200  		defer refresh.Stop()
   201  		for range refresh.C {
   202  			addMapping()
   203  			refresh.Reset(mapTimeout)
   204  		}
   205  	}()
   206  
   207  	return extaddr
   208  }