refactor(oohelperd): use netxlite rather than netx (#806)

The oohelperd implementation did not actually need using netx because
it was just constructing default types with logging, which is what
netxlite already does. Hence, let's avoid using netx here.

See https://github.com/ooni/probe/issues/2121
This commit is contained in:
Simone Basso 2022-06-08 10:14:10 +02:00 committed by GitHub
parent 87d35f4225
commit dea23b49d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 21 deletions

View File

@ -8,6 +8,7 @@ import (
"sync" "sync"
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity" "github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite" "github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/tracex" "github.com/ooni/probe-cli/v3/internal/tracex"
) )
@ -18,7 +19,7 @@ type CtrlHTTPResponse = webconnectivity.ControlHTTPRequestResult
// HTTPConfig configures the HTTP check. // HTTPConfig configures the HTTP check.
type HTTPConfig struct { type HTTPConfig struct {
Client *http.Client Client model.HTTPClient
Headers map[string][]string Headers map[string][]string
MaxAcceptableBody int64 MaxAcceptableBody int64
Out chan CtrlHTTPResponse Out chan CtrlHTTPResponse

View File

@ -3,7 +3,6 @@ package webconnectivity
import ( import (
"context" "context"
"net" "net"
"net/http"
"net/url" "net/url"
"sync" "sync"
@ -21,7 +20,7 @@ type (
// MeasureConfig contains configuration for Measure. // MeasureConfig contains configuration for Measure.
type MeasureConfig struct { type MeasureConfig struct {
Client *http.Client Client model.HTTPClient
Dialer model.Dialer Dialer model.Dialer
MaxAcceptableBody int64 MaxAcceptableBody int64
Resolver model.Resolver Resolver model.Resolver

View File

@ -14,7 +14,7 @@ import (
// Handler implements the Web Connectivity test helper HTTP API. // Handler implements the Web Connectivity test helper HTTP API.
type Handler struct { type Handler struct {
Client *http.Client Client model.HTTPClient
Dialer model.Dialer Dialer model.Dialer
MaxAcceptableBody int64 MaxAcceptableBody int64
Resolver model.Resolver Resolver model.Resolver

View File

@ -10,32 +10,33 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/cmd/oohelperd/internal/webconnectivity" "github.com/ooni/probe-cli/v3/internal/cmd/oohelperd/internal/webconnectivity"
"github.com/ooni/probe-cli/v3/internal/engine/netx"
"github.com/ooni/probe-cli/v3/internal/model" "github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/runtimex" "github.com/ooni/probe-cli/v3/internal/netxlite"
) )
const maxAcceptableBody = 1 << 24 const maxAcceptableBody = 1 << 24
var ( var (
dialer model.Dialer dialer model.Dialer
endpoint = flag.String("endpoint", ":8080", "Endpoint where to listen") endpoint = flag.String("endpoint", ":8080", "Endpoint where to listen")
httpx *http.Client httpClient model.HTTPClient
resolver model.Resolver resolver model.Resolver
srvcancel context.CancelFunc srvcancel context.CancelFunc
srvctx context.Context srvctx context.Context
srvwg = new(sync.WaitGroup) srvwg = new(sync.WaitGroup)
) )
func init() { func init() {
srvctx, srvcancel = context.WithCancel(context.Background()) srvctx, srvcancel = context.WithCancel(context.Background())
dialer = netx.NewDialer(netx.Config{Logger: log.Log}) // Implementation note: pin to a specific resolver so we don't depend upon the
txp := netx.NewHTTPTransport(netx.Config{Logger: log.Log}) // default resolver configured by the box. Also, use an encrypted transport thus
httpx = &http.Client{Transport: txp} // we're less vulnerable to any policy implemented by the box's provider.
// fix: use 8.8.8.8:53/udp so we pin to a specific resolver. resolver = netxlite.NewParallelDNSOverHTTPSResolver(log.Log, "https://8.8.8.8/dns-query")
var err error thx := netxlite.NewTLSHandshakerStdlib(log.Log)
resolver, err = netx.NewDNSClient(netx.Config{Logger: log.Log}, "udp://8.8.8.8:53") dialer = netxlite.NewDialerWithResolver(log.Log, resolver)
runtimex.PanicOnError(err, "NewDNSClient failed") tlsDialer := netxlite.NewTLSDialer(dialer, thx)
txp := netxlite.NewHTTPTransport(log.Log, dialer, tlsDialer)
httpClient = netxlite.NewHTTPClient(txp)
} }
func shutdown(srv *http.Server) { func shutdown(srv *http.Server) {
@ -58,7 +59,7 @@ func main() {
func testableMain() { func testableMain() {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle("/", webconnectivity.Handler{ mux.Handle("/", webconnectivity.Handler{
Client: httpx, Client: httpClient,
Dialer: dialer, Dialer: dialer,
MaxAcceptableBody: maxAcceptableBody, MaxAcceptableBody: maxAcceptableBody,
Resolver: resolver, Resolver: resolver,