Skip to content

Commit

Permalink
refactor: continue to simplify engine/netx (#769)
Browse files Browse the repository at this point in the history
The objective of this diff is to simplify the code inside engine/netx
while moving more bits of code inside netxlite.

See ooni/probe#2121
  • Loading branch information
bassosimone authored May 31, 2022
1 parent 3265bc6 commit e4f10ee
Show file tree
Hide file tree
Showing 24 changed files with 312 additions and 344 deletions.
3 changes: 1 addition & 2 deletions internal/engine/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/version"
Expand Down Expand Up @@ -286,7 +285,7 @@ func (e *Experiment) OpenReportContext(ctx context.Context) error {
}
// use custom client to have proper byte accounting
httpClient := &http.Client{
Transport: &httptransport.ByteCountingTransport{
Transport: &bytecounter.HTTPTransport{
HTTPTransport: e.session.httpDefaultTransport, // proxy is OK
Counter: e.byteCounter,
},
Expand Down
5 changes: 0 additions & 5 deletions internal/engine/netx/dialer/bytecounter.go

This file was deleted.

8 changes: 6 additions & 2 deletions internal/engine/netx/dialer/dialer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Package dialer allows you to create a net.Dialer-compatible
// DialContext-enabled dialer with error wrapping, optional logging,
// optional network-events saving, and optional proxying.
package dialer

import (
"net/url"

"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
Expand Down Expand Up @@ -63,9 +67,9 @@ func New(config *Config, resolver model.Resolver) model.Dialer {
Resolver: resolver,
Dialer: d,
}
d = &proxyDialer{ProxyURL: config.ProxyURL, Dialer: d}
d = &netxlite.MaybeProxyDialer{ProxyURL: config.ProxyURL, Dialer: d}
if config.ContextByteCounting {
d = &byteCounterDialer{Dialer: d}
d = &bytecounter.ContextAwareDialer{Dialer: d}
}
return d
}
21 changes: 19 additions & 2 deletions internal/engine/netx/dialer/dialer_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dialer

import (
"net/http"
"net/url"
"testing"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
Expand All @@ -18,11 +20,11 @@ func TestNewCreatesTheExpectedChain(t *testing.T) {
ProxyURL: &url.URL{},
ReadWriteSaver: saver,
}, netxlite.DefaultResolver)
bcd, ok := dlr.(*byteCounterDialer)
bcd, ok := dlr.(*bytecounter.ContextAwareDialer)
if !ok {
t.Fatal("not a byteCounterDialer")
}
pd, ok := bcd.Dialer.(*proxyDialer)
pd, ok := bcd.Dialer.(*netxlite.MaybeProxyDialer)
if !ok {
t.Fatal("not a proxyDialer")
}
Expand Down Expand Up @@ -51,3 +53,18 @@ func TestNewCreatesTheExpectedChain(t *testing.T) {
t.Fatal("not a DialerSystem")
}
}

func TestDialerNewSuccess(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
log.SetLevel(log.DebugLevel)
d := New(&Config{Logger: log.Log}, netxlite.DefaultResolver)
txp := &http.Transport{DialContext: d.DialContext}
client := &http.Client{Transport: txp}
resp, err := client.Get("http://www.google.com")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
}
4 changes: 0 additions & 4 deletions internal/engine/netx/dialer/doc.go

This file was deleted.

30 changes: 0 additions & 30 deletions internal/engine/netx/dialer/example_test.go

This file was deleted.

25 changes: 0 additions & 25 deletions internal/engine/netx/dialer/integration_test.go

This file was deleted.

5 changes: 0 additions & 5 deletions internal/engine/netx/dialer/proxy.go

This file was deleted.

5 changes: 0 additions & 5 deletions internal/engine/netx/httptransport/bytecounter.go

This file was deleted.

62 changes: 0 additions & 62 deletions internal/engine/netx/httptransport/fake_test.go

This file was deleted.

24 changes: 0 additions & 24 deletions internal/engine/netx/httptransport/http3transport.go

This file was deleted.

40 changes: 0 additions & 40 deletions internal/engine/netx/httptransport/http3transport_test.go

This file was deleted.

23 changes: 23 additions & 0 deletions internal/engine/netx/httptransport/httptransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"

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

// Config contains the configuration required for constructing an HTTP transport
Expand All @@ -14,3 +15,25 @@ type Config struct {
TLSDialer model.TLSDialer
TLSConfig *tls.Config
}

// NewHTTP3Transport creates a new HTTP3Transport instance.
//
// Deprecation warning
//
// New code should use netxlite.NewHTTP3Transport instead.
func NewHTTP3Transport(config Config) model.HTTPTransport {
// Rationale for using NoLogger here: previously this code did
// not use a logger as well, so it's fine to keep it as is.
return netxlite.NewHTTP3Transport(model.DiscardLogger,
config.QUICDialer, config.TLSConfig)
}

// NewSystemTransport creates a new "system" HTTP transport. That is a transport
// using the Go standard library with custom dialer and TLS dialer.
//
// Deprecation warning
//
// New code should use netxlite.NewHTTPTransport instead.
func NewSystemTransport(config Config) model.HTTPTransport {
return netxlite.NewOOHTTPBaseTransport(config.Dialer, config.TLSDialer)
}
Loading

0 comments on commit e4f10ee

Please sign in to comment.