2015-07-07 00:54:22 +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
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2015-02-10 13:26:54 +00:00
|
|
|
|
2015-07-07 03:08:16 +00:00
|
|
|
// bootnode runs a bootstrap node for the Ethereum Discovery Protocol.
|
2015-02-10 13:26:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"flag"
|
2016-07-15 10:09:37 +00:00
|
|
|
"fmt"
|
2018-01-22 12:38:34 +00:00
|
|
|
"net"
|
2015-02-10 13:26:54 +00:00
|
|
|
"os"
|
|
|
|
|
2022-08-10 12:04:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common"
|
2022-10-25 02:58:25 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/logging"
|
2022-08-10 12:04:13 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
|
|
|
"github.com/ledgerwatch/erigon/crypto"
|
|
|
|
"github.com/ledgerwatch/erigon/p2p/discover"
|
|
|
|
"github.com/ledgerwatch/erigon/p2p/enode"
|
|
|
|
"github.com/ledgerwatch/erigon/p2p/nat"
|
|
|
|
"github.com/ledgerwatch/erigon/p2p/netutil"
|
2015-02-10 13:26:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
listenAddr = flag.String("addr", ":30301", "listen address")
|
2016-07-15 10:09:37 +00:00
|
|
|
genKey = flag.String("genkey", "", "generate a node key")
|
2018-10-19 13:41:27 +00:00
|
|
|
writeAddr = flag.Bool("writeaddress", false, "write out the node's public key and quit")
|
2015-02-10 13:26:54 +00:00
|
|
|
nodeKeyFile = flag.String("nodekey", "", "private key filename")
|
|
|
|
nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
|
2022-05-18 15:50:50 +00:00
|
|
|
natdesc = flag.String(utils.NATFlag.Name, "", utils.NATFlag.Usage)
|
2016-11-22 19:52:31 +00:00
|
|
|
netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
|
2016-10-19 11:04:55 +00:00
|
|
|
runv5 = flag.Bool("v5", false, "run a v5 topic discovery bootnode")
|
2015-02-13 10:38:34 +00:00
|
|
|
|
|
|
|
nodeKey *ecdsa.PrivateKey
|
|
|
|
err error
|
2015-02-10 13:26:54 +00:00
|
|
|
)
|
|
|
|
flag.Parse()
|
|
|
|
|
2022-10-20 18:25:06 +00:00
|
|
|
_ = logging.GetLogger("bootnode")
|
2017-02-22 12:10:07 +00:00
|
|
|
|
2015-02-13 10:38:34 +00:00
|
|
|
natm, err := nat.Parse(*natdesc)
|
|
|
|
if err != nil {
|
2017-02-22 15:22:50 +00:00
|
|
|
utils.Fatalf("-nat: %v", err)
|
2015-02-13 10:38:34 +00:00
|
|
|
}
|
2015-02-10 13:26:54 +00:00
|
|
|
switch {
|
2016-07-15 10:09:37 +00:00
|
|
|
case *genKey != "":
|
|
|
|
nodeKey, err = crypto.GenerateKey()
|
|
|
|
if err != nil {
|
2017-02-22 15:22:50 +00:00
|
|
|
utils.Fatalf("could not generate key: %v", err)
|
2016-07-15 10:09:37 +00:00
|
|
|
}
|
|
|
|
if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
|
2017-02-22 15:22:50 +00:00
|
|
|
utils.Fatalf("%v", err)
|
2016-07-15 10:09:37 +00:00
|
|
|
}
|
2019-10-02 09:32:02 +00:00
|
|
|
if !*writeAddr {
|
|
|
|
return
|
|
|
|
}
|
2015-02-10 13:26:54 +00:00
|
|
|
case *nodeKeyFile == "" && *nodeKeyHex == "":
|
2017-02-22 15:22:50 +00:00
|
|
|
utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
|
2015-02-10 13:26:54 +00:00
|
|
|
case *nodeKeyFile != "" && *nodeKeyHex != "":
|
2017-02-22 15:22:50 +00:00
|
|
|
utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
|
2015-02-10 13:26:54 +00:00
|
|
|
case *nodeKeyFile != "":
|
|
|
|
if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
|
2017-02-22 15:22:50 +00:00
|
|
|
utils.Fatalf("-nodekey: %v", err)
|
2015-02-10 13:26:54 +00:00
|
|
|
}
|
|
|
|
case *nodeKeyHex != "":
|
|
|
|
if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
|
2017-02-22 15:22:50 +00:00
|
|
|
utils.Fatalf("-nodekeyhex: %v", err)
|
2015-02-10 13:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 10:09:37 +00:00
|
|
|
if *writeAddr {
|
2022-03-31 04:06:20 +00:00
|
|
|
fmt.Printf("%x\n", crypto.MarshalPubkey(&nodeKey.PublicKey))
|
2016-07-15 10:09:37 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:52:31 +00:00
|
|
|
var restrictList *netutil.Netlist
|
|
|
|
if *netrestrict != "" {
|
|
|
|
restrictList, err = netutil.ParseNetlist(*netrestrict)
|
|
|
|
if err != nil {
|
2017-02-22 15:22:50 +00:00
|
|
|
utils.Fatalf("-netrestrict: %v", err)
|
2016-11-22 19:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 12:38:34 +00:00
|
|
|
addr, err := net.ResolveUDPAddr("udp", *listenAddr)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("-ResolveUDPAddr: %v", err)
|
|
|
|
}
|
|
|
|
conn, err := net.ListenUDP("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("-ListenUDP: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
realaddr := conn.LocalAddr().(*net.UDPAddr)
|
|
|
|
if natm != nil {
|
2022-02-24 15:09:56 +00:00
|
|
|
if !realaddr.IP.IsLoopback() && natm.SupportsMapping() {
|
2018-01-22 12:38:34 +00:00
|
|
|
go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery")
|
|
|
|
}
|
|
|
|
if ext, err := natm.ExternalIP(); err == nil {
|
|
|
|
realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 11:39:52 +00:00
|
|
|
printNotice(&nodeKey.PublicKey, *realaddr)
|
|
|
|
|
2023-02-11 20:44:51 +00:00
|
|
|
db, err := enode.OpenDB("" /* path */, "" /* tmpDir */)
|
2021-05-20 16:46:12 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-01-26 20:41:35 +00:00
|
|
|
ln := enode.NewLocalNode(db, nodeKey)
|
|
|
|
cfg := discover.Config{
|
|
|
|
PrivateKey: nodeKey,
|
|
|
|
NetRestrict: restrictList,
|
|
|
|
}
|
2022-02-22 18:18:43 +00:00
|
|
|
|
|
|
|
ctx, cancel := common.RootContext()
|
|
|
|
defer cancel()
|
|
|
|
|
2016-10-19 11:04:55 +00:00
|
|
|
if *runv5 {
|
2022-02-22 18:18:43 +00:00
|
|
|
if _, err := discover.ListenV5(ctx, conn, ln, cfg); err != nil {
|
2017-02-22 15:22:50 +00:00
|
|
|
utils.Fatalf("%v", err)
|
2016-10-19 11:04:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-22 18:18:43 +00:00
|
|
|
if _, err := discover.ListenUDP(ctx, conn, ln, cfg); err != nil {
|
2017-02-22 15:22:50 +00:00
|
|
|
utils.Fatalf("%v", err)
|
2016-10-19 11:04:55 +00:00
|
|
|
}
|
2015-02-10 13:26:54 +00:00
|
|
|
}
|
2016-10-19 11:04:55 +00:00
|
|
|
|
2015-02-10 13:26:54 +00:00
|
|
|
select {}
|
|
|
|
}
|
2019-01-25 11:39:52 +00:00
|
|
|
|
|
|
|
func printNotice(nodeKey *ecdsa.PublicKey, addr net.UDPAddr) {
|
|
|
|
if addr.IP.IsUnspecified() {
|
|
|
|
addr.IP = net.IP{127, 0, 0, 1}
|
|
|
|
}
|
|
|
|
n := enode.NewV4(nodeKey, addr.IP, 0, addr.Port)
|
2019-06-07 13:31:00 +00:00
|
|
|
fmt.Println(n.URLv4())
|
2019-01-25 11:39:52 +00:00
|
|
|
fmt.Println("Note: you're using cmd/bootnode, a developer tool.")
|
|
|
|
fmt.Println("We recommend using a regular node as bootstrap node for production deployments.")
|
|
|
|
}
|