forked from tongxin97/noise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upnp.go
161 lines (131 loc) · 4.22 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
package nat
import (
"github.com/huin/goupnp"
"github.com/huin/goupnp/dcps/internetgateway1"
"github.com/huin/goupnp/dcps/internetgateway2"
"github.com/pkg/errors"
"net"
"strings"
"time"
)
type upnp struct {
host string
client 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)
}
}
func (u *upnp) ExternalIP() (net.IP, error) {
raw, err := u.client.GetExternalIPAddress()
if err != nil {
return nil, err
}
ip := net.ParseIP(raw)
if ip == nil {
return nil, errors.Errorf("upnp: got invalid IP %+v", raw)
}
return ip, nil
}
func (u *upnp) AddMapping(protocol string, externalPort, internalPort uint16, lifetime time.Duration) error {
ip, err := u.deviceInternalAddress()
if err != nil {
return nil
}
_ = u.DeleteMapping(protocol, externalPort, internalPort)
return u.client.AddPortMapping("", externalPort, strings.ToUpper(protocol), internalPort, ip.String(), true, "noise", uint32(lifetime/time.Second))
}
func (u *upnp) DeleteMapping(protocol string, externalPort, internalPort uint16) error {
return u.client.DeletePortMapping("", externalPort, strings.ToUpper(protocol))
}
func (u *upnp) deviceInternalAddress() (net.IP, error) {
deviceAddress, err := net.ResolveUDPAddr("udp4", u.host)
if err != nil {
return nil, errors.Wrap(err, "upnp: failed to resolve device address")
}
interfaces, err := net.Interfaces()
if err != nil {
return nil, errors.Wrap(err, "upnp: could not load network interfaces")
}
for _, iface := range interfaces {
addresses, err := iface.Addrs()
if err != nil {
return nil, errors.Wrap(err, "upnp: could not list addresses of network interface")
}
for _, address := range addresses {
if address, ok := address.(*net.IPNet); ok && address.Contains(deviceAddress.IP) {
return address.IP, nil
}
}
}
return nil, errors.Errorf("upnp: could not find local address within device net range %v", deviceAddress)
}
func NewUPnP() Provider {
found := make(chan *upnp, 2)
// IGDv1
go discover(found, internetgateway1.URN_WANConnectionDevice_1, func(device *goupnp.RootDevice, client goupnp.ServiceClient) *upnp {
switch client.Service.ServiceType {
case internetgateway1.URN_WANIPConnection_1:
return &upnp{device.URLBase.Host, &internetgateway1.WANIPConnection1{ServiceClient: client}}
case internetgateway1.URN_WANPPPConnection_1:
return &upnp{device.URLBase.Host, &internetgateway1.WANPPPConnection1{ServiceClient: client}}
}
return nil
})
// IGDv2
go discover(found, internetgateway2.URN_WANConnectionDevice_2, func(device *goupnp.RootDevice, client goupnp.ServiceClient) *upnp {
switch client.Service.ServiceType {
case internetgateway2.URN_WANIPConnection_1:
return &upnp{device.URLBase.Host, &internetgateway2.WANIPConnection1{ServiceClient: client}}
case internetgateway2.URN_WANIPConnection_2:
return &upnp{device.URLBase.Host, &internetgateway2.WANIPConnection2{ServiceClient: client}}
case internetgateway2.URN_WANPPPConnection_1:
return &upnp{device.URLBase.Host, &internetgateway2.WANPPPConnection1{ServiceClient: client}}
}
return nil
})
for i := 0; i < cap(found); i++ {
if c := <-found; c != nil {
return c
}
}
panic("upnp: unable to find gateway that supports UPnP")
}
func discover(out chan<- *upnp, target string, matcher func(*goupnp.RootDevice, goupnp.ServiceClient) *upnp) {
devices, err := goupnp.DiscoverDevices(target)
if err != nil {
out <- nil
return
}
found := false
for i := 0; i < len(devices) && !found; i++ {
if devices[i].Root == nil {
continue
}
devices[i].Root.Device.VisitServices(func(service *goupnp.Service) {
if found {
return
}
client := goupnp.ServiceClient{
SOAPClient: service.NewSOAPClient(),
RootDevice: devices[i].Root,
Location: devices[i].Location,
Service: service,
}
client.SOAPClient.HTTPClient.Timeout = 3 * time.Second
upnp := matcher(devices[i].Root, client)
if upnp == nil {
return
}
if _, nat, err := upnp.client.GetNATRSIPStatus(); err != nil || !nat {
return
}
out <- upnp
found = true
})
}
if !found {
out <- nil
}
}