566c6b246a
This diff addresses another point of https://github.com/ooni/probe/issues/1956: > - [ ] observe that we're still using a bunch of private interfaces for common interfaces such as the `Dialer`, so we can get rid of these private interfaces and always use the ones in `model`, which allows us to remove a bunch of legacy wrappers Additional cleanups may still be possible. The more I cleanup, the more I see there's extra legacy code we can dispose of (which seems good?).
58 lines
1.9 KiB
Go
58 lines
1.9 KiB
Go
package dialer
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/bytecounter"
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
)
|
|
|
|
// byteCounterDialer is a byte-counting-aware dialer. To perform byte counting, you
|
|
// should make sure that you insert this dialer in the dialing chain.
|
|
type byteCounterDialer struct {
|
|
model.Dialer
|
|
}
|
|
|
|
// DialContext implements Dialer.DialContext
|
|
func (d *byteCounterDialer) DialContext(
|
|
ctx context.Context, network, address string) (net.Conn, error) {
|
|
conn, err := d.Dialer.DialContext(ctx, network, address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if exp := contextExperimentByteCounter(ctx); exp != nil {
|
|
conn = &bytecounter.Conn{Conn: conn, Counter: exp}
|
|
}
|
|
if sess := contextSessionByteCounter(ctx); sess != nil {
|
|
conn = &bytecounter.Conn{Conn: conn, Counter: sess}
|
|
}
|
|
return conn, nil
|
|
}
|
|
|
|
type byteCounterSessionKey struct{}
|
|
|
|
// contextSessionByteCounter retrieves the session byte counter from the context
|
|
func contextSessionByteCounter(ctx context.Context) *bytecounter.Counter {
|
|
counter, _ := ctx.Value(byteCounterSessionKey{}).(*bytecounter.Counter)
|
|
return counter
|
|
}
|
|
|
|
// WithSessionByteCounter assigns the session byte counter to the context.
|
|
func WithSessionByteCounter(ctx context.Context, counter *bytecounter.Counter) context.Context {
|
|
return context.WithValue(ctx, byteCounterSessionKey{}, counter)
|
|
}
|
|
|
|
type byteCounterExperimentKey struct{}
|
|
|
|
// contextExperimentByteCounter retrieves the experiment byte counter from the context
|
|
func contextExperimentByteCounter(ctx context.Context) *bytecounter.Counter {
|
|
counter, _ := ctx.Value(byteCounterExperimentKey{}).(*bytecounter.Counter)
|
|
return counter
|
|
}
|
|
|
|
// WithExperimentByteCounter assigns the experiment byte counter to the context.
|
|
func WithExperimentByteCounter(ctx context.Context, counter *bytecounter.Counter) context.Context {
|
|
return context.WithValue(ctx, byteCounterExperimentKey{}, counter)
|
|
}
|