quic_dial.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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(id 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. closeConn := false
  38. if listenConn := registry.Get(uri.Scheme, packetConnLess); listenConn != nil {
  39. conn = listenConn.(net.PacketConn)
  40. } else {
  41. if packetConn, err := net.ListenPacket("udp", ":0"); err != nil {
  42. return internalConn{}, err
  43. } else {
  44. closeConn = true
  45. conn = packetConn
  46. }
  47. }
  48. ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
  49. session, err := quic.DialContext(ctx, conn, addr, uri.Host, d.tlsCfg, quicConfig)
  50. if err != nil {
  51. if closeConn {
  52. _ = conn.Close()
  53. }
  54. return internalConn{}, err
  55. }
  56. // OpenStreamSync is blocks, but we want to make sure the connection is usable
  57. // before we start killing off other connections, so do the dance.
  58. ok := make(chan struct{})
  59. go func() {
  60. select {
  61. case <-ok:
  62. return
  63. case <-time.After(10 * time.Second):
  64. l.Debugln("timed out waiting for OpenStream on", session.RemoteAddr())
  65. // This will unblock OpenStreamSync
  66. _ = session.Close()
  67. }
  68. }()
  69. stream, err := session.OpenStreamSync()
  70. close(ok)
  71. if err != nil {
  72. // It's ok to close these, this does not close the underlying packetConn.
  73. _ = session.Close()
  74. if closeConn {
  75. _ = conn.Close()
  76. }
  77. return internalConn{}, err
  78. }
  79. return internalConn{&quicTlsConn{session, stream}, connTypeQUICClient, quicPriority}, nil
  80. }
  81. func (d *quicDialer) RedialFrequency() time.Duration {
  82. return time.Duration(d.cfg.Options().ReconnectIntervalS) * time.Second
  83. }
  84. type quicDialerFactory struct {
  85. cfg config.Wrapper
  86. tlsCfg *tls.Config
  87. }
  88. func (quicDialerFactory) New(cfg config.Wrapper, tlsCfg *tls.Config) genericDialer {
  89. return &quicDialer{
  90. cfg: cfg,
  91. tlsCfg: tlsCfg,
  92. }
  93. }
  94. func (quicDialerFactory) Priority() int {
  95. return quicPriority
  96. }
  97. func (quicDialerFactory) AlwaysWAN() bool {
  98. return false
  99. }
  100. func (quicDialerFactory) Valid(_ config.Configuration) error {
  101. // Always valid
  102. return nil
  103. }
  104. func (quicDialerFactory) String() string {
  105. return "QUIC Dialer"
  106. }