refactor: interfaces and data types into the model package (#642)
## 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/1885 - [x] related ooni/spec pull request: N/A Location of the issue tracker: https://github.com/ooni/probe ## Description This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package. The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity. An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other. This is what I want to move in `internal/model`: - [x] most important interfaces from `internal/netxlite` - [x] everything that was previously part of `internal/engine/model` - [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/httpx"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
type avastResponse struct {
|
||||
@@ -14,7 +15,7 @@ type avastResponse struct {
|
||||
func avastIPLookup(
|
||||
ctx context.Context,
|
||||
httpClient *http.Client,
|
||||
logger Logger,
|
||||
logger model.Logger,
|
||||
userAgent string,
|
||||
) (string, error) {
|
||||
var v avastResponse
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/version"
|
||||
)
|
||||
|
||||
@@ -17,7 +18,7 @@ const (
|
||||
DefaultProbeCC = "ZZ"
|
||||
|
||||
// DefaultProbeIP is the default probe IP.
|
||||
DefaultProbeIP = "127.0.0.1"
|
||||
DefaultProbeIP = model.DefaultProbeIP
|
||||
|
||||
// DefaultProbeNetworkName is the default probe network name.
|
||||
DefaultProbeNetworkName = ""
|
||||
@@ -40,13 +41,6 @@ var (
|
||||
DefaultResolverASNString = fmt.Sprintf("AS%d", DefaultResolverASN)
|
||||
)
|
||||
|
||||
// Logger is the definition of Logger used by this package.
|
||||
type Logger interface {
|
||||
Debug(msg string)
|
||||
Debugf(format string, v ...interface{})
|
||||
Infof(format string, v ...interface{})
|
||||
}
|
||||
|
||||
// Results contains geolocate results.
|
||||
type Results struct {
|
||||
// ASN is the autonomous system number.
|
||||
@@ -111,26 +105,17 @@ type Config struct {
|
||||
|
||||
// Logger is the logger to use. If not set, then we will
|
||||
// use a logger that discards all messages.
|
||||
Logger Logger
|
||||
Logger model.Logger
|
||||
|
||||
// UserAgent is the user agent to use. If not set, then
|
||||
// we will use a default user agent.
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
// discardLogger just ignores log messages thrown at it.
|
||||
type discardLogger struct{}
|
||||
|
||||
func (*discardLogger) Debug(msg string) {}
|
||||
|
||||
func (*discardLogger) Debugf(format string, v ...interface{}) {}
|
||||
|
||||
func (*discardLogger) Infof(format string, v ...interface{}) {}
|
||||
|
||||
// NewTask creates a new instance of Task from config.
|
||||
func NewTask(config Config) *Task {
|
||||
if config.Logger == nil {
|
||||
config.Logger = &discardLogger{}
|
||||
config.Logger = model.DiscardLogger
|
||||
}
|
||||
if config.UserAgent == "" {
|
||||
config.UserAgent = fmt.Sprintf("ooniprobe-engine/%s", version.Version)
|
||||
|
||||
@@ -3,12 +3,14 @@ package geolocate
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
func invalidIPLookup(
|
||||
ctx context.Context,
|
||||
httpClient *http.Client,
|
||||
logger Logger,
|
||||
logger model.Logger,
|
||||
userAgent string,
|
||||
) (string, error) {
|
||||
return "invalid IP", nil
|
||||
|
||||
@@ -7,12 +7,13 @@ import (
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/httpx"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
func ipConfigIPLookup(
|
||||
ctx context.Context,
|
||||
httpClient *http.Client,
|
||||
logger Logger,
|
||||
logger model.Logger,
|
||||
userAgent string,
|
||||
) (string, error) {
|
||||
data, err := (httpx.Client{
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/httpx"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
type ipInfoResponse struct {
|
||||
@@ -15,7 +16,7 @@ type ipInfoResponse struct {
|
||||
func ipInfoIPLookup(
|
||||
ctx context.Context,
|
||||
httpClient *http.Client,
|
||||
logger Logger,
|
||||
logger model.Logger,
|
||||
userAgent string,
|
||||
) (string, error) {
|
||||
var v ipInfoResponse
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/multierror"
|
||||
)
|
||||
|
||||
@@ -25,7 +26,7 @@ var (
|
||||
|
||||
type lookupFunc func(
|
||||
ctx context.Context, client *http.Client,
|
||||
logger Logger, userAgent string,
|
||||
logger model.Logger, userAgent string,
|
||||
) (string, error)
|
||||
|
||||
type method struct {
|
||||
@@ -67,7 +68,7 @@ type ipLookupClient struct {
|
||||
Resolver Resolver
|
||||
|
||||
// Logger is the logger to use
|
||||
Logger Logger
|
||||
Logger model.Logger
|
||||
|
||||
// UserAgent is the user agent to use
|
||||
UserAgent string
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/pion/stun"
|
||||
)
|
||||
|
||||
@@ -20,7 +21,7 @@ type stunClient interface {
|
||||
type stunConfig struct {
|
||||
Dial func(network string, address string) (stunClient, error)
|
||||
Endpoint string
|
||||
Logger Logger
|
||||
Logger model.Logger
|
||||
}
|
||||
|
||||
func stunDialer(network string, address string) (stunClient, error) {
|
||||
@@ -75,7 +76,7 @@ func stunIPLookup(ctx context.Context, config stunConfig) (string, error) {
|
||||
func stunEkigaIPLookup(
|
||||
ctx context.Context,
|
||||
httpClient *http.Client,
|
||||
logger Logger,
|
||||
logger model.Logger,
|
||||
userAgent string,
|
||||
) (string, error) {
|
||||
return stunIPLookup(ctx, stunConfig{
|
||||
@@ -87,7 +88,7 @@ func stunEkigaIPLookup(
|
||||
func stunGoogleIPLookup(
|
||||
ctx context.Context,
|
||||
httpClient *http.Client,
|
||||
logger Logger,
|
||||
logger model.Logger,
|
||||
userAgent string,
|
||||
) (string, error) {
|
||||
return stunIPLookup(ctx, stunConfig{
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/httpx"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
type ubuntuResponse struct {
|
||||
@@ -16,7 +17,7 @@ type ubuntuResponse struct {
|
||||
func ubuntuIPLookup(
|
||||
ctx context.Context,
|
||||
httpClient *http.Client,
|
||||
logger Logger,
|
||||
logger model.Logger,
|
||||
userAgent string,
|
||||
) (string, error) {
|
||||
data, err := (httpx.Client{
|
||||
|
||||
Reference in New Issue
Block a user