fix(netxlite): prefer composition over embedding (#733)

This diff has been extracted and adapted from 8848c8c516

The reason to prefer composition over embedding is that we want the
build to break if we add new methods to interfaces we define. If the build
does not break, we may forget about wrapping methods we should
actually be wrapping. I noticed this issue inside netxlite when I was working
on websteps-illustrated and I added support for NS and PTR queries.

See https://github.com/ooni/probe/issues/2096

While there, perform comprehensive netxlite code review
and apply minor changes and improve the docs.
This commit is contained in:
Simone Basso 2022-05-15 19:25:27 +02:00 committed by GitHub
commit c1b06a2d09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 328 additions and 92 deletions

View file

@ -1,5 +1,9 @@
package netxlite
//
// TLS implementation
//
import (
"context"
"crypto/tls"
@ -11,8 +15,12 @@ import (
oohttp "github.com/ooni/oohttp"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
// TODO(bassosimone): check whether there's now equivalent functionality
// inside the standard library allowing us to map numbers to names.
var (
tlsVersionString = map[uint16]string{
tls.VersionTLS10: "TLSv1",
@ -81,7 +89,8 @@ func NewDefaultCertPool() *x509.CertPool {
pool := x509.NewCertPool()
// Assumption: AppendCertsFromPEM cannot fail because we
// have a test in certify_test.go that guarantees that
pool.AppendCertsFromPEM([]byte(pemcerts))
ok := pool.AppendCertsFromPEM([]byte(pemcerts))
runtimex.PanicIfFalse(ok, "pool.AppendCertsFromPEM failed")
return pool
}
@ -201,8 +210,8 @@ var defaultTLSHandshaker = &tlsHandshakerConfigurable{}
// tlsHandshakerLogger is a TLSHandshaker with logging.
type tlsHandshakerLogger struct {
model.TLSHandshaker
model.DebugLogger
TLSHandshaker model.TLSHandshaker
DebugLogger model.DebugLogger
}
var _ model.TLSHandshaker = &tlsHandshakerLogger{}
@ -313,7 +322,7 @@ func NewSingleUseTLSDialer(conn TLSConn) model.TLSDialer {
// tlsDialerSingleUseAdapter adapts dialerSingleUse to
// be a TLSDialer type rather than a Dialer type.
type tlsDialerSingleUseAdapter struct {
model.Dialer
Dialer model.Dialer
}
var _ model.TLSDialer = &tlsDialerSingleUseAdapter{}
@ -323,9 +332,13 @@ func (d *tlsDialerSingleUseAdapter) DialTLSContext(ctx context.Context, network,
return d.Dialer.DialContext(ctx, network, address)
}
func (d *tlsDialerSingleUseAdapter) CloseIdleConnections() {
d.Dialer.CloseIdleConnections()
}
// tlsHandshakerErrWrapper wraps the returned error to be an OONI error
type tlsHandshakerErrWrapper struct {
model.TLSHandshaker
TLSHandshaker model.TLSHandshaker
}
// Handshake implements TLSHandshaker.Handshake
@ -334,7 +347,7 @@ func (h *tlsHandshakerErrWrapper) Handshake(
) (net.Conn, tls.ConnectionState, error) {
tlsconn, state, err := h.TLSHandshaker.Handshake(ctx, conn, config)
if err != nil {
return nil, tls.ConnectionState{}, NewErrWrapper(
return nil, tls.ConnectionState{}, newErrWrapper(
classifyTLSHandshakeError, TLSHandshakeOperation, err)
}
return tlsconn, state, nil