2018-05-18 13:43:07 +00:00
|
|
|
// bootnode runs a bootstrap node for the Ethereum Discovery Protocol.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-06-04 05:16:54 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"encoding/hex"
|
2018-05-18 13:43:07 +00:00
|
|
|
"flag"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
|
|
|
)
|
|
|
|
|
2018-05-23 05:31:59 +00:00
|
|
|
type bootnodes []*discv5.Node
|
|
|
|
|
|
|
|
func (f *bootnodes) String() string {
|
|
|
|
return "discv5 nodes"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set unmarshals enode into discv5.Node.
|
|
|
|
func (f *bootnodes) Set(value string) error {
|
|
|
|
n, err := discv5.ParseNode(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*f = append(*f, n)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-18 13:43:07 +00:00
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
listenAddr = flag.String("addr", ":30301", "listen address")
|
|
|
|
nodeKeyFile = flag.String("nodekey", "", "private key filename")
|
2018-06-04 05:16:54 +00:00
|
|
|
keydata = flag.String("keydata", "", "hex encoded private key")
|
2018-05-18 13:43:07 +00:00
|
|
|
verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)")
|
|
|
|
vmodule = flag.String("vmodule", "", "log verbosity pattern")
|
2018-05-23 05:31:59 +00:00
|
|
|
nursery = bootnodes{}
|
2018-06-12 16:50:25 +00:00
|
|
|
nodeKey *ecdsa.PrivateKey
|
|
|
|
err error
|
2018-05-18 13:43:07 +00:00
|
|
|
)
|
2018-05-23 05:31:59 +00:00
|
|
|
flag.Var(&nursery, "n", "These nodes are used to connect to the network if the table is empty and there are no known nodes in the database.")
|
2018-05-18 13:43:07 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
|
|
|
|
glogger.Verbosity(log.Lvl(*verbosity))
|
2018-06-12 16:50:25 +00:00
|
|
|
if err = glogger.Vmodule(*vmodule); err != nil {
|
2018-05-18 13:43:07 +00:00
|
|
|
log.Crit("Failed to set glog verbosity", "value", *vmodule, "err", err)
|
|
|
|
}
|
|
|
|
log.Root().SetHandler(glogger)
|
|
|
|
|
2018-06-04 05:16:54 +00:00
|
|
|
if len(*nodeKeyFile) == 0 && len(*keydata) == 0 {
|
|
|
|
log.Crit("either `nodekey` or `keydata` must be provided")
|
|
|
|
}
|
|
|
|
if len(*nodeKeyFile) != 0 {
|
|
|
|
nodeKey, err = crypto.LoadECDSA(*nodeKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Crit("Failed to load ecdsa key from", "file", *nodeKeyFile, "error", err)
|
|
|
|
}
|
|
|
|
} else if len(*keydata) != 0 {
|
|
|
|
log.Warn("key will be visible in process list. should be used only for tests")
|
|
|
|
key, err := hex.DecodeString(*keydata)
|
|
|
|
if err != nil {
|
|
|
|
log.Crit("unable to decode hex", "data", keydata, "error", err)
|
|
|
|
}
|
|
|
|
nodeKey, err = crypto.ToECDSA(key)
|
|
|
|
if err != nil {
|
|
|
|
log.Crit("unable to convert decoded hex into ecdsa.PrivateKey", "data", key, "error", err)
|
|
|
|
}
|
2018-05-18 13:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := net.ResolveUDPAddr("udp", *listenAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Crit("Unable to resolve UDP", "address", *listenAddr, "error", err)
|
|
|
|
}
|
|
|
|
conn, err := net.ListenUDP("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Crit("Unable to listen on udp", "address", addr, "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
realaddr := conn.LocalAddr().(*net.UDPAddr)
|
|
|
|
tab, err := discv5.ListenUDP(nodeKey, conn, realaddr, "", nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Crit("Failed to create discovery v5 table:", "error", err)
|
|
|
|
}
|
|
|
|
defer tab.Close()
|
2018-05-23 05:31:59 +00:00
|
|
|
if err := tab.SetFallbackNodes(nursery); err != nil {
|
|
|
|
log.Crit("Failed to set fallback", "nodes", nursery, "error", err)
|
|
|
|
}
|
2018-05-18 13:43:07 +00:00
|
|
|
select {}
|
|
|
|
}
|