refactor: move TH structs and definitions to model (#894)

This commit moves the TH structs and definitions to model. We don't want
oohelperd to depend on web_connectivity@v0.4.

Part of https://github.com/ooni/probe/issues/2240
This commit is contained in:
Simone Basso
2022-08-28 20:20:12 +02:00
committed by GitHub
parent 110a11828b
commit bb6563f363
12 changed files with 161 additions and 157 deletions
+2 -3
View File
@@ -9,7 +9,6 @@ import (
"sync"
"time"
"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"
"github.com/ooni/probe-cli/v3/internal/tracex"
@@ -20,7 +19,7 @@ var newfailure = tracex.NewFailure
// ctrlDNSResult is the result of the DNS check performed by
// the Web Connectivity test helper.
type ctrlDNSResult = webconnectivity.ControlDNSResult
type ctrlDNSResult = model.THDNSResult
// dnsConfig configures the DNS check.
type dnsConfig struct {
@@ -70,7 +69,7 @@ func dnsMapFailure(failure *string) *string {
case netxlite.FailureDNSNXDOMAINError:
// We have a name for this string because dnsanalysis.go is
// already checking for this specific error string.
s := webconnectivity.DNSNameError
s := model.THDNSNameError
return &s
case netxlite.FailureDNSNoAnswer:
// In this case the legacy TH would produce an empty
+2 -3
View File
@@ -6,7 +6,6 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
"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/model/mocks"
"github.com/ooni/probe-cli/v3/internal/netxlite"
@@ -28,7 +27,7 @@ func Test_dnsMapFailure(t *testing.T) {
}, {
name: "nxdomain",
failure: stringPointerForString(netxlite.FailureDNSNXDOMAINError),
want: stringPointerForString(webconnectivity.DNSNameError),
want: stringPointerForString(model.THDNSNameError),
}, {
name: "no answer",
failure: stringPointerForString(netxlite.FailureDNSNoAnswer),
@@ -79,7 +78,7 @@ func TestDNSDo(t *testing.T) {
},
}
},
Out: make(chan webconnectivity.ControlDNSResult, 1),
Out: make(chan model.THDNSResult, 1),
Wg: &sync.WaitGroup{},
}
config.Wg.Add(1)
+1 -1
View File
@@ -23,7 +23,7 @@ import (
// ctrlHTTPResponse is the result of the HTTP check performed by
// the Web Connectivity test helper.
type ctrlHTTPResponse = webconnectivity.ControlHTTPRequestResult
type ctrlHTTPResponse = model.THHTTPRequestResult
// httpConfig configures the HTTP check.
type httpConfig struct {
+9 -9
View File
@@ -10,34 +10,34 @@ import (
"sort"
"strings"
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
"github.com/ooni/probe-cli/v3/internal/geoipx"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// newIPInfo creates an IP to IPInfo mapping from addresses resolved
// by the probe (inside [creq]) or the TH (inside [addrs]).
func newIPInfo(creq *ctrlRequest, addrs []string) map[string]*webconnectivity.ControlIPInfo {
func newIPInfo(creq *ctrlRequest, addrs []string) map[string]*model.THIPInfo {
discoveredby := make(map[string]int64)
for _, epnt := range creq.TCPConnect {
addr, _, err := net.SplitHostPort(epnt)
if err != nil || net.ParseIP(addr) == nil {
continue
}
discoveredby[addr] |= webconnectivity.ControlIPInfoFlagResolvedByProbe
discoveredby[addr] |= model.THIPInfoFlagResolvedByProbe
}
for _, addr := range addrs {
if net.ParseIP(addr) != nil {
discoveredby[addr] |= webconnectivity.ControlIPInfoFlagResolvedByTH
discoveredby[addr] |= model.THIPInfoFlagResolvedByTH
}
}
ipinfo := make(map[string]*webconnectivity.ControlIPInfo)
ipinfo := make(map[string]*model.THIPInfo)
for addr, flags := range discoveredby {
if netxlite.IsBogon(addr) { // note: we already excluded non-IP addrs above
flags |= webconnectivity.ControlIPInfoFlagIsBogon
flags |= model.THIPInfoFlagIsBogon
}
asn, _, _ := geoipx.LookupASN(addr) // AS0 on failure
ipinfo[addr] = &webconnectivity.ControlIPInfo{
ipinfo[addr] = &model.THIPInfo{
ASN: int64(asn),
Flags: flags,
}
@@ -70,7 +70,7 @@ type endpointInfo struct {
// whether an IP address is valid for a domain;
//
// 4. otherwise, we don't generate any endpoint to measure.
func ipInfoToEndpoints(URL *url.URL, ipinfo map[string]*webconnectivity.ControlIPInfo) []endpointInfo {
func ipInfoToEndpoints(URL *url.URL, ipinfo map[string]*model.THIPInfo) []endpointInfo {
var ports []string
if port := URL.Port(); port != "" {
ports = []string{port} // as documented
@@ -81,7 +81,7 @@ func ipInfoToEndpoints(URL *url.URL, ipinfo map[string]*webconnectivity.ControlI
}
out := []endpointInfo{}
for addr, info := range ipinfo {
if (info.Flags & webconnectivity.ControlIPInfoFlagIsBogon) != 0 {
if (info.Flags & model.THIPInfoFlagIsBogon) != 0 {
continue // as documented
}
for _, port := range ports {
+31 -31
View File
@@ -5,7 +5,7 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
"github.com/ooni/probe-cli/v3/internal/model"
)
func Test_newIPInfo(t *testing.T) {
@@ -16,22 +16,22 @@ func Test_newIPInfo(t *testing.T) {
tests := []struct {
name string
args args
want map[string]*webconnectivity.ControlIPInfo
want map[string]*model.THIPInfo
}{{
name: "with empty input",
args: args{
creq: &webconnectivity.ControlRequest{
creq: &model.THRequest{
HTTPRequest: "",
HTTPRequestHeaders: map[string][]string{},
TCPConnect: []string{},
},
addrs: []string{},
},
want: map[string]*webconnectivity.ControlIPInfo{},
want: map[string]*model.THIPInfo{},
}, {
name: "typical case with also bogons",
args: args{
creq: &webconnectivity.ControlRequest{
creq: &model.THRequest{
HTTPRequest: "",
HTTPRequestHeaders: map[string][]string{},
TCPConnect: []string{
@@ -44,24 +44,24 @@ func Test_newIPInfo(t *testing.T) {
"8.8.4.4",
},
},
want: map[string]*webconnectivity.ControlIPInfo{
want: map[string]*model.THIPInfo{
"10.0.0.1": {
ASN: 0,
Flags: webconnectivity.ControlIPInfoFlagIsBogon | webconnectivity.ControlIPInfoFlagResolvedByProbe,
Flags: model.THIPInfoFlagIsBogon | model.THIPInfoFlagResolvedByProbe,
},
"8.8.8.8": {
ASN: 15169,
Flags: webconnectivity.ControlIPInfoFlagResolvedByProbe | webconnectivity.ControlIPInfoFlagResolvedByTH,
Flags: model.THIPInfoFlagResolvedByProbe | model.THIPInfoFlagResolvedByTH,
},
"8.8.4.4": {
ASN: 15169,
Flags: webconnectivity.ControlIPInfoFlagResolvedByTH,
Flags: model.THIPInfoFlagResolvedByTH,
},
},
}, {
name: "with invalid endpoint",
args: args{
creq: &webconnectivity.ControlRequest{
creq: &model.THRequest{
HTTPRequest: "",
HTTPRequestHeaders: map[string][]string{},
TCPConnect: []string{
@@ -70,11 +70,11 @@ func Test_newIPInfo(t *testing.T) {
},
addrs: []string{},
},
want: map[string]*webconnectivity.ControlIPInfo{},
want: map[string]*model.THIPInfo{},
}, {
name: "with invalid IP addr",
args: args{
creq: &webconnectivity.ControlRequest{
creq: &model.THRequest{
HTTPRequest: "",
HTTPRequestHeaders: map[string][]string{},
TCPConnect: []string{
@@ -83,7 +83,7 @@ func Test_newIPInfo(t *testing.T) {
},
addrs: []string{},
},
want: map[string]*webconnectivity.ControlIPInfo{},
want: map[string]*model.THIPInfo{},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -98,7 +98,7 @@ func Test_newIPInfo(t *testing.T) {
func Test_ipInfoToEndpoints(t *testing.T) {
type args struct {
URL *url.URL
ipinfo map[string]*webconnectivity.ControlIPInfo
ipinfo map[string]*model.THIPInfo
}
tests := []struct {
name string
@@ -115,7 +115,7 @@ func Test_ipInfoToEndpoints(t *testing.T) {
name: "with empty map and empty URL",
args: args{
URL: &url.URL{},
ipinfo: map[string]*webconnectivity.ControlIPInfo{},
ipinfo: map[string]*model.THIPInfo{},
},
want: []endpointInfo{},
}, {
@@ -124,18 +124,18 @@ func Test_ipInfoToEndpoints(t *testing.T) {
URL: &url.URL{
Scheme: "http",
},
ipinfo: map[string]*webconnectivity.ControlIPInfo{
ipinfo: map[string]*model.THIPInfo{
"10.0.0.1": {
ASN: 0,
Flags: webconnectivity.ControlIPInfoFlagIsBogon | webconnectivity.ControlIPInfoFlagResolvedByProbe,
Flags: model.THIPInfoFlagIsBogon | model.THIPInfoFlagResolvedByProbe,
},
"8.8.8.8": {
ASN: 15169,
Flags: webconnectivity.ControlIPInfoFlagResolvedByProbe | webconnectivity.ControlIPInfoFlagResolvedByTH,
Flags: model.THIPInfoFlagResolvedByProbe | model.THIPInfoFlagResolvedByTH,
},
"8.8.4.4": {
ASN: 15169,
Flags: webconnectivity.ControlIPInfoFlagResolvedByTH,
Flags: model.THIPInfoFlagResolvedByTH,
},
},
},
@@ -162,18 +162,18 @@ func Test_ipInfoToEndpoints(t *testing.T) {
URL: &url.URL{
Host: "dns.google:5432",
},
ipinfo: map[string]*webconnectivity.ControlIPInfo{
ipinfo: map[string]*model.THIPInfo{
"10.0.0.1": {
ASN: 0,
Flags: webconnectivity.ControlIPInfoFlagIsBogon | webconnectivity.ControlIPInfoFlagResolvedByProbe,
Flags: model.THIPInfoFlagIsBogon | model.THIPInfoFlagResolvedByProbe,
},
"8.8.8.8": {
ASN: 15169,
Flags: webconnectivity.ControlIPInfoFlagResolvedByProbe | webconnectivity.ControlIPInfoFlagResolvedByTH,
Flags: model.THIPInfoFlagResolvedByProbe | model.THIPInfoFlagResolvedByTH,
},
"8.8.4.4": {
ASN: 15169,
Flags: webconnectivity.ControlIPInfoFlagResolvedByTH,
Flags: model.THIPInfoFlagResolvedByTH,
},
},
},
@@ -190,18 +190,18 @@ func Test_ipInfoToEndpoints(t *testing.T) {
name: "with addresses and some bogons, no port, and unknown scheme",
args: args{
URL: &url.URL{},
ipinfo: map[string]*webconnectivity.ControlIPInfo{
ipinfo: map[string]*model.THIPInfo{
"10.0.0.1": {
ASN: 0,
Flags: webconnectivity.ControlIPInfoFlagIsBogon | webconnectivity.ControlIPInfoFlagResolvedByProbe,
Flags: model.THIPInfoFlagIsBogon | model.THIPInfoFlagResolvedByProbe,
},
"8.8.8.8": {
ASN: 15169,
Flags: webconnectivity.ControlIPInfoFlagResolvedByProbe | webconnectivity.ControlIPInfoFlagResolvedByTH,
Flags: model.THIPInfoFlagResolvedByProbe | model.THIPInfoFlagResolvedByTH,
},
"8.8.4.4": {
ASN: 15169,
Flags: webconnectivity.ControlIPInfoFlagResolvedByTH,
Flags: model.THIPInfoFlagResolvedByTH,
},
},
},
@@ -212,18 +212,18 @@ func Test_ipInfoToEndpoints(t *testing.T) {
URL: &url.URL{
Scheme: "https",
},
ipinfo: map[string]*webconnectivity.ControlIPInfo{
ipinfo: map[string]*model.THIPInfo{
"10.0.0.1": {
ASN: 0,
Flags: webconnectivity.ControlIPInfoFlagIsBogon | webconnectivity.ControlIPInfoFlagResolvedByProbe,
Flags: model.THIPInfoFlagIsBogon | model.THIPInfoFlagResolvedByProbe,
},
"8.8.8.8": {
ASN: 15169,
Flags: webconnectivity.ControlIPInfoFlagResolvedByProbe | webconnectivity.ControlIPInfoFlagResolvedByTH,
Flags: model.THIPInfoFlagResolvedByProbe | model.THIPInfoFlagResolvedByTH,
},
"8.8.4.4": {
ASN: 15169,
Flags: webconnectivity.ControlIPInfoFlagResolvedByTH,
Flags: model.THIPInfoFlagResolvedByTH,
},
},
},
+9 -9
View File
@@ -10,15 +10,15 @@ import (
"net/url"
"sync"
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
"github.com/ooni/probe-cli/v3/internal/model"
)
type (
// ctrlRequest is the request sent to the test helper
ctrlRequest = webconnectivity.ControlRequest
ctrlRequest = model.THRequest
// ctrlResponse is the response from the test helper
ctrlResponse = webconnectivity.ControlResponse
ctrlResponse = model.THResponse
)
// measure performs the measurement described by the request and
@@ -48,11 +48,11 @@ func measure(ctx context.Context, config *handler, creq *ctrlRequest) (*ctrlResp
// start assembling the response
cresp := &ctrlResponse{
TCPConnect: map[string]webconnectivity.ControlTCPConnectResult{},
TLSHandshake: map[string]webconnectivity.ControlTLSHandshakeResult{},
HTTPRequest: webconnectivity.ControlHTTPRequestResult{},
DNS: webconnectivity.ControlDNSResult{},
IPInfo: map[string]*webconnectivity.ControlIPInfo{},
TCPConnect: map[string]model.THTCPConnectResult{},
TLSHandshake: map[string]model.THTLSHandshakeResult{},
HTTPRequest: model.THHTTPRequestResult{},
DNS: model.THDNSResult{},
IPInfo: map[string]*model.THIPInfo{},
}
select {
case cresp.DNS = <-dnsch:
@@ -111,7 +111,7 @@ Loop:
if tcpconn.TLS != nil {
cresp.TLSHandshake[tcpconn.Endpoint] = *tcpconn.TLS
if info := cresp.IPInfo[tcpconn.Address]; info != nil && tcpconn.TLS.Failure == nil {
info.Flags |= webconnectivity.ControlIPInfoFlagValidForDomain
info.Flags |= model.THIPInfoFlagValidForDomain
}
}
default:
+3 -4
View File
@@ -10,17 +10,16 @@ import (
"sync"
"time"
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
"github.com/ooni/probe-cli/v3/internal/measurexlite"
"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
type ctrlTCPResult = model.THTCPConnectResult
// ctrlTLSResult is the result of the TLS check performed by the test helper.
type ctrlTLSResult = webconnectivity.ControlTLSHandshakeResult
type ctrlTLSResult = model.THTLSHandshakeResult
// tcpResultPair contains the endpoint and the corresponding result.
type tcpResultPair struct {
@@ -73,7 +72,7 @@ func tcpDo(ctx context.Context, config *tcpConfig) {
out := &tcpResultPair{
Address: config.Address,
Endpoint: config.Endpoint,
TCP: webconnectivity.ControlTCPConnectResult{},
TCP: model.THTCPConnectResult{},
TLS: nil, // means: not measured
}
defer func() {