Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upnp automatic mapping to localnode in nat environment #12077

Open
du5 opened this issue Sep 24, 2024 · 1 comment
Open

Add upnp automatic mapping to localnode in nat environment #12077

du5 opened this issue Sep 24, 2024 · 1 comment
Labels
imp2 Medium importance

Comments

@du5
Copy link

du5 commented Sep 24, 2024

Rationale

Run nodes in a NAT environment, have a better network, and establish more peer links

Implementation

>>

After NAT ExternalIP is resolved successfully, nat upnp or pmp should be added

erigon/p2p/server.go

Lines 588 to 591 in 6d975a9

if ip, err := srv.NAT.ExternalIP(); err == nil {
srv.logger.Info("NAT ExternalIP resolved", "ip", ip)
srv.localnode.SetStaticIP(ip)
srv.updateLocalNodeStaticAddrCache()

>>

Before adding, should we check whether the external port is already in use? If so, check whether the upnp description is consistent with the program settings. If not, abort. Otherwise, refresh it, or return an unused random high port from nat.Map and return it to the program.

erigon/p2p/nat/nat.go

Lines 116 to 132 in 6d975a9

func Map(m Interface, c <-chan struct{}, protocol string, extport, intport int, name string, logger log.Logger) {
if !m.SupportsMapping() {
panic("Port mapping is not supported")
}
logger1 := logger.New("proto", protocol, "extport", extport, "intport", intport, "interface", m)
refresh := time.NewTimer(mapTimeout)
defer func() {
refresh.Stop()
logger1.Trace("Deleting port mapping")
m.DeleteMapping(protocol, extport, intport)
}()
if err := m.AddMapping(protocol, extport, intport, name, mapTimeout); err != nil {
logger1.Debug("Couldn't add port mapping", "err", err)
} else {
logger1.Info("Mapped network port")
}

@du5
Copy link
Author

du5 commented Sep 24, 2024

➜  berigon git:(main) ✗ upnpc -l          
upnpc : miniupnpc library test client, version 2.2.4.
 (c) 2005-2022 Thomas Bernard.
Go to http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
for more information.
List of UPNP devices found on the network :
 desc: http://192.168.0.1:2828/gateway_description.xml
 st: urn:schemas-upnp-org:device:InternetGatewayDevice:1

Found valid IGD : http://192.168.0.1:2828/upnp/control/rjviirgxfq/wanipconn-2
Local LAN ip address : 192.168.0.103
Connection Type : IP_Routed
Status : Connected, uptime=43445s, LastConnectionError : ERROR_NONE
  Time started : Mon Sep 23 11:01:16 2024
MaxBitRateDown : 10000000 bps (10.0 Mbps)   MaxBitRateUp 10000000 bps (10.0 Mbps)
ExternalIPAddress = xxx.xxx.xx.xx
 i protocol exPort->inAddr:inPort description remoteHost leaseTime
 0 TCP     0->0.0.0.0:0     'Dummy inactive rule for windows to work' '' 0
 1 TCP 42069->192.168.0.103:42069 'github.com/erigontech/erigon (devel)leecher' '' 0
 2 UDP 42069->192.168.0.103:42069 'github.com/erigontech/erigon (devel)leecher' '' 0
GetGenericPortMappingEntry() returned 713 (SpecifiedArrayIndexInvalid)

Btw, currently I only see the mapping of the bt download protocol

@VBulikov VBulikov added the imp2 Medium importance label Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imp2 Medium importance
Projects
None yet
Development

No branches or pull requests

2 participants