-
Notifications
You must be signed in to change notification settings - Fork 2
/
upnp.go
106 lines (84 loc) · 2.18 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
package upnp
import (
"errors"
"log"
"net"
"net/url"
"strings"
"time"
"github.com/DeNetPRO/turbo-upnp/internet"
"github.com/DeNetPRO/turbo-upnp/ssdp"
)
const (
clientURLEnd = "/ctl/IPConn"
)
//Device contains real location your router (ip address)
//and clientURL. ClientURl is address, where we will send requests
type Device struct {
location string
clientURL *url.URL
}
//Initialisation device, which is detected by ssdp
func InitDevice() (*Device, error) {
device := &Device{}
times := 3
timeSleep := time.Millisecond * 500
for i := 0; i < times; i++ {
list, err := ssdp.Search(ssdp.All, 1, "")
if err != nil {
return nil, err
}
var address string
for _, srv := range list {
if srv.Type == internet.URN_WANIPConnection_1 {
address = srv.Location
}
}
if address == "" {
time.Sleep(timeSleep)
continue
}
addressSplit := strings.Split(address, "http://")
if len(addressSplit) != 2 {
return nil, errors.New("invalid address")
}
ipSplit := strings.Split(addressSplit[1], "/")
if len(ipSplit) != 2 {
return nil, errors.New("invalid address")
}
device.location = ipSplit[0]
clientAddress := "http://" + device.location + clientURLEnd
clientUrl, err := url.Parse(clientAddress)
if err != nil {
return device, err
}
device.clientURL = clientUrl
return device, nil
}
return nil, errors.New("no available internet gateway devices")
}
//Internet Gateway Device address
func (d *Device) Location() string {
return d.location
}
//Real public IP address
func (d *Device) PublicIP() (string, error) {
return internet.GetExternalIPAddress(d.clientURL)
}
//Forward opens ports on the router
func (d *Device) Forward(port int, mapDescription string) error {
return internet.AddPortMapping("", getInternalIP(), "TCP", mapDescription, uint16(port), uint16(port), true, 0, d.clientURL)
}
//Close ports on the router
func (d *Device) Close(port int) error {
return internet.DeletePortMapping("", uint16(port), "TCP", d.clientURL)
}
func getInternalIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}