igd.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "net"
  35. "net/url"
  36. "strings"
  37. "time"
  38. "github.com/syncthing/syncthing/lib/nat"
  39. )
  40. // An IGD is a UPnP InternetGatewayDevice.
  41. type IGD struct {
  42. uuid string
  43. friendlyName string
  44. services []IGDService
  45. url *url.URL
  46. localIPAddress net.IP
  47. }
  48. func (n *IGD) ID() string {
  49. return n.uuid
  50. }
  51. func (n *IGD) FriendlyName() string {
  52. return n.friendlyName
  53. }
  54. // FriendlyIdentifier returns a friendly identifier (friendly name + IP
  55. // address) for the IGD.
  56. func (n *IGD) FriendlyIdentifier() string {
  57. return "'" + n.FriendlyName() + "' (" + strings.Split(n.URL().Host, ":")[0] + ")"
  58. }
  59. func (n *IGD) URL() *url.URL {
  60. return n.url
  61. }
  62. // AddPortMapping adds a port mapping to all relevant services on the
  63. // specified InternetGatewayDevice. Port mapping will fail and return an error
  64. // if action is fails for _any_ of the relevant services. For this reason, it
  65. // is generally better to configure port mapping for each individual service
  66. // instead.
  67. func (n *IGD) AddPortMapping(protocol nat.Protocol, internalPort, externalPort int, description string, duration time.Duration) (int, error) {
  68. for _, service := range n.services {
  69. err := service.AddPortMapping(n.localIPAddress, protocol, internalPort, externalPort, description, duration)
  70. if err != nil {
  71. return externalPort, err
  72. }
  73. }
  74. return externalPort, nil
  75. }
  76. // DeletePortMapping deletes a port mapping from all relevant services on the
  77. // specified InternetGatewayDevice. Port mapping will fail and return an error
  78. // if action is fails for _any_ of the relevant services. For this reason, it
  79. // is generally better to configure port mapping for each individual service
  80. // instead.
  81. func (n *IGD) DeletePortMapping(protocol nat.Protocol, externalPort int) error {
  82. for _, service := range n.services {
  83. err := service.DeletePortMapping(protocol, externalPort)
  84. if err != nil {
  85. return err
  86. }
  87. }
  88. return nil
  89. }
  90. // GetExternalIPAddress returns the external IP address of the IGD, or an error
  91. // if no service providing this feature exists.
  92. func (n *IGD) GetExternalIPAddress() (ip net.IP, err error) {
  93. for _, service := range n.services {
  94. ip, err = service.GetExternalIPAddress()
  95. if err == nil {
  96. break
  97. }
  98. }
  99. return
  100. }
  101. // GetLocalIPAddress returns the IP address of the local network interface
  102. // which is facing the IGD.
  103. func (n *IGD) GetLocalIPAddress() net.IP {
  104. return n.localIPAddress
  105. }