mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-28 07:36:44 +00:00
add portMapping to localNode creation
This commit is contained in:
parent
52c2bfb5b3
commit
120be2f1ee
3 changed files with 66 additions and 3 deletions
|
|
@ -13,6 +13,7 @@ import (
|
|||
"slices"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"os"
|
||||
|
||||
|
|
@ -249,8 +250,13 @@ func initDiscV5(config Config, conn discover.UDPConn) (*discover.UDPv5, *enode.L
|
|||
}
|
||||
|
||||
localNode := enode.NewLocalNode(nodeDB, config.PrivateKey)
|
||||
localNode.SetFallbackIP(net.IP{127, 0, 0, 1})
|
||||
|
||||
localNode.Set(discover.Tag)
|
||||
listenerAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||
nat := config.Protocol.NAT
|
||||
if nat != nil && !listenerAddr.IP.IsLoopback() {
|
||||
doPortMapping(nat, localNode, listenerAddr)
|
||||
}
|
||||
|
||||
discV5, err := discover.ListenV5(conn, localNode, discCfg)
|
||||
if err != nil {
|
||||
|
|
@ -259,6 +265,57 @@ func initDiscV5(config Config, conn discover.UDPConn) (*discover.UDPv5, *enode.L
|
|||
return discV5, localNode, nil
|
||||
}
|
||||
|
||||
func doPortMapping(natm nat.Interface, ln *enode.LocalNode, addr *net.UDPAddr) {
|
||||
const (
|
||||
protocol = "udp"
|
||||
name = "ethereum discovery"
|
||||
)
|
||||
|
||||
var (
|
||||
intport = addr.Port
|
||||
extaddr = &net.UDPAddr{IP: addr.IP, Port: addr.Port}
|
||||
mapTimeout = nat.DefaultMapTimeout
|
||||
)
|
||||
addMapping := func() {
|
||||
// Get the external address.
|
||||
var err error
|
||||
extaddr.IP, err = natm.ExternalIP()
|
||||
if err != nil {
|
||||
log.Debug("Couldn't get external IP", "err", err)
|
||||
return
|
||||
}
|
||||
// Create the mapping.
|
||||
p, err := natm.AddMapping(protocol, extaddr.Port, intport, name, mapTimeout)
|
||||
if err != nil {
|
||||
log.Debug("Couldn't add port mapping", "err", err)
|
||||
return
|
||||
}
|
||||
if p != uint16(extaddr.Port) {
|
||||
extaddr.Port = int(p)
|
||||
log.Info("NAT mapped alternative port")
|
||||
} else {
|
||||
log.Info("NAT mapped port")
|
||||
}
|
||||
// Update IP/port information of the local node.
|
||||
ln.SetStaticIP(extaddr.IP)
|
||||
ln.SetFallbackUDP(extaddr.Port)
|
||||
}
|
||||
|
||||
// Perform mapping once, synchronously.
|
||||
log.Info("Attempting port mapping")
|
||||
addMapping()
|
||||
|
||||
// Refresh the mapping periodically.
|
||||
go func() {
|
||||
refresh := time.NewTimer(mapTimeout)
|
||||
defer refresh.Stop()
|
||||
for range refresh.C {
|
||||
addMapping()
|
||||
refresh.Reset(mapTimeout)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func initHistory(config Config, server *rpc.Server, conn discover.UDPConn, localNode *enode.LocalNode, discV5 *discover.UDPv5) (*history.HistoryNetwork, error) {
|
||||
networkName := portalwire.History.Name()
|
||||
db, err := history.NewDB(config.DataDir, networkName)
|
||||
|
|
|
|||
|
|
@ -47,6 +47,12 @@ func (p *PortalProtocol) setupPortMapping() {
|
|||
p.localNode.SetStaticIP(ip)
|
||||
go p.consumePortMappingRequests()
|
||||
|
||||
case nat.STUN:
|
||||
// STUN doesn't block, set the IP right away.
|
||||
ip, _ := p.NAT.ExternalIP()
|
||||
p.localNode.SetStaticIP(ip)
|
||||
go p.consumePortMappingRequests()
|
||||
|
||||
default:
|
||||
go p.portMappingLoop()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,8 +29,8 @@ func (STUN) SupportsMapping() bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (STUN) AddMapping(string, int, int, string, time.Duration) (uint16, error) {
|
||||
return 0, nil
|
||||
func (STUN) AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) (uint16, error) {
|
||||
return uint16(extport), nil
|
||||
}
|
||||
|
||||
func (STUN) DeleteMapping(string, int, int) error {
|
||||
|
|
|
|||
Loading…
Reference in a new issue