2021-02-02 12:05:47 +01:00
|
|
|
// Package geolocate implements IP lookup, resolver lookup, and geolocation.
|
|
|
|
package geolocate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
fix(geolocate): no proxy when discovering our IP address (#251)
* fix(geolocate): no proxy when discovering our IP address
The use case of --proxy is that you cannot contact the OONI
backend otherwise. It is wrong, though, using the proxy when
discovering our IP address. The measurement won't use the
proxy anyway. Therefore, we need to use the IP address that
is performing the measurement. Not the one of the proxy.
What's more, stun is not using a proxy. Therefore, it does
not make much sense that http IP resolvers use a proxy. This
leads to inconsistencies. So, here's anothe reason why this
patch is a good thing (TM).
Finally, because knowing the IP address enables us to sanitize
the data, it's important we discover the correct IP.
Now, up until this point, the `--proxy` option has mostly
been a developers toy. But, users have asked us to have the
possibility of configuring a proxy.
This explains why I have been looking into making `--proxy`
right for a couple of hours now.
See https://github.com/ooni/probe/issues/1382
* fix(session): properly configure the IP lookupper
2021-03-10 12:01:08 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-02-04 11:00:27 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/version"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-06-04 16:06:24 +02:00
|
|
|
// DefaultProbeASN is the default probe ASN as a number.
|
2021-02-02 12:05:47 +01:00
|
|
|
DefaultProbeASN uint = 0
|
|
|
|
|
|
|
|
// DefaultProbeCC is the default probe CC.
|
|
|
|
DefaultProbeCC = "ZZ"
|
|
|
|
|
|
|
|
// DefaultProbeIP is the default probe IP.
|
2022-01-03 13:53:23 +01:00
|
|
|
DefaultProbeIP = model.DefaultProbeIP
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
// DefaultProbeNetworkName is the default probe network name.
|
|
|
|
DefaultProbeNetworkName = ""
|
|
|
|
|
|
|
|
// DefaultResolverASN is the default resolver ASN.
|
|
|
|
DefaultResolverASN uint = 0
|
|
|
|
|
|
|
|
// DefaultResolverIP is the default resolver IP.
|
|
|
|
DefaultResolverIP = "127.0.0.2"
|
|
|
|
|
|
|
|
// DefaultResolverNetworkName is the default resolver network name.
|
|
|
|
DefaultResolverNetworkName = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultProbeASNString is the default probe ASN as a string.
|
|
|
|
DefaultProbeASNString = fmt.Sprintf("AS%d", DefaultProbeASN)
|
|
|
|
|
|
|
|
// DefaultResolverASNString is the default resolver ASN as a string.
|
|
|
|
DefaultResolverASNString = fmt.Sprintf("AS%d", DefaultResolverASN)
|
|
|
|
)
|
|
|
|
|
2021-06-04 16:06:24 +02:00
|
|
|
// Results contains geolocate results.
|
2021-02-02 12:05:47 +01:00
|
|
|
type Results struct {
|
2021-06-04 16:06:24 +02:00
|
|
|
// ASN is the autonomous system number.
|
2021-02-02 12:05:47 +01:00
|
|
|
ASN uint
|
|
|
|
|
2021-06-04 16:06:24 +02:00
|
|
|
// CountryCode is the country code.
|
2021-02-02 12:05:47 +01:00
|
|
|
CountryCode string
|
|
|
|
|
2021-04-07 18:48:02 +02:00
|
|
|
// didResolverLookup indicates whether we did a resolver lookup.
|
|
|
|
didResolverLookup bool
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2021-06-04 16:06:24 +02:00
|
|
|
// NetworkName is the network name.
|
2021-02-02 12:05:47 +01:00
|
|
|
NetworkName string
|
|
|
|
|
2021-06-04 16:06:24 +02:00
|
|
|
// IP is the probe IP.
|
2021-02-02 12:05:47 +01:00
|
|
|
ProbeIP string
|
|
|
|
|
2021-06-04 16:06:24 +02:00
|
|
|
// ResolverASN is the resolver ASN.
|
2021-02-02 12:05:47 +01:00
|
|
|
ResolverASN uint
|
|
|
|
|
2021-06-04 16:06:24 +02:00
|
|
|
// ResolverIP is the resolver IP.
|
2021-02-02 12:05:47 +01:00
|
|
|
ResolverIP string
|
|
|
|
|
2021-06-04 16:06:24 +02:00
|
|
|
// ResolverNetworkName is the resolver network name.
|
2021-02-02 12:05:47 +01:00
|
|
|
ResolverNetworkName string
|
|
|
|
}
|
|
|
|
|
2021-06-04 16:06:24 +02:00
|
|
|
// ASNString returns the ASN as a string.
|
2021-02-02 12:05:47 +01:00
|
|
|
func (r *Results) ASNString() string {
|
|
|
|
return fmt.Sprintf("AS%d", r.ASN)
|
|
|
|
}
|
|
|
|
|
|
|
|
type probeIPLookupper interface {
|
|
|
|
LookupProbeIP(ctx context.Context) (addr string, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type asnLookupper interface {
|
2021-04-01 16:57:31 +02:00
|
|
|
LookupASN(ip string) (asn uint, network string, err error)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type countryLookupper interface {
|
2021-04-01 16:57:31 +02:00
|
|
|
LookupCC(ip string) (cc string, err error)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type resolverIPLookupper interface {
|
|
|
|
LookupResolverIP(ctx context.Context) (addr string, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config contains configuration for a geolocate Task.
|
|
|
|
type Config struct {
|
fix(geolocate): no proxy when discovering our IP address (#251)
* fix(geolocate): no proxy when discovering our IP address
The use case of --proxy is that you cannot contact the OONI
backend otherwise. It is wrong, though, using the proxy when
discovering our IP address. The measurement won't use the
proxy anyway. Therefore, we need to use the IP address that
is performing the measurement. Not the one of the proxy.
What's more, stun is not using a proxy. Therefore, it does
not make much sense that http IP resolvers use a proxy. This
leads to inconsistencies. So, here's anothe reason why this
patch is a good thing (TM).
Finally, because knowing the IP address enables us to sanitize
the data, it's important we discover the correct IP.
Now, up until this point, the `--proxy` option has mostly
been a developers toy. But, users have asked us to have the
possibility of configuring a proxy.
This explains why I have been looking into making `--proxy`
right for a couple of hours now.
See https://github.com/ooni/probe/issues/1382
* fix(session): properly configure the IP lookupper
2021-03-10 12:01:08 +01:00
|
|
|
// Resolver is the resolver we should use when
|
|
|
|
// making requests for discovering the IP. When
|
|
|
|
// this field is not set, we use the stdlib.
|
2022-01-07 18:33:37 +01:00
|
|
|
Resolver model.Resolver
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
// Logger is the logger to use. If not set, then we will
|
|
|
|
// use a logger that discards all messages.
|
2022-01-03 13:53:23 +01:00
|
|
|
Logger model.Logger
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
// UserAgent is the user agent to use. If not set, then
|
|
|
|
// we will use a default user agent.
|
|
|
|
UserAgent string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTask creates a new instance of Task from config.
|
2021-06-04 16:06:24 +02:00
|
|
|
func NewTask(config Config) *Task {
|
2021-02-02 12:05:47 +01:00
|
|
|
if config.Logger == nil {
|
2022-01-03 13:53:23 +01:00
|
|
|
config.Logger = model.DiscardLogger
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
if config.UserAgent == "" {
|
|
|
|
config.UserAgent = fmt.Sprintf("ooniprobe-engine/%s", version.Version)
|
|
|
|
}
|
fix(geolocate): no proxy when discovering our IP address (#251)
* fix(geolocate): no proxy when discovering our IP address
The use case of --proxy is that you cannot contact the OONI
backend otherwise. It is wrong, though, using the proxy when
discovering our IP address. The measurement won't use the
proxy anyway. Therefore, we need to use the IP address that
is performing the measurement. Not the one of the proxy.
What's more, stun is not using a proxy. Therefore, it does
not make much sense that http IP resolvers use a proxy. This
leads to inconsistencies. So, here's anothe reason why this
patch is a good thing (TM).
Finally, because knowing the IP address enables us to sanitize
the data, it's important we discover the correct IP.
Now, up until this point, the `--proxy` option has mostly
been a developers toy. But, users have asked us to have the
possibility of configuring a proxy.
This explains why I have been looking into making `--proxy`
right for a couple of hours now.
See https://github.com/ooni/probe/issues/1382
* fix(session): properly configure the IP lookupper
2021-03-10 12:01:08 +01:00
|
|
|
if config.Resolver == nil {
|
|
|
|
config.Resolver = netx.NewResolver(
|
|
|
|
netx.Config{Logger: config.Logger})
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
return &Task{
|
|
|
|
countryLookupper: mmdbLookupper{},
|
2021-04-07 18:48:02 +02:00
|
|
|
probeIPLookupper: ipLookupClient(config),
|
2021-02-02 12:05:47 +01:00
|
|
|
probeASNLookupper: mmdbLookupper{},
|
|
|
|
resolverASNLookupper: mmdbLookupper{},
|
|
|
|
resolverIPLookupper: resolverLookupClient{},
|
2021-06-04 16:06:24 +02:00
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Task performs a geolocation. You must create a new
|
|
|
|
// instance of Task using the NewTask factory.
|
|
|
|
type Task struct {
|
|
|
|
countryLookupper countryLookupper
|
|
|
|
probeIPLookupper probeIPLookupper
|
|
|
|
probeASNLookupper asnLookupper
|
|
|
|
resolverASNLookupper asnLookupper
|
|
|
|
resolverIPLookupper resolverIPLookupper
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the task.
|
|
|
|
func (op Task) Run(ctx context.Context) (*Results, error) {
|
|
|
|
var err error
|
|
|
|
out := &Results{
|
|
|
|
ASN: DefaultProbeASN,
|
|
|
|
CountryCode: DefaultProbeCC,
|
|
|
|
NetworkName: DefaultProbeNetworkName,
|
|
|
|
ProbeIP: DefaultProbeIP,
|
|
|
|
ResolverASN: DefaultResolverASN,
|
|
|
|
ResolverIP: DefaultResolverIP,
|
|
|
|
ResolverNetworkName: DefaultResolverNetworkName,
|
|
|
|
}
|
|
|
|
ip, err := op.probeIPLookupper.LookupProbeIP(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return out, fmt.Errorf("lookupProbeIP failed: %w", err)
|
|
|
|
}
|
|
|
|
out.ProbeIP = ip
|
2021-04-01 16:57:31 +02:00
|
|
|
asn, networkName, err := op.probeASNLookupper.LookupASN(out.ProbeIP)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return out, fmt.Errorf("lookupASN failed: %w", err)
|
|
|
|
}
|
|
|
|
out.ASN = asn
|
|
|
|
out.NetworkName = networkName
|
2021-04-01 16:57:31 +02:00
|
|
|
cc, err := op.countryLookupper.LookupCC(out.ProbeIP)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return out, fmt.Errorf("lookupProbeCC failed: %w", err)
|
|
|
|
}
|
|
|
|
out.CountryCode = cc
|
2021-04-07 18:48:02 +02:00
|
|
|
out.didResolverLookup = true
|
|
|
|
// Note: ignoring the result of lookupResolverIP and lookupASN
|
|
|
|
// here is intentional. We don't want this (~minor) failure
|
|
|
|
// to influence the result of the overall lookup. Another design
|
|
|
|
// here could be that of retrying the operation N times?
|
|
|
|
resolverIP, err := op.resolverIPLookupper.LookupResolverIP(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return out, nil // intentional
|
|
|
|
}
|
|
|
|
out.ResolverIP = resolverIP
|
|
|
|
resolverASN, resolverNetworkName, err := op.resolverASNLookupper.LookupASN(
|
|
|
|
out.ResolverIP,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return out, nil // intentional
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-04-07 18:48:02 +02:00
|
|
|
out.ResolverASN = resolverASN
|
|
|
|
out.ResolverNetworkName = resolverNetworkName
|
2021-02-02 12:05:47 +01:00
|
|
|
return out, nil
|
|
|
|
}
|