/
maploop.go
233 lines (189 loc) · 4.43 KB
/
maploop.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package portmap
import "net"
import "time"
import "github.com/hlandau/portmap/ssdp"
import "github.com/hlandau/portmap/upnp"
import "github.com/hlandau/portmap/natpmp"
import "github.com/hlandau/xlog"
var log, Log = xlog.NewQuiet("portmap")
type mode int
const (
modeNATPMP mode = iota
modeUPnP
)
func (m *mapping) portMappingLoop(gwa []net.IP) {
aborting := false
mode := modeNATPMP
var ok bool
var d time.Duration
for {
// Already inactive (e.g. expired or was never active), so no need to do anything.
if aborting && !m.lIsActive() {
return
}
switch mode {
case modeNATPMP:
ok = m.tryNATPMP(gwa, aborting)
if ok {
d = m.cfg.Lifetime / 2
} else {
svc := ssdp.GetServicesByType(upnpWANIPConnectionURN)
if len(svc) > 0 {
// NAT-PMP failed and UPnP is available, so switch to it
mode = modeUPnP
log.Debug("NAT-PMP failed and UPnP is available, switching to UPnP")
continue
}
}
case modeUPnP:
svcs := ssdp.GetServicesByType(upnpWANIPConnectionURN)
if len(svcs) == 0 {
mode = modeNATPMP
log.Debug("UPnP not available, switching to NAT-PMP")
continue
}
ok = m.tryUPnP(svcs, aborting)
d = 1 * time.Hour
}
// If we are aborting, then the call we just made was to remove the mapping,
// not set it, and we're done.
if aborting {
m.setInactive()
return
}
// Backoff
if ok {
m.cfg.Backoff.Reset()
} else {
// failed, do retry delay
d = m.cfg.Backoff.NextDelay()
if d == 0 {
// max tries occurred
m.setInactive()
return
}
}
m.notify()
select {
case <-m.abortChan:
aborting = true
case <-time.After(d):
// wait until we need to renew
}
}
}
func (m *mapping) notify() {
ea := m.ExternalAddr()
m.mutex.Lock()
defer m.mutex.Unlock()
if m.prevValue == ea {
// no change
return
}
m.prevValue = ea
select {
case m.notifyChan <- struct{}{}:
default:
}
}
// NAT-PMP
func (m *mapping) tryNATPMP(gwa []net.IP, destroy bool) bool {
for _, gw := range gwa {
if m.tryNATPMPGW(gw, destroy) {
return true
}
}
return false
}
func (m *mapping) tryNATPMPGW(gw net.IP, destroy bool) bool {
var externalPort uint16
var actualLifetime time.Duration
var err error
var preferredLifetime time.Duration
if destroy && !m.lIsActive() {
// no point destroying if we're not active
return true
} else if !destroy {
// lifetime is zero if we're destroying
preferredLifetime = m.cfg.Lifetime
}
// attempt mapping
externalPort, actualLifetime, err = natpmp.Map(gw,
natpmp.Protocol(m.cfg.Protocol), m.cfg.InternalPort, m.cfg.ExternalPort, preferredLifetime)
if err != nil {
log.Infof("NAT-PMP failed: %v", err)
return false
}
m.mutex.Lock()
m.cfg.ExternalPort = externalPort
m.cfg.Lifetime = actualLifetime
m.mutex.Unlock()
if preferredLifetime == 0 {
// we have finished tearing down the mapping by mapping it with a
// lifetime of zero, so return
return true
}
expireTime := time.Now().Add(actualLifetime)
// Now attempt to get the external IP.
extIP, err := natpmp.GetExternalAddr(gw)
m.mutex.Lock()
defer m.mutex.Unlock()
// update external address
if err == nil {
m.externalAddr = extIP.String()
}
m.expireTime = expireTime
return true
}
// UPnP
func (m *mapping) tryUPnP(svcs []ssdp.Service, destroy bool) bool {
for _, svc := range svcs {
if m.tryUPnPSvc(svc, destroy) {
return true
}
}
return false
}
const upnpWANIPConnectionURN = "urn:schemas-upnp-org:service:WANIPConnection:1"
func (m *mapping) tryUPnPSvc(svc ssdp.Service, destroy bool) bool {
if destroy {
// unmapping
if !m.lIsActive() {
return true
}
err := upnp.Unmap(svc.Location.String(), upnp.Protocol(m.cfg.Protocol), m.cfg.ExternalPort)
return err == nil
}
// mapping
actualExternalPort, err := upnp.Map(svc.Location.String(), upnp.Protocol(m.cfg.Protocol),
m.cfg.InternalPort,
m.cfg.ExternalPort, m.cfg.Name, m.cfg.Lifetime)
if err != nil {
return false
}
m.mutex.Lock()
m.expireTime = time.Now().Add(m.cfg.Lifetime)
m.cfg.ExternalPort = actualExternalPort
m.mutex.Unlock()
// Now attempt to get the external IP.
if destroy {
return true
}
extIP, err := upnp.GetExternalAddr(svc.Location.String())
if err != nil {
// mapping till succeeded
return true
}
// update external address
m.mutex.Lock()
m.externalAddr = extIP.String()
m.mutex.Unlock()
return true
}
//
func (m *mapping) setInactive() {
m.mutex.Lock()
m.expireTime = time.Time{}
m.mutex.Unlock()
m.notify()
}