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