diff --git a/cmd/bootnode/main.go b/cmd/bootnode/main.go index 491a35fb6d..c90e23d0d9 100644 --- a/cmd/bootnode/main.go +++ b/cmd/bootnode/main.go @@ -15,7 +15,8 @@ // along with go-ethereum. If not, see . // bootnode runs a bootstrap node for the Ethereum Discovery Protocol. -package main +// Keep package as bootnode during upstram merge. +package bootnode import ( "crypto/ecdsa" @@ -213,3 +214,12 @@ func doPortMapping(natm nat.Interface, ln *enode.LocalNode, addr *net.UDPAddr) * return extaddr } + +// Implemented separate function so that there are minimal conflicts during upstream merge +func PrintNotice(nodeKey *ecdsa.PublicKey, addr net.UDPAddr) { + printNotice(nodeKey, addr) +} + +func DoPortMapping(natm nat.Interface, ln *enode.LocalNode, addr *net.UDPAddr) *net.UDPAddr { + return doPortMapping(natm, ln, addr) +} diff --git a/internal/cli/bootnode.go b/internal/cli/bootnode.go index 756956daa9..979d69ec23 100644 --- a/internal/cli/bootnode.go +++ b/internal/cli/bootnode.go @@ -13,6 +13,7 @@ import ( "syscall" "time" + "github.com/ethereum/go-ethereum/cmd/bootnode" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/internal/cli/flagset" @@ -213,33 +214,25 @@ func (b *BootnodeCommand) Run(args []string) int { } conn, err := net.ListenUDP("udp", addr) - if err != nil { b.UI.Error(fmt.Sprintf("failed to listen udp addr '%s': %v", b.listenAddr, err)) return 1 } + defer conn.Close() - realaddr := conn.LocalAddr().(*net.UDPAddr) - if natm != nil { - if !realaddr.IP.IsLoopback() { - go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery") - } + db, _ := enode.OpenDB("") + ln := enode.NewLocalNode(db, nodeKey) - if ext, err := natm.ExternalIP(); err == nil { - // nolint: govet - realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port} + listenerAddr := conn.LocalAddr().(*net.UDPAddr) + if natm != nil { + natAddr := bootnode.DoPortMapping(natm, ln, listenerAddr) + if natAddr != nil { + listenerAddr = natAddr } } - n := enode.NewV4(&nodeKey.PublicKey, addr.IP, addr.Port, addr.Port) - b.UI.Info(n.String()) - - if b.dryRun { - return 0 - } + bootnode.PrintNotice(&nodeKey.PublicKey, *listenerAddr) - db, _ := enode.OpenDB("") - ln := enode.NewLocalNode(db, nodeKey) cfg := discover.Config{ PrivateKey: nodeKey, Log: log.Root(),