2019-06-09 07:24:20 +00:00
|
|
|
// Package nat implements NAT handling facilities
|
|
|
|
package nat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrNoExternalAddress = errors.New("no external address")
|
|
|
|
var ErrNoInternalAddress = errors.New("no internal address")
|
|
|
|
var ErrNoNATFound = errors.New("no NAT found")
|
|
|
|
|
|
|
|
// protocol is either "udp" or "tcp"
|
|
|
|
type NAT interface {
|
|
|
|
// Type returns the kind of NAT port mapping service that is used
|
|
|
|
Type() string
|
|
|
|
|
|
|
|
// GetDeviceAddress returns the internal address of the gateway device.
|
|
|
|
GetDeviceAddress() (addr net.IP, err error)
|
|
|
|
|
|
|
|
// GetExternalAddress returns the external address of the gateway device.
|
|
|
|
GetExternalAddress() (addr net.IP, err error)
|
|
|
|
|
|
|
|
// GetInternalAddress returns the address of the local host.
|
|
|
|
GetInternalAddress() (addr net.IP, err error)
|
|
|
|
|
|
|
|
// AddPortMapping maps a port on the local host to an external port.
|
2023-06-30 13:41:32 +00:00
|
|
|
AddPortMapping(ctx context.Context, protocol string, internalPort int, description string, timeout time.Duration) (mappedExternalPort int, err error)
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
// DeletePortMapping removes a port mapping.
|
2023-06-30 13:41:32 +00:00
|
|
|
DeletePortMapping(ctx context.Context, protocol string, internalPort int) (err error)
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DiscoverNATs returns all NATs discovered in the network.
|
|
|
|
func DiscoverNATs(ctx context.Context) <-chan NAT {
|
|
|
|
nats := make(chan NAT)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(nats)
|
|
|
|
|
|
|
|
upnpIg1 := discoverUPNP_IG1(ctx)
|
|
|
|
upnpIg2 := discoverUPNP_IG2(ctx)
|
|
|
|
natpmp := discoverNATPMP(ctx)
|
|
|
|
upnpGenIGDev := discoverUPNP_GenIGDev(ctx)
|
|
|
|
for upnpIg1 != nil || upnpIg2 != nil || natpmp != nil || upnpGenIGDev != nil {
|
|
|
|
var (
|
|
|
|
nat NAT
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
select {
|
|
|
|
case nat, ok = <-upnpIg1:
|
|
|
|
if !ok {
|
|
|
|
upnpIg1 = nil
|
|
|
|
}
|
|
|
|
case nat, ok = <-upnpIg2:
|
|
|
|
if !ok {
|
|
|
|
upnpIg2 = nil
|
|
|
|
}
|
|
|
|
case nat, ok = <-upnpGenIGDev:
|
|
|
|
if !ok {
|
|
|
|
upnpGenIGDev = nil
|
|
|
|
}
|
|
|
|
case nat, ok = <-natpmp:
|
|
|
|
if !ok {
|
|
|
|
natpmp = nil
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
// timeout.
|
|
|
|
return
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
select {
|
|
|
|
case nats <- nat:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nats
|
|
|
|
}
|
|
|
|
|
|
|
|
// DiscoverGateway attempts to find a gateway device.
|
2022-04-01 16:16:46 +00:00
|
|
|
func DiscoverGateway(ctx context.Context) (NAT, error) {
|
2019-06-09 07:24:20 +00:00
|
|
|
var nats []NAT
|
|
|
|
for nat := range DiscoverNATs(ctx) {
|
|
|
|
nats = append(nats, nat)
|
|
|
|
}
|
|
|
|
switch len(nats) {
|
|
|
|
case 0:
|
|
|
|
return nil, ErrNoNATFound
|
|
|
|
case 1:
|
|
|
|
return nats[0], nil
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
gw, _ := getDefaultGateway()
|
2019-06-09 07:24:20 +00:00
|
|
|
bestNAT := nats[0]
|
|
|
|
natGw, _ := bestNAT.GetDeviceAddress()
|
|
|
|
bestNATIsGw := gw != nil && natGw.Equal(gw)
|
|
|
|
// 1. Prefer gateways discovered _last_. This is an OK heuristic for
|
|
|
|
// discovering the most-upstream (furthest) NAT.
|
|
|
|
// 2. Prefer gateways that actually match our known gateway address.
|
|
|
|
// Some relays like to claim to be NATs even if they aren't.
|
|
|
|
for _, nat := range nats[1:] {
|
|
|
|
natGw, _ := nat.GetDeviceAddress()
|
|
|
|
natIsGw := gw != nil && natGw.Equal(gw)
|
|
|
|
|
|
|
|
if bestNATIsGw && !natIsGw {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
bestNATIsGw = natIsGw
|
|
|
|
bestNAT = nat
|
|
|
|
}
|
|
|
|
return bestNAT, nil
|
|
|
|
}
|
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func randomPort() int {
|
2023-06-30 13:41:32 +00:00
|
|
|
return random.Intn(math.MaxUint16-10000) + 10000
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|