|
| 1 | +package nat |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/huin/goupnp/httpu" |
| 12 | +) |
| 13 | + |
| 14 | +func TestUPNP_DDWRT(t *testing.T) { |
| 15 | + dev := &fakeIGD{ |
| 16 | + t: t, |
| 17 | + ssdpResp: "HTTP/1.1 200 OK\r\n" + |
| 18 | + "Cache-Control: max-age=300\r\n" + |
| 19 | + "Date: Sun, 10 May 2015 10:05:33 GMT\r\n" + |
| 20 | + "Ext: \r\n" + |
| 21 | + "Location: http://{{listenAddr}}/InternetGatewayDevice.xml\r\n" + |
| 22 | + "Server: POSIX UPnP/1.0 DD-WRT Linux/V24\r\n" + |
| 23 | + "ST: urn:schemas-upnp-org:device:WANConnectionDevice:1\r\n" + |
| 24 | + "USN: uuid:CB2471CC-CF2E-9795-8D9C-E87B34C16800::urn:schemas-upnp-org:device:WANConnectionDevice:1\r\n" + |
| 25 | + "\r\n", |
| 26 | + httpResps: map[string]string{ |
| 27 | + "GET /InternetGatewayDevice.xml": ` |
| 28 | + <?xml version="1.0"?> |
| 29 | + <root xmlns="urn:schemas-upnp-org:device-1-0"> |
| 30 | + <specVersion> |
| 31 | + <major>1</major> |
| 32 | + <minor>0</minor> |
| 33 | + </specVersion> |
| 34 | + <device> |
| 35 | + <deviceType>urn:schemas-upnp-org:device:InternetGatewayDevice:1</deviceType> |
| 36 | + <manufacturer>DD-WRT</manufacturer> |
| 37 | + <manufacturerURL>http://www.dd-wrt.com</manufacturerURL> |
| 38 | + <modelDescription>Gateway</modelDescription> |
| 39 | + <friendlyName>Asus RT-N16:DD-WRT</friendlyName> |
| 40 | + <modelName>Asus RT-N16</modelName> |
| 41 | + <modelNumber>V24</modelNumber> |
| 42 | + <serialNumber>0000001</serialNumber> |
| 43 | + <modelURL>http://www.dd-wrt.com</modelURL> |
| 44 | + <UDN>uuid:A13AB4C3-3A14-E386-DE6A-EFEA923A06FE</UDN> |
| 45 | + <serviceList> |
| 46 | + <service> |
| 47 | + <serviceType>urn:schemas-upnp-org:service:Layer3Forwarding:1</serviceType> |
| 48 | + <serviceId>urn:upnp-org:serviceId:L3Forwarding1</serviceId> |
| 49 | + <SCPDURL>/x_layer3forwarding.xml</SCPDURL> |
| 50 | + <controlURL>/control?Layer3Forwarding</controlURL> |
| 51 | + <eventSubURL>/event?Layer3Forwarding</eventSubURL> |
| 52 | + </service> |
| 53 | + </serviceList> |
| 54 | + <deviceList> |
| 55 | + <device> |
| 56 | + <deviceType>urn:schemas-upnp-org:device:WANDevice:1</deviceType> |
| 57 | + <friendlyName>WANDevice</friendlyName> |
| 58 | + <manufacturer>DD-WRT</manufacturer> |
| 59 | + <manufacturerURL>http://www.dd-wrt.com</manufacturerURL> |
| 60 | + <modelDescription>Gateway</modelDescription> |
| 61 | + <modelName>router</modelName> |
| 62 | + <modelURL>http://www.dd-wrt.com</modelURL> |
| 63 | + <UDN>uuid:48FD569B-F9A9-96AE-4EE6-EB403D3DB91A</UDN> |
| 64 | + <serviceList> |
| 65 | + <service> |
| 66 | + <serviceType>urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1</serviceType> |
| 67 | + <serviceId>urn:upnp-org:serviceId:WANCommonIFC1</serviceId> |
| 68 | + <SCPDURL>/x_wancommoninterfaceconfig.xml</SCPDURL> |
| 69 | + <controlURL>/control?WANCommonInterfaceConfig</controlURL> |
| 70 | + <eventSubURL>/event?WANCommonInterfaceConfig</eventSubURL> |
| 71 | + </service> |
| 72 | + </serviceList> |
| 73 | + <deviceList> |
| 74 | + <device> |
| 75 | + <deviceType>urn:schemas-upnp-org:device:WANConnectionDevice:1</deviceType> |
| 76 | + <friendlyName>WAN Connection Device</friendlyName> |
| 77 | + <manufacturer>DD-WRT</manufacturer> |
| 78 | + <manufacturerURL>http://www.dd-wrt.com</manufacturerURL> |
| 79 | + <modelDescription>Gateway</modelDescription> |
| 80 | + <modelName>router</modelName> |
| 81 | + <modelURL>http://www.dd-wrt.com</modelURL> |
| 82 | + <UDN>uuid:CB2471CC-CF2E-9795-8D9C-E87B34C16800</UDN> |
| 83 | + <serviceList> |
| 84 | + <service> |
| 85 | + <serviceType>urn:schemas-upnp-org:service:WANIPConnection:1</serviceType> |
| 86 | + <serviceId>urn:upnp-org:serviceId:WANIPConn1</serviceId> |
| 87 | + <SCPDURL>/x_wanipconnection.xml</SCPDURL> |
| 88 | + <controlURL>/control?WANIPConnection</controlURL> |
| 89 | + <eventSubURL>/event?WANIPConnection</eventSubURL> |
| 90 | + </service> |
| 91 | + </serviceList> |
| 92 | + </device> |
| 93 | + </deviceList> |
| 94 | + </device> |
| 95 | + <device> |
| 96 | + <deviceType>urn:schemas-upnp-org:device:LANDevice:1</deviceType> |
| 97 | + <friendlyName>LANDevice</friendlyName> |
| 98 | + <manufacturer>DD-WRT</manufacturer> |
| 99 | + <manufacturerURL>http://www.dd-wrt.com</manufacturerURL> |
| 100 | + <modelDescription>Gateway</modelDescription> |
| 101 | + <modelName>router</modelName> |
| 102 | + <modelURL>http://www.dd-wrt.com</modelURL> |
| 103 | + <UDN>uuid:04021998-3B35-2BDB-7B3C-99DA4435DA09</UDN> |
| 104 | + <serviceList> |
| 105 | + <service> |
| 106 | + <serviceType>urn:schemas-upnp-org:service:LANHostConfigManagement:1</serviceType> |
| 107 | + <serviceId>urn:upnp-org:serviceId:LANHostCfg1</serviceId> |
| 108 | + <SCPDURL>/x_lanhostconfigmanagement.xml</SCPDURL> |
| 109 | + <controlURL>/control?LANHostConfigManagement</controlURL> |
| 110 | + <eventSubURL>/event?LANHostConfigManagement</eventSubURL> |
| 111 | + </service> |
| 112 | + </serviceList> |
| 113 | + </device> |
| 114 | + </deviceList> |
| 115 | + <presentationURL>http://{{listenAddr}}</presentationURL> |
| 116 | + </device> |
| 117 | + </root> |
| 118 | + `, |
| 119 | + // The response to our GetNATRSIPStatus call. This |
| 120 | + // particular implementation has a bug where the elements |
| 121 | + // inside u:GetNATRSIPStatusResponse are not properly |
| 122 | + // namespaced. |
| 123 | + "POST /control?WANIPConnection": ` |
| 124 | + <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> |
| 125 | + <s:Body> |
| 126 | + <u:GetNATRSIPStatusResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"> |
| 127 | + <NewRSIPAvailable>0</NewRSIPAvailable> |
| 128 | + <NewNATEnabled>1</NewNATEnabled> |
| 129 | + </u:GetNATRSIPStatusResponse> |
| 130 | + </s:Body> |
| 131 | + </s:Envelope> |
| 132 | + `, |
| 133 | + }, |
| 134 | + } |
| 135 | + if err := dev.listen(); err != nil { |
| 136 | + t.Skipf("cannot listen: %v", err) |
| 137 | + } |
| 138 | + dev.serve() |
| 139 | + defer dev.close() |
| 140 | + |
| 141 | + // Attempt to discover the fake device. |
| 142 | + discovered := discoverUPnP() |
| 143 | + if discovered == nil { |
| 144 | + t.Fatalf("not discovered") |
| 145 | + } |
| 146 | + upnp, _ := discovered.(*upnp) |
| 147 | + if upnp.service != "IGDv1-IP1" { |
| 148 | + t.Errorf("upnp.service mismatch: got %q, want %q", upnp.service, "IGDv1-IP1") |
| 149 | + } |
| 150 | + wantURL := "http://" + dev.listener.Addr().String() + "/InternetGatewayDevice.xml" |
| 151 | + if upnp.dev.URLBaseStr != wantURL { |
| 152 | + t.Errorf("upnp.dev.URLBaseStr mismatch: got %q, want %q", upnp.dev.URLBaseStr, wantURL) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// fakeIGD presents itself as a discoverable UPnP device which sends |
| 157 | +// canned responses to HTTPU and HTTP requests. |
| 158 | +type fakeIGD struct { |
| 159 | + t *testing.T // for logging |
| 160 | + |
| 161 | + listener net.Listener |
| 162 | + mcastListener *net.UDPConn |
| 163 | + |
| 164 | + // This should be a complete HTTP response (including headers). |
| 165 | + // It is sent as the response to any sspd packet. Any occurrence |
| 166 | + // of "{{listenAddr}}" is replaced with the actual TCP listen |
| 167 | + // address of the HTTP server. |
| 168 | + ssdpResp string |
| 169 | + // This one should contain XML payloads for all requests |
| 170 | + // performed. The keys contain method and path, e.g. "GET /foo/bar". |
| 171 | + // As with ssdpResp, "{{listenAddr}}" is replaced with the TCP |
| 172 | + // listen address. |
| 173 | + httpResps map[string]string |
| 174 | +} |
| 175 | + |
| 176 | +// httpu.Handler |
| 177 | +func (dev *fakeIGD) ServeMessage(r *http.Request) { |
| 178 | + dev.t.Logf(`HTTPU request %s %s`, r.Method, r.RequestURI) |
| 179 | + conn, err := net.Dial("udp4", r.RemoteAddr) |
| 180 | + if err != nil { |
| 181 | + fmt.Printf("reply Dial error: %v", err) |
| 182 | + return |
| 183 | + } |
| 184 | + defer conn.Close() |
| 185 | + io.WriteString(conn, dev.replaceListenAddr(dev.ssdpResp)) |
| 186 | +} |
| 187 | + |
| 188 | +// http.Handler |
| 189 | +func (dev *fakeIGD) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 190 | + if resp, ok := dev.httpResps[r.Method+" "+r.RequestURI]; ok { |
| 191 | + dev.t.Logf(`HTTP request "%s %s" --> %d`, r.Method, r.RequestURI, 200) |
| 192 | + io.WriteString(w, dev.replaceListenAddr(resp)) |
| 193 | + } else { |
| 194 | + dev.t.Logf(`HTTP request "%s %s" --> %d`, r.Method, r.RequestURI, 404) |
| 195 | + w.WriteHeader(http.StatusNotFound) |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +func (dev *fakeIGD) replaceListenAddr(resp string) string { |
| 200 | + return strings.Replace(resp, "{{listenAddr}}", dev.listener.Addr().String(), -1) |
| 201 | +} |
| 202 | + |
| 203 | +func (dev *fakeIGD) listen() (err error) { |
| 204 | + if dev.listener, err = net.Listen("tcp", "127.0.0.1:0"); err != nil { |
| 205 | + return err |
| 206 | + } |
| 207 | + laddr := &net.UDPAddr{IP: net.ParseIP("239.255.255.250"), Port: 1900} |
| 208 | + if dev.mcastListener, err = net.ListenMulticastUDP("udp", nil, laddr); err != nil { |
| 209 | + dev.listener.Close() |
| 210 | + return err |
| 211 | + } |
| 212 | + return nil |
| 213 | +} |
| 214 | + |
| 215 | +func (dev *fakeIGD) serve() { |
| 216 | + go httpu.Serve(dev.mcastListener, dev) |
| 217 | + go http.Serve(dev.listener, dev) |
| 218 | +} |
| 219 | + |
| 220 | +func (dev *fakeIGD) close() { |
| 221 | + dev.mcastListener.Close() |
| 222 | + dev.listener.Close() |
| 223 | +} |
0 commit comments