quic_dial.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.14,!noquic,!go1.17
  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/pkg/errors"
  16. "github.com/syncthing/syncthing/lib/config"
  17. "github.com/syncthing/syncthing/lib/connections/registry"
  18. "github.com/syncthing/syncthing/lib/protocol"
  19. )
  20. const (
  21. quicPriority = 100
  22. // The timeout for connecting, accepting and creating the various
  23. // streams.
  24. quicOperationTimeout = 10 * time.Second
  25. )
  26. func init() {
  27. factory := &quicDialerFactory{}
  28. for _, scheme := range []string{"quic", "quic4", "quic6"} {
  29. dialers[scheme] = factory
  30. }
  31. }
  32. type quicDialer struct {
  33. commonDialer
  34. }
  35. func (d *quicDialer) Dial(ctx context.Context, _ protocol.DeviceID, uri *url.URL) (internalConn, error) {
  36. uri = fixupPort(uri, config.DefaultQUICPort)
  37. network := quicNetwork(uri)
  38. addr, err := net.ResolveUDPAddr(network, uri.Host)
  39. if err != nil {
  40. return internalConn{}, err
  41. }
  42. var conn net.PacketConn
  43. // We need to track who created the conn.
  44. // Given we always pass the connection to quic, it assumes it's a remote connection it never closes it,
  45. // So our wrapper around it needs to close it, but it only needs to close it if it's not the listening connection.
  46. var createdConn net.PacketConn
  47. if listenConn := registry.Get(uri.Scheme, packetConnLess); listenConn != nil {
  48. conn = listenConn.(net.PacketConn)
  49. } else {
  50. if packetConn, err := net.ListenPacket("udp", ":0"); err != nil {
  51. return internalConn{}, err
  52. } else {
  53. conn = packetConn
  54. createdConn = packetConn
  55. }
  56. }
  57. ctx, cancel := context.WithTimeout(ctx, quicOperationTimeout)
  58. defer cancel()
  59. session, err := quic.DialContext(ctx, conn, addr, uri.Host, d.tlsCfg, quicConfig)
  60. if err != nil {
  61. if createdConn != nil {
  62. _ = createdConn.Close()
  63. }
  64. return internalConn{}, errors.Wrap(err, "dial")
  65. }
  66. stream, err := session.OpenStreamSync(ctx)
  67. if err != nil {
  68. // It's ok to close these, this does not close the underlying packetConn.
  69. _ = session.CloseWithError(1, err.Error())
  70. if createdConn != nil {
  71. _ = createdConn.Close()
  72. }
  73. return internalConn{}, errors.Wrap(err, "open stream")
  74. }
  75. return newInternalConn(&quicTlsConn{session, stream, createdConn}, connTypeQUICClient, quicPriority), nil
  76. }
  77. type quicDialerFactory struct{}
  78. func (quicDialerFactory) New(opts config.OptionsConfiguration, tlsCfg *tls.Config) genericDialer {
  79. // So the idea is that we should probably try dialing every 20 seconds.
  80. // However it would still be nice if this was adjustable/proportional to ReconnectIntervalS
  81. // But prevent something silly like 1/3 = 0 etc.
  82. quicInterval := opts.ReconnectIntervalS / 3
  83. if quicInterval < 10 {
  84. quicInterval = 10
  85. }
  86. return &quicDialer{commonDialer{
  87. reconnectInterval: time.Duration(quicInterval) * time.Second,
  88. tlsCfg: tlsCfg,
  89. }}
  90. }
  91. func (quicDialerFactory) Priority() int {
  92. return quicPriority
  93. }
  94. func (quicDialerFactory) AlwaysWAN() bool {
  95. return false
  96. }
  97. func (quicDialerFactory) Valid(_ config.Configuration) error {
  98. // Always valid
  99. return nil
  100. }
  101. func (quicDialerFactory) String() string {
  102. return "QUIC Dialer"
  103. }