igd_service.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // Adapted from https://github.com/jackpal/Taipei-Torrent/blob/dd88a8bfac6431c01d959ce3c745e74b8a911793/IGD.go
  4. // Copyright (c) 2010 Jack Palevich (https://github.com/jackpal/Taipei-Torrent/blob/dd88a8bfac6431c01d959ce3c745e74b8a911793/LICENSE)
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. //
  32. package upnp
  33. import (
  34. "encoding/xml"
  35. "fmt"
  36. "net"
  37. "time"
  38. "github.com/syncthing/syncthing/lib/nat"
  39. )
  40. // An IGDService is a specific service provided by an IGD.
  41. type IGDService struct {
  42. UUID string
  43. Device upnpDevice
  44. ServiceID string
  45. URL string
  46. URN string
  47. LocalIP net.IP
  48. }
  49. // AddPortMapping adds a port mapping to the specified IGD service.
  50. func (s *IGDService) AddPortMapping(protocol nat.Protocol, internalPort, externalPort int, description string, duration time.Duration) (int, error) {
  51. tpl := `<u:AddPortMapping xmlns:u="%s">
  52. <NewRemoteHost></NewRemoteHost>
  53. <NewExternalPort>%d</NewExternalPort>
  54. <NewProtocol>%s</NewProtocol>
  55. <NewInternalPort>%d</NewInternalPort>
  56. <NewInternalClient>%s</NewInternalClient>
  57. <NewEnabled>1</NewEnabled>
  58. <NewPortMappingDescription>%s</NewPortMappingDescription>
  59. <NewLeaseDuration>%d</NewLeaseDuration>
  60. </u:AddPortMapping>`
  61. body := fmt.Sprintf(tpl, s.URN, externalPort, protocol, internalPort, s.LocalIP, description, duration/time.Second)
  62. response, err := soapRequest(s.URL, s.URN, "AddPortMapping", body)
  63. if err != nil && duration > 0 {
  64. // Try to repair error code 725 - OnlyPermanentLeasesSupported
  65. envelope := &soapErrorResponse{}
  66. if unmarshalErr := xml.Unmarshal(response, envelope); unmarshalErr != nil {
  67. return externalPort, unmarshalErr
  68. }
  69. if envelope.ErrorCode == 725 {
  70. return s.AddPortMapping(protocol, internalPort, externalPort, description, 0)
  71. }
  72. }
  73. return externalPort, err
  74. }
  75. // DeletePortMapping deletes a port mapping from the specified IGD service.
  76. func (s *IGDService) DeletePortMapping(protocol nat.Protocol, externalPort int) error {
  77. tpl := `<u:DeletePortMapping xmlns:u="%s">
  78. <NewRemoteHost></NewRemoteHost>
  79. <NewExternalPort>%d</NewExternalPort>
  80. <NewProtocol>%s</NewProtocol>
  81. </u:DeletePortMapping>`
  82. body := fmt.Sprintf(tpl, s.URN, externalPort, protocol)
  83. _, err := soapRequest(s.URL, s.URN, "DeletePortMapping", body)
  84. return err
  85. }
  86. // GetExternalIPAddress queries the IGD service for its external IP address.
  87. // Returns nil if the external IP address is invalid or undefined, along with
  88. // any relevant errors
  89. func (s *IGDService) GetExternalIPAddress() (net.IP, error) {
  90. tpl := `<u:GetExternalIPAddress xmlns:u="%s" />`
  91. body := fmt.Sprintf(tpl, s.URN)
  92. response, err := soapRequest(s.URL, s.URN, "GetExternalIPAddress", body)
  93. if err != nil {
  94. return nil, err
  95. }
  96. envelope := &soapGetExternalIPAddressResponseEnvelope{}
  97. err = xml.Unmarshal(response, envelope)
  98. if err != nil {
  99. return nil, err
  100. }
  101. result := net.ParseIP(envelope.Body.GetExternalIPAddressResponse.NewExternalIPAddress)
  102. return result, nil
  103. }
  104. // GetLocalIPAddress returns local IP address used to contact this service
  105. func (s *IGDService) GetLocalIPAddress() net.IP {
  106. return s.LocalIP
  107. }
  108. // ID returns a unique ID for the servic
  109. func (s *IGDService) ID() string {
  110. return s.UUID + "/" + s.Device.FriendlyName + "/" + s.ServiceID + "/" + s.URN + "/" + s.URL
  111. }