Skip to content

Commit

Permalink
chore(netxlite): isolate and annotate quirky functions (#1269)
Browse files Browse the repository at this point in the history
This diff isolates and annotates netxlite quirky functions such that
ooni/probe#2534 will be easier.

This work is also useful to ooni/probe#2531.

While there, commit workaround for issue
ooni/probe#2535.
  • Loading branch information
bassosimone authored Sep 14, 2023
1 parent 873b4e1 commit 0f6b122
Show file tree
Hide file tree
Showing 35 changed files with 151 additions and 39 deletions.
2 changes: 2 additions & 0 deletions internal/cmd/apitool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
)

func newclient() probeservices.Client {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPTransportStdlib has QUIRKS but we
// don't actually care about those QUIRKS in this context
txp := netxlite.NewHTTPTransportStdlib(log.Log)
ua := fmt.Sprintf("apitool/%s ooniprobe-engine/%s", version.Version, version.Version)
return probeservices.Client{
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/oohelper/oohelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func init() {
// puzzling https://github.com/ooni/probe/issues/1409 issue.
const resolverURL = "https://8.8.8.8/dns-query"
resolver = netxlite.NewParallelDNSOverHTTPSResolver(log.Log, resolverURL)
// TODO(https://github.com/ooni/probe/issues/2534): the NewHTTPClientWithResolver func has QUIRKS but we don't care.
httpClient = netxlite.NewHTTPClientWithResolver(log.Log, resolver)
}

Expand Down
3 changes: 3 additions & 0 deletions internal/dslx/httptcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type httpTransportTCPFunc struct{}
// Apply implements Func
func (f *httpTransportTCPFunc) Apply(
ctx context.Context, input *TCPConnection) *Maybe[*HTTPTransport] {
// TODO(https://github.com/ooni/probe/issues/2534): here we're using the QUIRKY netxlite.NewHTTPTransport
// function, but we can probably avoid using it, given that this code is
// not using tracing and does not care about those quirks.
httpTransport := netxlite.NewHTTPTransport(
input.Logger,
netxlite.NewSingleUseDialer(input.Conn),
Expand Down
3 changes: 3 additions & 0 deletions internal/dslx/httptls.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type httpTransportTLSFunc struct{}
// Apply implements Func.
func (f *httpTransportTLSFunc) Apply(
ctx context.Context, input *TLSConnection) *Maybe[*HTTPTransport] {
// TODO(https://github.com/ooni/probe/issues/2534): here we're using the QUIRKY netxlite.NewHTTPTransport
// function, but we can probably avoid using it, given that this code is
// not using tracing and does not care about those quirks.
httpTransport := netxlite.NewHTTPTransport(
input.Logger,
netxlite.NewNullDialer(),
Expand Down
2 changes: 2 additions & 0 deletions internal/enginelocate/iplookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func (c ipLookupClient) doWithCustomFunc(
// Implementation note: we MUST use an HTTP client that we're
// sure IS NOT using any proxy. To this end, we construct a
// client ourself that we know is not proxied.
// TODO(https://github.com/ooni/probe/issues/2534): the NewHTTPTransportWithResolver has QUIRKS but
// we don't care about them in this context
txp := netxlite.NewHTTPTransportWithResolver(c.Logger, c.Resolver)
clnt := &http.Client{Transport: txp}
defer clnt.CloseIdleConnections()
Expand Down
3 changes: 3 additions & 0 deletions internal/enginenetx/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func NewHTTPTransport(
dialer = netxlite.MaybeWrapWithProxyDialer(dialer, proxyURL)
handshaker := netxlite.NewTLSHandshakerStdlib(logger)
tlsDialer := netxlite.NewTLSDialer(dialer, handshaker)
// TODO(https://github.com/ooni/probe/issues/2534): here we're using the QUIRKY netxlite.NewHTTPTransport
// function, but we can probably avoid using it, given that this code is
// not using tracing and does not care about those quirks.
txp := netxlite.NewHTTPTransport(logger, dialer, tlsDialer)
txp = bytecounter.WrapHTTPTransport(txp, counter)
return &HTTPTransport{txp}
Expand Down
3 changes: 3 additions & 0 deletions internal/engineresolver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func newChildResolverHTTPS(
)
thx := netxlite.NewTLSHandshakerStdlib(logger)
tlsDialer := netxlite.NewTLSDialer(dialer, thx)
// TODO(https://github.com/ooni/probe/issues/2534): here we're using the QUIRKY netxlite.NewHTTPTransport
// function, but we can probably avoid using it, given that this code is
// not using tracing and does not care about those quirks.
txp = netxlite.NewHTTPTransport(logger, dialer, tlsDialer)
case true:
txp = netxlite.NewHTTP3TransportStdlib(logger)
Expand Down
4 changes: 4 additions & 0 deletions internal/experiment/ndt7/ndt7.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ type Measurer struct {

func (m *Measurer) discover(
ctx context.Context, sess model.ExperimentSession) (*mlablocatev2.NDT7Result, error) {
// Implementation note: here we cannot use the session's HTTP client because it MAY be proxied
// and instead we need to connect directly to M-Lab's locate service.
//
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS and maybe here it doesn't matter?
httpClient := netxlite.NewHTTPClientStdlib(sess.Logger())
defer httpClient.CloseIdleConnections()
client := mlablocatev2.NewClient(httpClient, sess.Logger(), sess.UserAgent())
Expand Down
3 changes: 3 additions & 0 deletions internal/experiment/webconnectivitylte/cleartextflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func (t *CleartextFlow) Run(parentCtx context.Context, index int64) error {
}

// create HTTP transport
// TODO(https://github.com/ooni/probe/issues/2534): here we're using the QUIRKY netxlite.NewHTTPTransport
// function, but we can probably avoid using it, given that this code is
// not using tracing and does not care about those quirks.
httpTransport := netxlite.NewHTTPTransport(
t.Logger,
netxlite.NewSingleUseDialer(tcpConn),
Expand Down
3 changes: 3 additions & 0 deletions internal/experiment/webconnectivitylte/secureflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ func (t *SecureFlow) Run(parentCtx context.Context, index int64) error {
}

// create HTTP transport
// TODO(https://github.com/ooni/probe/issues/2534): here we're using the QUIRKY netxlite.NewHTTPTransport
// function, but we can probably avoid using it, given that this code is
// not using tracing and does not care about those quirks.
httpTransport := netxlite.NewHTTPTransport(
t.Logger,
netxlite.NewNullDialer(),
Expand Down
1 change: 1 addition & 0 deletions internal/experiment/webconnectivityqa/badssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestBadSSLConditions(t *testing.T) {
tc.testCase.Configure(env)

env.Do(func() {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
req := runtimex.Try1(http.NewRequest("GET", tc.testCase.Input, nil))
resp, err := client.Do(req)
Expand Down
2 changes: 2 additions & 0 deletions internal/experiment/webconnectivityqa/httpdiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestHTTPDiffWithConsistentDNS(t *testing.T) {
tc.Configure(env)

env.Do(func() {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
req := runtimex.Try1(http.NewRequest("GET", "http://www.example.com/", nil))
resp, err := client.Do(req)
Expand Down Expand Up @@ -58,6 +59,7 @@ func TestHTTPDiffWithInconsistentDNS(t *testing.T) {

env.Do(func() {
t.Run("there is blockpage spoofing", func(t *testing.T) {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
req := runtimex.Try1(http.NewRequest("GET", "http://www.example.com/", nil))
resp, err := client.Do(req)
Expand Down
3 changes: 3 additions & 0 deletions internal/experiment/webconnectivityqa/redirect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestRedirectWithConsistentDNSAndThenConnectionReset(t *testing.T) {

for _, URL := range urls {
t.Run(fmt.Sprintf("for URL %s", URL), func(t *testing.T) {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
req := runtimex.Try1(http.NewRequest("GET", URL, nil))
resp, err := client.Do(req)
Expand Down Expand Up @@ -140,6 +141,7 @@ func TestRedirectWithConsistentDNSAndThenEOF(t *testing.T) {

for _, URL := range urls {
t.Run(fmt.Sprintf("for URL %s", URL), func(t *testing.T) {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
req := runtimex.Try1(http.NewRequest("GET", URL, nil))
resp, err := client.Do(req)
Expand Down Expand Up @@ -176,6 +178,7 @@ func TestRedirectWithConsistentDNSAndThenTimeout(t *testing.T) {
t.Run(fmt.Sprintf("for URL %s", URL), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
req := runtimex.Try1(http.NewRequestWithContext(ctx, "GET", URL, nil))
resp, err := client.Do(req)
Expand Down
1 change: 1 addition & 0 deletions internal/experiment/webconnectivityqa/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func RunTestCase(measurer model.ExperimentMeasurer, tc *TestCase) error {
var err error
env.Do(func() {
// create an HTTP client inside the env.Do function so we're using netem
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
httpClient := netxlite.NewHTTPClientStdlib(prefixLogger)
arguments := &model.ExperimentArgs{
Callbacks: model.NewPrinterCallbacks(prefixLogger),
Expand Down
2 changes: 2 additions & 0 deletions internal/experiment/webconnectivityqa/tlsblocking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestBlockingTLSConnectionResetWithConsistentDNS(t *testing.T) {
urls := []string{"https://www.example.com/", "https://www.example.com/"}
for _, URL := range urls {
t.Run(fmt.Sprintf("for %s", URL), func(t *testing.T) {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
Expand Down Expand Up @@ -51,6 +52,7 @@ func TestBlockingTLSConnectionResetWithInconsistentDNS(t *testing.T) {
urls := []string{"https://www.example.com/", "https://www.example.com/"}
for _, URL := range urls {
t.Run(fmt.Sprintf("for %s", URL), func(t *testing.T) {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/netemx/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func Example_dpiRule() {
reso := netxlite.NewStdlibResolver(model.DiscardLogger)

// create the HTTP client
// TODO(https://github.com/ooni/probe/issues/2534): the NewHTTPClientWithResolver func has QUIRKS but we don't care.
client := netxlite.NewHTTPClientWithResolver(model.DiscardLogger, reso)

// create the HTTP request
Expand Down Expand Up @@ -310,6 +311,7 @@ func Example_exampleWebServerWithInternetScenario() {
defer env.Close()

env.Do(func() {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)

req, err := http.NewRequest("GET", "https://www.example.com/", nil)
Expand Down Expand Up @@ -389,6 +391,7 @@ func Example_ooniAPIWithInternetScenario() {
defer env.Close()

env.Do(func() {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)

req, err := http.NewRequest("GET", "https://api.ooni.io/api/v1/test-helpers", nil)
Expand Down Expand Up @@ -419,6 +422,7 @@ func Example_oohelperdWithInternetScenario() {
defer env.Close()

env.Do(func() {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
thRequest := []byte(`{"http_request": "https://www.example.com/","http_request_headers":{},"tcp_connect":["93.184.216.34:443"]}`)

Expand Down Expand Up @@ -452,6 +456,7 @@ func Example_ubuntuGeoIPWithInternetScenario() {
defer env.Close()

env.Do(func() {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)

req, err := http.NewRequest("GET", "https://geoip.ubuntu.com/lookup", nil)
Expand Down Expand Up @@ -482,6 +487,7 @@ func Example_examplePublicBlockpage() {
defer env.Close()

env.Do(func() {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)

req, err := http.NewRequest("GET", "http://"+netemx.AddressPublicBlockpage+"/", nil)
Expand Down Expand Up @@ -523,6 +529,8 @@ func Example_exampleURLShortener() {
defer env.Close()

env.Do(func() {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPTransportStdlib has QUIRKS but we
// don't actually care about those QUIRKS in this context
client := netxlite.NewHTTPTransportStdlib(log.Log)

req, err := http.NewRequest("GET", "https://bit.ly/21645", nil)
Expand Down
1 change: 1 addition & 0 deletions internal/netemx/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestHTTPCleartextServerFactory(t *testing.T) {
env.AddRecordToAllResolvers("www.example.com", "", AddressWwwExampleCom)

env.Do(func() {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
req := runtimex.Try1(http.NewRequest("GET", "http://www.example.com/", nil))
resp, err := client.Do(req)
Expand Down
2 changes: 2 additions & 0 deletions internal/netemx/https_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestHTTPSecureServerFactory(t *testing.T) {
env.AddRecordToAllResolvers("www.example.com", "", AddressWwwExampleCom)

env.Do(func() {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
req := runtimex.Try1(http.NewRequest("GET", "https://www.example.com/", nil))
resp, err := client.Do(req)
Expand Down Expand Up @@ -66,6 +67,7 @@ func TestHTTPSecureServerFactory(t *testing.T) {
env.AddRecordToAllResolvers("www.example.com", "", AddressWwwExampleCom)

env.Do(func() {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
req := runtimex.Try1(http.NewRequest("GET", "https://www.example.com/", nil))
resp, err := client.Do(req)
Expand Down
2 changes: 2 additions & 0 deletions internal/netemx/oohelperd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func (f *OOHelperDFactory) NewHandler(env NetStackServerFactoryEnv, unet *netem.
cookieJar, _ := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPTransportStdlib is QUIRKY but we probably
// don't care about using a QUIRKY function here
return &http.Client{
Transport: netx.NewHTTPTransportStdlib(logger),
CheckRedirect: nil,
Expand Down
1 change: 1 addition & 0 deletions internal/netemx/oohelperd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestOOHelperDHandler(t *testing.T) {

//log.SetLevel(log.DebugLevel)

// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
httpClient := netxlite.NewHTTPClientStdlib(log.Log)

req, err := http.NewRequest(http.MethodPost, "https://0.th.ooni.org/", bytes.NewReader(thReqRaw))
Expand Down
3 changes: 3 additions & 0 deletions internal/netemx/qaenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func TestQAEnv(t *testing.T) {
env.Do(func() {
// create client, which will use the underlying client stack's
// DialContext method to dial connections
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(model.DiscardLogger)

// create request using a domain that has been configured in the
Expand Down Expand Up @@ -211,6 +212,7 @@ func TestQAEnv(t *testing.T) {
env.Do(func() {
// create client, which will use the underlying client stack's
// DialContext method to dial connections
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(model.DiscardLogger)

// create the request
Expand Down Expand Up @@ -257,6 +259,7 @@ func TestQAEnv(t *testing.T) {
env.Do(func() {
// create client, which will use the underlying client stack's
// DialContext method to dial connections
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(model.DiscardLogger)

// create the request
Expand Down
3 changes: 3 additions & 0 deletions internal/netxlite/dnsoverhttps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

func TestNewDNSOverHTTPSTransport(t *testing.T) {
const URL = "https://1.1.1.1/dns-query"
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
clnt := NewHTTPClientStdlib(model.DiscardLogger)
txp := NewDNSOverHTTPSTransport(clnt, URL)
ew := txp.(*dnsTransportErrWrapper)
Expand All @@ -29,6 +30,8 @@ func TestNewDNSOverHTTPSTransport(t *testing.T) {

func TestNewDNSOverHTTPSTransportWithHTTPTransport(t *testing.T) {
const URL = "https://1.1.1.1/dns-query"
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPTransportStdlib has QUIRKS but we
// don't actually care about those QUIRKS in this context
httpTxp := NewHTTPTransportStdlib(model.DiscardLogger)
txp := NewDNSOverHTTPSTransportWithHTTPTransport(httpTxp, URL)
ew := txp.(*dnsTransportErrWrapper)
Expand Down
12 changes: 12 additions & 0 deletions internal/netxlite/httpfactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package netxlite

import (
"net/http"

"github.com/ooni/probe-cli/v3/internal/model"
)

// NewHTTPClient creates a new, wrapped HTTPClient using the given transport.
func NewHTTPClient(txp model.HTTPTransport) model.HTTPClient {
return WrapHTTPClient(&http.Client{Transport: txp})
}
Loading

0 comments on commit 0f6b122

Please sign in to comment.