geolocate: first pass of code review and minor fixes (#359)
* doc(geolocate): minor cleanup * more minor cleanups of geolocate * remove disabled test and see whether now it works
This commit is contained in:
parent
3cb6c7c6fb
commit
f271e71c0b
|
@ -1,3 +0,0 @@
|
||||||
# Package github.com/ooni/probe-engine/geolocate
|
|
||||||
|
|
||||||
Package geolocate implements IP lookup, resolver lookup, and geolocation.
|
|
|
@ -5,21 +5,19 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/version"
|
"github.com/ooni/probe-cli/v3/internal/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// DefaultProbeASN is the default probe ASN as number.
|
// DefaultProbeASN is the default probe ASN as a number.
|
||||||
DefaultProbeASN uint = 0
|
DefaultProbeASN uint = 0
|
||||||
|
|
||||||
// DefaultProbeCC is the default probe CC.
|
// DefaultProbeCC is the default probe CC.
|
||||||
DefaultProbeCC = "ZZ"
|
DefaultProbeCC = "ZZ"
|
||||||
|
|
||||||
// DefaultProbeIP is the default probe IP.
|
// DefaultProbeIP is the default probe IP.
|
||||||
DefaultProbeIP = model.DefaultProbeIP
|
DefaultProbeIP = "127.0.0.1"
|
||||||
|
|
||||||
// DefaultProbeNetworkName is the default probe network name.
|
// DefaultProbeNetworkName is the default probe network name.
|
||||||
DefaultProbeNetworkName = ""
|
DefaultProbeNetworkName = ""
|
||||||
|
@ -46,40 +44,37 @@ var (
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
Debug(msg string)
|
Debug(msg string)
|
||||||
Debugf(format string, v ...interface{})
|
Debugf(format string, v ...interface{})
|
||||||
Info(msg string)
|
|
||||||
Infof(format string, v ...interface{})
|
Infof(format string, v ...interface{})
|
||||||
Warn(msg string)
|
|
||||||
Warnf(format string, v ...interface{})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Results contains geolocate results
|
// Results contains geolocate results.
|
||||||
type Results struct {
|
type Results struct {
|
||||||
// ASN is the autonomous system number
|
// ASN is the autonomous system number.
|
||||||
ASN uint
|
ASN uint
|
||||||
|
|
||||||
// CountryCode is the country code
|
// CountryCode is the country code.
|
||||||
CountryCode string
|
CountryCode string
|
||||||
|
|
||||||
// didResolverLookup indicates whether we did a resolver lookup.
|
// didResolverLookup indicates whether we did a resolver lookup.
|
||||||
didResolverLookup bool
|
didResolverLookup bool
|
||||||
|
|
||||||
// NetworkName is the network name
|
// NetworkName is the network name.
|
||||||
NetworkName string
|
NetworkName string
|
||||||
|
|
||||||
// IP is the probe IP
|
// IP is the probe IP.
|
||||||
ProbeIP string
|
ProbeIP string
|
||||||
|
|
||||||
// ResolverASN is the resolver ASN
|
// ResolverASN is the resolver ASN.
|
||||||
ResolverASN uint
|
ResolverASN uint
|
||||||
|
|
||||||
// ResolverIP is the resolver IP
|
// ResolverIP is the resolver IP.
|
||||||
ResolverIP string
|
ResolverIP string
|
||||||
|
|
||||||
// ResolverNetworkName is the resolver network name
|
// ResolverNetworkName is the resolver network name.
|
||||||
ResolverNetworkName string
|
ResolverNetworkName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ASNString returns the ASN as a string
|
// ASNString returns the ASN as a string.
|
||||||
func (r *Results) ASNString() string {
|
func (r *Results) ASNString() string {
|
||||||
return fmt.Sprintf("AS%d", r.ASN)
|
return fmt.Sprintf("AS%d", r.ASN)
|
||||||
}
|
}
|
||||||
|
@ -123,16 +118,19 @@ type Config struct {
|
||||||
UserAgent string
|
UserAgent string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Must ensures that NewTask is successful.
|
// discardLogger just ignores log messages thrown at it.
|
||||||
func Must(task *Task, err error) *Task {
|
type discardLogger struct{}
|
||||||
runtimex.PanicOnError(err, "NewTask failed")
|
|
||||||
return task
|
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.
|
// NewTask creates a new instance of Task from config.
|
||||||
func NewTask(config Config) (*Task, error) {
|
func NewTask(config Config) *Task {
|
||||||
if config.Logger == nil {
|
if config.Logger == nil {
|
||||||
config.Logger = model.DiscardLogger
|
config.Logger = &discardLogger{}
|
||||||
}
|
}
|
||||||
if config.UserAgent == "" {
|
if config.UserAgent == "" {
|
||||||
config.UserAgent = fmt.Sprintf("ooniprobe-engine/%s", version.Version)
|
config.UserAgent = fmt.Sprintf("ooniprobe-engine/%s", version.Version)
|
||||||
|
@ -147,7 +145,7 @@ func NewTask(config Config) (*Task, error) {
|
||||||
probeASNLookupper: mmdbLookupper{},
|
probeASNLookupper: mmdbLookupper{},
|
||||||
resolverASNLookupper: mmdbLookupper{},
|
resolverASNLookupper: mmdbLookupper{},
|
||||||
resolverIPLookupper: resolverLookupClient{},
|
resolverIPLookupper: resolverLookupClient{},
|
||||||
}, nil
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Task performs a geolocation. You must create a new
|
// Task performs a geolocation. You must create a new
|
||||||
|
|
|
@ -265,7 +265,7 @@ func TestLocationLookupSuccessWithResolverLookup(t *testing.T) {
|
||||||
|
|
||||||
func TestSmoke(t *testing.T) {
|
func TestSmoke(t *testing.T) {
|
||||||
config := Config{}
|
config := Config{}
|
||||||
task := Must(NewTask(config))
|
task := NewTask(config)
|
||||||
result, err := task.Run(context.Background())
|
result, err := task.Run(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
|
@ -12,11 +11,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIPLookupWorksUsingIPConfig(t *testing.T) {
|
func TestIPLookupWorksUsingIPConfig(t *testing.T) {
|
||||||
if os.Getenv("CI") == "true" {
|
|
||||||
// See https://github.com/ooni/probe-cli/pull/259/checks?check_run_id=2166066881#step:5:123
|
|
||||||
// as well as https://github.com/ooni/probe/issues/1418.
|
|
||||||
t.Skip("This test does not work with GitHub Actions")
|
|
||||||
}
|
|
||||||
ip, err := ipConfigIPLookup(
|
ip, err := ipConfigIPLookup(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
http.DefaultClient,
|
http.DefaultClient,
|
||||||
|
|
|
@ -28,7 +28,7 @@ func (mmdbLookupper) LookupASN(ip string) (asn uint, org string, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// LookupASN returns the ASN and the organization associated with the
|
// LookupASN returns the ASN and the organization associated with the
|
||||||
// given ip using the ASN database at path.
|
// given IP address.
|
||||||
func LookupASN(ip string) (asn uint, org string, err error) {
|
func LookupASN(ip string) (asn uint, org string, err error) {
|
||||||
return (mmdbLookupper{}).LookupASN(ip)
|
return (mmdbLookupper{}).LookupASN(ip)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
// Package model defines shared data structures and interfaces.
|
// Package model defines shared data structures and interfaces.
|
||||||
package model
|
package model
|
||||||
|
|
||||||
|
import "github.com/ooni/probe-cli/v3/internal/engine/geolocate"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// DefaultProbeIP is the default probe IP.
|
// DefaultProbeIP is the default probe IP.
|
||||||
DefaultProbeIP = "127.0.0.1"
|
DefaultProbeIP = geolocate.DefaultProbeIP
|
||||||
)
|
)
|
||||||
|
|
|
@ -645,11 +645,11 @@ func (s *Session) MaybeLookupBackendsContext(ctx context.Context) error {
|
||||||
// LookupLocationContext performs a location lookup. If you want memoisation
|
// LookupLocationContext performs a location lookup. If you want memoisation
|
||||||
// of the results, you should use MaybeLookupLocationContext.
|
// of the results, you should use MaybeLookupLocationContext.
|
||||||
func (s *Session) LookupLocationContext(ctx context.Context) (*geolocate.Results, error) {
|
func (s *Session) LookupLocationContext(ctx context.Context) (*geolocate.Results, error) {
|
||||||
task := geolocate.Must(geolocate.NewTask(geolocate.Config{
|
task := geolocate.NewTask(geolocate.Config{
|
||||||
Logger: s.Logger(),
|
Logger: s.Logger(),
|
||||||
Resolver: s.resolver,
|
Resolver: s.resolver,
|
||||||
UserAgent: s.UserAgent(),
|
UserAgent: s.UserAgent(),
|
||||||
}))
|
})
|
||||||
return task.Run(ctx)
|
return task.Run(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user