2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2015-02-10 07:26:54 -06:00
|
|
|
|
2015-07-06 22:08:16 -05:00
|
|
|
// bootnode runs a bootstrap node for the Ethereum Discovery Protocol.
|
2015-02-10 07:26:54 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"flag"
|
2016-07-15 05:09:37 -05:00
|
|
|
"fmt"
|
2015-02-10 07:26:54 -06:00
|
|
|
"os"
|
|
|
|
|
2017-02-22 09:22:50 -06:00
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
2015-02-10 07:26:54 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-02-22 06:10:07 -06:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2015-02-10 07:26:54 -06:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2016-10-19 06:04:55 -05:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2015-02-13 04:38:34 -06:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
2016-11-22 13:52:31 -06:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/netutil"
|
2015-02-10 07:26:54 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
listenAddr = flag.String("addr", ":30301", "listen address")
|
2016-07-15 05:09:37 -05:00
|
|
|
genKey = flag.String("genkey", "", "generate a node key")
|
|
|
|
writeAddr = flag.Bool("writeaddress", false, "write out the node's pubkey hash and quit")
|
2015-02-10 07:26:54 -06:00
|
|
|
nodeKeyFile = flag.String("nodekey", "", "private key filename")
|
|
|
|
nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
|
2015-02-13 04:38:34 -06:00
|
|
|
natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
|
2016-11-22 13:52:31 -06:00
|
|
|
netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
|
2016-10-19 06:04:55 -05:00
|
|
|
runv5 = flag.Bool("v5", false, "run a v5 topic discovery bootnode")
|
2017-02-22 06:10:07 -06:00
|
|
|
verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)")
|
|
|
|
vmodule = flag.String("vmodule", "", "log verbosity pattern")
|
2015-02-13 04:38:34 -06:00
|
|
|
|
|
|
|
nodeKey *ecdsa.PrivateKey
|
|
|
|
err error
|
2015-02-10 07:26:54 -06:00
|
|
|
)
|
|
|
|
flag.Parse()
|
|
|
|
|
2017-02-23 12:31:13 -06:00
|
|
|
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
|
2017-02-22 06:10:07 -06:00
|
|
|
glogger.Verbosity(log.Lvl(*verbosity))
|
|
|
|
glogger.Vmodule(*vmodule)
|
|
|
|
log.Root().SetHandler(glogger)
|
|
|
|
|
2015-02-13 04:38:34 -06:00
|
|
|
natm, err := nat.Parse(*natdesc)
|
|
|
|
if err != nil {
|
2017-02-22 09:22:50 -06:00
|
|
|
utils.Fatalf("-nat: %v", err)
|
2015-02-13 04:38:34 -06:00
|
|
|
}
|
2015-02-10 07:26:54 -06:00
|
|
|
switch {
|
2016-07-15 05:09:37 -05:00
|
|
|
case *genKey != "":
|
|
|
|
nodeKey, err = crypto.GenerateKey()
|
|
|
|
if err != nil {
|
2017-02-22 09:22:50 -06:00
|
|
|
utils.Fatalf("could not generate key: %v", err)
|
2016-07-15 05:09:37 -05:00
|
|
|
}
|
|
|
|
if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
|
2017-02-22 09:22:50 -06:00
|
|
|
utils.Fatalf("%v", err)
|
2016-07-15 05:09:37 -05:00
|
|
|
}
|
2015-02-10 07:26:54 -06:00
|
|
|
case *nodeKeyFile == "" && *nodeKeyHex == "":
|
2017-02-22 09:22:50 -06:00
|
|
|
utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
|
2015-02-10 07:26:54 -06:00
|
|
|
case *nodeKeyFile != "" && *nodeKeyHex != "":
|
2017-02-22 09:22:50 -06:00
|
|
|
utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
|
2015-02-10 07:26:54 -06:00
|
|
|
case *nodeKeyFile != "":
|
|
|
|
if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
|
2017-02-22 09:22:50 -06:00
|
|
|
utils.Fatalf("-nodekey: %v", err)
|
2015-02-10 07:26:54 -06:00
|
|
|
}
|
|
|
|
case *nodeKeyHex != "":
|
|
|
|
if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
|
2017-02-22 09:22:50 -06:00
|
|
|
utils.Fatalf("-nodekeyhex: %v", err)
|
2015-02-10 07:26:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 05:09:37 -05:00
|
|
|
if *writeAddr {
|
|
|
|
fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey))
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2016-11-22 13:52:31 -06:00
|
|
|
var restrictList *netutil.Netlist
|
|
|
|
if *netrestrict != "" {
|
|
|
|
restrictList, err = netutil.ParseNetlist(*netrestrict)
|
|
|
|
if err != nil {
|
2017-02-22 09:22:50 -06:00
|
|
|
utils.Fatalf("-netrestrict: %v", err)
|
2016-11-22 13:52:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 06:04:55 -05:00
|
|
|
if *runv5 {
|
2016-11-22 13:52:31 -06:00
|
|
|
if _, err := discv5.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
|
2017-02-22 09:22:50 -06:00
|
|
|
utils.Fatalf("%v", err)
|
2016-10-19 06:04:55 -05:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-22 13:52:31 -06:00
|
|
|
if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
|
2017-02-22 09:22:50 -06:00
|
|
|
utils.Fatalf("%v", err)
|
2016-10-19 06:04:55 -05:00
|
|
|
}
|
2015-02-10 07:26:54 -06:00
|
|
|
}
|
2016-10-19 06:04:55 -05:00
|
|
|
|
2015-02-10 07:26:54 -06:00
|
|
|
select {}
|
|
|
|
}
|