igd_service.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. // Adapted from https://github.com/jackpal/Taipei-Torrent/blob/dd88a8bfac6431c01d959ce3c745e74b8a911793/IGD.go
  7. // Copyright (c) 2010 Jack Palevich (https://github.com/jackpal/Taipei-Torrent/blob/dd88a8bfac6431c01d959ce3c745e74b8a911793/LICENSE)
  8. package upnp
  9. import (
  10. "encoding/xml"
  11. "fmt"
  12. "net"
  13. "time"
  14. "github.com/syncthing/syncthing/lib/nat"
  15. )
  16. // An IGDService is a specific service provided by an IGD.
  17. type IGDService struct {
  18. ID string
  19. URL string
  20. URN string
  21. }
  22. // AddPortMapping adds a port mapping to the specified IGD service.
  23. func (s *IGDService) AddPortMapping(localIPAddress net.IP, protocol nat.Protocol, internalPort, externalPort int, description string, duration time.Duration) error {
  24. tpl := `<u:AddPortMapping xmlns:u="%s">
  25. <NewRemoteHost></NewRemoteHost>
  26. <NewExternalPort>%d</NewExternalPort>
  27. <NewProtocol>%s</NewProtocol>
  28. <NewInternalPort>%d</NewInternalPort>
  29. <NewInternalClient>%s</NewInternalClient>
  30. <NewEnabled>1</NewEnabled>
  31. <NewPortMappingDescription>%s</NewPortMappingDescription>
  32. <NewLeaseDuration>%d</NewLeaseDuration>
  33. </u:AddPortMapping>`
  34. body := fmt.Sprintf(tpl, s.URN, externalPort, protocol, internalPort, localIPAddress, description, duration/time.Second)
  35. response, err := soapRequest(s.URL, s.URN, "AddPortMapping", body)
  36. if err != nil && duration > 0 {
  37. // Try to repair error code 725 - OnlyPermanentLeasesSupported
  38. envelope := &soapErrorResponse{}
  39. if unmarshalErr := xml.Unmarshal(response, envelope); unmarshalErr != nil {
  40. return unmarshalErr
  41. }
  42. if envelope.ErrorCode == 725 {
  43. return s.AddPortMapping(localIPAddress, protocol, externalPort, internalPort, description, 0)
  44. }
  45. }
  46. return err
  47. }
  48. // DeletePortMapping deletes a port mapping from the specified IGD service.
  49. func (s *IGDService) DeletePortMapping(protocol nat.Protocol, externalPort int) error {
  50. tpl := `<u:DeletePortMapping xmlns:u="%s">
  51. <NewRemoteHost></NewRemoteHost>
  52. <NewExternalPort>%d</NewExternalPort>
  53. <NewProtocol>%s</NewProtocol>
  54. </u:DeletePortMapping>`
  55. body := fmt.Sprintf(tpl, s.URN, externalPort, protocol)
  56. _, err := soapRequest(s.URL, s.URN, "DeletePortMapping", body)
  57. if err != nil {
  58. return err
  59. }
  60. return nil
  61. }
  62. // GetExternalIPAddress queries the IGD service for its external IP address.
  63. // Returns nil if the external IP address is invalid or undefined, along with
  64. // any relevant errors
  65. func (s *IGDService) GetExternalIPAddress() (net.IP, error) {
  66. tpl := `<u:GetExternalIPAddress xmlns:u="%s" />`
  67. body := fmt.Sprintf(tpl, s.URN)
  68. response, err := soapRequest(s.URL, s.URN, "GetExternalIPAddress", body)
  69. if err != nil {
  70. return nil, err
  71. }
  72. envelope := &soapGetExternalIPAddressResponseEnvelope{}
  73. err = xml.Unmarshal(response, envelope)
  74. if err != nil {
  75. return nil, err
  76. }
  77. result := net.ParseIP(envelope.Body.GetExternalIPAddressResponse.NewExternalIPAddress)
  78. return result, nil
  79. }