refactor(netx): dialer does not use legacy/netx anymore (#370)
Part of https://github.com/ooni/probe-engine/issues/897
This commit is contained in:
parent
3672e14d3e
commit
ee35b10a98
|
@ -3,27 +3,9 @@ package dialer
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/connid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Dialer is the interface we expect from a dialer
|
// Dialer is the interface we expect from a dialer
|
||||||
type Dialer interface {
|
type Dialer interface {
|
||||||
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolver is the interface we expect from a resolver
|
|
||||||
type Resolver interface {
|
|
||||||
LookupHost(ctx context.Context, hostname string) (addrs []string, err error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func safeLocalAddress(conn net.Conn) (s string) {
|
|
||||||
if conn != nil && conn.LocalAddr() != nil {
|
|
||||||
s = conn.LocalAddr().String()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func safeConnID(network string, conn net.Conn) int64 {
|
|
||||||
return connid.Compute(network, safeLocalAddress(conn))
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,10 +6,14 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/dialid"
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Resolver is the interface we expect from a resolver
|
||||||
|
type Resolver interface {
|
||||||
|
LookupHost(ctx context.Context, hostname string) (addrs []string, err error)
|
||||||
|
}
|
||||||
|
|
||||||
// DNSDialer is a dialer that uses the configured Resolver to resolver a
|
// DNSDialer is a dialer that uses the configured Resolver to resolver a
|
||||||
// domain name to IP addresses, and the configured Dialer to connect.
|
// domain name to IP addresses, and the configured Dialer to connect.
|
||||||
type DNSDialer struct {
|
type DNSDialer struct {
|
||||||
|
@ -23,7 +27,6 @@ func (d DNSDialer) DialContext(ctx context.Context, network, address string) (ne
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ctx = dialid.WithDialID(ctx) // important to create before lookupHost
|
|
||||||
var addrs []string
|
var addrs []string
|
||||||
addrs, err = d.LookupHost(ctx, onlyhost)
|
addrs, err = d.LookupHost(ctx, onlyhost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/dialid"
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,35 +14,26 @@ type ErrorWrapperDialer struct {
|
||||||
|
|
||||||
// DialContext implements Dialer.DialContext
|
// DialContext implements Dialer.DialContext
|
||||||
func (d ErrorWrapperDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
func (d ErrorWrapperDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
dialID := dialid.ContextDialID(ctx)
|
|
||||||
conn, err := d.Dialer.DialContext(ctx, network, address)
|
conn, err := d.Dialer.DialContext(ctx, network, address)
|
||||||
err = errorx.SafeErrWrapperBuilder{
|
err = errorx.SafeErrWrapperBuilder{
|
||||||
// ConnID does not make any sense if we've failed and the error
|
|
||||||
// does not make any sense (and is nil) if we succeeded.
|
|
||||||
DialID: dialID,
|
|
||||||
Error: err,
|
Error: err,
|
||||||
Operation: errorx.ConnectOperation,
|
Operation: errorx.ConnectOperation,
|
||||||
}.MaybeBuild()
|
}.MaybeBuild()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &ErrorWrapperConn{
|
return &ErrorWrapperConn{Conn: conn}, nil
|
||||||
Conn: conn, ConnID: safeConnID(network, conn), DialID: dialID}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrorWrapperConn is a net.Conn that performs error wrapping.
|
// ErrorWrapperConn is a net.Conn that performs error wrapping.
|
||||||
type ErrorWrapperConn struct {
|
type ErrorWrapperConn struct {
|
||||||
net.Conn
|
net.Conn
|
||||||
ConnID int64
|
|
||||||
DialID int64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read implements net.Conn.Read
|
// Read implements net.Conn.Read
|
||||||
func (c ErrorWrapperConn) Read(b []byte) (n int, err error) {
|
func (c ErrorWrapperConn) Read(b []byte) (n int, err error) {
|
||||||
n, err = c.Conn.Read(b)
|
n, err = c.Conn.Read(b)
|
||||||
err = errorx.SafeErrWrapperBuilder{
|
err = errorx.SafeErrWrapperBuilder{
|
||||||
ConnID: c.ConnID,
|
|
||||||
DialID: c.DialID,
|
|
||||||
Error: err,
|
Error: err,
|
||||||
Operation: errorx.ReadOperation,
|
Operation: errorx.ReadOperation,
|
||||||
}.MaybeBuild()
|
}.MaybeBuild()
|
||||||
|
@ -54,8 +44,6 @@ func (c ErrorWrapperConn) Read(b []byte) (n int, err error) {
|
||||||
func (c ErrorWrapperConn) Write(b []byte) (n int, err error) {
|
func (c ErrorWrapperConn) Write(b []byte) (n int, err error) {
|
||||||
n, err = c.Conn.Write(b)
|
n, err = c.Conn.Write(b)
|
||||||
err = errorx.SafeErrWrapperBuilder{
|
err = errorx.SafeErrWrapperBuilder{
|
||||||
ConnID: c.ConnID,
|
|
||||||
DialID: c.DialID,
|
|
||||||
Error: err,
|
Error: err,
|
||||||
Operation: errorx.WriteOperation,
|
Operation: errorx.WriteOperation,
|
||||||
}.MaybeBuild()
|
}.MaybeBuild()
|
||||||
|
@ -66,8 +54,6 @@ func (c ErrorWrapperConn) Write(b []byte) (n int, err error) {
|
||||||
func (c ErrorWrapperConn) Close() (err error) {
|
func (c ErrorWrapperConn) Close() (err error) {
|
||||||
err = c.Conn.Close()
|
err = c.Conn.Close()
|
||||||
err = errorx.SafeErrWrapperBuilder{
|
err = errorx.SafeErrWrapperBuilder{
|
||||||
ConnID: c.ConnID,
|
|
||||||
DialID: c.DialID,
|
|
||||||
Error: err,
|
Error: err,
|
||||||
Operation: errorx.CloseOperation,
|
Operation: errorx.CloseOperation,
|
||||||
}.MaybeBuild()
|
}.MaybeBuild()
|
||||||
|
|
|
@ -7,14 +7,13 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/dialid"
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/dialer"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/dialer"
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/mockablex"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/mockablex"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestErrorWrapperFailure(t *testing.T) {
|
func TestErrorWrapperFailure(t *testing.T) {
|
||||||
ctx := dialid.WithDialID(context.Background())
|
ctx := context.Background()
|
||||||
d := dialer.ErrorWrapperDialer{Dialer: mockablex.Dialer{
|
d := dialer.ErrorWrapperDialer{Dialer: mockablex.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) {
|
||||||
return nil, io.EOF
|
return nil, io.EOF
|
||||||
|
@ -35,9 +34,6 @@ func errorWrapperCheckErr(t *testing.T, err error, op string) {
|
||||||
if !errors.As(err, &errWrapper) {
|
if !errors.As(err, &errWrapper) {
|
||||||
t.Fatal("cannot cast to ErrWrapper")
|
t.Fatal("cannot cast to ErrWrapper")
|
||||||
}
|
}
|
||||||
if errWrapper.DialID == 0 {
|
|
||||||
t.Fatal("unexpected DialID")
|
|
||||||
}
|
|
||||||
if errWrapper.Operation != op {
|
if errWrapper.Operation != op {
|
||||||
t.Fatal("unexpected Operation")
|
t.Fatal("unexpected Operation")
|
||||||
}
|
}
|
||||||
|
@ -47,7 +43,7 @@ func errorWrapperCheckErr(t *testing.T, err error, op string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrorWrapperSuccess(t *testing.T) {
|
func TestErrorWrapperSuccess(t *testing.T) {
|
||||||
ctx := dialid.WithDialID(context.Background())
|
ctx := context.Background()
|
||||||
d := dialer.ErrorWrapperDialer{Dialer: mockablex.Dialer{
|
d := dialer.ErrorWrapperDialer{Dialer: mockablex.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) {
|
||||||
return &mockablex.Conn{
|
return &mockablex.Conn{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user