Merge pull request #1843 from Zillode/fix-1831
Set permanent UPnP lease when required
This commit is contained in:
commit
76174d31ce
|
@ -471,7 +471,7 @@ func soapRequest(url, service, function, message string) ([]byte, error) {
|
||||||
|
|
||||||
resp, _ = ioutil.ReadAll(r.Body)
|
resp, _ = ioutil.ReadAll(r.Body)
|
||||||
if debug {
|
if debug {
|
||||||
l.Debugln("SOAP Response:\n\n" + string(resp) + "\n")
|
l.Debugf("SOAP Response: %v\n\n%v\n\n", r.StatusCode, string(resp))
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Body.Close()
|
r.Body.Close()
|
||||||
|
@ -527,6 +527,11 @@ type getExternalIPAddressResponse struct {
|
||||||
NewExternalIPAddress string `xml:"NewExternalIPAddress"`
|
NewExternalIPAddress string `xml:"NewExternalIPAddress"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type soapErrorResponse struct {
|
||||||
|
ErrorCode int `xml:"Body>Fault>detail>UPnPError>errorCode"`
|
||||||
|
ErrorDescription string `xml:"Body>Fault>detail>UPnPError>errorDescription"`
|
||||||
|
}
|
||||||
|
|
||||||
// AddPortMapping adds a port mapping to the specified IGD service.
|
// AddPortMapping adds a port mapping to the specified IGD service.
|
||||||
func (s *IGDService) AddPortMapping(localIPAddress string, protocol Protocol, externalPort, internalPort int, description string, timeout int) error {
|
func (s *IGDService) AddPortMapping(localIPAddress string, protocol Protocol, externalPort, internalPort int, description string, timeout int) error {
|
||||||
tpl := `<u:AddPortMapping xmlns:u="%s">
|
tpl := `<u:AddPortMapping xmlns:u="%s">
|
||||||
|
@ -541,12 +546,20 @@ func (s *IGDService) AddPortMapping(localIPAddress string, protocol Protocol, ex
|
||||||
</u:AddPortMapping>`
|
</u:AddPortMapping>`
|
||||||
body := fmt.Sprintf(tpl, s.serviceURN, externalPort, protocol, internalPort, localIPAddress, description, timeout)
|
body := fmt.Sprintf(tpl, s.serviceURN, externalPort, protocol, internalPort, localIPAddress, description, timeout)
|
||||||
|
|
||||||
_, err := soapRequest(s.serviceURL, s.serviceURN, "AddPortMapping", body)
|
response, err := soapRequest(s.serviceURL, s.serviceURN, "AddPortMapping", body)
|
||||||
if err != nil {
|
if err != nil && timeout > 0 {
|
||||||
return err
|
// Try to repair error code 725 - OnlyPermanentLeasesSupported
|
||||||
|
envelope := &soapErrorResponse{}
|
||||||
|
err = xml.Unmarshal(response, envelope)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if envelope.ErrorCode == 725 {
|
||||||
|
return s.AddPortMapping(localIPAddress, protocol, externalPort, internalPort, description, 0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeletePortMapping deletes a port mapping from the specified IGD service.
|
// DeletePortMapping deletes a port mapping from the specified IGD service.
|
||||||
|
|
|
@ -33,6 +33,33 @@ func TestExternalIPParsing(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSoapFaultParsing(t *testing.T) {
|
||||||
|
soapResponse :=
|
||||||
|
[]byte(`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
|
||||||
|
<s:Body>
|
||||||
|
<s:Fault>
|
||||||
|
<faultcode>s:Client</faultcode>
|
||||||
|
<faultstring>UPnPError</faultstring>
|
||||||
|
<detail>
|
||||||
|
<UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
|
||||||
|
<errorCode>725</errorCode>
|
||||||
|
<errorDescription>OnlyPermanentLeasesSupported</errorDescription></UPnPError>
|
||||||
|
</detail>
|
||||||
|
</s:Fault>
|
||||||
|
</s:Body>
|
||||||
|
</s:Envelope>`)
|
||||||
|
|
||||||
|
envelope := &soapErrorResponse{}
|
||||||
|
err := xml.Unmarshal(soapResponse, envelope)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if envelope.ErrorCode != 725 {
|
||||||
|
t.Error("Parse of SOAP request failed.", envelope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestControlURLParsing(t *testing.T) {
|
func TestControlURLParsing(t *testing.T) {
|
||||||
rootURL := "http://192.168.243.1:80/igd.xml"
|
rootURL := "http://192.168.243.1:80/igd.xml"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue