Geolocation in Lantern finds wrong location for some ip addresses
In Lantern, a certain IP address that's actually in India comes back as being located in China (as Ox for the address if you need it).
I also tested this manually by using GeoIpLookupService directly.
Thinking that maybe we had an out of date database, I downloaded the latest GeoLite City file from here - http://dev.maxmind.com/geoip/legacy/geolite/. I then ran it through our compressor and repeated the test, with the same result.
If you put the same address into GeoLite's online demo site, you get back an address in India.
Having a discrepancy is not totally unexpected, as the accuracy for GeoLite City, especially in some countries, is actually quite poor. Interestingly, the paid version of GeoIP City has better accuracy, but it's still quite bad. For example, in India, even with the paid database, 38% of addresses will either not resolve, or will resolve incorrectly.
Wanting to see how GeoLite's own API did with this, I tested the same IP address with the GeoLite Java API. This resulted in not finding a location at all.
From this, I conclude the following:
- The primary effect we're seeing here is that GeoLite City's accuracy is very poor in India.
- The secondary effect we're seeing here is that our compression algorithm is actually altering GeoLite's database in a way that, rather than returning an unknown value, returns a wildly incorrect value.
If we actually want this thing to work reliably for all users, I recommend that we:
- Find a service that is reliable (if such a thing exists)
- Don't implement our own compression schemes, as these run the risk of breaking the database
I think we should strongly consider this option - #1106. I'll note that the country database is supposedly quite accurate at finding the right country at least, which is better than we're doing right now.
As suggested in #1106, we could replace the city db we're currently shipping with the country db, and offer users the option to download the city db as a Lantern Pluggable Module for a more accurate map visualization once they get up and running. This would save us from having to run our own centralized service or rely on another company's. This would also allow us to detect discrepancies between the country reported by the two local db's and just prefer the one from the more accurate db. Finally, formalizing a Lantern Pluggable Module system a bit could be a nice abstraction for offering and keeping up-to-date other optional or required functionality we might want to provide as separate modules.
Assuming that the optional download is GeoIp's own database (rather than our own compressed version), I think that would help with the proximate issue.
I'd actually be a little surprised if the discrepancy between our version and the latest geo IP java version is related to the compression scheme. Certainly could be, and I think the changes at this point are buried in emails, but it would surprise me because most of the stripped data was just unnecessary and the primary changes were in the compression scheme itself.
That said, I personally think we should ditch it just due to size constraints. We could simply use the service at http://www.maxmind.com/en/geoip_demo and pay for it for now if it does really have better results as it claims. It still won't be great for India, but it will be better than anything else and won't cause a hit with our download size. I think we should got back to using the country-level db for determining censored status and should use the maxmind city level API for everything else for the time being. We also might be able to convince them to give us free service.
The only downside I can see is privacy, but I doubt censored regions would have any sort of subpoena power over MaxMind regardless.
-Adam
On Fri, Nov 8, 2013 at 10:59 AM, oxtoacart [email protected] wrote:
Assuming that the optional download is GeoIp's own database (rather than our own compressed version), I think that would help with the proximate issue.
— Reply to this email directly or view it on GitHubhttps://github.com/getlantern/lantern/issues/1125#issuecomment-28088129 .
What about no https?
On Saturday, November 9, 2013, myleshorton wrote:
I'd actually be a little surprised if the discrepancy between our version and the latest geo IP java version is related to the compression scheme. Certainly could be, and I think the changes at this point are buried in emails, but it would surprise me because most of the stripped data was just unnecessary and the primary changes were in the compression scheme itself.
That said, I personally think we should ditch it just due to size constraints. We could simply use the service at http://www.maxmind.com/en/geoip_demo and pay for it for now if it does really have better results as it claims. It still won't be great for India, but it will be better than anything else and won't cause a hit with our download size. I think we should got back to using the country-level db for determining censored status and should use the maxmind city level API for everything else for the time being. We also might be able to convince them to give us free service.
The only downside I can see is privacy, but I doubt censored regions would have any sort of subpoena power over MaxMind regardless.
-Adam
On Fri, Nov 8, 2013 at 10:59 AM, oxtoacart <[email protected]<javascript:_e({}, 'cvml', '[email protected]');>> wrote:
Assuming that the optional download is GeoIp's own database (rather than our own compressed version), I think that would help with the proximate issue.
— Reply to this email directly or view it on GitHub< https://github.com/getlantern/lantern/issues/1125#issuecomment-28088129> .
— Reply to this email directly or view it on GitHubhttps://github.com/getlantern/lantern/issues/1125#issuecomment-28121211 .
Is the maxmind API no https?
On Friday, November 8, 2013, skivvies wrote:
What about no https?
On Saturday, November 9, 2013, myleshorton wrote:
I'd actually be a little surprised if the discrepancy between our version and the latest geo IP java version is related to the compression scheme. Certainly could be, and I think the changes at this point are buried in emails, but it would surprise me because most of the stripped data was just unnecessary and the primary changes were in the compression scheme itself.
That said, I personally think we should ditch it just due to size constraints. We could simply use the service at http://www.maxmind.com/en/geoip_demo and pay for it for now if it does really have better results as it claims. It still won't be great for India, but it will be better than anything else and won't cause a hit with our download size. I think we should got back to using the country-level db for determining censored status and should use the maxmind city level API for everything else for the time being. We also might be able to convince them to give us free service.
The only downside I can see is privacy, but I doubt censored regions would have any sort of subpoena power over MaxMind regardless.
-Adam
On Fri, Nov 8, 2013 at 10:59 AM, oxtoacart <[email protected]<javascript:_e({}, 'cvml', '[email protected]');><javascript:_e({}, 'cvml', '[email protected] <javascript:_e({}, 'cvml', '[email protected]');>');>> wrote:
Assuming that the optional download is GeoIp's own database (rather than our own compressed version), I think that would help with the proximate issue.
— Reply to this email directly or view it on GitHub< https://github.com/getlantern/lantern/issues/1125#issuecomment-28088129>
.
— Reply to this email directly or view it on GitHub< https://github.com/getlantern/lantern/issues/1125#issuecomment-28121211> .
— Reply to this email directly or view it on GitHubhttps://github.com/getlantern/lantern/issues/1125#issuecomment-28122041 .
Just because I wanted to understand how close we are to MaxMind's results, I created a little test program to compare our results with MaxMind's. I downloaded the most recent GeoCity Lite database in both binary and csv format. I converted the CSV into our format using the converter. Then I added a dependency to our pom to pull in the MaxMind api, and added a main method to our GeoIpLookupService to run the test. These are the results of one run:
8241 out of 10000 iterations matched
--------- Mismatched Countries ------------
null : ID
null : IE
null : BR
null : MU
null : MX
null : TO
null : CA
null : DE
null : JP
null : PA
null : HN
null : NL
null : ZA
null : NO
A2 : O1
null : SC
null : US
null : NZ
null : RU
null : CH
null : IR
null : CI
null : IT
null : IN
null : CN
null : CO
null : AU
null : AR
The exact number of matches fluctuates a little, but it's always slightly over 8000. You'll notice that other than the weird "A2 : 01" case, all the mismatches are due to us finding a country when MaxMind says it couldn't. This effectively means that for a random distribution of users, up to 18% of the time we're going to be showing them in the wrong country.
The severity of this discrepancy would mean that:
- MaxMind is really horrible at keeping their CSV and Binary files in sync or
- There's something wrong with my test or
- There's something wrong with our algorithm that causes us to think we found a match when in fact there wasn't one to be had.
To convince myself that our algorithm is broken, I tried geolocating 0.0.0.0 and 255.255.255.255. They came back as JP and AU respectively.
My test code is below:
public static void main(String[] args) throws Exception {
InetAddressValidator validator = new InetAddressValidator();
LookupService mm = new LookupService(
"/Users/ox.to.a.cart/Downloads/GeoLiteCity.dat",
LookupService.GEOIP_MEMORY_CACHE);
GeoIpLookupService lantern = new GeoIpLookupService(true);
Random random = new SecureRandom();
int numberOfIterations = 0;
int numberOfMatches = 0;
Set<String> mismatchedCountries = new HashSet<String>();
for (int i = 0; i < 10000; i++) {
String ip = String.format("%1$s.%2$s.%3$s.%4$s",
random.nextInt(256),
random.nextInt(256),
random.nextInt(256),
random.nextInt(256));
boolean ipValid = false;
try {
InetAddress.getByName(ip);
ipValid = true;
} catch (UnknownHostException uhe) {
// ignore
}
if (ipValid) {
Location location = mm.getLocation(ip);
String maxMindCountry = location != null ? location.countryCode
: null;
GeoData geoData = lantern.getGeoData(ip);
String lanternCountry = geoData != null ? geoData
.getCountrycode()
: null;
numberOfIterations += 1;
if (maxMindCountry == null && lanternCountry == null) {
numberOfMatches += 1;
} else if (maxMindCountry != null
&& maxMindCountry.equals(lanternCountry)) {
numberOfMatches += 1;
} else {
mismatchedCountries.add(String.format("%1$s : %2$s",
maxMindCountry, lanternCountry));
}
}
}
System.out.println(String.format("%1$s out of %2$s iterations matched",
numberOfMatches, numberOfIterations));
System.out.println("\n--------- Mismatched Countries ------------");
for (String country : mismatchedCountries) {
System.out.println(country);
}
}
The demo seems to be available at https: https://www.maxmind.com/en/geoip_demo
Sweet, maybe they added https since last we checked then?
On Saturday, November 9, 2013, oxtoacart wrote:
The demo seems to be available at https: https://www.maxmind.com/en/geoip_demo
— Reply to this email directly or view it on GitHubhttps://github.com/getlantern/lantern/issues/1125#issuecomment-28127393 .
This references an old version of the geolocation stuff, so closing. :)