2019-05-29 07:56:40 +00: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/.
|
|
|
|
|
2021-08-19 19:05:28 +00:00
|
|
|
//go:build go1.15 && !noquic
|
|
|
|
// +build go1.15,!noquic
|
2019-05-29 07:56:40 +00:00
|
|
|
|
|
|
|
package connections
|
|
|
|
|
|
|
|
import (
|
2020-05-01 07:14:28 +00:00
|
|
|
"crypto/tls"
|
2019-05-29 07:56:40 +00:00
|
|
|
"net"
|
2021-07-10 11:53:51 +00:00
|
|
|
"net/url"
|
2019-05-29 07:56:40 +00:00
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
quicConfig = &quic.Config{
|
|
|
|
ConnectionIDLength: 4,
|
|
|
|
KeepAlive: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-07-10 11:53:51 +00:00
|
|
|
func quicNetwork(uri *url.URL) string {
|
|
|
|
switch uri.Scheme {
|
|
|
|
case "quic4":
|
|
|
|
return "udp4"
|
|
|
|
case "quic6":
|
|
|
|
return "udp6"
|
|
|
|
default:
|
|
|
|
return "udp"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 07:56:40 +00:00
|
|
|
type quicTlsConn struct {
|
2022-04-12 12:27:29 +00:00
|
|
|
quic.Connection
|
2019-05-29 07:56:40 +00:00
|
|
|
quic.Stream
|
2019-06-09 21:14:00 +00:00
|
|
|
// If we created this connection, we should be the ones closing it.
|
|
|
|
createdConn net.PacketConn
|
2019-05-29 07:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *quicTlsConn) Close() error {
|
|
|
|
sterr := q.Stream.Close()
|
2022-04-12 12:27:29 +00:00
|
|
|
seerr := q.Connection.CloseWithError(0, "closing")
|
2019-06-09 21:14:00 +00:00
|
|
|
var pcerr error
|
|
|
|
if q.createdConn != nil {
|
|
|
|
pcerr = q.createdConn.Close()
|
|
|
|
}
|
2019-05-29 07:56:40 +00:00
|
|
|
if sterr != nil {
|
|
|
|
return sterr
|
|
|
|
}
|
2019-06-09 21:14:00 +00:00
|
|
|
if seerr != nil {
|
|
|
|
return seerr
|
|
|
|
}
|
|
|
|
return pcerr
|
2019-05-29 07:56:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 07:14:28 +00:00
|
|
|
func (q *quicTlsConn) ConnectionState() tls.ConnectionState {
|
2022-04-12 12:27:29 +00:00
|
|
|
return q.Connection.ConnectionState().TLS.ConnectionState
|
2020-05-01 07:14:28 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 08:52:51 +00:00
|
|
|
func packetConnUnspecified(conn interface{}) bool {
|
|
|
|
// Since QUIC connections are wrapped, we can't do a simple typecheck
|
|
|
|
// on *net.UDPAddr here.
|
|
|
|
addr := conn.(net.PacketConn).LocalAddr()
|
|
|
|
host, _, err := net.SplitHostPort(addr.String())
|
|
|
|
return err == nil && net.ParseIP(host).IsUnspecified()
|
2019-05-29 07:56:40 +00:00
|
|
|
}
|