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 (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/connid"
|
||||
)
|
||||
|
||||
// Dialer is the interface we expect from a dialer
|
||||
type Dialer interface {
|
||||
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"
|
||||
"strings"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/dialid"
|
||||
"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
|
||||
// domain name to IP addresses, and the configured Dialer to connect.
|
||||
type DNSDialer struct {
|
||||
|
@ -23,7 +27,6 @@ func (d DNSDialer) DialContext(ctx context.Context, network, address string) (ne
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx = dialid.WithDialID(ctx) // important to create before lookupHost
|
||||
var addrs []string
|
||||
addrs, err = d.LookupHost(ctx, onlyhost)
|
||||
if err != nil {
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/dialid"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
)
|
||||
|
||||
|
@ -15,35 +14,26 @@ type ErrorWrapperDialer struct {
|
|||
|
||||
// DialContext implements Dialer.DialContext
|
||||
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)
|
||||
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,
|
||||
Operation: errorx.ConnectOperation,
|
||||
}.MaybeBuild()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ErrorWrapperConn{
|
||||
Conn: conn, ConnID: safeConnID(network, conn), DialID: dialID}, nil
|
||||
return &ErrorWrapperConn{Conn: conn}, nil
|
||||
}
|
||||
|
||||
// ErrorWrapperConn is a net.Conn that performs error wrapping.
|
||||
type ErrorWrapperConn struct {
|
||||
net.Conn
|
||||
ConnID int64
|
||||
DialID int64
|
||||
}
|
||||
|
||||
// Read implements net.Conn.Read
|
||||
func (c ErrorWrapperConn) Read(b []byte) (n int, err error) {
|
||||
n, err = c.Conn.Read(b)
|
||||
err = errorx.SafeErrWrapperBuilder{
|
||||
ConnID: c.ConnID,
|
||||
DialID: c.DialID,
|
||||
Error: err,
|
||||
Operation: errorx.ReadOperation,
|
||||
}.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) {
|
||||
n, err = c.Conn.Write(b)
|
||||
err = errorx.SafeErrWrapperBuilder{
|
||||
ConnID: c.ConnID,
|
||||
DialID: c.DialID,
|
||||
Error: err,
|
||||
Operation: errorx.WriteOperation,
|
||||
}.MaybeBuild()
|
||||
|
@ -66,8 +54,6 @@ func (c ErrorWrapperConn) Write(b []byte) (n int, err error) {
|
|||
func (c ErrorWrapperConn) Close() (err error) {
|
||||
err = c.Conn.Close()
|
||||
err = errorx.SafeErrWrapperBuilder{
|
||||
ConnID: c.ConnID,
|
||||
DialID: c.DialID,
|
||||
Error: err,
|
||||
Operation: errorx.CloseOperation,
|
||||
}.MaybeBuild()
|
||||
|
|
|
@ -7,14 +7,13 @@ import (
|
|||
"net"
|
||||
"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/errorx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/mockablex"
|
||||
)
|
||||
|
||||
func TestErrorWrapperFailure(t *testing.T) {
|
||||
ctx := dialid.WithDialID(context.Background())
|
||||
ctx := context.Background()
|
||||
d := dialer.ErrorWrapperDialer{Dialer: mockablex.Dialer{
|
||||
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
||||
return nil, io.EOF
|
||||
|
@ -35,9 +34,6 @@ func errorWrapperCheckErr(t *testing.T, err error, op string) {
|
|||
if !errors.As(err, &errWrapper) {
|
||||
t.Fatal("cannot cast to ErrWrapper")
|
||||
}
|
||||
if errWrapper.DialID == 0 {
|
||||
t.Fatal("unexpected DialID")
|
||||
}
|
||||
if errWrapper.Operation != op {
|
||||
t.Fatal("unexpected Operation")
|
||||
}
|
||||
|
@ -47,7 +43,7 @@ func errorWrapperCheckErr(t *testing.T, err error, op string) {
|
|||
}
|
||||
|
||||
func TestErrorWrapperSuccess(t *testing.T) {
|
||||
ctx := dialid.WithDialID(context.Background())
|
||||
ctx := context.Background()
|
||||
d := dialer.ErrorWrapperDialer{Dialer: mockablex.Dialer{
|
||||
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
||||
return &mockablex.Conn{
|
||||
|
|
Loading…
Reference in New Issue
Block a user