2022-05-31 21:53:01 +02:00
|
|
|
package tracex
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
//
|
|
|
|
// All the possible events
|
|
|
|
//
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
import (
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
"errors"
|
2021-02-02 12:05:47 +01:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2022-06-01 14:32:16 +02:00
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
// FailureStr is the string representation of an error. The empty
|
|
|
|
// string represents the absence of any error.
|
|
|
|
type FailureStr string
|
|
|
|
|
|
|
|
// NewFailureStr creates a FailureStr from an error. If the error is not
|
|
|
|
// already an ErrWrapper, it's converted to an ErrWrapper. If the ErrWrapper's
|
|
|
|
// Failure is not empty, we return that. Otherwise we return a string
|
|
|
|
// indicating that an ErrWrapper has an empty failure (should not happen).
|
|
|
|
func NewFailureStr(err error) FailureStr {
|
|
|
|
if err == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
// The following code guarantees that the error is always wrapped even
|
|
|
|
// when we could not actually hit our code that does the wrapping. A case
|
|
|
|
// in which this could happen is with context deadline for HTTP when you
|
|
|
|
// have wrapped the underlying dialers but not the Transport.
|
|
|
|
var errWrapper *netxlite.ErrWrapper
|
|
|
|
if !errors.As(err, &errWrapper) {
|
|
|
|
err := netxlite.NewTopLevelGenericErrWrapper(err)
|
|
|
|
couldConvert := errors.As(err, &errWrapper)
|
2022-08-31 18:40:27 +02:00
|
|
|
runtimex.Assert(couldConvert, "we should have an ErrWrapper here")
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
}
|
|
|
|
s := errWrapper.Failure
|
|
|
|
if s == "" {
|
|
|
|
s = "unknown_failure: errWrapper.Failure is empty"
|
|
|
|
}
|
|
|
|
return FailureStr(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNil returns whether this FailureStr is nil. Technically speaking, the
|
|
|
|
// failure cannot be nil, but an empty string is equivalent to nil after
|
|
|
|
// we convert using ToFailure(). Also, this type is often called Err, Error,
|
|
|
|
// or Failure. So, the resulting code actually reads correct.
|
|
|
|
func (fs FailureStr) IsNil() bool {
|
|
|
|
return fs == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNotNil is the opposite of IsNil. Technically speaking, the
|
|
|
|
// failure cannot be nil, but an empty string is equivalent to nil after
|
|
|
|
// we convert using ToFailure(). Also, this type is often called Err, Error,
|
|
|
|
// or Failure. So, the resulting code actually reads correct.
|
|
|
|
func (fs FailureStr) IsNotNil() bool {
|
|
|
|
return !fs.IsNil()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToFailure converts a FailureStr to a OONI failure (i.e., a string
|
|
|
|
// on error and nil in case of success).
|
|
|
|
func (fs FailureStr) ToFailure() (out *string) {
|
|
|
|
if fs != "" {
|
|
|
|
s := string(fs)
|
|
|
|
out = &s
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-01 14:32:16 +02:00
|
|
|
// Event is one of the events within a trace.
|
|
|
|
type Event interface {
|
|
|
|
// Value returns the event value
|
|
|
|
Value() *EventValue
|
|
|
|
|
|
|
|
// Name returns the event name
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventTLSHandshakeStart is the beginning of the TLS handshake.
|
|
|
|
type EventTLSHandshakeStart struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventTLSHandshakeStart) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventTLSHandshakeStart) Name() string {
|
|
|
|
return "tls_handshake_start"
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventTLSHandshakeDone is the end of the TLS handshake.
|
|
|
|
type EventTLSHandshakeDone struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventTLSHandshakeDone) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventTLSHandshakeDone) Name() string {
|
|
|
|
return "tls_handshake_done"
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventResolveStart is the beginning of a DNS lookup operation.
|
|
|
|
type EventResolveStart struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventResolveStart) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventResolveStart) Name() string {
|
|
|
|
return "resolve_start"
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventResolveDone is the end of a DNS lookup operation.
|
|
|
|
type EventResolveDone struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventResolveDone) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventResolveDone) Name() string {
|
|
|
|
return "resolve_done"
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventDNSRoundTripStart is the start of a DNS round trip.
|
|
|
|
type EventDNSRoundTripStart struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventDNSRoundTripStart) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventDNSRoundTripStart) Name() string {
|
|
|
|
return "dns_round_trip_start"
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventDNSRoundTripDone is the end of a DNS round trip.
|
|
|
|
type EventDNSRoundTripDone struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventDNSRoundTripDone) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventDNSRoundTripDone) Name() string {
|
|
|
|
return "dns_round_trip_done"
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventQUICHandshakeStart is the start of a QUIC handshake.
|
|
|
|
type EventQUICHandshakeStart struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventQUICHandshakeStart) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventQUICHandshakeStart) Name() string {
|
|
|
|
return "quic_handshake_start"
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventQUICHandshakeDone is the end of a QUIC handshake.
|
|
|
|
type EventQUICHandshakeDone struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventQUICHandshakeDone) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventQUICHandshakeDone) Name() string {
|
|
|
|
return "quic_handshake_done"
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventWriteToOperation summarizes the WriteTo operation.
|
|
|
|
type EventWriteToOperation struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventWriteToOperation) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventWriteToOperation) Name() string {
|
|
|
|
return netxlite.WriteToOperation
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventReadFromOperation summarizes the ReadFrom operation.
|
|
|
|
type EventReadFromOperation struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventReadFromOperation) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventReadFromOperation) Name() string {
|
|
|
|
return netxlite.ReadFromOperation
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventHTTPTransactionStart is the beginning of an HTTP transaction.
|
|
|
|
type EventHTTPTransactionStart struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventHTTPTransactionStart) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventHTTPTransactionStart) Name() string {
|
|
|
|
return "http_transaction_start"
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventHTTPTransactionDone is the end of an HTTP transaction.
|
|
|
|
type EventHTTPTransactionDone struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventHTTPTransactionDone) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventHTTPTransactionDone) Name() string {
|
|
|
|
return "http_transaction_done"
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventConnectOperation contains information about the connect operation.
|
|
|
|
type EventConnectOperation struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventConnectOperation) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventConnectOperation) Name() string {
|
|
|
|
return netxlite.ConnectOperation
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventReadOperation contains information about a read operation.
|
|
|
|
type EventReadOperation struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventReadOperation) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventReadOperation) Name() string {
|
|
|
|
return netxlite.ReadOperation
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventWriteOperation contains information about a write operation.
|
|
|
|
type EventWriteOperation struct {
|
|
|
|
V *EventValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventWriteOperation) Value() *EventValue {
|
|
|
|
return ev.V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EventWriteOperation) Name() string {
|
|
|
|
return netxlite.WriteOperation
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
// Event is one of the events within a trace
|
2022-06-01 14:32:16 +02:00
|
|
|
type EventValue struct {
|
2022-06-02 11:07:02 +02:00
|
|
|
Addresses []string `json:",omitempty"`
|
|
|
|
Address string `json:",omitempty"`
|
|
|
|
DNSQuery []byte `json:",omitempty"`
|
|
|
|
DNSResponse []byte `json:",omitempty"`
|
|
|
|
Data []byte `json:",omitempty"`
|
|
|
|
Duration time.Duration `json:",omitempty"`
|
|
|
|
Err FailureStr `json:",omitempty"`
|
|
|
|
HTTPMethod string `json:",omitempty"`
|
|
|
|
HTTPRequestHeaders http.Header `json:",omitempty"`
|
|
|
|
HTTPResponseHeaders http.Header `json:",omitempty"`
|
|
|
|
HTTPResponseBody []byte `json:",omitempty"`
|
|
|
|
HTTPResponseBodyIsTruncated bool `json:",omitempty"`
|
|
|
|
HTTPStatusCode int `json:",omitempty"`
|
|
|
|
HTTPURL string `json:",omitempty"`
|
|
|
|
Hostname string `json:",omitempty"`
|
|
|
|
NoTLSVerify bool `json:",omitempty"`
|
|
|
|
NumBytes int `json:",omitempty"`
|
|
|
|
Proto string `json:",omitempty"`
|
|
|
|
TLSServerName string `json:",omitempty"`
|
|
|
|
TLSCipherSuite string `json:",omitempty"`
|
|
|
|
TLSNegotiatedProto string `json:",omitempty"`
|
|
|
|
TLSNextProtos []string `json:",omitempty"`
|
|
|
|
TLSPeerCerts [][]byte `json:",omitempty"`
|
|
|
|
TLSVersion string `json:",omitempty"`
|
|
|
|
Time time.Time `json:",omitempty"`
|
|
|
|
Transport string `json:",omitempty"`
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|