github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/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 VNT Discovery Protocol.
    18  package main
    19  
    20  import (
    21  	"crypto/ecdsa"
    22  	"flag"
    23  	"fmt"
    24  	"net"
    25  	"os"
    26  
    27  	"context"
    28  
    29  	"github.com/vntchain/go-vnt/cmd/utils"
    30  	"github.com/vntchain/go-vnt/crypto"
    31  	"github.com/vntchain/go-vnt/log"
    32  	p2p "github.com/vntchain/go-vnt/vntp2p"
    33  	// "log"/
    34  )
    35  
    36  func main() {
    37  	var (
    38  		dataDir    = flag.String("datadir", "./", "data directory for the database")
    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 pubkey hash 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)")
    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-9)")
    48  		vmodule   = flag.String("vmodule", "", "log verbosity pattern")
    49  		nodeKey   *ecdsa.PrivateKey
    50  		err       error
    51  	)
    52  	flag.Parse()
    53  
    54  	glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
    55  	glogger.Verbosity(log.Lvl(*verbosity))
    56  	glogger.Vmodule(*vmodule)
    57  	log.Root().SetHandler(glogger)
    58  
    59  	natm, err := p2p.NATParse(*natdesc)
    60  	if err != nil {
    61  		utils.Fatalf("-nat: %v", err)
    62  	}
    63  	switch {
    64  	case *genKey != "":
    65  		nodeKey, err = crypto.GenerateKey()
    66  		if err != nil {
    67  			utils.Fatalf("could not generate key: %v", err)
    68  		}
    69  		if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
    70  			utils.Fatalf("%v", err)
    71  		}
    72  		return
    73  	case *nodeKeyFile == "" && *nodeKeyHex == "":
    74  		// utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
    75  	case *nodeKeyFile != "" && *nodeKeyHex != "":
    76  		utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
    77  	case *nodeKeyFile != "":
    78  		if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
    79  			utils.Fatalf("-nodekey: %v", err)
    80  		}
    81  	case *nodeKeyHex != "":
    82  		if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
    83  			utils.Fatalf("-nodekeyhex: %v", err)
    84  		}
    85  	}
    86  
    87  	// if *writeAddr {
    88  	// 	fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey))
    89  	// 	os.Exit(0)
    90  	// }
    91  
    92  	var restrictList []*net.IPNet
    93  	if *netrestrict != "" {
    94  		restrictList, err = p2p.ParseNetlist(*netrestrict)
    95  		if err != nil {
    96  			utils.Fatalf("-netrestrict: %v", err)
    97  		}
    98  	}
    99  
   100  	ctx, cancel := context.WithCancel(context.Background())
   101  	defer cancel()
   102  
   103  	_, host, err := p2p.ConstructDHT(ctx, p2p.MakePort(*listenAddr), nodeKey, *dataDir, restrictList, natm)
   104  	if err != nil {
   105  		log.Error(fmt.Sprintf("constructDHT a: %s", err))
   106  		return
   107  	}
   108  
   109  	log.Info("Listen for connect")
   110  	log.Info(fmt.Sprintf("PID: %s, Addr: %s", host.ID(), host.Addrs()))
   111  
   112  	// h := &vntprotocol.HostWrapper{
   113  	// 	Host: host,
   114  	// }
   115  
   116  	// host.SetStreamHandler(p2p.PID, h.HandleStream)
   117  
   118  	select {}
   119  }