quic_misc.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. )
  13. var (
  14. quicConfig = &quic.Config{
  15. ConnectionIDLength: 4,
  16. KeepAlive: true,
  17. }
  18. )
  19. type quicTlsConn struct {
  20. quic.Session
  21. quic.Stream
  22. // If we created this connection, we should be the ones closing it.
  23. createdConn net.PacketConn
  24. }
  25. func (q *quicTlsConn) Close() error {
  26. sterr := q.Stream.Close()
  27. seerr := q.Session.CloseWithError(0, "closing")
  28. var pcerr error
  29. if q.createdConn != nil {
  30. pcerr = q.createdConn.Close()
  31. }
  32. if sterr != nil {
  33. return sterr
  34. }
  35. if seerr != nil {
  36. return seerr
  37. }
  38. return pcerr
  39. }
  40. func (q *quicTlsConn) ConnectionState() tls.ConnectionState {
  41. qcs := q.Session.ConnectionState()
  42. return tls.ConnectionState{
  43. Version: qcs.Version,
  44. HandshakeComplete: qcs.HandshakeComplete,
  45. DidResume: qcs.DidResume,
  46. CipherSuite: qcs.CipherSuite,
  47. NegotiatedProtocol: qcs.NegotiatedProtocol,
  48. NegotiatedProtocolIsMutual: qcs.NegotiatedProtocolIsMutual,
  49. ServerName: qcs.ServerName,
  50. PeerCertificates: qcs.PeerCertificates,
  51. VerifiedChains: qcs.VerifiedChains,
  52. SignedCertificateTimestamps: qcs.SignedCertificateTimestamps,
  53. OCSPResponse: qcs.OCSPResponse,
  54. TLSUnique: qcs.TLSUnique,
  55. }
  56. }
  57. // Sort available packet connections by ip address, preferring unspecified local address.
  58. func packetConnLess(i interface{}, j interface{}) bool {
  59. iIsUnspecified := false
  60. jIsUnspecified := false
  61. iLocalAddr := i.(net.PacketConn).LocalAddr()
  62. jLocalAddr := j.(net.PacketConn).LocalAddr()
  63. if host, _, err := net.SplitHostPort(iLocalAddr.String()); err == nil {
  64. iIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  65. }
  66. if host, _, err := net.SplitHostPort(jLocalAddr.String()); err == nil {
  67. jIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  68. }
  69. if jIsUnspecified == iIsUnspecified {
  70. return len(iLocalAddr.Network()) < len(jLocalAddr.Network())
  71. }
  72. return iIsUnspecified
  73. }