-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: split geolocate in three packages #1150
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,17 +1,18 @@ | ||||||
// Package geolocate implements IP lookup, resolver lookup, and geolocation. | ||||||
package geolocate | ||||||
package engine | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"fmt" | ||||||
|
||||||
"github.com/ooni/probe-cli/v3/internal/iplookup" | ||||||
"github.com/ooni/probe-cli/v3/internal/model" | ||||||
"github.com/ooni/probe-cli/v3/internal/netxlite" | ||||||
"github.com/ooni/probe-cli/v3/internal/resolverlookup" | ||||||
"github.com/ooni/probe-cli/v3/internal/version" | ||||||
) | ||||||
|
||||||
// Results contains geolocate results. | ||||||
type Results struct { | ||||||
type GeolocateResults struct { | ||||||
// ASN is the autonomous system number. | ||||||
ASN uint | ||||||
|
||||||
|
@@ -38,7 +39,7 @@ type Results struct { | |||||
} | ||||||
|
||||||
// ASNString returns the ASN as a string. | ||||||
func (r *Results) ASNString() string { | ||||||
func (r *GeolocateResults) ASNString() string { | ||||||
return fmt.Sprintf("AS%d", r.ASN) | ||||||
} | ||||||
|
||||||
|
@@ -59,7 +60,7 @@ type resolverIPLookupper interface { | |||||
} | ||||||
|
||||||
// Config contains configuration for a geolocate Task. | ||||||
type Config struct { | ||||||
type GeolocateConfig struct { | ||||||
// Resolver is the resolver we should use when | ||||||
// making requests for discovering the IP. When | ||||||
// this field is not set, we use the stdlib. | ||||||
|
@@ -74,8 +75,8 @@ type Config struct { | |||||
UserAgent string | ||||||
} | ||||||
|
||||||
// NewTask creates a new instance of Task from config. | ||||||
func NewTask(config Config) *Task { | ||||||
// NewIplookupTask creates a new instance of IpLookupTask from config. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
func NewGeolocateTask(config GeolocateConfig) *GeolocateTask { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something to think about: we may want to pass a |
||||||
if config.Logger == nil { | ||||||
config.Logger = model.DiscardLogger | ||||||
} | ||||||
|
@@ -85,20 +86,18 @@ func NewTask(config Config) *Task { | |||||
if config.Resolver == nil { | ||||||
config.Resolver = netxlite.NewStdlibResolver(config.Logger) | ||||||
} | ||||||
return &Task{ | ||||||
countryLookupper: mmdbLookupper{}, | ||||||
probeIPLookupper: ipLookupClient(config), | ||||||
probeASNLookupper: mmdbLookupper{}, | ||||||
resolverASNLookupper: mmdbLookupper{}, | ||||||
resolverIPLookupper: resolverLookupClient{ | ||||||
Logger: config.Logger, | ||||||
}, | ||||||
return &GeolocateTask{ | ||||||
countryLookupper: iplookup.MMDBLookupper{}, | ||||||
probeIPLookupper: iplookup.IpLookupClient(config), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be cleaner to have a constructor to which we pass the resolver, the logger, and the user agent, rather than relying on a cast. Because it's just three arguments, I would say we do not need to have a structure and it's just fine to pass the three arguments to the constructor instead. This also provides us with an opportunity to make the |
||||||
probeASNLookupper: iplookup.MMDBLookupper{}, | ||||||
resolverASNLookupper: iplookup.MMDBLookupper{}, | ||||||
resolverIPLookupper: resolverlookup.NewResolverLookupClient(config.Logger), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the best name here is |
||||||
} | ||||||
} | ||||||
|
||||||
// Task performs a geolocation. You must create a new | ||||||
// instance of Task using the NewTask factory. | ||||||
type Task struct { | ||||||
type GeolocateTask struct { | ||||||
countryLookupper countryLookupper | ||||||
probeIPLookupper probeIPLookupper | ||||||
probeASNLookupper asnLookupper | ||||||
|
@@ -107,9 +106,9 @@ type Task struct { | |||||
} | ||||||
|
||||||
// Run runs the task. | ||||||
func (op Task) Run(ctx context.Context) (*Results, error) { | ||||||
func (op *GeolocateTask) Run(ctx context.Context) (*GeolocateResults, error) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see you converted from |
||||||
var err error | ||||||
out := &Results{ | ||||||
out := &GeolocateResults{ | ||||||
ASN: model.DefaultProbeASN, | ||||||
CountryCode: model.DefaultProbeCC, | ||||||
NetworkName: model.DefaultProbeNetworkName, | ||||||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package geolocate | ||
package iplookup | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure you add a |
||
|
||
import ( | ||
"context" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package geolocate | ||
package iplookup | ||
|
||
import ( | ||
"context" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package geolocate | ||
package iplookup | ||
|
||
import ( | ||
"net/http" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package geolocate | ||
package iplookup | ||
|
||
import ( | ||
"context" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering the extent to which we need to expose details of how we implement geolocation outside of the
engine
package. It may be desirable to make all the types we moved here from the formergeolocate
package private and just accept that they're an implementation detail of a session. What do you reckon?