2021-02-02 12:05:47 +01:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
|
|
|
|
)
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
// byteCounterDialer is a byte-counting-aware dialer. To perform byte counting, you
|
2021-02-02 12:05:47 +01:00
|
|
|
// should make sure that you insert this dialer in the dialing chain.
|
2021-06-09 09:42:31 +02:00
|
|
|
type byteCounterDialer struct {
|
2021-02-02 12:05:47 +01:00
|
|
|
Dialer
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext implements Dialer.DialContext
|
2021-06-09 09:42:31 +02:00
|
|
|
func (d *byteCounterDialer) DialContext(
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
conn, err := d.Dialer.DialContext(ctx, network, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-09 09:42:31 +02:00
|
|
|
exp := contextExperimentByteCounter(ctx)
|
|
|
|
sess := contextSessionByteCounter(ctx)
|
2021-02-02 12:05:47 +01:00
|
|
|
if exp == nil && sess == nil {
|
|
|
|
return conn, nil // no point in wrapping
|
|
|
|
}
|
2021-06-09 09:42:31 +02:00
|
|
|
return &byteCounterConnWrapper{Conn: conn, exp: exp, sess: sess}, nil
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type byteCounterSessionKey struct{}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
// contextSessionByteCounter retrieves the session byte counter from the context
|
|
|
|
func contextSessionByteCounter(ctx context.Context) *bytecounter.Counter {
|
2021-02-02 12:05:47 +01:00
|
|
|
counter, _ := ctx.Value(byteCounterSessionKey{}).(*bytecounter.Counter)
|
|
|
|
return counter
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
// WithSessionByteCounter assigns the session byte counter to the context.
|
2021-02-02 12:05:47 +01:00
|
|
|
func WithSessionByteCounter(ctx context.Context, counter *bytecounter.Counter) context.Context {
|
|
|
|
return context.WithValue(ctx, byteCounterSessionKey{}, counter)
|
|
|
|
}
|
|
|
|
|
|
|
|
type byteCounterExperimentKey struct{}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
// contextExperimentByteCounter retrieves the experiment byte counter from the context
|
|
|
|
func contextExperimentByteCounter(ctx context.Context) *bytecounter.Counter {
|
2021-02-02 12:05:47 +01:00
|
|
|
counter, _ := ctx.Value(byteCounterExperimentKey{}).(*bytecounter.Counter)
|
|
|
|
return counter
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
// WithExperimentByteCounter assigns the experiment byte counter to the context.
|
2021-02-02 12:05:47 +01:00
|
|
|
func WithExperimentByteCounter(ctx context.Context, counter *bytecounter.Counter) context.Context {
|
|
|
|
return context.WithValue(ctx, byteCounterExperimentKey{}, counter)
|
|
|
|
}
|
|
|
|
|
|
|
|
type byteCounterConnWrapper struct {
|
|
|
|
net.Conn
|
|
|
|
exp *bytecounter.Counter
|
|
|
|
sess *bytecounter.Counter
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
func (c *byteCounterConnWrapper) Read(p []byte) (int, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
count, err := c.Conn.Read(p)
|
|
|
|
if c.exp != nil {
|
|
|
|
c.exp.CountBytesReceived(count)
|
|
|
|
}
|
|
|
|
if c.sess != nil {
|
|
|
|
c.sess.CountBytesReceived(count)
|
|
|
|
}
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
func (c *byteCounterConnWrapper) Write(p []byte) (int, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
count, err := c.Conn.Write(p)
|
|
|
|
if c.exp != nil {
|
|
|
|
c.exp.CountBytesSent(count)
|
|
|
|
}
|
|
|
|
if c.sess != nil {
|
|
|
|
c.sess.CountBytesSent(count)
|
|
|
|
}
|
|
|
|
return count, err
|
|
|
|
}
|