-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
upnp.go
108 lines (83 loc) · 2.34 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
package nat
import (
"context"
"fmt"
"net"
"sync"
upnp "github.com/NebulousLabs/go-upnp"
)
// Compile-time check to ensure UPnP implements the Traversal interface.
var _ Traversal = (*UPnP)(nil)
// UPnP is a concrete implementation of the Traversal interface that uses the
// UPnP technique.
type UPnP struct {
device *upnp.IGD
forwardedPortsMtx sync.Mutex
forwardedPorts map[uint16]struct{}
}
// DiscoverUPnP scans the local network for a UPnP enabled device.
func DiscoverUPnP(ctx context.Context) (*UPnP, error) {
// Scan the local network for a UPnP-enabled device.
device, err := upnp.DiscoverCtx(ctx)
if err != nil {
return nil, err
}
u := &UPnP{
device: device,
forwardedPorts: make(map[uint16]struct{}),
}
// We'll then attempt to retrieve the external IP address of this
// device to ensure it is not behind multiple NATs.
if _, err := u.ExternalIP(); err != nil {
return nil, err
}
return u, nil
}
// ExternalIP returns the external IP address of the UPnP enabled device.
func (u *UPnP) ExternalIP() (net.IP, error) {
ip, err := u.device.ExternalIP()
if err != nil {
return nil, err
}
if isPrivateIP(net.ParseIP(ip)) {
return nil, ErrMultipleNAT
}
return net.ParseIP(ip), nil
}
// AddPortMapping enables port forwarding for the given port.
func (u *UPnP) AddPortMapping(port uint16) error {
u.forwardedPortsMtx.Lock()
defer u.forwardedPortsMtx.Unlock()
if err := u.device.Forward(port, ""); err != nil {
return err
}
u.forwardedPorts[port] = struct{}{}
return nil
}
// DeletePortMapping disables port forwarding for the given port.
func (u *UPnP) DeletePortMapping(port uint16) error {
u.forwardedPortsMtx.Lock()
defer u.forwardedPortsMtx.Unlock()
if _, exists := u.forwardedPorts[port]; !exists {
return fmt.Errorf("port %d is not being forwarded", port)
}
if err := u.device.Clear(port); err != nil {
return err
}
delete(u.forwardedPorts, port)
return nil
}
// ForwardedPorts returns a list of ports currently being forwarded.
func (u *UPnP) ForwardedPorts() []uint16 {
u.forwardedPortsMtx.Lock()
defer u.forwardedPortsMtx.Unlock()
ports := make([]uint16, 0, len(u.forwardedPorts))
for port := range u.forwardedPorts {
ports = append(ports, port)
}
return ports
}
// Name returns the name of the specific NAT traversal technique used.
func (u *UPnP) Name() string {
return "UPnP"
}