quic_dial.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/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. addr, err := net.ResolveUDPAddr("udp", uri.Host)
  38. if err != nil {
  39. return internalConn{}, err
  40. }
  41. var conn net.PacketConn
  42. // We need to track who created the conn.
  43. // Given we always pass the connection to quic, it assumes it's a remote connection it never closes it,
  44. // So our wrapper around it needs to close it, but it only needs to close it if it's not the listening connection.
  45. var createdConn net.PacketConn
  46. if listenConn := registry.Get(uri.Scheme, packetConnLess); listenConn != nil {
  47. conn = listenConn.(net.PacketConn)
  48. } else {
  49. if packetConn, err := net.ListenPacket("udp", ":0"); err != nil {
  50. return internalConn{}, err
  51. } else {
  52. conn = packetConn
  53. createdConn = packetConn
  54. }
  55. }
  56. ctx, cancel := context.WithTimeout(ctx, quicOperationTimeout)
  57. defer cancel()
  58. session, err := quic.DialContext(ctx, conn, addr, uri.Host, d.tlsCfg, quicConfig)
  59. if err != nil {
  60. if createdConn != nil {
  61. _ = createdConn.Close()
  62. }
  63. return internalConn{}, errors.Wrap(err, "dial")
  64. }
  65. stream, err := session.OpenStreamSync(ctx)
  66. if err != nil {
  67. // It's ok to close these, this does not close the underlying packetConn.
  68. _ = session.Close()
  69. if createdConn != nil {
  70. _ = createdConn.Close()
  71. }
  72. return internalConn{}, errors.Wrap(err, "open stream")
  73. }
  74. return internalConn{&quicTlsConn{session, stream, createdConn}, connTypeQUICClient, quicPriority}, nil
  75. }
  76. type quicDialerFactory struct {
  77. cfg config.Wrapper
  78. tlsCfg *tls.Config
  79. }
  80. func (quicDialerFactory) New(opts config.OptionsConfiguration, tlsCfg *tls.Config) genericDialer {
  81. return &quicDialer{commonDialer{
  82. reconnectInterval: time.Duration(opts.ReconnectIntervalS) * time.Second,
  83. tlsCfg: tlsCfg,
  84. }}
  85. }
  86. func (quicDialerFactory) Priority() int {
  87. return quicPriority
  88. }
  89. func (quicDialerFactory) AlwaysWAN() bool {
  90. return false
  91. }
  92. func (quicDialerFactory) Valid(_ config.Configuration) error {
  93. // Always valid
  94. return nil
  95. }
  96. func (quicDialerFactory) String() string {
  97. return "QUIC Dialer"
  98. }