igd_service.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. ID string
  43. URL string
  44. URN string
  45. }
  46. // AddPortMapping adds a port mapping to the specified IGD service.
  47. func (s *IGDService) AddPortMapping(localIPAddress net.IP, protocol nat.Protocol, internalPort, externalPort int, description string, duration time.Duration) error {
  48. tpl := `<u:AddPortMapping xmlns:u="%s">
  49. <NewRemoteHost></NewRemoteHost>
  50. <NewExternalPort>%d</NewExternalPort>
  51. <NewProtocol>%s</NewProtocol>
  52. <NewInternalPort>%d</NewInternalPort>
  53. <NewInternalClient>%s</NewInternalClient>
  54. <NewEnabled>1</NewEnabled>
  55. <NewPortMappingDescription>%s</NewPortMappingDescription>
  56. <NewLeaseDuration>%d</NewLeaseDuration>
  57. </u:AddPortMapping>`
  58. body := fmt.Sprintf(tpl, s.URN, externalPort, protocol, internalPort, localIPAddress, description, duration/time.Second)
  59. response, err := soapRequest(s.URL, s.URN, "AddPortMapping", body)
  60. if err != nil && duration > 0 {
  61. // Try to repair error code 725 - OnlyPermanentLeasesSupported
  62. envelope := &soapErrorResponse{}
  63. if unmarshalErr := xml.Unmarshal(response, envelope); unmarshalErr != nil {
  64. return unmarshalErr
  65. }
  66. if envelope.ErrorCode == 725 {
  67. return s.AddPortMapping(localIPAddress, protocol, externalPort, internalPort, description, 0)
  68. }
  69. }
  70. return err
  71. }
  72. // DeletePortMapping deletes a port mapping from the specified IGD service.
  73. func (s *IGDService) DeletePortMapping(protocol nat.Protocol, externalPort int) error {
  74. tpl := `<u:DeletePortMapping xmlns:u="%s">
  75. <NewRemoteHost></NewRemoteHost>
  76. <NewExternalPort>%d</NewExternalPort>
  77. <NewProtocol>%s</NewProtocol>
  78. </u:DeletePortMapping>`
  79. body := fmt.Sprintf(tpl, s.URN, externalPort, protocol)
  80. _, err := soapRequest(s.URL, s.URN, "DeletePortMapping", body)
  81. return err
  82. }
  83. // GetExternalIPAddress queries the IGD service for its external IP address.
  84. // Returns nil if the external IP address is invalid or undefined, along with
  85. // any relevant errors
  86. func (s *IGDService) GetExternalIPAddress() (net.IP, error) {
  87. tpl := `<u:GetExternalIPAddress xmlns:u="%s" />`
  88. body := fmt.Sprintf(tpl, s.URN)
  89. response, err := soapRequest(s.URL, s.URN, "GetExternalIPAddress", body)
  90. if err != nil {
  91. return nil, err
  92. }
  93. envelope := &soapGetExternalIPAddressResponseEnvelope{}
  94. err = xml.Unmarshal(response, envelope)
  95. if err != nil {
  96. return nil, err
  97. }
  98. result := net.ParseIP(envelope.Body.GetExternalIPAddressResponse.NewExternalIPAddress)
  99. return result, nil
  100. }