quic_dial.go 3.3 KB

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