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:
parent
5371c7f486
commit
5ebdeb56ca
48 changed files with 2825 additions and 299 deletions
67
internal/netxlite/trace.go
Normal file
67
internal/netxlite/trace.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package netxlite
|
||||
|
||||
//
|
||||
// Context-based tracing
|
||||
//
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
// traceKey is the private type used to set/retrieve the context's trace.
|
||||
type traceKey struct{}
|
||||
|
||||
// ContextTraceOrDefault retrieves the trace bound to the context or returns
|
||||
// a default implementation of the trace in case no tracing was configured.
|
||||
func ContextTraceOrDefault(ctx context.Context) model.Trace {
|
||||
t, _ := ctx.Value(traceKey{}).(model.Trace)
|
||||
return traceOrDefault(t)
|
||||
}
|
||||
|
||||
// ContextWithTrace returns a new context that binds to the given trace. If the
|
||||
// given trace is nil, this function will call panic.
|
||||
func ContextWithTrace(ctx context.Context, trace model.Trace) context.Context {
|
||||
runtimex.PanicIfTrue(trace == nil, "netxlite.WithTrace passed a nil trace")
|
||||
return context.WithValue(ctx, traceKey{}, trace)
|
||||
}
|
||||
|
||||
// traceOrDefault takes in input a trace and returns in output the
|
||||
// given trace, if not nil, or a default trace implementation.
|
||||
func traceOrDefault(trace model.Trace) model.Trace {
|
||||
if trace != nil {
|
||||
return trace
|
||||
}
|
||||
return &traceDefault{}
|
||||
}
|
||||
|
||||
// traceDefault is a default model.Trace implementation where each method is a no-op.
|
||||
type traceDefault struct{}
|
||||
|
||||
var _ model.Trace = &traceDefault{}
|
||||
|
||||
// TimeNow implements model.Trace.TimeNow
|
||||
func (*traceDefault) TimeNow() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
// OnConnectDone implements model.Trace.OnConnectDone.
|
||||
func (*traceDefault) OnConnectDone(
|
||||
started time.Time, network, domain, remoteAddr string, err error, finished time.Time) {
|
||||
// nothing
|
||||
}
|
||||
|
||||
// OnTLSHandshakeStart implements model.Trace.OnTLSHandshakeStart.
|
||||
func (*traceDefault) OnTLSHandshakeStart(now time.Time, remoteAddr string, config *tls.Config) {
|
||||
// nothing
|
||||
}
|
||||
|
||||
// OnTLSHandshakeDone implements model.Trace.OnTLSHandshakeDone.
|
||||
func (*traceDefault) OnTLSHandshakeDone(started time.Time, remoteAddr string, config *tls.Config,
|
||||
state tls.ConnectionState, err error, finished time.Time) {
|
||||
// nothing
|
||||
}
|
||||
Loading…
Reference in a new issue