quic_misc.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 http://mozilla.org/MPL/2.0/.
  6. // +build go1.14,!noquic,!go1.17
  7. package connections
  8. import (
  9. "crypto/tls"
  10. "net"
  11. "net/url"
  12. "github.com/lucas-clemente/quic-go"
  13. "github.com/syncthing/syncthing/lib/util"
  14. )
  15. var (
  16. quicConfig = &quic.Config{
  17. ConnectionIDLength: 4,
  18. KeepAlive: true,
  19. }
  20. )
  21. func quicNetwork(uri *url.URL) string {
  22. switch uri.Scheme {
  23. case "quic4":
  24. return "udp4"
  25. case "quic6":
  26. return "udp6"
  27. default:
  28. return "udp"
  29. }
  30. }
  31. type quicTlsConn struct {
  32. quic.Session
  33. quic.Stream
  34. // If we created this connection, we should be the ones closing it.
  35. createdConn net.PacketConn
  36. }
  37. func (q *quicTlsConn) Close() error {
  38. sterr := q.Stream.Close()
  39. seerr := q.Session.CloseWithError(0, "closing")
  40. var pcerr error
  41. if q.createdConn != nil {
  42. pcerr = q.createdConn.Close()
  43. }
  44. if sterr != nil {
  45. return sterr
  46. }
  47. if seerr != nil {
  48. return seerr
  49. }
  50. return pcerr
  51. }
  52. func (q *quicTlsConn) ConnectionState() tls.ConnectionState {
  53. qcs := q.Session.ConnectionState()
  54. return tls.ConnectionState{
  55. Version: qcs.Version,
  56. HandshakeComplete: qcs.HandshakeComplete,
  57. DidResume: qcs.DidResume,
  58. CipherSuite: qcs.CipherSuite,
  59. NegotiatedProtocol: qcs.NegotiatedProtocol,
  60. NegotiatedProtocolIsMutual: qcs.NegotiatedProtocolIsMutual,
  61. ServerName: qcs.ServerName,
  62. PeerCertificates: qcs.PeerCertificates,
  63. VerifiedChains: qcs.VerifiedChains,
  64. SignedCertificateTimestamps: qcs.SignedCertificateTimestamps,
  65. OCSPResponse: qcs.OCSPResponse,
  66. TLSUnique: qcs.TLSUnique,
  67. }
  68. }
  69. // Sort available packet connections by ip address, preferring unspecified local address.
  70. func packetConnLess(i interface{}, j interface{}) bool {
  71. return util.AddressUnspecifiedLess(i.(net.PacketConn).LocalAddr(), j.(net.PacketConn).LocalAddr())
  72. }