This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
igd.go
237 lines (207 loc) · 8.08 KB
/
igd.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
/*
* Copyright (c) 2014, The Tor Project, Inc.
* See LICENSE for licensing information
*/
package upnp
import (
"bufio"
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"math"
"net"
"net/http"
"strconv"
"syscall"
)
const maxMappingDuration = 604800
// The people who made this abomination of a protocol used SOAP. Presumably
// the "right" way to do this is to use an existing SOAP client, but Go does
// not have such a thing.
type soapEnvelope struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
EncodingStyle string `xml:"http://schemas.xmlsoap.org/soap/envelope/ encodingStyle,attr"`
Body soapBody `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
}
type soapBody struct {
Fault *soapFault `xml:"Fault"`
GetExternalIPAddressResponse *getExtIPResponse `xml:"GetExternalIPAddressResponse"`
GetGenericPortMappingEntryResponse *getGenPMapEntResponse `xml:"GetGenericPortMappingEntryResponse"`
}
type soapFault struct {
FaultCode string `xml:"faultcode"`
FaultString string `xml:"faultstring"`
Detail *soapDetail `xml:"detail"`
}
type soapDetail struct {
UPnPError *upnpError `xml:"UPnPError"`
}
type upnpError struct {
ErrorCode int `xml:"errorCode"`
ErrorDescription string `xml:"errorDescription"`
}
type getExtIPResponse struct {
IP string `xml:"NewExternalIPAddress"`
}
type getGenPMapEntResponse struct {
RemoteHost string `xml:"NewRemoteHost"`
ExternalPort int `xml:"NewExternalPort"`
Protocol string `xml:"NewProtocol"`
InternalPort int `xml:"NewInternalPort"`
InternalClient string `xml:"NewInternalClient"`
Enabled int `xml:"NewEnabled"`
PortMappingDescription string `xml:"NewPortMappingDescription"`
LeaseDuration int `xml:"NewLeaseDuration"`
}
func (f *soapFault) String() string {
if f.Detail.UPnPError != nil {
return fmt.Sprintf("upnp error: %d - %s", f.Detail.UPnPError.ErrorCode, f.Detail.UPnPError.ErrorDescription)
}
return fmt.Sprintf("fault: %s - %s", f.FaultCode, f.FaultString)
}
func (c *Client) issueSoapRequest(actionName, argsXML string) (*soapBody, error) {
// Apparently a lot of routers puke horribly on XML that's well-formed but
// not exactly what they expect, so requests are crafted by hand. At a
// future time when more than 2 requests need to be supported, revisit.
const header = xml.Header +
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
"<s:Body>"
const footer = "</s:Body></s:Envelope>"
actionOpen := "<u:" + actionName + " xmlns:u=\"" + c.ctrl.urn.String() + "\">"
actionClose := "</u:" + actionName + ">"
body := []byte(header + actionOpen + argsXML + actionClose + footer)
soapAction := "\"" + c.ctrl.urn.String() + "#" + actionName + "\""
c.Vlogf("soap: issuing %s\n", actionName)
// miniupnpd (used by a lot of routers) can't handle chunked transfer
// encoding at all and just passes the raw body to it's XML parser. This
// is all sorts of garbage and violates RFC 2616.
reqBuf := bytes.NewBuffer(body)
req, err := http.NewRequest("POST", c.ctrl.url.String(), bufio.NewReader(reqBuf))
if err != nil {
return nil, err
}
req.ContentLength = int64(len(body))
req.TransferEncoding = []string{"identity"}
req.Header.Set("Content-Type", "text/xml; charset=\"utf-8\"")
req.Header.Set("User-Agent", userAgent)
req.Header.Set("SOAPAction", soapAction)
httpTransport := &http.Transport{DisableKeepAlives: true, DisableCompression: true}
httpClient := &http.Client{Transport: httpTransport}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
respEnvelope := &soapEnvelope{}
if err = xml.Unmarshal(body, respEnvelope); err != nil {
return nil, err
}
if respEnvelope.Body.Fault != nil {
return nil, fmt.Errorf("soap: %s", respEnvelope.Body.Fault)
}
if resp.StatusCode != http.StatusOK {
// Yes, this is at the end because the SOAP Fault gives more useful
// diagnostics than "500 Internal Server Error".
return nil, fmt.Errorf("soap: request failed with status: %s", resp.Status)
}
return &respEnvelope.Body, nil
}
// GetExternalIPAddress queries the router's external IP address.
func (c *Client) GetExternalIPAddress() (net.IP, error) {
respBody, err := c.issueSoapRequest("GetExternalIPAddress", "")
if err != nil {
return nil, err
}
if respBody.GetExternalIPAddressResponse != nil {
ip := net.ParseIP(respBody.GetExternalIPAddressResponse.IP)
if ip != nil {
return ip, nil
}
}
return nil, fmt.Errorf("igd: GetExternalIPAddress() failed")
}
// GetListOfPortMappings queries the router for the list of port forwarding
// entries.
func (c *Client) GetListOfPortMappings() ([]string, error) {
// Sad panda, GetListOfPortMappings requires IGD2 or later, so emulate it
// with GetGenericPortMappingEntry. Theoretically if the number of entries
// changes during this process we would need to start over from the
// begining, but we don't monitor events so we can't tell.
var resps []string
for idx := 0; idx < math.MaxUint16; idx++ {
argsXML := "<NewPortMappingIndex>" + strconv.FormatUint(uint64(idx), 10) + "</NewPortMappingIndex>"
respBody, err := c.issueSoapRequest("GetGenericPortMappingEntry", argsXML)
if err != nil {
// Probably SpecifiedArrayIndexInvalid. (XXX: Check?)
c.Vlogf("igd: GetGenericPortMappingEntry returned: %s\n", err)
break
}
if respBody.GetGenericPortMappingEntryResponse != nil {
// Too long, much too long.
r := respBody.GetGenericPortMappingEntryResponse
remoteHost := r.RemoteHost
if remoteHost == "" {
remoteHost = "0.0.0.0"
}
s := fmt.Sprintf("'%s' %s:%d <-> %s:%d %s (%d sec)",
r.PortMappingDescription,
r.InternalClient,
r.InternalPort,
remoteHost,
r.ExternalPort,
r.Protocol,
r.LeaseDuration)
c.Vlogf("%s\n", s)
resps = append(resps, s)
}
}
return resps, nil
}
// AddPortMapping adds a new TCP/IP port mapping. The internal IP address of
// the client is used as the destination. Per the UPnP spec, duration can
// range from 0 to 604800, with the behavior on 0 changing depending on the
// version of the spec.
func (c *Client) AddPortMapping(descr string, internalPort, externalPort, duration int) error {
if duration > maxMappingDuration {
return syscall.ERANGE
}
c.Vlogf("AddPortMapping: '%s' %s:%d <-> 0.0.0.0:%d (%d sec)\n", descr, c.internalAddr, internalPort, externalPort, duration)
argsXML := "<NewRemoteHost></NewRemoteHost>" +
"<NewExternalPort>" + strconv.FormatUint(uint64(externalPort), 10) + "</NewExternalPort>" +
"<NewProtocol>TCP</NewProtocol>" +
"<NewInternalPort>" + strconv.FormatUint(uint64(internalPort), 10) + "</NewInternalPort>" +
"<NewInternalClient>" + c.internalAddr.String() + "</NewInternalClient>" +
"<NewEnabled>1</NewEnabled>" +
"<NewPortMappingDescription>" + descr + "</NewPortMappingDescription>" +
"<NewLeaseDuration>" + strconv.FormatUint(uint64(duration), 10) + "</NewLeaseDuration>"
// HTTP 200 means that things worked. The response isn't interesting
// enough to warrant parsing.
_, err := c.issueSoapRequest("AddPortMapping", argsXML)
if err != nil {
c.Vlogf("igd: AddPortMapping failed: %s\n", err)
return err
}
return nil
}
// DeletePortMapping removes an existing TCP/IP port forwarding entry
// between clientIP:internalPort and 0.0.0.0:externalPort.
func (c *Client) DeletePortMapping(internalPort, externalPort int) error {
c.Vlogf("DeletePortMapping: %s:%d <-> 0.0.0.0:%d\n", c.internalAddr, internalPort, externalPort)
argsXML := "<NewRemoteHost></NewRemoteHost>" +
"<NewExternalPort>" + strconv.FormatUint(uint64(externalPort), 10) + "</NewExternalPort>" +
"<NewProtocol>TCP</NewProtocol>"
// HTTP 200 means that things worked. The response isn't interesting
// enough to warrant parsing.
_, err := c.issueSoapRequest("DeletePortMapping", argsXML)
if err != nil {
c.Vlogf("igd: DeletePortMapping failed: %s\n", err)
return err
}
return nil
}