Skip to content

Commit

Permalink
Merge pull request getlantern#2923 from getlantern/issue2922
Browse files Browse the repository at this point in the history
Override the IP address sent to analytics closes #2922
  • Loading branch information
uaalto committed Aug 13, 2015
2 parents ebff00b + bff1112 commit e573bb4
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 25 deletions.
11 changes: 10 additions & 1 deletion src/github.com/getlantern/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
var (
log = golog.LoggerFor("analytics")
httpClient *http.Client
ip string
)

type HitType string
Expand Down Expand Up @@ -69,7 +70,8 @@ type Payload struct {
Event *Event
}

func Configure(trackingId string, version string, proxyAddr string) {
func Configure(addr string, trackingId string, version string, proxyAddr string) {
ip = addr
var err error
go func() {
httpClient, err = util.HTTPClient("", proxyAddr)
Expand All @@ -92,6 +94,13 @@ func collectArgs(payload *Payload) string {

// Add default payload
vals.Add("v", ProtocolVersion)

// Override the users IP so we get accurate geo data.
vals.Add("uip", ip)

// Make call to anonymize the user's IP address.
vals.Add("aip", "1")

if payload.ClientVersion != "" {
vals.Add("_v", payload.ClientVersion)
}
Expand Down
14 changes: 9 additions & 5 deletions src/github.com/getlantern/flashlight/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/getlantern/flashlight/config"
"github.com/getlantern/flashlight/pubsub"
"github.com/mitchellh/mapstructure"

"github.com/getlantern/analytics"
Expand All @@ -27,12 +28,15 @@ var (
func Configure(cfg *config.Config, version string) {

if cfg.AutoReport != nil && *cfg.AutoReport {
analytics.Configure(TrackingId, version, cfg.Addr)
pubsub.Sub(pubsub.IP, func(ip string) {
log.Debugf("Got IP %v -- starting analytics", ip)
analytics.Configure(ip, TrackingId, version, cfg.Addr)

err := StartService()
if err != nil {
log.Errorf("Error starting analytics service: %q", err)
}
err := StartService()
if err != nil {
log.Errorf("Error starting analytics service: %q", err)
}
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/github.com/getlantern/flashlight/flashlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func applyClientConfig(client *client.Client, cfg *config.Config) {
// Update client configuration and get the highest QOS dialer available.
hqfd := client.Configure(cfg.Client)
if hqfd == nil {
log.Errorf("No fronted dialer available, not enabling geolocation, stats or analytics")
log.Errorf("No fronted dialer available, not enabling geolocation, config lookup, or stats")
} else {
// An *http.Client that uses the highest QOS dialer.
hqfdClient := hqfd.NewDirectDomainFronter()
Expand Down
4 changes: 2 additions & 2 deletions src/github.com/getlantern/flashlight/geolookup/geolookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ func write() {
wait := time.Duration(basePublishSeconds-publishSecondsVariance/2+n) * time.Second

oldLocation := GetLocation()
newLocation, err := geolookup.LookupIPWithClient("", client.Load().(*http.Client))
newLocation, ip, err := geolookup.LookupIPWithClient("", client.Load().(*http.Client))
if err == nil {
consecutiveFailures = 0
if !reflect.DeepEqual(newLocation, oldLocation) {
log.Debugf("Location changed")
location.Store(newLocation)
pubsub.Pub(pubsub.Location, newLocation)
}
// Always publish location, even if unchanged
pubsub.Pub(pubsub.IP, ip)
service.Out <- newLocation
} else {
msg := fmt.Sprintf("Unable to get current location: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion src/github.com/getlantern/flashlight/pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
// The constant topics to publish and subscribe to. All topics should
// be defined here directly.
const (
Location = iota
IP = iota
// Someothertopic = iota
)

Expand Down
8 changes: 4 additions & 4 deletions src/github.com/getlantern/flashlight/pubsub/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ type str struct {
}

func TestSub(t *testing.T) {
if Sub(Location, func(teststr *str) {}) != nil {
if Sub(IP, func(teststr *str) {}) != nil {
t.Fail()
}

// Make sure we get an error if we don't pass a func
if Sub(Location, "String") == nil {
if Sub(IP, "String") == nil {
t.Fail()
}
}

func TestPublish(t *testing.T) {
msgs := make(chan string)
err := Sub(Location, func(s string) {
err := Sub(IP, func(s string) {
msgs <- s
})
if err != nil {
t.Fatalf("Unable to subscribe: %v", err)
}

Pub(Location, "test")
Pub(IP, "test")

msg := <-msgs
if msg != "test" {
Expand Down
2 changes: 1 addition & 1 deletion src/github.com/getlantern/flashlight/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (server *Server) checkForBannedCountry(req *http.Request) error {
country = cachedCountry.(string)
} else {
log.Tracef("Country for %v not cached, perform geolookup", clientIp)
city, err := geolookup.LookupIPWithClient(clientIp, nil)
city, _, err := geolookup.LookupIPWithClient(clientIp, nil)
if err != nil {
log.Debugf("Unable to perform geolookup for ip %v: %v", clientIp, err)
return nil
Expand Down
2 changes: 1 addition & 1 deletion src/github.com/getlantern/flashlight/statserver/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (peer *Peer) geolocate() error {
}

func (peer *Peer) doGeolocate() error {
geodata, err := geolookup.LookupIPWithClient(peer.IP, geoClient.Load().(*http.Client))
geodata, _, err := geolookup.LookupIPWithClient(peer.IP, geoClient.Load().(*http.Client))

if err != nil {
return err
Expand Down
14 changes: 8 additions & 6 deletions src/github.com/getlantern/geolookup/geolookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type Country struct {
// LookupIPWithClient looks up the given IP using a geolocation service and returns a
// City struct. If an httpClient was provided, it uses that, otherwise it uses
// a default http.Client.
func LookupIPWithClient(ipAddr string, httpClient *http.Client) (*City, error) {
func LookupIPWithClient(ipAddr string, httpClient *http.Client) (*City, string, error) {
if httpClient == nil {
log.Trace("Using default http.Client")
httpClient = defaultHttpClient
Expand All @@ -116,11 +116,11 @@ func LookupIPWithClient(ipAddr string, httpClient *http.Client) (*City, error) {
lookupURL := fmt.Sprintf(geoServeEndpoint, ipAddr)

if req, err = http.NewRequest("GET", lookupURL, nil); err != nil {
return nil, fmt.Errorf("Could not create request: %q", err)
return nil, "", fmt.Errorf("Could not create request: %q", err)
}

if resp, err = httpClient.Do(req); err != nil {
return nil, fmt.Errorf("Could not get response from server: %q", err)
return nil, "", fmt.Errorf("Could not get response from server: %q", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
Expand All @@ -134,15 +134,17 @@ func LookupIPWithClient(ipAddr string, httpClient *http.Client) (*City, error) {
if err == nil {
body = string(b)
}
return nil, fmt.Errorf("Unexpected response status %d: %v", resp.StatusCode, body)
return nil, "", fmt.Errorf("Unexpected response status %d: %v", resp.StatusCode, body)
}

ip := resp.Header.Get("X-Reflected-Ip")

decoder := json.NewDecoder(resp.Body)

city := &City{}
if err = decoder.Decode(city); err != nil {
return nil, err
return nil, ip, err
}

return city, nil
return city, ip, nil
}
4 changes: 2 additions & 2 deletions src/github.com/getlantern/geolookup/geolookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestCityLookup(t *testing.T) {
city, err := LookupIPWithClient("198.199.72.101", nil)
city, _, err := LookupIPWithClient("198.199.72.101", nil)
if assert.NoError(t, err) {
assert.Equal(t, "New York", city.City.Names["en"])
}
Expand All @@ -26,6 +26,6 @@ func TestNonDefaultClient(t *testing.T) {
},
}

_, err := LookupIPWithClient("", client)
_, _, err := LookupIPWithClient("", client)
assert.Error(t, err, "Using bad client should have resulted in error")
}
2 changes: 1 addition & 1 deletion src/github.com/getlantern/lantern-mobile/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (client *mobileClient) serveHTTP() {
go func() {
onListening := func() {
log.Debugf("Now listening for connections...")
analytics.Configure(trackingCodes["FireTweet"], "", client.Client.Addr)
analytics.Configure("", trackingCodes["FireTweet"], "", client.Client.Addr)
logging.Configure(client.Client.Addr, cloudConfigCA, instanceId, version, revisionDate)
}

Expand Down
1 change: 1 addition & 0 deletions testpackages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ github.com/getlantern/flashlight/logging
github.com/getlantern/flashlight/pubsub
github.com/getlantern/flashlight/statreporter
github.com/getlantern/fronted
github.com/getlantern/geolookup
github.com/getlantern/golog
github.com/getlantern/idletiming
github.com/getlantern/keyman
Expand Down

0 comments on commit e573bb4

Please sign in to comment.