/
upnp.go
244 lines (227 loc) · 7.2 KB
/
upnp.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
234
235
236
237
238
239
240
241
242
243
244
// Copyright (C) 2019 gyee authors
//
// This file is part of the gyee library.
//
// The gyee library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gyee library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the gyee library. If not, see <http://www.gnu.org/licenses/>.
package nat
import (
"net"
"strings"
"time"
"github.com/huin/goupnp"
"github.com/huin/goupnp/dcps/internetgateway1"
"github.com/huin/goupnp/dcps/internetgateway2"
"github.com/yeeco/gyee/log"
)
// see following packages under github.com/huin/goupnp for details:
// internetgateway1.WANIPConnection1, internetgateway1.WANPPPConnection1,
// internetgateway2.WANIPConnection1, internetgateway2.WANIPConnection2, internetgateway2.WANPPPConnection1
const (
devType1 = internetgateway1.URN_WANConnectionDevice_1
devType1Str = "internetgateway1.URN_WANConnectionDevice_1"
devType2 = internetgateway2.URN_WANConnectionDevice_2
devType2Str = "internetgateway2.URN_WANConnectionDevice_2"
srvType11 = internetgateway1.URN_WANIPConnection_1
srvType11Str = "internetgateway1.URN_WANIPConnection_1"
srvType12 = internetgateway1.URN_WANPPPConnection_1
srvType12Str = "internetgateway1.URN_WANPPPConnection_1"
srvType21 = internetgateway2.URN_WANIPConnection_1
srvType21Str = "internetgateway2.URN_WANIPConnection_1"
srvType22 = internetgateway2.URN_WANIPConnection_2
srvType22Str = "internetgateway2.URN_WANIPConnection_2"
srvType23 = internetgateway2.URN_WANPPPConnection_1
srvType23Str = "internetgateway2.URN_WANPPPConnection_1"
)
const upnpClientTimout = 3 * time.Second
type upnpClient interface {
GetExternalIPAddress() (string, error)
AddPortMapping(string, uint16, string, uint16, string, bool, string, uint32) error
DeletePortMapping(string, uint16, string) error
GetNATRSIPStatus() (sip bool, nat bool, err error)
}
type upnpCtrlBlock struct {
devType string // device type
srvType string // service type
device *goupnp.RootDevice // root device
client upnpClient // client to access services
}
func NewUpnpInterface() *upnpCtrlBlock {
cb := queryUpnp()
if cb == nil {
log.Debugf("NewUpnpInterface: queryUpnp failed")
return (*upnpCtrlBlock)(nil)
}
return cb
}
func (upnp *upnpCtrlBlock) makeMap(name string, proto string, locPort int, pubPort int, durKeep time.Duration) NatEno {
ip, eno := upnp.getLocalAddress()
if eno != NatEnoNone {
log.Debugf("makeMap: getLocalAddress failed, error: %s", eno.Error())
return eno
}
proto = strings.ToUpper(proto)
seconds := uint32(durKeep / time.Second)
// we had filtered out the duplicated case in function makeMapReq,
// but would someone else(other application)... try removing...
upnp.removeMap(proto, locPort, pubPort)
err := upnp.client.AddPortMapping("", uint16(pubPort), proto, uint16(locPort), ip.String(), true, name, seconds)
if err != nil {
log.Debugf("makeMap: AddPortMapping failed, error: %s", err.Error())
return NatEnoFromUpnpLib
}
return NatEnoNone
}
func (upnp *upnpCtrlBlock) removeMap(proto string, locPort int, pubPort int) NatEno {
proto = strings.ToUpper(proto)
if err := upnp.client.DeletePortMapping("", uint16(pubPort), proto); err != nil {
log.Debugf("removeMap: DeletePortMapping failed, error: %s", err.Error())
return NatEnoFromUpnpLib
}
return NatEnoNone
}
func (upnp *upnpCtrlBlock) getPublicIpAddr() (net.IP, NatEno) {
ipStr, err := upnp.client.GetExternalIPAddress()
if err != nil {
log.Debugf("getPublicIpAddr: GetExternalIPAddress failed, error: %s", err.Error())
return nil, NatEnoFromUpnpLib
}
ip := net.ParseIP(ipStr)
if ip == nil {
log.Debugf("getPublicIpAddr: ParseIP failed, ipStr: %s", ipStr)
return nil, NatEnoFromUpnpLib
}
return ip, NatEnoNone
}
func (upnp *upnpCtrlBlock) getLocalAddress() (net.IP, NatEno) {
devaddr, err := net.ResolveUDPAddr("udp4", upnp.device.URLBase.Host)
if err != nil {
log.Debugf("getLocalAddress: bad device address: %s", upnp.device.URLBase.Host)
return nil, NatEnoFromUpnpLib
}
ifaces, err := net.Interfaces()
if err != nil {
log.Debugf("getLocalAddress: Interfaces failed, error: %s", err.Error())
return nil, NatEnoFromSystem
}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
log.Debugf("getLocalAddress: Addrs failed, error: %s", err.Error())
return nil, NatEnoFromSystem
}
for _, addr := range addrs {
switch x := addr.(type) {
case *net.IPNet:
if x.Contains(devaddr.IP) {
return x.IP, NatEnoNone
}
}
}
}
return nil, NatEnoNotFound
}
func devType1Matcher(dev *goupnp.RootDevice, sc goupnp.ServiceClient) *upnpCtrlBlock {
switch sc.Service.ServiceType {
case srvType11:
return &upnpCtrlBlock{
devType: devType1Str,
srvType: srvType11Str,
device: dev,
client: &internetgateway1.WANIPConnection1{ServiceClient: sc},
}
case srvType12:
return &upnpCtrlBlock{
devType: devType1Str,
srvType: srvType12Str,
device: dev,
client: &internetgateway1.WANPPPConnection1{ServiceClient: sc},
}
}
return nil
}
type matcherT func(*goupnp.RootDevice, goupnp.ServiceClient) *upnpCtrlBlock
func devType2Mathcer(dev *goupnp.RootDevice, sc goupnp.ServiceClient) *upnpCtrlBlock {
switch sc.Service.ServiceType {
case srvType21:
return &upnpCtrlBlock{
devType: devType2Str,
srvType: srvType21Str,
device: dev,
client: &internetgateway2.WANIPConnection1{ServiceClient: sc},
}
case srvType22:
return &upnpCtrlBlock{
devType: devType2Str,
srvType: srvType22Str,
device: dev,
client: &internetgateway2.WANIPConnection2{ServiceClient: sc},
}
case srvType23:
return &upnpCtrlBlock{
devType: devType2Str,
srvType: srvType23Str,
device: dev,
client: &internetgateway2.WANPPPConnection1{ServiceClient: sc},
}
}
return nil
}
func queryUpnp() *upnpCtrlBlock {
found := make(chan *upnpCtrlBlock, 2)
go query(found, devType1, devType1Matcher)
go query(found, devType2, devType2Mathcer)
for i := 0; i < cap(found); i++ {
if c := <-found; c != nil {
return c
}
}
return nil
}
func query(found chan<- *upnpCtrlBlock, target string, matcher matcherT) {
devs, err := goupnp.DiscoverDevices(target)
if err != nil {
found <- nil
return
}
getone := false
for i := 0; i < len(devs) && !getone; i++ {
if devs[i].Root == nil {
continue
}
devs[i].Root.Device.VisitServices(func(service *goupnp.Service) {
if getone {
return
}
sc := goupnp.ServiceClient{
SOAPClient: service.NewSOAPClient(),
RootDevice: devs[i].Root,
Location: devs[i].Location,
Service: service,
}
sc.SOAPClient.HTTPClient.Timeout = upnpClientTimout
upnp := matcher(devs[i].Root, sc)
if upnp == nil {
return
}
if _, nat, err := upnp.client.GetNATRSIPStatus(); err != nil || !nat {
return
}
found <- upnp
getone = true
})
}
if !getone {
found <- nil
}
}