public.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "fmt"
  9. "net"
  10. "time"
  11. "golang.org/x/net/ipv4"
  12. "golang.org/x/net/ipv6"
  13. )
  14. // Dial tries dialing via proxy if a proxy is configured, and falls back to
  15. // a direct connection if no proxy is defined, or connecting via proxy fails.
  16. func Dial(network, addr string) (net.Conn, error) {
  17. if usingProxy {
  18. return dialWithFallback(proxyDialer.Dial, net.Dial, network, addr)
  19. }
  20. return net.Dial(network, addr)
  21. }
  22. // DialTimeout tries dialing via proxy with a timeout if a proxy is configured,
  23. // and falls back to a direct connection if no proxy is defined, or connecting
  24. // via proxy fails. The timeout can potentially be applied twice, once trying
  25. // to connect via the proxy connection, and second time trying to connect
  26. // directly.
  27. func DialTimeout(network, addr string, timeout time.Duration) (net.Conn, error) {
  28. if usingProxy {
  29. // Because the proxy package is poorly structured, we have to
  30. // construct a struct that matches proxy.Dialer but has a timeout
  31. // and reconstrcut the proxy dialer using that, in order to be able to
  32. // set a timeout.
  33. dd := &timeoutDirectDialer{
  34. timeout: timeout,
  35. }
  36. // Check if the dialer we are getting is not timeoutDirectDialer we just
  37. // created. It could happen that usingProxy is true, but getDialer
  38. // returns timeoutDirectDialer due to env vars changing.
  39. if timeoutProxyDialer := getDialer(dd); timeoutProxyDialer != dd {
  40. directDialFunc := func(inetwork, iaddr string) (net.Conn, error) {
  41. return net.DialTimeout(inetwork, iaddr, timeout)
  42. }
  43. return dialWithFallback(timeoutProxyDialer.Dial, directDialFunc, network, addr)
  44. }
  45. }
  46. return net.DialTimeout(network, addr, timeout)
  47. }
  48. // SetTCPOptions sets our default TCP options on a TCP connection, possibly
  49. // digging through dialerConn to extract the *net.TCPConn
  50. func SetTCPOptions(conn net.Conn) error {
  51. switch conn := conn.(type) {
  52. case *net.TCPConn:
  53. var err error
  54. if err = conn.SetLinger(0); err != nil {
  55. return err
  56. }
  57. if err = conn.SetNoDelay(false); err != nil {
  58. return err
  59. }
  60. if err = conn.SetKeepAlivePeriod(60 * time.Second); err != nil {
  61. return err
  62. }
  63. if err = conn.SetKeepAlive(true); err != nil {
  64. return err
  65. }
  66. return nil
  67. case dialerConn:
  68. return SetTCPOptions(conn.Conn)
  69. default:
  70. return fmt.Errorf("unknown connection type %T", conn)
  71. }
  72. }
  73. func SetTrafficClass(conn net.Conn, class int) error {
  74. switch conn := conn.(type) {
  75. case *net.TCPConn:
  76. e1 := ipv4.NewConn(conn).SetTOS(class)
  77. e2 := ipv6.NewConn(conn).SetTrafficClass(class)
  78. if e1 != nil {
  79. return e1
  80. }
  81. return e2
  82. case dialerConn:
  83. return SetTrafficClass(conn.Conn, class)
  84. default:
  85. return fmt.Errorf("unknown connection type %T", conn)
  86. }
  87. }