2021-06-30 15:19:10 +02:00
|
|
|
package netxlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2021-09-08 00:59:48 +02:00
|
|
|
"io"
|
2021-06-30 15:19:10 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go"
|
|
|
|
"github.com/lucas-clemente/quic-go/http3"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-06-30 15:19:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// http3Dialer adapts a QUICContextDialer to work with
|
|
|
|
// an http3.RoundTripper. This is necessary because the
|
|
|
|
// http3.RoundTripper does not support DialContext.
|
|
|
|
type http3Dialer struct {
|
2022-01-03 13:53:23 +01:00
|
|
|
model.QUICDialer
|
2021-06-30 15:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// dial is like QUICContextDialer.DialContext but without context.
|
|
|
|
func (d *http3Dialer) dial(network, address string, tlsConfig *tls.Config,
|
|
|
|
quicConfig *quic.Config) (quic.EarlySession, error) {
|
2021-09-08 00:59:48 +02:00
|
|
|
return d.QUICDialer.DialContext(
|
2021-06-30 15:19:10 +02:00
|
|
|
context.Background(), network, address, tlsConfig, quicConfig)
|
|
|
|
}
|
|
|
|
|
2021-09-08 00:59:48 +02:00
|
|
|
// http3RoundTripper is the abstract type of quic-go/http3.RoundTripper.
|
|
|
|
type http3RoundTripper interface {
|
|
|
|
http.RoundTripper
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
2021-06-30 15:19:10 +02:00
|
|
|
// http3Transport is an HTTPTransport using the http3 protocol.
|
|
|
|
type http3Transport struct {
|
2021-09-08 00:59:48 +02:00
|
|
|
child http3RoundTripper
|
2022-01-03 13:53:23 +01:00
|
|
|
dialer model.QUICDialer
|
2021-06-30 15:19:10 +02:00
|
|
|
}
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
var _ model.HTTPTransport = &http3Transport{}
|
2021-06-30 15:19:10 +02:00
|
|
|
|
|
|
|
// RoundTrip implements HTTPTransport.RoundTrip.
|
|
|
|
func (txp *http3Transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
return txp.child.RoundTrip(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseIdleConnections implements HTTPTransport.CloseIdleConnections.
|
|
|
|
func (txp *http3Transport) CloseIdleConnections() {
|
|
|
|
txp.child.Close()
|
2021-09-06 21:52:00 +02:00
|
|
|
txp.dialer.CloseIdleConnections()
|
2021-06-30 15:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHTTP3Transport creates a new HTTPTransport using http3. The
|
|
|
|
// dialer argument MUST NOT be nil. If the tlsConfig argument is nil,
|
|
|
|
// then the code will use the default TLS configuration.
|
|
|
|
func NewHTTP3Transport(
|
2022-01-03 13:53:23 +01:00
|
|
|
logger model.DebugLogger, dialer model.QUICDialer, tlsConfig *tls.Config) model.HTTPTransport {
|
2021-09-08 20:49:01 +02:00
|
|
|
return &httpTransportLogger{
|
|
|
|
HTTPTransport: &http3Transport{
|
|
|
|
child: &http3.RoundTripper{
|
|
|
|
Dial: (&http3Dialer{dialer}).dial,
|
|
|
|
// The following (1) reduces the number of headers that Go will
|
|
|
|
// automatically send for us and (2) ensures that we always receive
|
|
|
|
// back the true headers, such as Content-Length. This change is
|
|
|
|
// functional to OONI's goal of observing the network.
|
|
|
|
DisableCompression: true,
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
},
|
|
|
|
dialer: dialer,
|
2021-06-30 15:19:10 +02:00
|
|
|
},
|
2021-09-08 20:49:01 +02:00
|
|
|
Logger: logger,
|
2021-06-30 15:19:10 +02:00
|
|
|
}
|
|
|
|
}
|