refactor(oohelperd): flatten package hierarchy (#834)
In https://github.com/ooni/probe-cli/pull/832's initial diff, I mentioned it would be cool to flatten oohelperd's hier. I'm doing this now, and just for the master branch. This diff is mostly a mechanical refactoring with very light and apparently rather safe manual changes.
This commit is contained in:
parent
a4d17085f5
commit
535a5d3e00
12 changed files with 151 additions and 98 deletions
85
internal/cmd/oohelperd/tcpconnect.go
Normal file
85
internal/cmd/oohelperd/tcpconnect.go
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
package main
|
||||
|
||||
//
|
||||
// TCP connect measurements
|
||||
//
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
)
|
||||
|
||||
// ctrlTCPResult is the result of the TCP check performed by the test helper.
|
||||
type ctrlTCPResult = webconnectivity.ControlTCPConnectResult
|
||||
|
||||
// tcpResultPair contains the endpoint and the corresponding result.
|
||||
type tcpResultPair struct {
|
||||
// Endpoint is the endpoint we measured.
|
||||
Endpoint string
|
||||
|
||||
// Result contains the results.
|
||||
Result ctrlTCPResult
|
||||
}
|
||||
|
||||
// tcpConfig configures the TCP connect check.
|
||||
type tcpConfig struct {
|
||||
// Endpoint is the MANDATORY endpoint to connect to.
|
||||
Endpoint string
|
||||
|
||||
// NewDialer is the MANDATORY factory for creating a new dialer.
|
||||
NewDialer func() model.Dialer
|
||||
|
||||
// Out is the MANDATORY where we'll post the TCP measurement results.
|
||||
Out chan tcpResultPair
|
||||
|
||||
// Wg is MANDATORY and is used to sync with the parent.
|
||||
Wg *sync.WaitGroup
|
||||
}
|
||||
|
||||
// tcpDo performs the TCP check.
|
||||
func tcpDo(ctx context.Context, config *tcpConfig) {
|
||||
defer config.Wg.Done()
|
||||
dialer := config.NewDialer()
|
||||
defer dialer.CloseIdleConnections()
|
||||
conn, err := dialer.DialContext(ctx, "tcp", config.Endpoint)
|
||||
if conn != nil {
|
||||
conn.Close()
|
||||
}
|
||||
config.Out <- tcpResultPair{
|
||||
Endpoint: config.Endpoint,
|
||||
Result: ctrlTCPResult{
|
||||
Failure: tcpMapFailure(newfailure(err)),
|
||||
Status: err == nil,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// tcpMapFailure attempts to map netxlite failures to the strings
|
||||
// used by the original OONI test helper.
|
||||
//
|
||||
// See https://github.com/ooni/backend/blob/6ec4fda5b18/oonib/testhelpers/http_helpers.py#L392
|
||||
func tcpMapFailure(failure *string) *string {
|
||||
switch failure {
|
||||
case nil:
|
||||
return nil
|
||||
default:
|
||||
switch *failure {
|
||||
case netxlite.FailureGenericTimeoutError:
|
||||
return failure // already using the same name
|
||||
case netxlite.FailureConnectionRefused:
|
||||
s := "connection_refused_error"
|
||||
return &s
|
||||
default:
|
||||
// The definition of this error according to Twisted is
|
||||
// "something went wrong when connecting". Because we are
|
||||
// indeed basically just connecting here, it seems safe
|
||||
// to map any other error to "connect_error" here.
|
||||
s := "connect_error"
|
||||
return &s
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue