ooni-probe-cli/internal/engine/netx/http.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

82 lines
2.7 KiB
Go

package netx
//
// HTTPTransport from Config.
//
import (
"crypto/tls"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// NewHTTPTransport creates a new HTTPRoundTripper from the given Config.
func NewHTTPTransport(config Config) model.HTTPTransport {
if config.Dialer == nil {
// TODO(https://github.com/ooni/probe/issues/2121#issuecomment-1147424810)
config.Dialer = NewDialer(config)
}
if config.TLSDialer == nil {
// TODO(https://github.com/ooni/probe/issues/2121#issuecomment-1147424810)
config.TLSDialer = NewTLSDialer(config)
}
if config.QUICDialer == nil {
// TODO(https://github.com/ooni/probe/issues/2121#issuecomment-1147424810)
config.QUICDialer = NewQUICDialer(config)
}
tInfo := allTransportsInfo[config.HTTP3Enabled]
txp := tInfo.Factory(httpTransportConfig{
Dialer: config.Dialer,
Logger: model.ValidLoggerOrDefault(config.Logger),
QUICDialer: config.QUICDialer,
TLSDialer: config.TLSDialer,
TLSConfig: config.TLSConfig,
})
// TODO(https://github.com/ooni/probe/issues/2121#issuecomment-1147424810): I am
// not super convinced by this code because it
// seems we're currently counting bytes twice in some cases. I think we
// should review how we're counting bytes and using netx currently.
txp = config.ByteCounter.MaybeWrapHTTPTransport(txp) // WAI with ByteCounter == nil
const defaultSnapshotSize = 0 // means: use the default snapsize
return config.Saver.MaybeWrapHTTPTransport(txp, defaultSnapshotSize) // WAI with Saver == nil
}
// httpTransportInfo contains the constructing function as well as the transport name
type httpTransportInfo struct {
Factory func(httpTransportConfig) model.HTTPTransport
TransportName string
}
var allTransportsInfo = map[bool]httpTransportInfo{
false: {
Factory: newHTTPTransport,
TransportName: "tcp",
},
true: {
Factory: newHTTP3Transport,
TransportName: "udp",
},
}
// httpTransportConfig contains configuration for constructing an HTTPTransport.
//
// All the fields in this structure MUST be initialized.
type httpTransportConfig struct {
Dialer model.Dialer
Logger model.Logger
QUICDialer model.QUICDialer
TLSDialer model.TLSDialer
TLSConfig *tls.Config
}
// newHTTP3Transport creates a new HTTP3Transport instance.
func newHTTP3Transport(config httpTransportConfig) model.HTTPTransport {
return netxlite.NewHTTP3Transport(config.Logger, config.QUICDialer, config.TLSConfig)
}
// newHTTPTransport creates a new "system" HTTP transport.
func newHTTPTransport(config httpTransportConfig) model.HTTPTransport {
return netxlite.NewHTTPTransport(config.Logger, config.Dialer, config.TLSDialer)
}