refactor(netxlite): more abstract proxy-enabled dialer construction (#812)

This will help with https://github.com/ooni/probe/issues/2135
This commit is contained in:
Simone Basso 2022-06-08 23:10:06 +02:00 committed by GitHub
parent bf7ea423d3
commit 1a706e47bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 35 deletions

View File

@ -21,7 +21,7 @@ func NewDialer(config Config) model.Dialer {
logger, config.FullResolver, config.Saver.NewConnectObserver(), logger, config.FullResolver, config.Saver.NewConnectObserver(),
config.ReadWriteSaver.NewReadWriteObserver(), config.ReadWriteSaver.NewReadWriteObserver(),
) )
d = netxlite.NewMaybeProxyDialer(d, config.ProxyURL) d = netxlite.MaybeWrapWithProxyDialer(d, config.ProxyURL)
d = bytecounter.MaybeWrapWithContextAwareDialer(config.ContextByteCounting, d) d = bytecounter.MaybeWrapWithContextAwareDialer(config.ContextByteCounting, d)
return d return d
} }

View File

@ -198,7 +198,7 @@ func NewSession(ctx context.Context, config SessionConfig) (*Session, error) {
ProxyURL: proxyURL, ProxyURL: proxyURL,
} }
dialer := netxlite.NewDialerWithResolver(sess.logger, sess.resolver) dialer := netxlite.NewDialerWithResolver(sess.logger, sess.resolver)
dialer = netxlite.NewMaybeProxyDialer(dialer, proxyURL) dialer = netxlite.MaybeWrapWithProxyDialer(dialer, proxyURL)
handshaker := netxlite.NewTLSHandshakerStdlib(sess.logger) handshaker := netxlite.NewTLSHandshakerStdlib(sess.logger)
tlsDialer := netxlite.NewTLSDialer(dialer, handshaker) tlsDialer := netxlite.NewTLSDialer(dialer, handshaker)
txp := netxlite.NewHTTPTransport(sess.logger, dialer, tlsDialer) txp := netxlite.NewHTTPTransport(sess.logger, dialer, tlsDialer)

View File

@ -1,5 +1,9 @@
package netxlite package netxlite
//
// Optional proxy support
//
import ( import (
"context" "context"
"errors" "errors"
@ -10,38 +14,37 @@ import (
"golang.org/x/net/proxy" "golang.org/x/net/proxy"
) )
// MaybeProxyDialer is a dialer that may use a proxy. If the ProxyURL is not configured, // proxyDialer is a dialer using a proxy.
// this dialer is a passthrough for the next Dialer in chain. Otherwise, it will internally type proxyDialer struct {
// create a SOCKS5 dialer that will connect to the proxy using the underlying Dialer.
type MaybeProxyDialer struct {
Dialer model.Dialer Dialer model.Dialer
ProxyURL *url.URL ProxyURL *url.URL
} }
// NewMaybeProxyDialer creates a new NewMaybeProxyDialer. // MaybeWrapWithProxyDialer returns the original dialer if the proxyURL is nil
func NewMaybeProxyDialer(dialer model.Dialer, proxyURL *url.URL) *MaybeProxyDialer { // and otherwise returns a wrapped dialer that implements proxying.
return &MaybeProxyDialer{ func MaybeWrapWithProxyDialer(dialer model.Dialer, proxyURL *url.URL) model.Dialer {
if proxyURL == nil {
return dialer
}
return &proxyDialer{
Dialer: dialer, Dialer: dialer,
ProxyURL: proxyURL, ProxyURL: proxyURL,
} }
} }
var _ model.Dialer = &MaybeProxyDialer{} var _ model.Dialer = &proxyDialer{}
// CloseIdleConnections implements Dialer.CloseIdleConnections. // CloseIdleConnections implements Dialer.CloseIdleConnections.
func (d *MaybeProxyDialer) CloseIdleConnections() { func (d *proxyDialer) CloseIdleConnections() {
d.Dialer.CloseIdleConnections() d.Dialer.CloseIdleConnections()
} }
// ErrProxyUnsupportedScheme indicates we don't support a protocol scheme. // ErrProxyUnsupportedScheme indicates we don't support the proxy scheme.
var ErrProxyUnsupportedScheme = errors.New("proxy: unsupported scheme") var ErrProxyUnsupportedScheme = errors.New("proxy: unsupported scheme")
// DialContext implements Dialer.DialContext. // DialContext implements Dialer.DialContext.
func (d *MaybeProxyDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) { func (d *proxyDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
url := d.ProxyURL url := d.ProxyURL
if url == nil {
return d.Dialer.DialContext(ctx, network, address)
}
if url.Scheme != "socks5" { if url.Scheme != "socks5" {
return nil, ErrProxyUnsupportedScheme return nil, ErrProxyUnsupportedScheme
} }
@ -50,7 +53,7 @@ func (d *MaybeProxyDialer) DialContext(ctx context.Context, network, address str
return d.dial(ctx, child, network, address) return d.dial(ctx, child, network, address)
} }
func (d *MaybeProxyDialer) dial( func (d *proxyDialer) dial(
ctx context.Context, child proxy.Dialer, network, address string) (net.Conn, error) { ctx context.Context, child proxy.Dialer, network, address string) (net.Conn, error) {
cd := child.(proxy.ContextDialer) // will work cd := child.(proxy.ContextDialer) // will work
return cd.DialContext(ctx, network, address) return cd.DialContext(ctx, network, address)

View File

@ -12,28 +12,34 @@ import (
) )
func TestMaybeProxyDialer(t *testing.T) { func TestMaybeProxyDialer(t *testing.T) {
t.Run("DialContext", func(t *testing.T) { t.Run("MaybeWrapWithProxyDialer", func(t *testing.T) {
t.Run("missing proxy URL", func(t *testing.T) { t.Run("without a proxy URL", func(t *testing.T) {
expected := errors.New("mocked error") underlying := &mocks.Dialer{}
d := &MaybeProxyDialer{ dialer := MaybeWrapWithProxyDialer(underlying, nil)
Dialer: &mocks.Dialer{MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) { if dialer != underlying {
return nil, expected t.Fatal("should not have wrapped")
}},
ProxyURL: nil,
}
conn, err := d.DialContext(context.Background(), "tcp", "www.google.com:443")
if !errors.Is(err, expected) {
t.Fatal(err)
}
if conn != nil {
t.Fatal("conn is not nil")
} }
}) })
t.Run("with a proxy URL", func(t *testing.T) {
URL := &url.URL{}
underlying := &mocks.Dialer{}
dialer := MaybeWrapWithProxyDialer(underlying, URL)
real := dialer.(*proxyDialer)
if real.Dialer != underlying {
t.Fatal("did not wrap correctly")
}
if real.ProxyURL != URL {
t.Fatal("invalid URL")
}
})
})
t.Run("DialContext", func(t *testing.T) {
t.Run("invalid scheme", func(t *testing.T) { t.Run("invalid scheme", func(t *testing.T) {
child := &mocks.Dialer{} child := &mocks.Dialer{}
URL := &url.URL{Scheme: "antani"} URL := &url.URL{Scheme: "antani"}
d := NewMaybeProxyDialer(child, URL) d := MaybeWrapWithProxyDialer(child, URL)
conn, err := d.DialContext(context.Background(), "tcp", "www.google.com:443") conn, err := d.DialContext(context.Background(), "tcp", "www.google.com:443")
if !errors.Is(err, ErrProxyUnsupportedScheme) { if !errors.Is(err, ErrProxyUnsupportedScheme) {
t.Fatal("not the error we expected") t.Fatal("not the error we expected")
@ -45,7 +51,7 @@ func TestMaybeProxyDialer(t *testing.T) {
t.Run("underlying dial fails with EOF", func(t *testing.T) { t.Run("underlying dial fails with EOF", func(t *testing.T) {
const expect = "10.0.0.1:9050" const expect = "10.0.0.1:9050"
d := &MaybeProxyDialer{ d := &proxyDialer{
Dialer: &mocks.Dialer{ Dialer: &mocks.Dialer{
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) { MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
if address != expect { if address != expect {
@ -77,7 +83,7 @@ func TestMaybeProxyDialer(t *testing.T) {
}, },
} }
URL := &url.URL{} URL := &url.URL{}
dialer := NewMaybeProxyDialer(child, URL) dialer := MaybeWrapWithProxyDialer(child, URL)
dialer.CloseIdleConnections() dialer.CloseIdleConnections()
if !called { if !called {
t.Fatal("not called") t.Fatal("not called")