2018-07-04 10:51:47 +00:00
|
|
|
package basichost
|
|
|
|
|
|
|
|
import (
|
2022-04-01 16:16:46 +00:00
|
|
|
"context"
|
|
|
|
"io"
|
2019-06-09 07:24:20 +00:00
|
|
|
"net"
|
2023-06-30 13:41:32 +00:00
|
|
|
"net/netip"
|
2019-06-09 07:24:20 +00:00
|
|
|
"strconv"
|
2018-07-04 10:51:47 +00:00
|
|
|
"sync"
|
2022-04-01 16:16:46 +00:00
|
|
|
"time"
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
2022-08-19 16:34:07 +00:00
|
|
|
inat "github.com/libp2p/go-libp2p/p2p/net/nat"
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2023-06-30 13:41:32 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
// NATManager is a simple interface to manage NAT devices.
|
2023-06-30 13:41:32 +00:00
|
|
|
// It listens Listen and ListenClose notifications from the network.Network,
|
|
|
|
// and tries to obtain port mappings for those.
|
2018-07-04 10:51:47 +00:00
|
|
|
type NATManager interface {
|
2023-06-30 13:41:32 +00:00
|
|
|
GetMapping(ma.Multiaddr) ma.Multiaddr
|
|
|
|
HasDiscoveredNAT() bool
|
2022-04-01 16:16:46 +00:00
|
|
|
io.Closer
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
// NewNATManager creates a NAT manager.
|
2019-06-09 07:24:20 +00:00
|
|
|
func NewNATManager(net network.Network) NATManager {
|
2023-06-30 13:41:32 +00:00
|
|
|
return newNATManager(net)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
type entry struct {
|
|
|
|
protocol string
|
|
|
|
port int
|
|
|
|
}
|
|
|
|
|
|
|
|
type nat interface {
|
|
|
|
AddMapping(ctx context.Context, protocol string, port int) error
|
|
|
|
RemoveMapping(ctx context.Context, protocol string, port int) error
|
|
|
|
GetMapping(protocol string, port int) (netip.AddrPort, bool)
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
// so we can mock it in tests
|
|
|
|
var discoverNAT = func(ctx context.Context) (nat, error) { return inat.DiscoverNAT(ctx) }
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
// natManager takes care of adding + removing port mappings to the nat.
|
|
|
|
// Initialized with the host if it has a NATPortMap option enabled.
|
|
|
|
// natManager receives signals from the network, and check on nat mappings:
|
2022-11-04 13:57:20 +00:00
|
|
|
// - natManager listens to the network and adds or closes port mappings
|
|
|
|
// as the network signals Listen() or ListenClose().
|
|
|
|
// - closing the natManager closes the nat and its mappings.
|
2018-07-04 10:51:47 +00:00
|
|
|
type natManager struct {
|
2019-06-09 07:24:20 +00:00
|
|
|
net network.Network
|
2022-08-19 16:34:07 +00:00
|
|
|
natMx sync.RWMutex
|
2023-06-30 13:41:32 +00:00
|
|
|
nat nat
|
|
|
|
|
|
|
|
syncFlag chan struct{} // cap: 1
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
tracked map[entry]bool // the bool is only used in doSync and has no meaning outside of that function
|
2019-06-09 07:24:20 +00:00
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
refCount sync.WaitGroup
|
2023-06-30 13:41:32 +00:00
|
|
|
ctx context.Context
|
2022-04-01 16:16:46 +00:00
|
|
|
ctxCancel context.CancelFunc
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
func newNATManager(net network.Network) *natManager {
|
2022-04-01 16:16:46 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2018-07-04 10:51:47 +00:00
|
|
|
nmgr := &natManager{
|
2022-04-01 16:16:46 +00:00
|
|
|
net: net,
|
|
|
|
syncFlag: make(chan struct{}, 1),
|
2023-06-30 13:41:32 +00:00
|
|
|
ctx: ctx,
|
2022-04-01 16:16:46 +00:00
|
|
|
ctxCancel: cancel,
|
2023-06-30 13:41:32 +00:00
|
|
|
tracked: make(map[entry]bool),
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2022-04-01 16:16:46 +00:00
|
|
|
nmgr.refCount.Add(1)
|
|
|
|
go nmgr.background(ctx)
|
2018-07-04 10:51:47 +00:00
|
|
|
return nmgr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the natManager, closing the underlying nat
|
|
|
|
// and unregistering from network events.
|
|
|
|
func (nmgr *natManager) Close() error {
|
2022-04-01 16:16:46 +00:00
|
|
|
nmgr.ctxCancel()
|
|
|
|
nmgr.refCount.Wait()
|
|
|
|
return nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
func (nmgr *natManager) HasDiscoveredNAT() bool {
|
|
|
|
nmgr.natMx.RLock()
|
|
|
|
defer nmgr.natMx.RUnlock()
|
|
|
|
return nmgr.nat != nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
func (nmgr *natManager) background(ctx context.Context) {
|
|
|
|
defer nmgr.refCount.Done()
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
defer func() {
|
|
|
|
nmgr.natMx.Lock()
|
2023-06-30 13:41:32 +00:00
|
|
|
defer nmgr.natMx.Unlock()
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
if nmgr.nat != nil {
|
|
|
|
nmgr.nat.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
discoverCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
defer cancel()
|
2023-06-30 13:41:32 +00:00
|
|
|
natInstance, err := discoverNAT(discoverCtx)
|
2022-04-01 16:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Info("DiscoverNAT error:", err)
|
|
|
|
return
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
nmgr.natMx.Lock()
|
2022-04-01 16:16:46 +00:00
|
|
|
nmgr.nat = natInstance
|
2022-08-19 16:34:07 +00:00
|
|
|
nmgr.natMx.Unlock()
|
2022-04-01 16:16:46 +00:00
|
|
|
|
|
|
|
// sign natManager up for network notifications
|
|
|
|
// we need to sign up here to avoid missing some notifs
|
|
|
|
// before the NAT has been found.
|
|
|
|
nmgr.net.Notify((*nmgrNetNotifiee)(nmgr))
|
|
|
|
defer nmgr.net.StopNotify((*nmgrNetNotifiee)(nmgr))
|
|
|
|
|
|
|
|
nmgr.doSync() // sync one first.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-nmgr.syncFlag:
|
2024-06-05 20:10:03 +00:00
|
|
|
nmgr.doSync() // sync when our listen addresses change.
|
2022-04-01 16:16:46 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func (nmgr *natManager) sync() {
|
2021-06-16 20:19:45 +00:00
|
|
|
select {
|
|
|
|
case nmgr.syncFlag <- struct{}{}:
|
|
|
|
default:
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// doSync syncs the current NAT mappings, removing any outdated mappings and adding any
|
|
|
|
// new mappings.
|
|
|
|
func (nmgr *natManager) doSync() {
|
2023-06-30 13:41:32 +00:00
|
|
|
for e := range nmgr.tracked {
|
|
|
|
nmgr.tracked[e] = false
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
2023-06-30 13:41:32 +00:00
|
|
|
var newAddresses []entry
|
2021-06-16 20:19:45 +00:00
|
|
|
for _, maddr := range nmgr.net.ListenAddresses() {
|
|
|
|
// Strip the IP
|
|
|
|
maIP, rest := ma.SplitFirst(maddr)
|
|
|
|
if maIP == nil || rest == nil {
|
|
|
|
continue
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
switch maIP.Protocol().Code {
|
|
|
|
case ma.P_IP6, ma.P_IP4:
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
// Only bother if we're listening on an unicast / unspecified IP.
|
2021-06-16 20:19:45 +00:00
|
|
|
ip := net.IP(maIP.RawValue())
|
2023-06-30 13:41:32 +00:00
|
|
|
if !ip.IsGlobalUnicast() && !ip.IsUnspecified() {
|
2021-06-16 20:19:45 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// Extract the port/protocol
|
|
|
|
proto, _ := ma.SplitFirst(rest)
|
|
|
|
if proto == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
var protocol string
|
|
|
|
switch proto.Protocol().Code {
|
|
|
|
case ma.P_TCP:
|
|
|
|
protocol = "tcp"
|
|
|
|
case ma.P_UDP:
|
|
|
|
protocol = "udp"
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
port, err := strconv.ParseUint(proto.Value(), 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
// bug in multiaddr
|
|
|
|
panic(err)
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
2023-06-30 13:41:32 +00:00
|
|
|
e := entry{protocol: protocol, port: int(port)}
|
|
|
|
if _, ok := nmgr.tracked[e]; ok {
|
|
|
|
nmgr.tracked[e] = true
|
|
|
|
} else {
|
|
|
|
newAddresses = append(newAddresses, e)
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
defer wg.Wait()
|
|
|
|
|
|
|
|
// Close old mappings
|
2023-06-30 13:41:32 +00:00
|
|
|
for e, v := range nmgr.tracked {
|
|
|
|
if !v {
|
|
|
|
nmgr.nat.RemoveMapping(nmgr.ctx, e.protocol, e.port)
|
|
|
|
delete(nmgr.tracked, e)
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// Create new mappings.
|
2023-06-30 13:41:32 +00:00
|
|
|
for _, e := range newAddresses {
|
|
|
|
if err := nmgr.nat.AddMapping(nmgr.ctx, e.protocol, e.port); err != nil {
|
|
|
|
log.Errorf("failed to port-map %s port %d: %s", e.protocol, e.port, err)
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
2023-06-30 13:41:32 +00:00
|
|
|
nmgr.tracked[e] = false
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
func (nmgr *natManager) GetMapping(addr ma.Multiaddr) ma.Multiaddr {
|
2022-08-19 16:34:07 +00:00
|
|
|
nmgr.natMx.Lock()
|
|
|
|
defer nmgr.natMx.Unlock()
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
if nmgr.nat == nil { // NAT not yet initialized
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
var found bool
|
|
|
|
var proto int // ma.P_TCP or ma.P_UDP
|
|
|
|
transport, rest := ma.SplitFunc(addr, func(c ma.Component) bool {
|
|
|
|
if found {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
proto = c.Protocol().Code
|
|
|
|
found = proto == ma.P_TCP || proto == ma.P_UDP
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if !manet.IsThinWaist(transport) {
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
naddr, err := manet.ToNetAddr(transport)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("error parsing net multiaddr %q: %s", transport, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ip net.IP
|
|
|
|
port int
|
|
|
|
protocol string
|
|
|
|
)
|
|
|
|
switch naddr := naddr.(type) {
|
|
|
|
case *net.TCPAddr:
|
|
|
|
ip = naddr.IP
|
|
|
|
port = naddr.Port
|
|
|
|
protocol = "tcp"
|
|
|
|
case *net.UDPAddr:
|
|
|
|
ip = naddr.IP
|
|
|
|
port = naddr.Port
|
|
|
|
protocol = "udp"
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ip.IsGlobalUnicast() && !ip.IsUnspecified() {
|
|
|
|
// We only map global unicast & unspecified addresses ports, not broadcast, multicast, etc.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
extAddr, ok := nmgr.nat.GetMapping(protocol, port)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
var mappedAddr net.Addr
|
|
|
|
switch naddr.(type) {
|
|
|
|
case *net.TCPAddr:
|
|
|
|
mappedAddr = net.TCPAddrFromAddrPort(extAddr)
|
|
|
|
case *net.UDPAddr:
|
|
|
|
mappedAddr = net.UDPAddrFromAddrPort(extAddr)
|
|
|
|
}
|
|
|
|
mappedMaddr, err := manet.FromNetAddr(mappedAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("mapped addr can't be turned into a multiaddr %q: %s", mappedAddr, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
extMaddr := mappedMaddr
|
|
|
|
if rest != nil {
|
|
|
|
extMaddr = ma.Join(extMaddr, rest)
|
|
|
|
}
|
|
|
|
return extMaddr
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
type nmgrNetNotifiee natManager
|
|
|
|
|
|
|
|
func (nn *nmgrNetNotifiee) natManager() *natManager { return (*natManager)(nn) }
|
|
|
|
func (nn *nmgrNetNotifiee) Listen(network.Network, ma.Multiaddr) { nn.natManager().sync() }
|
|
|
|
func (nn *nmgrNetNotifiee) ListenClose(n network.Network, addr ma.Multiaddr) { nn.natManager().sync() }
|
|
|
|
func (nn *nmgrNetNotifiee) Connected(network.Network, network.Conn) {}
|
|
|
|
func (nn *nmgrNetNotifiee) Disconnected(network.Network, network.Conn) {}
|