igd_service.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "context"
  35. "encoding/xml"
  36. "fmt"
  37. "net"
  38. "time"
  39. "github.com/syncthing/syncthing/lib/nat"
  40. )
  41. // An IGDService is a specific service provided by an IGD.
  42. type IGDService struct {
  43. UUID string
  44. Device upnpDevice
  45. ServiceID string
  46. URL string
  47. URN string
  48. LocalIP net.IP
  49. }
  50. // AddPortMapping adds a port mapping to the specified IGD service.
  51. func (s *IGDService) AddPortMapping(ctx context.Context, protocol nat.Protocol, internalPort, externalPort int, description string, duration time.Duration) (int, error) {
  52. tpl := `<u:AddPortMapping xmlns:u="%s">
  53. <NewRemoteHost></NewRemoteHost>
  54. <NewExternalPort>%d</NewExternalPort>
  55. <NewProtocol>%s</NewProtocol>
  56. <NewInternalPort>%d</NewInternalPort>
  57. <NewInternalClient>%s</NewInternalClient>
  58. <NewEnabled>1</NewEnabled>
  59. <NewPortMappingDescription>%s</NewPortMappingDescription>
  60. <NewLeaseDuration>%d</NewLeaseDuration>
  61. </u:AddPortMapping>`
  62. body := fmt.Sprintf(tpl, s.URN, externalPort, protocol, internalPort, s.LocalIP, description, duration/time.Second)
  63. response, err := soapRequest(ctx, s.URL, s.URN, "AddPortMapping", body)
  64. if err != nil && duration > 0 {
  65. // Try to repair error code 725 - OnlyPermanentLeasesSupported
  66. envelope := &soapErrorResponse{}
  67. if unmarshalErr := xml.Unmarshal(response, envelope); unmarshalErr != nil {
  68. return externalPort, unmarshalErr
  69. }
  70. if envelope.ErrorCode == 725 {
  71. return s.AddPortMapping(ctx, protocol, internalPort, externalPort, description, 0)
  72. }
  73. }
  74. return externalPort, err
  75. }
  76. // DeletePortMapping deletes a port mapping from the specified IGD service.
  77. func (s *IGDService) DeletePortMapping(ctx context.Context, protocol nat.Protocol, externalPort int) error {
  78. tpl := `<u:DeletePortMapping xmlns:u="%s">
  79. <NewRemoteHost></NewRemoteHost>
  80. <NewExternalPort>%d</NewExternalPort>
  81. <NewProtocol>%s</NewProtocol>
  82. </u:DeletePortMapping>`
  83. body := fmt.Sprintf(tpl, s.URN, externalPort, protocol)
  84. _, err := soapRequest(ctx, s.URL, s.URN, "DeletePortMapping", body)
  85. return err
  86. }
  87. // GetExternalIPAddress queries the IGD service for its external IP address.
  88. // Returns nil if the external IP address is invalid or undefined, along with
  89. // any relevant errors
  90. func (s *IGDService) GetExternalIPAddress(ctx context.Context) (net.IP, error) {
  91. tpl := `<u:GetExternalIPAddress xmlns:u="%s" />`
  92. body := fmt.Sprintf(tpl, s.URN)
  93. response, err := soapRequest(ctx, s.URL, s.URN, "GetExternalIPAddress", body)
  94. if err != nil {
  95. return nil, err
  96. }
  97. envelope := &soapGetExternalIPAddressResponseEnvelope{}
  98. err = xml.Unmarshal(response, envelope)
  99. if err != nil {
  100. return nil, err
  101. }
  102. result := net.ParseIP(envelope.Body.GetExternalIPAddressResponse.NewExternalIPAddress)
  103. return result, nil
  104. }
  105. // GetLocalIPAddress returns local IP address used to contact this service
  106. func (s *IGDService) GetLocalIPAddress() net.IP {
  107. return s.LocalIP
  108. }
  109. // ID returns a unique ID for the servic
  110. func (s *IGDService) ID() string {
  111. return s.UUID + "/" + s.Device.FriendlyName + "/" + s.ServiceID + "/" + s.URN + "/" + s.URL
  112. }