2019-05-29 00:56:40 -07:00
|
|
|
// Copyright (C) 2019 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2020-12-09 11:23:50 -07:00
|
|
|
// +build go1.14,!noquic,!go1.16
|
2019-05-29 00:56:40 -07:00
|
|
|
|
|
|
|
package connections
|
|
|
|
|
|
|
|
import (
|
2020-05-01 00:14:28 -07:00
|
|
|
"crypto/tls"
|
2019-05-29 00:56:40 -07:00
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go"
|
2020-06-16 00:17:07 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
2019-05-29 00:56:40 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
quicConfig = &quic.Config{
|
|
|
|
ConnectionIDLength: 4,
|
|
|
|
KeepAlive: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type quicTlsConn struct {
|
|
|
|
quic.Session
|
|
|
|
quic.Stream
|
2019-06-09 14:14:00 -07:00
|
|
|
// If we created this connection, we should be the ones closing it.
|
|
|
|
createdConn net.PacketConn
|
2019-05-29 00:56:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *quicTlsConn) Close() error {
|
|
|
|
sterr := q.Stream.Close()
|
2020-05-01 00:14:28 -07:00
|
|
|
seerr := q.Session.CloseWithError(0, "closing")
|
2019-06-09 14:14:00 -07:00
|
|
|
var pcerr error
|
|
|
|
if q.createdConn != nil {
|
|
|
|
pcerr = q.createdConn.Close()
|
|
|
|
}
|
2019-05-29 00:56:40 -07:00
|
|
|
if sterr != nil {
|
|
|
|
return sterr
|
|
|
|
}
|
2019-06-09 14:14:00 -07:00
|
|
|
if seerr != nil {
|
|
|
|
return seerr
|
|
|
|
}
|
|
|
|
return pcerr
|
2019-05-29 00:56:40 -07:00
|
|
|
}
|
|
|
|
|
2020-05-01 00:14:28 -07:00
|
|
|
func (q *quicTlsConn) ConnectionState() tls.ConnectionState {
|
|
|
|
qcs := q.Session.ConnectionState()
|
|
|
|
return tls.ConnectionState{
|
|
|
|
Version: qcs.Version,
|
|
|
|
HandshakeComplete: qcs.HandshakeComplete,
|
|
|
|
DidResume: qcs.DidResume,
|
|
|
|
CipherSuite: qcs.CipherSuite,
|
|
|
|
NegotiatedProtocol: qcs.NegotiatedProtocol,
|
|
|
|
NegotiatedProtocolIsMutual: qcs.NegotiatedProtocolIsMutual,
|
|
|
|
ServerName: qcs.ServerName,
|
|
|
|
PeerCertificates: qcs.PeerCertificates,
|
|
|
|
VerifiedChains: qcs.VerifiedChains,
|
|
|
|
SignedCertificateTimestamps: qcs.SignedCertificateTimestamps,
|
|
|
|
OCSPResponse: qcs.OCSPResponse,
|
|
|
|
TLSUnique: qcs.TLSUnique,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 00:56:40 -07:00
|
|
|
// Sort available packet connections by ip address, preferring unspecified local address.
|
|
|
|
func packetConnLess(i interface{}, j interface{}) bool {
|
2020-06-16 00:17:07 -07:00
|
|
|
return util.AddressUnspecifiedLess(i.(net.PacketConn).LocalAddr(), j.(net.PacketConn).LocalAddr())
|
2019-05-29 00:56:40 -07:00
|
|
|
}
|