erigontech / erigon

Ethereum implementation on the efficiency frontier https://erigon.gitbook.io
GNU Lesser General Public License v3.0
3.11k stars 1.1k forks source link

Add upnp automatic mapping to localnode in nat environment #12077

Open du5 opened 3 hours ago

du5 commented 3 hours ago

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 https://github.com/erigontech/erigon/blob/6d975a954ff66196730b5847fc8e9682c27e1761/p2p/server.go#L588-L591

>>

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. https://github.com/erigontech/erigon/blob/6d975a954ff66196730b5847fc8e9682c27e1761/p2p/nat/nat.go#L116-L132

du5 commented 3 hours ago
➜  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