From 7410b1299a5cbcaa7b3e13574618775bc9d82dbb Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Mon, 11 Sep 2023 19:27:30 +0200 Subject: [PATCH] x --- .../gardener/internal/dnsreport/dnsreport.go | 12 ++++----- internal/netxlite/dnsoverhttps.go | 4 +-- internal/netxlite/dnstransport.go | 4 +-- internal/netxlite/dnstransport_test.go | 26 ++----------------- internal/netxlite/resolvercore.go | 8 +++--- 5 files changed, 15 insertions(+), 39 deletions(-) diff --git a/internal/cmd/gardener/internal/dnsreport/dnsreport.go b/internal/cmd/gardener/internal/dnsreport/dnsreport.go index 34a4dd7574..ab165b5430 100644 --- a/internal/cmd/gardener/internal/dnsreport/dnsreport.go +++ b/internal/cmd/gardener/internal/dnsreport/dnsreport.go @@ -8,7 +8,6 @@ import ( "encoding/json" "fmt" "net" - "net/http" "net/url" "os" "path/filepath" @@ -255,12 +254,11 @@ func (s *Subcommand) dnsLookupHost(domain string) ([]string, error) { defer cancel() // create DNS transport using HTTP default client - dnsTransport := netxlite.WrapDNSTransport(&netxlite.DNSOverHTTPSTransport{ - Client: http.DefaultClient, - Decoder: &netxlite.DNSDecoderMiekg{}, - URL: s.DNSOverHTTPSServerURL, - HostOverride: "", - }) + dnsTransport := netxlite.NewDNSOverHTTPSTransportWithHTTPTransport( + netxlite.NewHTTPTransportStdlib(log.Log), + s.DNSOverHTTPSServerURL, + ) + defer dnsTransport.CloseIdleConnections() // create DNS resolver dnsResolver := netxlite.WrapResolver( diff --git a/internal/netxlite/dnsoverhttps.go b/internal/netxlite/dnsoverhttps.go index aeb56f08e7..88f9e317f0 100644 --- a/internal/netxlite/dnsoverhttps.go +++ b/internal/netxlite/dnsoverhttps.go @@ -46,13 +46,13 @@ func NewUnwrappedDNSOverHTTPSTransport(client model.HTTPClient, URL string) *DNS // NewDNSOverHTTPSTransport is like NewUnwrappedDNSOverHTTPSTransport but // returns an already wrapped DNSTransport. func NewDNSOverHTTPSTransport(client model.HTTPClient, URL string) model.DNSTransport { - return WrapDNSTransport(NewUnwrappedDNSOverHTTPSTransport(client, URL)) + return wrapDNSTransport(NewUnwrappedDNSOverHTTPSTransport(client, URL)) } // NewDNSOverHTTPSTransportWithHTTPTransport is like NewDNSOverHTTPSTransport // but takes in input an HTTPTransport rather than an HTTPClient. func NewDNSOverHTTPSTransportWithHTTPTransport(txp model.HTTPTransport, URL string) model.DNSTransport { - return WrapDNSTransport(NewUnwrappedDNSOverHTTPSTransport(NewHTTPClient(txp), URL)) + return wrapDNSTransport(NewUnwrappedDNSOverHTTPSTransport(NewHTTPClient(txp), URL)) } // NewUnwrappedDNSOverHTTPSTransportWithHostOverride creates a new DNSOverHTTPSTransport diff --git a/internal/netxlite/dnstransport.go b/internal/netxlite/dnstransport.go index b49ad5c41b..e4cdf7e3a5 100644 --- a/internal/netxlite/dnstransport.go +++ b/internal/netxlite/dnstransport.go @@ -10,8 +10,8 @@ import ( "github.com/ooni/probe-cli/v3/internal/model" ) -// WrapDNSTransport wraps a DNSTransport to provide error wrapping. -func WrapDNSTransport(txp model.DNSTransport) (out model.DNSTransport) { +// wrapDNSTransport wraps a DNSTransport to provide error wrapping. +func wrapDNSTransport(txp model.DNSTransport) (out model.DNSTransport) { out = &dnsTransportErrWrapper{ DNSTransport: txp, } diff --git a/internal/netxlite/dnstransport_test.go b/internal/netxlite/dnstransport_test.go index eea16c5a50..ab13015bf9 100644 --- a/internal/netxlite/dnstransport_test.go +++ b/internal/netxlite/dnstransport_test.go @@ -10,32 +10,10 @@ import ( "github.com/ooni/probe-cli/v3/internal/model" ) -type dnsTransportExtensionFirst struct { - model.DNSTransport -} - -type dnsTransportWrapperFirst struct{} - -func (*dnsTransportWrapperFirst) WrapDNSTransport(txp model.DNSTransport) model.DNSTransport { - return &dnsTransportExtensionFirst{txp} -} - -type dnsTransportExtensionSecond struct { - model.DNSTransport -} - -type dnsTransportWrapperSecond struct{} - -func (*dnsTransportWrapperSecond) WrapDNSTransport(txp model.DNSTransport) model.DNSTransport { - return &dnsTransportExtensionSecond{txp} -} - func TestWrapDNSTransport(t *testing.T) { orig := &mocks.DNSTransport{} - txp := WrapDNSTransport(orig) - ext2 := txp.(*dnsTransportExtensionSecond) - ext1 := ext2.DNSTransport.(*dnsTransportExtensionFirst) - errWrapper := ext1.DNSTransport.(*dnsTransportErrWrapper) + txp := wrapDNSTransport(orig) + errWrapper := txp.(*dnsTransportErrWrapper) underlying := errWrapper.DNSTransport if orig != underlying { t.Fatal("unexpected underlying transport") diff --git a/internal/netxlite/resolvercore.go b/internal/netxlite/resolvercore.go index de03666c0d..b30cd2f5fe 100644 --- a/internal/netxlite/resolvercore.go +++ b/internal/netxlite/resolvercore.go @@ -41,13 +41,13 @@ func NewStdlibResolver(logger model.DebugLogger) model.Resolver { // all the building blocks and calls WrapResolver on the returned resolver. func NewParallelDNSOverHTTPSResolver(logger model.DebugLogger, URL string) model.Resolver { client := &http.Client{Transport: NewHTTPTransportStdlib(logger)} - txp := WrapDNSTransport(NewUnwrappedDNSOverHTTPSTransport(client, URL)) + txp := wrapDNSTransport(NewUnwrappedDNSOverHTTPSTransport(client, URL)) return WrapResolver(logger, NewUnwrappedParallelResolver(txp)) } func (netx *Netx) newUnwrappedStdlibResolver() model.Resolver { return &resolverSystem{ - t: WrapDNSTransport(netx.newDNSOverGetaddrinfoTransport()), + t: wrapDNSTransport(netx.newDNSOverGetaddrinfoTransport()), } } @@ -73,7 +73,7 @@ func NewUnwrappedStdlibResolver() model.Resolver { // - address is the server address (e.g., 1.1.1.1:53) func NewSerialUDPResolver(logger model.DebugLogger, dialer model.Dialer, address string) model.Resolver { return WrapResolver(logger, NewUnwrappedSerialResolver( - WrapDNSTransport(NewUnwrappedDNSOverUDPTransport(dialer, address)), + wrapDNSTransport(NewUnwrappedDNSOverUDPTransport(dialer, address)), )) } @@ -90,7 +90,7 @@ func NewSerialUDPResolver(logger model.DebugLogger, dialer model.Dialer, address func NewParallelUDPResolver(logger model.DebugLogger, dialer model.Dialer, address string) model.Resolver { return WrapResolver(logger, NewUnwrappedParallelResolver( - WrapDNSTransport(NewUnwrappedDNSOverUDPTransport(dialer, address)), + wrapDNSTransport(NewUnwrappedDNSOverUDPTransport(dialer, address)), )) }