internal.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2015 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 https://mozilla.org/MPL/2.0/.
  6. package dialer
  7. import (
  8. "net"
  9. "net/http"
  10. "net/url"
  11. "os"
  12. "strings"
  13. "time"
  14. "golang.org/x/net/proxy"
  15. "github.com/syncthing/syncthing/lib/logger"
  16. )
  17. var (
  18. l = logger.DefaultLogger.NewFacility("dialer", "Dialing connections")
  19. proxyDialer = getDialer(proxy.Direct)
  20. usingProxy = proxyDialer != proxy.Direct
  21. noFallback = os.Getenv("ALL_PROXY_NO_FALLBACK") != ""
  22. )
  23. type dialFunc func(network, addr string) (net.Conn, error)
  24. func init() {
  25. l.SetDebug("dialer", strings.Contains(os.Getenv("STTRACE"), "dialer") || os.Getenv("STTRACE") == "all")
  26. if usingProxy {
  27. http.DefaultTransport = &http.Transport{
  28. Dial: Dial,
  29. Proxy: http.ProxyFromEnvironment,
  30. TLSHandshakeTimeout: 10 * time.Second,
  31. }
  32. // Defer this, so that logging gets setup.
  33. go func() {
  34. time.Sleep(500 * time.Millisecond)
  35. l.Infoln("Proxy settings detected")
  36. if noFallback {
  37. l.Infoln("Proxy fallback disabled")
  38. }
  39. }()
  40. } else {
  41. go func() {
  42. time.Sleep(500 * time.Millisecond)
  43. l.Debugln("Dialer logging disabled, as no proxy was detected")
  44. }()
  45. }
  46. }
  47. func dialWithFallback(proxyDialFunc dialFunc, fallbackDialFunc dialFunc, network, addr string) (net.Conn, error) {
  48. conn, err := proxyDialFunc(network, addr)
  49. if err == nil {
  50. l.Debugf("Dialing %s address %s via proxy - success, %s -> %s", network, addr, conn.LocalAddr(), conn.RemoteAddr())
  51. SetTCPOptions(conn)
  52. return dialerConn{
  53. conn, newDialerAddr(network, addr),
  54. }, nil
  55. }
  56. l.Debugf("Dialing %s address %s via proxy - error %s", network, addr, err)
  57. if noFallback {
  58. return conn, err
  59. }
  60. conn, err = fallbackDialFunc(network, addr)
  61. if err == nil {
  62. l.Debugf("Dialing %s address %s via fallback - success, %s -> %s", network, addr, conn.LocalAddr(), conn.RemoteAddr())
  63. SetTCPOptions(conn)
  64. } else {
  65. l.Debugf("Dialing %s address %s via fallback - error %s", network, addr, err)
  66. }
  67. return conn, err
  68. }
  69. // This is a rip off of proxy.FromEnvironment with a custom forward dialer
  70. func getDialer(forward proxy.Dialer) proxy.Dialer {
  71. allProxy := os.Getenv("all_proxy")
  72. if len(allProxy) == 0 {
  73. return forward
  74. }
  75. proxyURL, err := url.Parse(allProxy)
  76. if err != nil {
  77. return forward
  78. }
  79. prxy, err := proxy.FromURL(proxyURL, forward)
  80. if err != nil {
  81. return forward
  82. }
  83. noProxy := os.Getenv("no_proxy")
  84. if len(noProxy) == 0 {
  85. return prxy
  86. }
  87. perHost := proxy.NewPerHost(prxy, forward)
  88. perHost.AddFromString(noProxy)
  89. return perHost
  90. }
  91. type timeoutDirectDialer struct {
  92. timeout time.Duration
  93. }
  94. func (d *timeoutDirectDialer) Dial(network, addr string) (net.Conn, error) {
  95. return net.DialTimeout(network, addr, d.timeout)
  96. }
  97. type dialerConn struct {
  98. net.Conn
  99. addr net.Addr
  100. }
  101. func (c dialerConn) RemoteAddr() net.Addr {
  102. return c.addr
  103. }
  104. func newDialerAddr(network, addr string) net.Addr {
  105. netaddr, err := net.ResolveIPAddr(network, addr)
  106. if err == nil {
  107. return netaddr
  108. }
  109. return fallbackAddr{network, addr}
  110. }
  111. type fallbackAddr struct {
  112. network string
  113. addr string
  114. }
  115. func (a fallbackAddr) Network() string {
  116. return a.network
  117. }
  118. func (a fallbackAddr) String() string {
  119. return a.addr
  120. }