57a3919d2a
This change ensures that, in turn, we're able to "remote" all the traffic generated by the `geolocate` package, rather than missing some bits of it that were still using the standard library and caused _some_ geolocations to geolocate as the local host rather than as the remote host. Extracted from https://github.com/ooni/probe-cli/pull/969, where we tested this functionality. Closes https://github.com/ooni/probe/issues/1383 (which was long overdue). Part of https://github.com/ooni/probe/issues/2340, because it allows us to make progress with that.
111 lines
2.5 KiB
Go
111 lines
2.5 KiB
Go
package geolocate
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
|
"github.com/pion/stun"
|
|
)
|
|
|
|
type stunClient interface {
|
|
Close() error
|
|
Start(m *stun.Message, h stun.Handler) error
|
|
}
|
|
|
|
type stunConfig struct {
|
|
Dialer model.Dialer // optional
|
|
Endpoint string
|
|
Logger model.Logger
|
|
NewClient func(conn net.Conn) (stunClient, error) // optional
|
|
Resolver model.Resolver
|
|
}
|
|
|
|
func stunNewClient(conn net.Conn) (stunClient, error) {
|
|
return stun.NewClient(conn)
|
|
}
|
|
|
|
func stunIPLookup(ctx context.Context, config stunConfig) (string, error) {
|
|
config.Logger.Debugf("STUNIPLookup: start using %s", config.Endpoint)
|
|
ip, err := func() (string, error) {
|
|
dialer := config.Dialer
|
|
if dialer == nil {
|
|
dialer = netxlite.NewDialerWithResolver(config.Logger, config.Resolver)
|
|
}
|
|
conn, err := dialer.DialContext(ctx, "udp", config.Endpoint)
|
|
if err != nil {
|
|
return model.DefaultProbeIP, err
|
|
}
|
|
newClient := config.NewClient
|
|
if newClient == nil {
|
|
newClient = stunNewClient
|
|
}
|
|
clnt, err := newClient(conn)
|
|
if err != nil {
|
|
conn.Close()
|
|
return model.DefaultProbeIP, err
|
|
}
|
|
defer clnt.Close()
|
|
message := stun.MustBuild(stun.TransactionID, stun.BindingRequest)
|
|
errch, ipch := make(chan error, 1), make(chan string, 1)
|
|
err = clnt.Start(message, func(ev stun.Event) {
|
|
if ev.Error != nil {
|
|
errch <- ev.Error
|
|
return
|
|
}
|
|
var xorAddr stun.XORMappedAddress
|
|
if err := xorAddr.GetFrom(ev.Message); err != nil {
|
|
errch <- err
|
|
return
|
|
}
|
|
ipch <- xorAddr.IP.String()
|
|
})
|
|
if err != nil {
|
|
return model.DefaultProbeIP, err
|
|
}
|
|
select {
|
|
case err := <-errch:
|
|
return model.DefaultProbeIP, err
|
|
case ip := <-ipch:
|
|
return ip, nil
|
|
case <-ctx.Done():
|
|
return model.DefaultProbeIP, ctx.Err()
|
|
}
|
|
}()
|
|
if err != nil {
|
|
config.Logger.Debugf("STUNIPLookup: failure using %s: %+v", config.Endpoint, err)
|
|
return model.DefaultProbeIP, err
|
|
}
|
|
return ip, nil
|
|
}
|
|
|
|
func stunEkigaIPLookup(
|
|
ctx context.Context,
|
|
httpClient *http.Client,
|
|
logger model.Logger,
|
|
userAgent string,
|
|
resolver model.Resolver,
|
|
) (string, error) {
|
|
return stunIPLookup(ctx, stunConfig{
|
|
Endpoint: "stun.ekiga.net:3478",
|
|
Logger: logger,
|
|
Resolver: resolver,
|
|
})
|
|
}
|
|
|
|
func stunGoogleIPLookup(
|
|
ctx context.Context,
|
|
httpClient *http.Client,
|
|
logger model.Logger,
|
|
userAgent string,
|
|
resolver model.Resolver,
|
|
) (string, error) {
|
|
return stunIPLookup(ctx, stunConfig{
|
|
Endpoint: "stun.l.google.com:19302",
|
|
Logger: logger,
|
|
Resolver: resolver,
|
|
})
|
|
}
|