feat: tlsping and tcpping using step-by-step (#815)

## 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/2158
- [x] if you changed anything related how experiments work and you need to reflect these changes in the ooni/spec repository, please link to the related ooni/spec pull request: https://github.com/ooni/spec/pull/250

## Description

This diff refactors the codebase to reimplement tlsping and tcpping
to use the step-by-step measurements style.

See docs/design/dd-003-step-by-step.md for more information on the
step-by-step measurement style.
This commit is contained in:
Simone Basso
2022-07-01 12:22:22 +02:00
committed by GitHub
parent 5371c7f486
commit 5ebdeb56ca
48 changed files with 2825 additions and 299 deletions
+24 -19
View File
@@ -10,13 +10,13 @@ import (
"net/url"
"time"
"github.com/ooni/probe-cli/v3/internal/measurex"
"github.com/ooni/probe-cli/v3/internal/measurexlite"
"github.com/ooni/probe-cli/v3/internal/model"
)
const (
testName = "tcpping"
testVersion = "0.1.0"
testVersion = "0.2.0"
)
// Config contains the experiment configuration.
@@ -49,7 +49,7 @@ type TestKeys struct {
// SinglePing contains the results of a single ping.
type SinglePing struct {
TCPConnect []*measurex.ArchivalTCPConnect `json:"tcp_connect"`
TCPConnect *model.ArchivalTCPConnectResult `json:"tcp_connect"`
}
// Measurer performs the measurement.
@@ -103,42 +103,47 @@ func (m *Measurer) Run(
}
tk := new(TestKeys)
measurement.TestKeys = tk
out := make(chan *measurex.EndpointMeasurement)
mxmx := measurex.NewMeasurerWithDefaultSettings()
go m.tcpPingLoop(ctx, mxmx, parsed.Host, out)
out := make(chan *SinglePing)
go m.tcpPingLoop(ctx, measurement.MeasurementStartTimeSaved, sess.Logger(), parsed.Host, out)
for len(tk.Pings) < int(m.config.repetitions()) {
meas := <-out
tk.Pings = append(tk.Pings, &SinglePing{
TCPConnect: measurex.NewArchivalTCPConnectList(meas.Connect),
})
tk.Pings = append(tk.Pings, <-out)
}
return nil // return nil so we always submit the measurement
}
// tcpPingLoop sends all the ping requests and emits the results onto the out channel.
func (m *Measurer) tcpPingLoop(ctx context.Context, mxmx *measurex.Measurer,
address string, out chan<- *measurex.EndpointMeasurement) {
func (m *Measurer) tcpPingLoop(ctx context.Context, zeroTime time.Time,
logger model.Logger, address string, out chan<- *SinglePing) {
ticker := time.NewTicker(m.config.delay())
defer ticker.Stop()
for i := int64(0); i < m.config.repetitions(); i++ {
go m.tcpPingAsync(ctx, mxmx, address, out)
go m.tcpPingAsync(ctx, i, zeroTime, logger, address, out)
<-ticker.C
}
}
// tcpPingAsync performs a TCP ping and emits the result onto the out channel.
func (m *Measurer) tcpPingAsync(ctx context.Context, mxmx *measurex.Measurer,
address string, out chan<- *measurex.EndpointMeasurement) {
out <- m.tcpConnect(ctx, mxmx, address)
func (m *Measurer) tcpPingAsync(ctx context.Context, index int64,
zeroTime time.Time, logger model.Logger, address string, out chan<- *SinglePing) {
out <- m.tcpConnect(ctx, index, zeroTime, logger, address)
}
// tcpConnect performs a TCP connect and returns the result to the caller.
func (m *Measurer) tcpConnect(ctx context.Context, mxmx *measurex.Measurer,
address string) *measurex.EndpointMeasurement {
func (m *Measurer) tcpConnect(ctx context.Context, index int64,
zeroTime time.Time, logger model.Logger, address string) *SinglePing {
// TODO(bassosimone): make the timeout user-configurable
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
return mxmx.TCPConnect(ctx, address)
trace := measurexlite.NewTrace(index, zeroTime)
dialer := trace.NewDialerWithoutResolver(logger)
ol := measurexlite.NewOperationLogger(logger, "TCPPing #%d %s", index, address)
conn, err := dialer.DialContext(ctx, "tcp", address)
ol.Stop(err)
measurexlite.MaybeClose(conn)
sp := &SinglePing{
TCPConnect: <-trace.TCPConnect,
}
return sp
}
// NewExperimentMeasurer creates a new ExperimentMeasurer.
@@ -40,14 +40,16 @@ func TestMeasurer_run(t *testing.T) {
if m.ExperimentName() != "tcpping" {
t.Fatal("invalid experiment name")
}
if m.ExperimentVersion() != "0.1.0" {
if m.ExperimentVersion() != "0.2.0" {
t.Fatal("invalid experiment version")
}
ctx := context.Background()
meas := &model.Measurement{
Input: model.MeasurementTarget(input),
}
sess := &mockable.Session{}
sess := &mockable.Session{
MockableLogger: model.DiscardLogger,
}
callbacks := model.NewPrinterCallbacks(model.DiscardLogger)
err := m.Run(ctx, sess, meas, callbacks)
return meas, m, err
+44 -26
View File
@@ -13,14 +13,14 @@ import (
"strings"
"time"
"github.com/ooni/probe-cli/v3/internal/measurex"
"github.com/ooni/probe-cli/v3/internal/measurexlite"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
const (
testName = "tlsping"
testVersion = "0.1.0"
testVersion = "0.2.0"
)
// Config contains the experiment configuration.
@@ -77,9 +77,9 @@ type TestKeys struct {
// SinglePing contains the results of a single ping.
type SinglePing struct {
NetworkEvents []*measurex.ArchivalNetworkEvent `json:"network_events"`
TCPConnect []*measurex.ArchivalTCPConnect `json:"tcp_connect"`
TLSHandshakes []*measurex.ArchivalQUICTLSHandshakeEvent `json:"tls_handshakes"`
NetworkEvents []*model.ArchivalNetworkEvent `json:"network_events"`
TCPConnect *model.ArchivalTCPConnectResult `json:"tcp_connect"`
TLSHandshake *model.ArchivalTLSOrQUICHandshakeResult `json:"tls_handshake"`
}
// Measurer performs the measurement.
@@ -133,49 +133,67 @@ func (m *Measurer) Run(
}
tk := new(TestKeys)
measurement.TestKeys = tk
out := make(chan *measurex.EndpointMeasurement)
mxmx := measurex.NewMeasurerWithDefaultSettings()
go m.tlsPingLoop(ctx, mxmx, parsed.Host, out)
out := make(chan *SinglePing)
go m.tlsPingLoop(ctx, measurement.MeasurementStartTimeSaved, sess.Logger(), parsed.Host, out)
for len(tk.Pings) < int(m.config.repetitions()) {
meas := <-out
tk.Pings = append(tk.Pings, &SinglePing{
NetworkEvents: measurex.NewArchivalNetworkEventList(meas.ReadWrite),
TCPConnect: measurex.NewArchivalTCPConnectList(meas.Connect),
TLSHandshakes: measurex.NewArchivalQUICTLSHandshakeEventList(meas.TLSHandshake),
})
tk.Pings = append(tk.Pings, <-out)
}
return nil // return nil so we always submit the measurement
}
// tlsPingLoop sends all the ping requests and emits the results onto the out channel.
func (m *Measurer) tlsPingLoop(ctx context.Context, mxmx *measurex.Measurer,
address string, out chan<- *measurex.EndpointMeasurement) {
func (m *Measurer) tlsPingLoop(ctx context.Context, zeroTime time.Time,
logger model.Logger, address string, out chan<- *SinglePing) {
ticker := time.NewTicker(m.config.delay())
defer ticker.Stop()
for i := int64(0); i < m.config.repetitions(); i++ {
go m.tlsPingAsync(ctx, mxmx, address, out)
go m.tlsPingAsync(ctx, i, zeroTime, logger, address, out)
<-ticker.C
}
}
// tlsPingAsync performs a TLS ping and emits the result onto the out channel.
func (m *Measurer) tlsPingAsync(ctx context.Context, mxmx *measurex.Measurer,
address string, out chan<- *measurex.EndpointMeasurement) {
out <- m.tlsConnectAndHandshake(ctx, mxmx, address)
func (m *Measurer) tlsPingAsync(ctx context.Context, index int64,
zeroTime time.Time, logger model.Logger, address string, out chan<- *SinglePing) {
out <- m.tlsConnectAndHandshake(ctx, index, zeroTime, logger, address)
}
// tlsConnectAndHandshake performs a TCP connect followed by a TLS handshake
// and returns the results of these operations to the caller.
func (m *Measurer) tlsConnectAndHandshake(ctx context.Context, mxmx *measurex.Measurer,
address string) *measurex.EndpointMeasurement {
func (m *Measurer) tlsConnectAndHandshake(ctx context.Context, index int64,
zeroTime time.Time, logger model.Logger, address string) *SinglePing {
// TODO(bassosimone): make the timeout user-configurable
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
return mxmx.TLSConnectAndHandshake(ctx, address, &tls.Config{
NextProtos: strings.Split(m.config.alpn(), " "),
sp := &SinglePing{
NetworkEvents: []*model.ArchivalNetworkEvent{},
TCPConnect: nil,
TLSHandshake: nil,
}
trace := measurexlite.NewTrace(index, zeroTime)
dialer := trace.NewDialerWithoutResolver(logger)
alpn := strings.Split(m.config.alpn(), " ")
sni := m.config.sni(address)
ol := measurexlite.NewOperationLogger(logger, "TLSPing #%d %s %s %v", index, address, sni, alpn)
conn, err := dialer.DialContext(ctx, "tcp", address)
sp.TCPConnect = <-trace.TCPConnect
if err != nil {
ol.Stop(err)
return sp
}
defer conn.Close()
conn = trace.WrapNetConn(conn)
thx := trace.NewTLSHandshakerStdlib(logger)
config := &tls.Config{
NextProtos: alpn,
RootCAs: netxlite.NewDefaultCertPool(),
ServerName: m.config.sni(address),
})
ServerName: sni,
}
_, _, err = thx.Handshake(ctx, conn, config)
ol.Stop(err)
sp.TLSHandshake = <-trace.TLSHandshake
sp.NetworkEvents = trace.NetworkEvents()
return sp
}
// NewExperimentMeasurer creates a new ExperimentMeasurer.
@@ -39,7 +39,7 @@ func TestMeasurer_run(t *testing.T) {
const expectedPings = 4
// runHelper is an helper function to run this set of tests.
runHelper := func(input string) (*model.Measurement, model.ExperimentMeasurer, error) {
runHelper := func(ctx context.Context, input string) (*model.Measurement, model.ExperimentMeasurer, error) {
m := NewExperimentMeasurer(Config{
ALPN: "http/1.1",
Delay: 1, // millisecond
@@ -48,10 +48,9 @@ func TestMeasurer_run(t *testing.T) {
if m.ExperimentName() != "tlsping" {
t.Fatal("invalid experiment name")
}
if m.ExperimentVersion() != "0.1.0" {
if m.ExperimentVersion() != "0.2.0" {
t.Fatal("invalid experiment version")
}
ctx := context.Background()
meas := &model.Measurement{
Input: model.MeasurementTarget(input),
}
@@ -64,34 +63,34 @@ func TestMeasurer_run(t *testing.T) {
}
t.Run("with empty input", func(t *testing.T) {
_, _, err := runHelper("")
_, _, err := runHelper(context.Background(), "")
if !errors.Is(err, errNoInputProvided) {
t.Fatal("unexpected error", err)
}
})
t.Run("with invalid URL", func(t *testing.T) {
_, _, err := runHelper("\t")
_, _, err := runHelper(context.Background(), "\t")
if !errors.Is(err, errInputIsNotAnURL) {
t.Fatal("unexpected error", err)
}
})
t.Run("with invalid scheme", func(t *testing.T) {
_, _, err := runHelper("https://8.8.8.8:443/")
_, _, err := runHelper(context.Background(), "https://8.8.8.8:443/")
if !errors.Is(err, errInvalidScheme) {
t.Fatal("unexpected error", err)
}
})
t.Run("with missing port", func(t *testing.T) {
_, _, err := runHelper("tlshandshake://8.8.8.8")
_, _, err := runHelper(context.Background(), "tlshandshake://8.8.8.8")
if !errors.Is(err, errMissingPort) {
t.Fatal("unexpected error", err)
}
})
t.Run("with local listener", func(t *testing.T) {
t.Run("with local listener and successful outcome", func(t *testing.T) {
srvr := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
@@ -101,7 +100,37 @@ func TestMeasurer_run(t *testing.T) {
t.Fatal(err)
}
URL.Scheme = "tlshandshake"
meas, m, err := runHelper(URL.String())
meas, m, err := runHelper(context.Background(), URL.String())
if err != nil {
t.Fatal(err)
}
tk := meas.TestKeys.(*TestKeys)
if len(tk.Pings) != expectedPings {
t.Fatal("unexpected number of pings")
}
ask, err := m.GetSummaryKeys(meas)
if err != nil {
t.Fatal("cannot obtain summary")
}
summary := ask.(SummaryKeys)
if summary.IsAnomaly {
t.Fatal("expected no anomaly")
}
})
t.Run("with local listener and connect issues", func(t *testing.T) {
srvr := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
defer srvr.Close()
URL, err := url.Parse(srvr.URL)
if err != nil {
t.Fatal(err)
}
URL.Scheme = "tlshandshake"
ctx, cancel := context.WithCancel(context.Background())
cancel() // so we cannot dial any connection
meas, m, err := runHelper(ctx, URL.String())
if err != nil {
t.Fatal(err)
}
@@ -1,6 +1,11 @@
// Package urlgetter implements a nettest that fetches a URL.
//
// See https://github.com/ooni/spec/blob/master/nettests/ts-027-urlgetter.md.
//
// This package is now frozen. Please, use measurexlite for new code. New
// network experiments should not depend on this package. Please see
// https://github.com/ooni/probe-cli/blob/master/docs/design/dd-003-step-by-step.md
// for details about this.
package urlgetter
import (
+4
View File
@@ -46,4 +46,8 @@
//
// See docs/design/dd-002-nets.md in the probe-cli repository for
// the design document describing this package.
//
// This package is now frozen. Please, use measurexlite for new code. See
// https://github.com/ooni/probe-cli/blob/master/docs/design/dd-003-step-by-step.md
// for details about this.
package netx