2016-07-26 02:54:35 +00: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
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// bootnode runs a bootstrap node for the Ethereum Discovery Protocol.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"flag"
|
2016-09-08 09:45:12 +00:00
|
|
|
"fmt"
|
2016-07-26 02:54:35 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2016-10-20 11:07:42 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2016-07-26 02:54:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
2016-12-07 20:54:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/netutil"
|
2016-07-26 02:54:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
listenAddr = flag.String("addr", ":30301", "listen address")
|
2016-09-08 09:45:12 +00:00
|
|
|
genKey = flag.String("genkey", "", "generate a node key")
|
|
|
|
writeAddr = flag.Bool("writeaddress", false, "write out the node's pubkey hash and quit")
|
2016-07-26 02:54:35 +00:00
|
|
|
nodeKeyFile = flag.String("nodekey", "", "private key filename")
|
|
|
|
nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
|
|
|
|
natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
|
2016-12-07 20:54:59 +00:00
|
|
|
netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
|
2016-10-20 11:07:42 +00:00
|
|
|
runv5 = flag.Bool("v5", false, "run a v5 topic discovery bootnode")
|
2016-07-26 02:54:35 +00:00
|
|
|
|
|
|
|
nodeKey *ecdsa.PrivateKey
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
flag.Var(glog.GetVerbosity(), "verbosity", "log verbosity (0-9)")
|
|
|
|
flag.Var(glog.GetVModule(), "vmodule", "log verbosity pattern")
|
|
|
|
glog.SetToStderr(true)
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
natm, err := nat.Parse(*natdesc)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("-nat: %v", err)
|
|
|
|
}
|
|
|
|
switch {
|
2016-09-08 09:45:12 +00:00
|
|
|
case *genKey != "":
|
|
|
|
nodeKey, err = crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("could not generate key: %v", err)
|
|
|
|
}
|
|
|
|
if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
|
|
|
|
utils.Fatalf("%v", err)
|
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
case *nodeKeyFile == "" && *nodeKeyHex == "":
|
|
|
|
utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
|
|
|
|
case *nodeKeyFile != "" && *nodeKeyHex != "":
|
|
|
|
utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
|
|
|
|
case *nodeKeyFile != "":
|
|
|
|
if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
|
|
|
|
utils.Fatalf("-nodekey: %v", err)
|
|
|
|
}
|
|
|
|
case *nodeKeyHex != "":
|
|
|
|
if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
|
|
|
|
utils.Fatalf("-nodekeyhex: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 09:45:12 +00:00
|
|
|
if *writeAddr {
|
|
|
|
fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey))
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2016-12-07 20:54:59 +00:00
|
|
|
var restrictList *netutil.Netlist
|
|
|
|
if *netrestrict != "" {
|
|
|
|
restrictList, err = netutil.ParseNetlist(*netrestrict)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("-netrestrict: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 05:50:30 +00:00
|
|
|
if *runv5 {
|
2016-12-07 20:54:59 +00:00
|
|
|
if _, err := discv5.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
|
2016-10-20 11:07:42 +00:00
|
|
|
utils.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
} else {
|
2016-12-07 20:54:59 +00:00
|
|
|
if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
|
2016-10-20 11:07:42 +00:00
|
|
|
utils.Fatalf("%v", err)
|
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
}
|
2016-10-20 11:07:42 +00:00
|
|
|
|
2016-07-26 02:54:35 +00:00
|
|
|
select {}
|
|
|
|
}
|