util.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. package connections
  7. import (
  8. "net"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "github.com/syncthing/syncthing/lib/nat"
  13. "github.com/syncthing/syncthing/lib/osutil"
  14. )
  15. func fixupPort(uri *url.URL, defaultPort int) *url.URL {
  16. copyURI := *uri
  17. host, port, err := net.SplitHostPort(uri.Host)
  18. if e, ok := err.(*net.AddrError); ok && strings.Contains(e.Err, "missing port") {
  19. // addr is of the form "1.2.3.4" or "[fe80::1]"
  20. host = uri.Host
  21. if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
  22. // net.JoinHostPort will add the brackets again
  23. host = host[1 : len(host)-1]
  24. }
  25. copyURI.Host = net.JoinHostPort(host, strconv.Itoa(defaultPort))
  26. } else if err == nil && port == "" {
  27. // addr is on the form "1.2.3.4:" or "[fe80::1]:"
  28. copyURI.Host = net.JoinHostPort(host, strconv.Itoa(defaultPort))
  29. }
  30. return &copyURI
  31. }
  32. func getURLsForAllAdaptersIfUnspecified(network string, uri *url.URL) []*url.URL {
  33. ip, port, err := resolve(network, uri.Host)
  34. // Failed to resolve
  35. if err != nil || port == 0 {
  36. return nil
  37. }
  38. // Not an unspecified address, so no point of substituting with local
  39. // interface addresses as it's listening on a specific adapter anyway.
  40. if len(ip) != 0 && !ip.IsUnspecified() {
  41. return nil
  42. }
  43. hostPorts := getHostPortsForAllAdapters(port)
  44. addrs := make([]*url.URL, 0, len(hostPorts))
  45. for _, hostPort := range hostPorts {
  46. newUri := *uri
  47. newUri.Host = hostPort
  48. addrs = append(addrs, &newUri)
  49. }
  50. return addrs
  51. }
  52. func getHostPortsForAllAdapters(port int) []string {
  53. nets, err := osutil.GetInterfaceAddrs(true)
  54. if err != nil {
  55. // Ignore failure.
  56. return nil
  57. }
  58. hostPorts := make([]string, 0, len(nets))
  59. portStr := strconv.Itoa(port)
  60. for _, network := range nets {
  61. // Only accept IPv4 link-local unicast and the private ranges defined in RFC 1918 and RFC 4193
  62. // IPv6 link-local addresses require an interface identifier to work correctly
  63. if (network.IP.To4() != nil && network.IP.IsLinkLocalUnicast()) || network.IP.IsPrivate() {
  64. hostPorts = append(hostPorts, net.JoinHostPort(network.IP.String(), portStr))
  65. }
  66. }
  67. return hostPorts
  68. }
  69. func resolve(network, hostPort string) (net.IP, int, error) {
  70. switch network {
  71. case "tcp", "tcp4", "tcp6":
  72. if addr, err := net.ResolveTCPAddr(network, hostPort); err != nil {
  73. return net.IPv4zero, 0, err
  74. } else {
  75. return addr.IP, addr.Port, nil
  76. }
  77. case "udp", "udp4", "udp6":
  78. if addr, err := net.ResolveUDPAddr(network, hostPort); err != nil {
  79. return net.IPv4zero, 0, err
  80. } else {
  81. return addr.IP, addr.Port, nil
  82. }
  83. case "ip", "ip4", "ip6":
  84. if addr, err := net.ResolveIPAddr(network, hostPort); err != nil {
  85. return net.IPv4zero, 0, err
  86. } else {
  87. return addr.IP, 0, nil
  88. }
  89. }
  90. return net.IPv4zero, 0, net.UnknownNetworkError(network)
  91. }
  92. func maybeReplacePort(uri *url.URL, laddr net.Addr) *url.URL {
  93. if laddr == nil {
  94. return uri
  95. }
  96. host, portStr, err := net.SplitHostPort(uri.Host)
  97. if err != nil {
  98. return uri
  99. }
  100. port, err := strconv.Atoi(portStr)
  101. if err != nil {
  102. return uri
  103. }
  104. if port != 0 {
  105. return uri
  106. }
  107. _, lportStr, err := net.SplitHostPort(laddr.String())
  108. if err != nil {
  109. return uri
  110. }
  111. uriCopy := *uri
  112. uriCopy.Host = net.JoinHostPort(host, lportStr)
  113. return &uriCopy
  114. }
  115. func portMappingURIs(mapping *nat.Mapping, listener_uri url.URL) []*url.URL {
  116. var uris []*url.URL
  117. if mapping != nil {
  118. addrs := mapping.ExternalAddresses()
  119. for _, addr := range addrs {
  120. uri := listener_uri
  121. // Does net.JoinHostPort internally
  122. uri.Host = addr.String()
  123. uris = append(uris, &uri)
  124. // For every address with a specified IP, add one without an IP,
  125. // just in case the specified IP is still internal (router behind DMZ).
  126. if len(addr.IP) != 0 && !addr.IP.IsUnspecified() {
  127. zeroUri := listener_uri
  128. addr.IP = nil
  129. zeroUri.Host = addr.String()
  130. uris = append(uris, &zeroUri)
  131. }
  132. }
  133. }
  134. return uris
  135. }