github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/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  	"encoding/hex"
    23  	"flag"
    24  	"fmt"
    25  	"io/ioutil"
    26  	"log"
    27  	"os"
    28  
    29  	"github.com/ethereum/go-ethereum/crypto"
    30  	"github.com/ethereum/go-ethereum/logger"
    31  	"github.com/ethereum/go-ethereum/p2p/discover"
    32  	"github.com/ethereum/go-ethereum/p2p/nat"
    33  )
    34  
    35  func main() {
    36  	var (
    37  		listenAddr  = flag.String("addr", ":30301", "listen address")
    38  		genKey      = flag.String("genkey", "", "generate a node key and quit")
    39  		nodeKeyFile = flag.String("nodekey", "", "private key filename")
    40  		nodeKeyHex  = flag.String("nodekeyhex", "", "private key as hex (for testing)")
    41  		natdesc     = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
    42  
    43  		nodeKey *ecdsa.PrivateKey
    44  		err     error
    45  	)
    46  	flag.Parse()
    47  	logger.AddLogSystem(logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.DebugLevel))
    48  
    49  	if *genKey != "" {
    50  		writeKey(*genKey)
    51  		os.Exit(0)
    52  	}
    53  
    54  	natm, err := nat.Parse(*natdesc)
    55  	if err != nil {
    56  		log.Fatalf("-nat: %v", err)
    57  	}
    58  	switch {
    59  	case *nodeKeyFile == "" && *nodeKeyHex == "":
    60  		log.Fatal("Use -nodekey or -nodekeyhex to specify a private key")
    61  	case *nodeKeyFile != "" && *nodeKeyHex != "":
    62  		log.Fatal("Options -nodekey and -nodekeyhex are mutually exclusive")
    63  	case *nodeKeyFile != "":
    64  		if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
    65  			log.Fatalf("-nodekey: %v", err)
    66  		}
    67  	case *nodeKeyHex != "":
    68  		if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
    69  			log.Fatalf("-nodekeyhex: %v", err)
    70  		}
    71  	}
    72  
    73  	if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, ""); err != nil {
    74  		log.Fatal(err)
    75  	}
    76  	select {}
    77  }
    78  
    79  func writeKey(target string) {
    80  	key, err := crypto.GenerateKey()
    81  	if err != nil {
    82  		log.Fatal("could not generate key: %v", err)
    83  	}
    84  	b := crypto.FromECDSA(key)
    85  	if target == "-" {
    86  		fmt.Println(hex.EncodeToString(b))
    87  	} else {
    88  		if err := ioutil.WriteFile(target, b, 0600); err != nil {
    89  			log.Fatal("write error: ", err)
    90  		}
    91  	}
    92  }