github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/cmd/nodeaddrgen/main.go (about)

     1  // Copyright 2019 The go-vnt Authors
     2  // This file is part of go-vnt.
     3  //
     4  // go-vnt 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-vnt 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-vnt. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"encoding/json"
    22  	"flag"
    23  	"fmt"
    24  	"net"
    25  	"os"
    26  
    27  	"context"
    28  
    29  	ma "github.com/multiformats/go-multiaddr"
    30  	"github.com/vntchain/go-vnt/cmd/utils"
    31  	"github.com/vntchain/go-vnt/crypto"
    32  	"github.com/vntchain/go-vnt/log"
    33  	p2p "github.com/vntchain/go-vnt/vntp2p"
    34  	// "log"/
    35  )
    36  
    37  func main() {
    38  	var (
    39  		dataDir    = flag.String("datadir", "./", "data directory for the database")
    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 pubkey hash 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)")
    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  		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 := p2p.NATParse(*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  		return
    74  	case *nodeKeyFile == "" && *nodeKeyHex == "":
    75  		// utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
    76  	case *nodeKeyFile != "" && *nodeKeyHex != "":
    77  		utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
    78  	case *nodeKeyFile != "":
    79  		if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
    80  			utils.Fatalf("-nodekey: %v", err)
    81  		}
    82  	case *nodeKeyHex != "":
    83  		if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
    84  			utils.Fatalf("-nodekeyhex: %v", err)
    85  		}
    86  	}
    87  	var restrictList []*net.IPNet
    88  	if *netrestrict != "" {
    89  		restrictList, err = p2p.ParseNetlist(*netrestrict)
    90  		if err != nil {
    91  			utils.Fatalf("-netrestrict: %v", err)
    92  		}
    93  	}
    94  	ctx, cancel := context.WithCancel(context.Background())
    95  	defer cancel()
    96  
    97  	vdht, host, err := p2p.ConstructDHT(ctx, p2p.MakePort(*listenAddr), nodeKey, *dataDir, restrictList, natm)
    98  	if err != nil {
    99  		log.Error(fmt.Sprintf("constructDHT a: %s", err))
   100  		return
   101  	}
   102  	pd := vdht.GetPersistentData()
   103  	pdByte, err := json.Marshal(pd)
   104  	if err != nil {
   105  		log.Error("persistDataPeriodly", "marshal error", err)
   106  		return
   107  	}
   108  	//log.Info("persistDataPeriodly TIME TO PERSIST DATA")
   109  	vdht.SaveData("/PersistentData", pdByte)
   110  	hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", host.ID().Pretty()))
   111  	addr := host.Addrs()[0]
   112  	fullAddr := addr.Encapsulate(hostAddr)
   113  	fmt.Printf("%s\n", fullAddr)
   114  }