refactor: redesign how we import assets (#260)
* fix(pkg.go.dev): import a subpackage containing the assets We're trying to fix this issue that pkg.go.dev does not build. Thanks to @hellais for this very neat idea! Let's keep our fingers crossed and see whether it fixes! * feat: use embedded geoip databases Closes https://github.com/ooni/probe/issues/1372. Work done as part of https://github.com/ooni/probe/issues/1369. * fix(assetsx): add tests * feat: simplify and just vendor uncompressed DBs * remove tests that seems not necessary anymore * fix: run go mod tidy * Address https://github.com/ooni/probe-cli/pull/260/files#r605181364 * rewrite a test in a better way * fix: gently cleanup the legacy assetsdir Do not remove the whole directory with brute force. Just zap the files whose name we know. Then attempt to delete the legacy directory as well. If not empty, just fail. This is fine because it means the user has stored other files inside the directory. * fix: create .miniooni if missing
This commit is contained in:
parent
7ca32b5ce6
commit
31e478b04e
51 changed files with 243 additions and 808 deletions
|
|
@ -3,7 +3,6 @@ package geolocate
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
|
|
@ -43,12 +42,6 @@ var (
|
|||
DefaultResolverASNString = fmt.Sprintf("AS%d", DefaultResolverASN)
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrMissingResourcesManager indicates that no resources
|
||||
// manager has been configured inside of Config.
|
||||
ErrMissingResourcesManager = errors.New("geolocate: ResourcesManager is nil")
|
||||
)
|
||||
|
||||
// Logger is the definition of Logger used by this package.
|
||||
type Logger interface {
|
||||
Debug(msg string)
|
||||
|
|
@ -96,30 +89,17 @@ type probeIPLookupper interface {
|
|||
}
|
||||
|
||||
type asnLookupper interface {
|
||||
LookupASN(path string, ip string) (asn uint, network string, err error)
|
||||
LookupASN(ip string) (asn uint, network string, err error)
|
||||
}
|
||||
|
||||
type countryLookupper interface {
|
||||
LookupCC(path string, ip string) (cc string, err error)
|
||||
LookupCC(ip string) (cc string, err error)
|
||||
}
|
||||
|
||||
type resolverIPLookupper interface {
|
||||
LookupResolverIP(ctx context.Context) (addr string, err error)
|
||||
}
|
||||
|
||||
// ResourcesManager manages the required resources.
|
||||
type ResourcesManager interface {
|
||||
// ASNDatabasePath returns the path of the ASN database.
|
||||
ASNDatabasePath() string
|
||||
|
||||
// CountryDatabasePath returns the path of the country database.
|
||||
CountryDatabasePath() string
|
||||
|
||||
// MaybeUpdateResources ensures that the required resources
|
||||
// have been downloaded and are current.
|
||||
MaybeUpdateResources(ctx context.Context) error
|
||||
}
|
||||
|
||||
// Resolver is a DNS resolver.
|
||||
type Resolver interface {
|
||||
LookupHost(ctx context.Context, domain string) ([]string, error)
|
||||
|
|
@ -142,10 +122,6 @@ type Config struct {
|
|||
// use a logger that discards all messages.
|
||||
Logger Logger
|
||||
|
||||
// ResourcesManager is the mandatory resources manager. If not
|
||||
// set, we will not be able to perform any lookup.
|
||||
ResourcesManager ResourcesManager
|
||||
|
||||
// UserAgent is the user agent to use. If not set, then
|
||||
// we will use a default user agent.
|
||||
UserAgent string
|
||||
|
|
@ -162,9 +138,6 @@ func NewTask(config Config) (*Task, error) {
|
|||
if config.Logger == nil {
|
||||
config.Logger = model.DiscardLogger
|
||||
}
|
||||
if config.ResourcesManager == nil {
|
||||
return nil, ErrMissingResourcesManager
|
||||
}
|
||||
if config.UserAgent == "" {
|
||||
config.UserAgent = fmt.Sprintf("ooniprobe-engine/%s", version.Version)
|
||||
}
|
||||
|
|
@ -183,7 +156,6 @@ func NewTask(config Config) (*Task, error) {
|
|||
probeASNLookupper: mmdbLookupper{},
|
||||
resolverASNLookupper: mmdbLookupper{},
|
||||
resolverIPLookupper: resolverLookupClient{},
|
||||
resourcesManager: config.ResourcesManager,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -196,7 +168,6 @@ type Task struct {
|
|||
probeASNLookupper asnLookupper
|
||||
resolverASNLookupper asnLookupper
|
||||
resolverIPLookupper resolverIPLookupper
|
||||
resourcesManager ResourcesManager
|
||||
}
|
||||
|
||||
// Run runs the task.
|
||||
|
|
@ -211,23 +182,18 @@ func (op Task) Run(ctx context.Context) (*Results, error) {
|
|||
ResolverIP: DefaultResolverIP,
|
||||
ResolverNetworkName: DefaultResolverNetworkName,
|
||||
}
|
||||
if err := op.resourcesManager.MaybeUpdateResources(ctx); err != nil {
|
||||
return out, fmt.Errorf("MaybeUpdateResource failed: %w", err)
|
||||
}
|
||||
ip, err := op.probeIPLookupper.LookupProbeIP(ctx)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("lookupProbeIP failed: %w", err)
|
||||
}
|
||||
out.ProbeIP = ip
|
||||
asn, networkName, err := op.probeASNLookupper.LookupASN(
|
||||
op.resourcesManager.ASNDatabasePath(), out.ProbeIP)
|
||||
asn, networkName, err := op.probeASNLookupper.LookupASN(out.ProbeIP)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("lookupASN failed: %w", err)
|
||||
}
|
||||
out.ASN = asn
|
||||
out.NetworkName = networkName
|
||||
cc, err := op.countryLookupper.LookupCC(
|
||||
op.resourcesManager.CountryDatabasePath(), out.ProbeIP)
|
||||
cc, err := op.countryLookupper.LookupCC(out.ProbeIP)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("lookupProbeCC failed: %w", err)
|
||||
}
|
||||
|
|
@ -244,7 +210,7 @@ func (op Task) Run(ctx context.Context) (*Results, error) {
|
|||
}
|
||||
out.ResolverIP = resolverIP
|
||||
resolverASN, resolverNetworkName, err := op.resolverASNLookupper.LookupASN(
|
||||
op.resourcesManager.ASNDatabasePath(), out.ResolverIP,
|
||||
out.ResolverIP,
|
||||
)
|
||||
if err != nil {
|
||||
return out, nil
|
||||
|
|
|
|||
Loading…
Reference in a new issue