2021-02-02 12:05:47 +01:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
2021-06-09 09:42:31 +02:00
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
refactor: start pivoting netx (#396)
What do I mean by pivoting? Netx is currently organized by row:
```
| dialer | quicdialer | resolver | ...
saving | | | | ...
errorwrapping | | | | ...
logging | | | | ...
mocking/sys | | | | ...
```
Every row needs to implement saving, errorwrapping, logging, mocking (or
adapting to the system or to some underlying library).
This causes cross package dependencies and, in turn, complexity. For
example, we need the `trace` package for supporting saving.
And `dialer`, `quickdialer`, et al. need to depend on such a package.
The same goes for errorwrapping.
This arrangement further complicates testing. For example, I am
currently working on https://github.com/ooni/probe/issues/1505 and
I realize it need to repeat integration tests in multiple places.
Let's say instead we pivot the above matrix as follows:
```
| saving | errorwrapping | logging | ...
dialer | | | | ...
quicdialer | | | | ...
logging | | | | ...
mocking/sys | | | | ...
...
```
In this way, now every row contains everything related to a specific
action to perform. We can now share code without relying on extra
support packages. What's more, we can write tests and, judding from
the way in which things are made, it seems we only need integration
testing in `errorwrapping` because it's where data quality matters
whereas, in all other cases, unit testing is fine.
I am going, therefore, to proceed with these changes and "pivot"
`netx`. Hopefully, it won't be too painful.
2021-06-23 15:53:12 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
// Config contains the settings for New.
|
|
|
|
type Config struct {
|
|
|
|
// ContextByteCounting optionally configures context-based
|
|
|
|
// byte counting. By default we don't do that.
|
|
|
|
//
|
|
|
|
// Use WithExperimentByteCounter and WithSessionByteCounter
|
|
|
|
// to assign byte counters to a context. The code will use
|
|
|
|
// corresponding, private functions to access the configured
|
|
|
|
// byte counters and will notify them about I/O events.
|
|
|
|
//
|
|
|
|
// Bug
|
|
|
|
//
|
|
|
|
// This implementation cannot properly account for the bytes that are sent by
|
2021-09-09 01:19:17 +02:00
|
|
|
// persistent connections, because they stick to the counters set when the
|
2021-06-09 09:42:31 +02:00
|
|
|
// connection was established. This typically means we miss the bytes sent and
|
|
|
|
// received when submitting a measurement. Such bytes are specifically not
|
|
|
|
// seen by the experiment specific byte counter.
|
|
|
|
//
|
|
|
|
// For this reason, this implementation may be heavily changed/removed.
|
|
|
|
ContextByteCounting bool
|
|
|
|
|
|
|
|
// DialSaver is the optional saver for dialing events. If not
|
|
|
|
// set, we will not save any dialing event.
|
|
|
|
DialSaver *trace.Saver
|
|
|
|
|
|
|
|
// Logger is the optional logger. If not set, there
|
|
|
|
// will be no logging from the new dialer.
|
2022-01-03 13:53:23 +01:00
|
|
|
Logger model.DebugLogger
|
2021-06-09 09:42:31 +02:00
|
|
|
|
|
|
|
// ProxyURL is the optional proxy URL.
|
|
|
|
ProxyURL *url.URL
|
|
|
|
|
|
|
|
// ReadWriteSaver is like DialSaver but for I/O events.
|
|
|
|
ReadWriteSaver *trace.Saver
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Dialer from the specified config and resolver.
|
2022-01-07 18:33:37 +01:00
|
|
|
func New(config *Config, resolver model.Resolver) model.Dialer {
|
|
|
|
var d model.Dialer = &netxlite.ErrorWrapperDialer{Dialer: netxlite.DefaultDialer}
|
2021-06-09 09:42:31 +02:00
|
|
|
if config.Logger != nil {
|
2021-09-05 19:55:28 +02:00
|
|
|
d = &netxlite.DialerLogger{
|
2022-01-07 18:33:37 +01:00
|
|
|
Dialer: d,
|
2022-01-03 13:53:23 +01:00
|
|
|
DebugLogger: config.Logger,
|
2021-09-05 19:55:28 +02:00
|
|
|
}
|
2021-06-09 09:42:31 +02:00
|
|
|
}
|
|
|
|
if config.DialSaver != nil {
|
|
|
|
d = &saverDialer{Dialer: d, Saver: config.DialSaver}
|
|
|
|
}
|
|
|
|
if config.ReadWriteSaver != nil {
|
|
|
|
d = &saverConnDialer{Dialer: d, Saver: config.ReadWriteSaver}
|
|
|
|
}
|
2021-09-05 18:03:50 +02:00
|
|
|
d = &netxlite.DialerResolver{
|
2022-01-07 18:33:37 +01:00
|
|
|
Resolver: resolver,
|
|
|
|
Dialer: d,
|
2021-09-05 18:03:50 +02:00
|
|
|
}
|
2021-06-09 09:42:31 +02:00
|
|
|
d = &proxyDialer{ProxyURL: config.ProxyURL, Dialer: d}
|
|
|
|
if config.ContextByteCounting {
|
|
|
|
d = &byteCounterDialer{Dialer: d}
|
|
|
|
}
|
|
|
|
d = &shapingDialer{Dialer: d}
|
|
|
|
return d
|
|
|
|
}
|