chore: merge probe-engine into probe-cli (#201)
This is how I did it: 1. `git clone https://github.com/ooni/probe-engine internal/engine` 2. ``` (cd internal/engine && git describe --tags) v0.23.0 ``` 3. `nvim go.mod` (merging `go.mod` with `internal/engine/go.mod` 4. `rm -rf internal/.git internal/engine/go.{mod,sum}` 5. `git add internal/engine` 6. `find . -type f -name \*.go -exec sed -i 's@/ooni/probe-engine@/ooni/probe-cli/v3/internal/engine@g' {} \;` 7. `go build ./...` (passes) 8. `go test -race ./...` (temporary failure on RiseupVPN) 9. `go mod tidy` 10. this commit message Once this piece of work is done, we can build a new version of `ooniprobe` that is using `internal/engine` directly. We need to do more work to ensure all the other functionality in `probe-engine` (e.g. making mobile packages) are still WAI. Part of https://github.com/ooni/probe/issues/1335
This commit is contained in:
parent
b1ce300c8d
commit
d57c78bc71
535 changed files with 66182 additions and 23 deletions
113
internal/engine/geolocate/iplookup.go
Normal file
113
internal/engine/geolocate/iplookup.go
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
package geolocate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/multierror"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrAllIPLookuppersFailed indicates that we failed with looking
|
||||
// up the probe IP for with all the lookuppers that we tried.
|
||||
ErrAllIPLookuppersFailed = errors.New("all IP lookuppers failed")
|
||||
|
||||
// ErrInvalidIPAddress indicates that the code returned to us a
|
||||
// string that actually isn't a valid IP address.
|
||||
ErrInvalidIPAddress = errors.New("lookupper did not return a valid IP")
|
||||
)
|
||||
|
||||
type lookupFunc func(
|
||||
ctx context.Context, client *http.Client,
|
||||
logger Logger, userAgent string,
|
||||
) (string, error)
|
||||
|
||||
type method struct {
|
||||
name string
|
||||
fn lookupFunc
|
||||
}
|
||||
|
||||
var (
|
||||
methods = []method{
|
||||
{
|
||||
name: "avast",
|
||||
fn: avastIPLookup,
|
||||
},
|
||||
{
|
||||
name: "ipconfig",
|
||||
fn: ipConfigIPLookup,
|
||||
},
|
||||
{
|
||||
name: "ipinfo",
|
||||
fn: ipInfoIPLookup,
|
||||
},
|
||||
{
|
||||
name: "stun_ekiga",
|
||||
fn: stunEkigaIPLookup,
|
||||
},
|
||||
{
|
||||
name: "stun_google",
|
||||
fn: stunGoogleIPLookup,
|
||||
},
|
||||
{
|
||||
name: "ubuntu",
|
||||
fn: ubuntuIPLookup,
|
||||
},
|
||||
}
|
||||
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
type ipLookupClient struct {
|
||||
// HTTPClient is the HTTP client to use
|
||||
HTTPClient *http.Client
|
||||
|
||||
// Logger is the logger to use
|
||||
Logger Logger
|
||||
|
||||
// UserAgent is the user agent to use
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
func makeSlice() []method {
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
ret := make([]method, len(methods))
|
||||
perm := r.Perm(len(methods))
|
||||
for idx, randIdx := range perm {
|
||||
ret[idx] = methods[randIdx]
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (c ipLookupClient) doWithCustomFunc(
|
||||
ctx context.Context, fn lookupFunc,
|
||||
) (string, error) {
|
||||
ip, err := fn(ctx, c.HTTPClient, c.Logger, c.UserAgent)
|
||||
if err != nil {
|
||||
return DefaultProbeIP, err
|
||||
}
|
||||
if net.ParseIP(ip) == nil {
|
||||
return DefaultProbeIP, fmt.Errorf("%w: %s", ErrInvalidIPAddress, ip)
|
||||
}
|
||||
c.Logger.Debugf("iplookup: IP: %s", ip)
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
func (c ipLookupClient) LookupProbeIP(ctx context.Context) (string, error) {
|
||||
union := multierror.New(ErrAllIPLookuppersFailed)
|
||||
for _, method := range makeSlice() {
|
||||
c.Logger.Debugf("iplookup: using %s", method.name)
|
||||
ip, err := c.doWithCustomFunc(ctx, method.fn)
|
||||
if err == nil {
|
||||
return ip, nil
|
||||
}
|
||||
union.Add(err)
|
||||
}
|
||||
return DefaultProbeIP, union
|
||||
}
|
||||
Loading…
Reference in a new issue