quic_misc.go 1.9 KB

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