refactor: interfaces and data types into the model package (#642)

## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/1885
- [x] related ooni/spec pull request: N/A

Location of the issue tracker: https://github.com/ooni/probe

## Description

This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package.

The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity.

An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other.

This is what I want to move in `internal/model`:

- [x] most important interfaces from `internal/netxlite`
- [x] everything that was previously part of `internal/engine/model`
- [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
This commit is contained in:
Simone Basso 2022-01-03 13:53:23 +01:00 committed by GitHub
commit 273b70bacc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
275 changed files with 1281 additions and 1375 deletions

View file

@ -9,56 +9,25 @@ import (
"sync"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/netxlite/quicx"
"github.com/ooni/probe-cli/v3/internal/model"
)
// UDPLikeConn is the kind of UDP socket used by QUIC.
type UDPLikeConn = quicx.UDPLikeConn
// QUICListener listens for QUIC connections.
type QUICListener interface {
// Listen creates a new listening UDPLikeConn.
Listen(addr *net.UDPAddr) (UDPLikeConn, error)
}
// NewQUICListener creates a new QUICListener using the standard
// library to create listening UDP sockets.
func NewQUICListener() QUICListener {
func NewQUICListener() model.QUICListener {
return &quicListenerErrWrapper{&quicListenerStdlib{}}
}
// quicListenerStdlib is a QUICListener using the standard library.
type quicListenerStdlib struct{}
var _ QUICListener = &quicListenerStdlib{}
var _ model.QUICListener = &quicListenerStdlib{}
// Listen implements QUICListener.Listen.
func (qls *quicListenerStdlib) Listen(addr *net.UDPAddr) (UDPLikeConn, error) {
func (qls *quicListenerStdlib) Listen(addr *net.UDPAddr) (model.UDPLikeConn, error) {
return TProxy.ListenUDP("udp", addr)
}
// QUICDialer dials QUIC sessions.
type QUICDialer interface {
// DialContext establishes a new QUIC session using the given
// network and address. The tlsConfig and the quicConfig arguments
// MUST NOT be nil. Returns either the session or an error.
//
// Recommended tlsConfig setup:
//
// - set ServerName to be the SNI;
//
// - set RootCAs to NewDefaultCertPool();
//
// - set NextProtos to []string{"h3"}.
//
// Typically, you want to pass `&quic.Config{}` as quicConfig.
DialContext(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error)
// CloseIdleConnections closes idle connections, if any.
CloseIdleConnections()
}
// NewQUICDialerWithResolver returns a QUICDialer using the given
// QUICListener to create listening connections and the given Resolver
// to resolve domain names (if needed).
@ -80,8 +49,8 @@ type QUICDialer interface {
// 6. if a dialer wraps a resolver, the dialer will forward
// the CloseIdleConnection call to its resolver (which is
// instrumental to manage a DoH resolver connections properly).
func NewQUICDialerWithResolver(listener QUICListener,
logger Logger, resolver Resolver) QUICDialer {
func NewQUICDialerWithResolver(listener model.QUICListener,
logger model.DebugLogger, resolver model.Resolver) model.QUICDialer {
return &quicDialerLogger{
Dialer: &quicDialerResolver{
Dialer: &quicDialerLogger{
@ -102,14 +71,14 @@ func NewQUICDialerWithResolver(listener QUICListener,
// except that there is no configured resolver. So, if you pass in
// an address containing a domain name, the dial will fail with
// the ErrNoResolver failure.
func NewQUICDialerWithoutResolver(listener QUICListener, logger Logger) QUICDialer {
func NewQUICDialerWithoutResolver(listener model.QUICListener, logger model.DebugLogger) model.QUICDialer {
return NewQUICDialerWithResolver(listener, logger, &nullResolver{})
}
// quicDialerQUICGo dials using the lucas-clemente/quic-go library.
type quicDialerQUICGo struct {
// QUICListener is the underlying QUICListener to use.
QUICListener QUICListener
QUICListener model.QUICListener
// mockDialEarlyContext allows to mock quic.DialEarlyContext.
mockDialEarlyContext func(ctx context.Context, pconn net.PacketConn,
@ -117,7 +86,7 @@ type quicDialerQUICGo struct {
quicConfig *quic.Config) (quic.EarlySession, error)
}
var _ QUICDialer = &quicDialerQUICGo{}
var _ model.QUICDialer = &quicDialerQUICGo{}
// errInvalidIP indicates that a string is not a valid IP.
var errInvalidIP = errors.New("netxlite: invalid IP")
@ -201,7 +170,7 @@ type quicSessionOwnsConn struct {
quic.EarlySession
// conn is the connection we own
conn UDPLikeConn
conn model.UDPLikeConn
}
// CloseWithError implements quic.EarlySession.CloseWithError.
@ -216,13 +185,13 @@ func (sess *quicSessionOwnsConn) CloseWithError(
// to resolve a domain name to IP addrs.
type quicDialerResolver struct {
// Dialer is the underlying QUICDialer.
Dialer QUICDialer
Dialer model.QUICDialer
// Resolver is the underlying Resolver.
Resolver Resolver
Resolver model.Resolver
}
var _ QUICDialer = &quicDialerResolver{}
var _ model.QUICDialer = &quicDialerResolver{}
// DialContext implements QUICDialer.DialContext. This function
// will apply the following TLS defaults:
@ -284,10 +253,10 @@ func (d *quicDialerResolver) CloseIdleConnections() {
// quicDialerLogger is a dialer with logging.
type quicDialerLogger struct {
// Dialer is the underlying QUIC dialer.
Dialer QUICDialer
Dialer model.QUICDialer
// Logger is the underlying logger.
Logger Logger
Logger model.DebugLogger
// operationSuffix is appended to the operation name.
//
@ -298,7 +267,7 @@ type quicDialerLogger struct {
operationSuffix string
}
var _ QUICDialer = &quicDialerLogger{}
var _ model.QUICDialer = &quicDialerLogger{}
// DialContext implements QUICContextDialer.DialContext.
func (d *quicDialerLogger) DialContext(
@ -321,7 +290,7 @@ func (d *quicDialerLogger) CloseIdleConnections() {
}
// NewSingleUseQUICDialer is like NewSingleUseDialer but for QUIC.
func NewSingleUseQUICDialer(sess quic.EarlySession) QUICDialer {
func NewSingleUseQUICDialer(sess quic.EarlySession) model.QUICDialer {
return &quicDialerSingleUse{sess: sess}
}
@ -331,7 +300,7 @@ type quicDialerSingleUse struct {
sess quic.EarlySession
}
var _ QUICDialer = &quicDialerSingleUse{}
var _ model.QUICDialer = &quicDialerSingleUse{}
// DialContext implements QUICDialer.DialContext.
func (s *quicDialerSingleUse) DialContext(
@ -355,13 +324,13 @@ func (s *quicDialerSingleUse) CloseIdleConnections() {
// quicListenerErrWrapper is a QUICListener that wraps errors.
type quicListenerErrWrapper struct {
// QUICListener is the underlying listener.
QUICListener
model.QUICListener
}
var _ QUICListener = &quicListenerErrWrapper{}
var _ model.QUICListener = &quicListenerErrWrapper{}
// Listen implements QUICListener.Listen.
func (qls *quicListenerErrWrapper) Listen(addr *net.UDPAddr) (UDPLikeConn, error) {
func (qls *quicListenerErrWrapper) Listen(addr *net.UDPAddr) (model.UDPLikeConn, error) {
pconn, err := qls.QUICListener.Listen(addr)
if err != nil {
return nil, NewErrWrapper(ClassifyGenericError, QUICListenOperation, err)
@ -372,10 +341,10 @@ func (qls *quicListenerErrWrapper) Listen(addr *net.UDPAddr) (UDPLikeConn, error
// quicErrWrapperUDPLikeConn is a UDPLikeConn that wraps errors.
type quicErrWrapperUDPLikeConn struct {
// UDPLikeConn is the underlying conn.
UDPLikeConn
model.UDPLikeConn
}
var _ UDPLikeConn = &quicErrWrapperUDPLikeConn{}
var _ model.UDPLikeConn = &quicErrWrapperUDPLikeConn{}
// WriteTo implements UDPLikeConn.WriteTo.
func (c *quicErrWrapperUDPLikeConn) WriteTo(p []byte, addr net.Addr) (int, error) {
@ -406,7 +375,7 @@ func (c *quicErrWrapperUDPLikeConn) Close() error {
// quicDialerErrWrapper is a dialer that performs quic err wrapping
type quicDialerErrWrapper struct {
QUICDialer
model.QUICDialer
}
// DialContext implements ContextDialer.DialContext