forked from scottjg/upnp
/
AddPortMapping.go
112 lines (94 loc) · 3.33 KB
/
AddPortMapping.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
package upnp
import (
// "log"
// "fmt"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
type AddPortMapping struct {
upnp *Upnp
}
func (this *AddPortMapping) Send(localPort, remotePort, duration int, internalClient string, protocol string, desc string) bool {
if !this.upnp.DurationUnsupported {
request := this.buildRequest(localPort, remotePort, duration, internalClient, protocol, desc)
response, err := http.DefaultClient.Do(request)
if err != nil {
fmt.Println("http.DurationUnsupported.Do:", err)
}
result, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println("ioutil.ReadAll:", err)
}
if response.StatusCode == 200 {
this.resolve(string(result))
return true
}
if strings.Contains(string(result), "<errorDescription>OnlyPermanentLeasesSupported</errorDescription>") {
this.upnp.DurationUnsupported = true
}
}
request := this.buildRequest(localPort, remotePort, 0, internalClient, protocol, desc)
response, err := http.DefaultClient.Do(request)
if err != nil {
fmt.Println("http.DetClient.Do:", err)
}
result, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println("ioutil.ReadAll:", err)
}
if response.StatusCode == 200 {
this.resolve(string(result))
return true
}
return false
}
func (this *AddPortMapping) buildRequest(localPort, remotePort, duration int, internalClient string, protocol string, desc string) *http.Request {
// Request header
header := http.Header{}
header.Set("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2")
header.Set("SOAPAction", `"urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"`)
header.Set("Content-Type", "text/xml")
header.Set("Connection", "Close")
header.Set("Content-Length", "")
// Request body
body := Node{Name: "SOAP-ENV:Envelope",
Attr: map[string]string{"xmlns:SOAP-ENV": `"http://schemas.xmlsoap.org/soap/envelope/"`,
"SOAP-ENV:encodingStyle": `"http://schemas.xmlsoap.org/soap/encoding/"`}}
childOne := Node{Name: `SOAP-ENV:Body`}
childTwo := Node{Name: `m:AddPortMapping`,
Attr: map[string]string{"xmlns:m": `"urn:schemas-upnp-org:service:WANIPConnection:1"`}}
childList1 := Node{Name: "NewExternalPort", Content: strconv.Itoa(remotePort)}
childList2 := Node{Name: "NewInternalPort", Content: strconv.Itoa(localPort)}
childList3 := Node{Name: "NewProtocol", Content: protocol}
childList4 := Node{Name: "NewEnabled", Content: "1"}
childList5 := Node{Name: "NewInternalClient", Content: internalClient}
childList6 := Node{Name: "NewLeaseDuration", Content: strconv.Itoa(duration)}
childList7 := Node{Name: "NewPortMappingDescription", Content: desc}
childList8 := Node{Name: "NewRemoteHost"}
childTwo.AddChild(childList1)
childTwo.AddChild(childList2)
childTwo.AddChild(childList3)
childTwo.AddChild(childList4)
childTwo.AddChild(childList5)
childTwo.AddChild(childList6)
childTwo.AddChild(childList7)
childTwo.AddChild(childList8)
childOne.AddChild(childTwo)
body.AddChild(childOne)
bodyStr := body.BuildXML()
// request
request, err := http.NewRequest("POST", "http://"+this.upnp.Gateway.Host+this.upnp.CtrlUrl,
strings.NewReader(bodyStr))
if err != nil {
fmt.Println("http.NewRequest:", err)
return nil
}
request.Header = header
request.Header.Set("Content-Length", strconv.Itoa(len([]byte(bodyStr))))
return request
}
func (this *AddPortMapping) resolve(resultStr string) {
}