ooni-probe-cli/internal/netxlite/http3.go
Simone Basso b78b9aca51
refactor(datafmt): use "udp" instead of "quic" (#946)
This diff changes the data format to prefer "udp" to "quic" everywhere we were previously using "quic".

Previously, the code inconsistently used "quic" for operations where we knew we were using "quic" and "udp" otherwise (e.g., for generic operations like ReadFrom).

While it would be more correct to say that a specific HTTP request used "quic" rather than "udp", using "udp" consistently allows one to see how distinct events such as ReadFrom and an handshake all refer to the same address, port, and protocol triple. Therefore, this change makes it easier to programmatically unpack a single measurement and create endpoint stats.

Before implementing this change, I discussed the problem with @hellais who mentioned that ooni/data is not currently using the "quic" string anywhere. I know that ooni/pipeline also doesn't rely on this string. The only users of this feature have been research-oriented experiments such as urlgetter, for which such a change would actually be acceptable.

See https://github.com/ooni/probe/issues/2238 and https://github.com/ooni/spec/pull/262.
2022-09-08 17:19:59 +02:00

80 lines
2.4 KiB
Go

package netxlite
//
// HTTP3 code
//
import (
"crypto/tls"
"io"
"net/http"
"github.com/lucas-clemente/quic-go/http3"
"github.com/ooni/probe-cli/v3/internal/model"
)
// http3RoundTripper is the abstract type of quic-go/http3.RoundTripper.
type http3RoundTripper interface {
http.RoundTripper
io.Closer
}
// http3Transport is an HTTPTransport using the http3 protocol.
type http3Transport struct {
child http3RoundTripper
dialer model.QUICDialer
}
var _ model.HTTPTransport = &http3Transport{}
// Network implements HTTPTransport.Network.
func (txp *http3Transport) Network() string {
return "udp"
}
// 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()
txp.dialer.CloseIdleConnections()
}
// 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(
logger model.DebugLogger, dialer model.QUICDialer, tlsConfig *tls.Config) model.HTTPTransport {
return WrapHTTPTransport(logger, &http3Transport{
child: &http3.RoundTripper{
Dial: dialer.DialContext,
// 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,
})
}
// NewHTTP3TransportStdlib creates a new HTTPTransport using http3 that
// uses standard functionality for everything but the logger.
func NewHTTP3TransportStdlib(logger model.DebugLogger) model.HTTPTransport {
ql := NewQUICListener()
reso := NewStdlibResolver(logger)
qd := NewQUICDialerWithResolver(ql, logger, reso)
return NewHTTP3Transport(logger, qd, nil)
}
// NewHTTPTransportWithResolver creates a new HTTPTransport using http3
// that uses the given logger and the given resolver.
func NewHTTP3TransportWithResolver(logger model.Logger, reso model.Resolver) model.HTTPTransport {
qd := NewQUICDialerWithResolver(NewQUICListener(), logger, reso)
return NewHTTP3Transport(logger, qd, nil)
}