quic_dial.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. //go:build go1.15 && !noquic
  7. // +build go1.15,!noquic
  8. package connections
  9. import (
  10. "context"
  11. "crypto/tls"
  12. "fmt"
  13. "net"
  14. "net/url"
  15. "time"
  16. "github.com/quic-go/quic-go"
  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. // 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. registry *registry.Registry
  35. }
  36. func (d *quicDialer) Dial(ctx context.Context, _ protocol.DeviceID, uri *url.URL) (internalConn, error) {
  37. uri = fixupPort(uri, config.DefaultQUICPort)
  38. network := quicNetwork(uri)
  39. addr, err := net.ResolveUDPAddr(network, uri.Host)
  40. if err != nil {
  41. return internalConn{}, err
  42. }
  43. // If we created the conn we need to close it at the end. If we got a
  44. // Transport from the registry we have no conn to close.
  45. var createdConn net.PacketConn
  46. transport, _ := d.registry.Get(uri.Scheme, transportConnUnspecified).(*quic.Transport)
  47. if transport == nil {
  48. if packetConn, err := net.ListenPacket("udp", ":0"); err != nil {
  49. return internalConn{}, err
  50. } else {
  51. createdConn = packetConn
  52. transport = &quic.Transport{Conn: packetConn}
  53. }
  54. }
  55. ctx, cancel := context.WithTimeout(ctx, quicOperationTimeout)
  56. defer cancel()
  57. session, err := transport.Dial(ctx, addr, d.tlsCfg, quicConfig)
  58. if err != nil {
  59. if createdConn != nil {
  60. _ = createdConn.Close()
  61. }
  62. return internalConn{}, fmt.Errorf("dial: %w", err)
  63. }
  64. stream, err := session.OpenStreamSync(ctx)
  65. if err != nil {
  66. // It's ok to close these, this does not close the underlying packetConn.
  67. _ = session.CloseWithError(1, err.Error())
  68. if createdConn != nil {
  69. _ = createdConn.Close()
  70. }
  71. return internalConn{}, fmt.Errorf("open stream: %w", err)
  72. }
  73. priority := d.wanPriority
  74. isLocal := d.lanChecker.isLAN(session.RemoteAddr())
  75. if isLocal {
  76. priority = d.lanPriority
  77. }
  78. return newInternalConn(&quicTlsConn{session, stream, createdConn}, connTypeQUICClient, isLocal, priority), nil
  79. }
  80. type quicDialerFactory struct{}
  81. func (quicDialerFactory) New(opts config.OptionsConfiguration, tlsCfg *tls.Config, registry *registry.Registry, lanChecker *lanChecker) genericDialer {
  82. // So the idea is that we should probably try dialing every 20 seconds.
  83. // However it would still be nice if this was adjustable/proportional to ReconnectIntervalS
  84. // But prevent something silly like 1/3 = 0 etc.
  85. quicInterval := opts.ReconnectIntervalS / 3
  86. if quicInterval < 10 {
  87. quicInterval = 10
  88. }
  89. return &quicDialer{
  90. commonDialer: commonDialer{
  91. reconnectInterval: time.Duration(quicInterval) * time.Second,
  92. tlsCfg: tlsCfg,
  93. lanChecker: lanChecker,
  94. lanPriority: opts.ConnectionPriorityQUICLAN,
  95. wanPriority: opts.ConnectionPriorityQUICWAN,
  96. allowsMultiConns: true,
  97. },
  98. registry: registry,
  99. }
  100. }
  101. func (quicDialerFactory) AlwaysWAN() bool {
  102. return false
  103. }
  104. func (quicDialerFactory) Valid(_ config.Configuration) error {
  105. // Always valid
  106. return nil
  107. }
  108. func (quicDialerFactory) String() string {
  109. return "QUIC Dialer"
  110. }