quic_dial.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 2019 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. // +build go1.12
  7. package connections
  8. import (
  9. "context"
  10. "crypto/tls"
  11. "net"
  12. "net/url"
  13. "time"
  14. "github.com/lucas-clemente/quic-go"
  15. "github.com/syncthing/syncthing/lib/config"
  16. "github.com/syncthing/syncthing/lib/connections/registry"
  17. "github.com/syncthing/syncthing/lib/protocol"
  18. )
  19. const quicPriority = 100
  20. func init() {
  21. factory := &quicDialerFactory{}
  22. for _, scheme := range []string{"quic", "quic4", "quic6"} {
  23. dialers[scheme] = factory
  24. }
  25. }
  26. type quicDialer struct {
  27. cfg config.Wrapper
  28. tlsCfg *tls.Config
  29. }
  30. func (d *quicDialer) Dial(_ protocol.DeviceID, uri *url.URL) (internalConn, error) {
  31. uri = fixupPort(uri, config.DefaultQUICPort)
  32. addr, err := net.ResolveUDPAddr("udp", uri.Host)
  33. if err != nil {
  34. return internalConn{}, err
  35. }
  36. var conn net.PacketConn
  37. // We need to track who created the conn.
  38. // Given we always pass the connection to quic, it assumes it's a remote connection it never closes it,
  39. // So our wrapper around it needs to close it, but it only needs to close it if it's not the listening connection.
  40. var createdConn net.PacketConn
  41. if listenConn := registry.Get(uri.Scheme, packetConnLess); listenConn != nil {
  42. conn = listenConn.(net.PacketConn)
  43. } else {
  44. if packetConn, err := net.ListenPacket("udp", ":0"); err != nil {
  45. return internalConn{}, err
  46. } else {
  47. conn = packetConn
  48. createdConn = packetConn
  49. }
  50. }
  51. ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
  52. session, err := quic.DialContext(ctx, conn, addr, uri.Host, d.tlsCfg, quicConfig)
  53. if err != nil {
  54. if createdConn != nil {
  55. _ = createdConn.Close()
  56. }
  57. return internalConn{}, err
  58. }
  59. // OpenStreamSync is blocks, but we want to make sure the connection is usable
  60. // before we start killing off other connections, so do the dance.
  61. ok := make(chan struct{})
  62. go func() {
  63. select {
  64. case <-ok:
  65. return
  66. case <-time.After(10 * time.Second):
  67. l.Debugln("timed out waiting for OpenStream on", session.RemoteAddr())
  68. // This will unblock OpenStreamSync
  69. _ = session.Close()
  70. }
  71. }()
  72. stream, err := session.OpenStreamSync()
  73. close(ok)
  74. if err != nil {
  75. // It's ok to close these, this does not close the underlying packetConn.
  76. _ = session.Close()
  77. if createdConn != nil {
  78. _ = createdConn.Close()
  79. }
  80. return internalConn{}, err
  81. }
  82. return internalConn{&quicTlsConn{session, stream, createdConn}, connTypeQUICClient, quicPriority}, nil
  83. }
  84. func (d *quicDialer) RedialFrequency() time.Duration {
  85. return time.Duration(d.cfg.Options().ReconnectIntervalS) * time.Second
  86. }
  87. type quicDialerFactory struct {
  88. cfg config.Wrapper
  89. tlsCfg *tls.Config
  90. }
  91. func (quicDialerFactory) New(cfg config.Wrapper, tlsCfg *tls.Config) genericDialer {
  92. return &quicDialer{
  93. cfg: cfg,
  94. tlsCfg: tlsCfg,
  95. }
  96. }
  97. func (quicDialerFactory) Priority() int {
  98. return quicPriority
  99. }
  100. func (quicDialerFactory) AlwaysWAN() bool {
  101. return false
  102. }
  103. func (quicDialerFactory) Valid(_ config.Configuration) error {
  104. // Always valid
  105. return nil
  106. }
  107. func (quicDialerFactory) String() string {
  108. return "QUIC Dialer"
  109. }