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:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user