-
Notifications
You must be signed in to change notification settings - Fork 671
/
nat.go
153 lines (131 loc) · 4.2 KB
/
nat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package nat
import (
"net"
"sync"
"time"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/logging"
)
const (
mapTimeout = 30 * time.Minute
maxRefreshRetries = 3
)
// Router describes the functionality that a network device must support to be
// able to open ports to an external IP.
type Router interface {
// True iff this router supports NAT
SupportsNAT() bool
// Map external port [extPort] to internal port [intPort] for [duration]
MapPort(protocol string, intPort, extPort uint16, desc string, duration time.Duration) error
// Undo a port mapping
UnmapPort(protocol string, intPort, extPort uint16) error
// Return our external IP
ExternalIP() (net.IP, error)
}
// GetRouter returns a router on the current network.
func GetRouter() Router {
if r := getUPnPRouter(); r != nil {
return r
}
if r := getPMPRouter(); r != nil {
return r
}
return NewNoRouter()
}
// Mapper attempts to open a set of ports on a router
type Mapper struct {
log logging.Logger
r Router
closer chan struct{}
wg sync.WaitGroup
}
// NewPortMapper returns an initialized mapper
func NewPortMapper(log logging.Logger, r Router) Mapper {
return Mapper{
log: log,
r: r,
closer: make(chan struct{}),
}
}
// Map external port [extPort] (exposed to the internet) to internal port [intPort] (where our process is listening)
// and set [ip]. Does this every [updateTime]. [ip] may be nil.
func (m *Mapper) Map(protocol string, intPort, extPort uint16, desc string, ip *utils.DynamicIPDesc, updateTime time.Duration) {
if !m.r.SupportsNAT() {
return
}
// we attempt a port map, and log an Error if it fails.
err := m.retryMapPort(protocol, intPort, extPort, desc, mapTimeout)
if err != nil {
m.log.Error("NAT Traversal failed from external port %d to internal port %d with %s", extPort, intPort, err)
} else {
m.log.Info("NAT Traversal successful from external port %d to internal port %d", extPort, intPort)
}
go m.keepPortMapping(protocol, intPort, extPort, desc, ip, updateTime)
}
// Retry port map up to maxRefreshRetries with a 1 second delay
func (m *Mapper) retryMapPort(protocol string, intPort, extPort uint16, desc string, timeout time.Duration) error {
var err error
for retryCnt := 0; retryCnt < maxRefreshRetries; retryCnt++ {
err = m.r.MapPort(protocol, intPort, extPort, desc, timeout)
if err == nil {
return nil
}
// log a message, sleep a second and retry.
m.log.Error("Renewing port mapping try #%d from external port %d to internal port %d failed with %s",
retryCnt+1, extPort, intPort, err)
time.Sleep(1 * time.Second)
}
return err
}
// keepPortMapping runs in the background to keep a port mapped. It renews the mapping from [extPort]
// to [intPort]] every [updateTime]. Updates [ip] every [updateTime].
func (m *Mapper) keepPortMapping(protocol string, intPort, extPort uint16, desc string, ip *utils.DynamicIPDesc, updateTime time.Duration) {
updateTimer := time.NewTimer(updateTime)
m.wg.Add(1)
defer func(extPort uint16) {
updateTimer.Stop()
m.log.Debug("Unmap protocol %s external port %d", protocol, extPort)
if err := m.r.UnmapPort(protocol, intPort, extPort); err != nil {
m.log.Debug("Error unmapping port %d to %d: %s", intPort, extPort, err)
}
m.wg.Done()
}(extPort)
for {
select {
case <-updateTimer.C:
err := m.retryMapPort(protocol, intPort, extPort, desc, mapTimeout)
if err != nil {
m.log.Warn("Renew NAT Traversal failed from external port %d to internal port %d with %s",
extPort, intPort, err)
}
m.updateIP(ip)
updateTimer.Reset(updateTime)
case <-m.closer:
return
}
}
}
func (m *Mapper) updateIP(ip *utils.DynamicIPDesc) {
if ip == nil {
return
}
newIP, err := m.r.ExternalIP()
if err != nil {
m.log.Error("failed to get external IP: %s", err)
return
}
oldIP := ip.IP().IP
ip.UpdateIP(newIP)
if !oldIP.Equal(newIP) {
m.log.Info("external IP updated to: %s", newIP)
}
}
// UnmapAllPorts stops mapping all ports from this mapper and attempts to unmap
// them.
func (m *Mapper) UnmapAllPorts() {
close(m.closer)
m.wg.Wait()
m.log.Info("Unmapped all ports")
}