[tor-commits] [ooni-probe/master] Make GeoIP AS and GeoIP City soft dependencies.

art at torproject.org art at torproject.org
Thu Feb 20 12:10:13 UTC 2014


commit 0daffadb44d61fcf3eb31f7e7192cbbc46f52c33
Author: Arturo Filastò <art at fuffa.org>
Date:   Thu Feb 20 12:02:48 2014 +0000

    Make GeoIP AS and GeoIP City soft dependencies.
    
    Allows ooniprobe to report back the country of the probe even if no GeoIP ASN
    and City database is present.
---
 ooni/geoip.py   |   35 ++++++++++++++++++-----------------
 ooni/nettest.py |   28 +++++++++++-----------------
 2 files changed, 29 insertions(+), 34 deletions(-)

diff --git a/ooni/geoip.py b/ooni/geoip.py
index a97a6f5..2792126 100644
--- a/ooni/geoip.py
+++ b/ooni/geoip.py
@@ -30,30 +30,31 @@ def IPToLocation(ipaddr):
     country_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIP.dat')
     asn_file = os.path.join(config.advanced.geoip_data_dir, 'GeoIPASNum.dat')
 
-    location = {'city': None, 'countrycode': None, 'asn': None}
-    try:
-        city_dat = GeoIP(city_file)
-        try:
-            location['city'] = city_dat.record_by_addr(ipaddr)['city']
-        except TypeError:
-            location['city'] = None
+    location = {'city': None, 'countrycode': 'ZZ', 'asn': 'AS0'}
     
+    try:
         country_dat = GeoIP(country_file)
         location['countrycode'] = country_dat.country_code_by_addr(ipaddr)
         if not location['countrycode']:
             location['countrycode'] = 'ZZ'
-
-        asn_dat = GeoIP(asn_file)
-        try:
-            location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0]
-        except AttributeError:
-            location['asn'] = 'AS0'
-
     except IOError:
-        log.err("Could not find GeoIP data files. Go into %s "
-                "and run make geoip or change the geoip_data_dir "
+        log.err("Could not find GeoIP data file. Go into %s "
+                "and make sure GeoIP.dat is present or change the location "
                 "in the config file" % config.advanced.geoip_data_dir)
-        raise GeoIPDataFilesNotFound
+    try:
+        city_dat = GeoIP(city_file)
+        location['city'] = city_dat.record_by_addr(ipaddr)['city']
+    except:
+         log.err("Could not find the city your IP is from. "
+                "Download the GeoLiteCity.dat file into the geoip_data_dir"
+                " or install geoip-database-contrib.")
+    try:
+        asn_dat = GeoIP(asn_file)
+        location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0]
+    except:
+        log.err("Could not find the ASN for your IP. "
+                "Download the GeoIPASNum.dat file into the geoip_data_dir"
+                " or install geoip-database-contrib.")
     
     return location
 
diff --git a/ooni/nettest.py b/ooni/nettest.py
index 526de5d..1780953 100644
--- a/ooni/nettest.py
+++ b/ooni/nettest.py
@@ -238,17 +238,19 @@ class NetTestLoader(object):
     def testDetails(self):
         from ooni import __version__ as software_version
 
-        client_geodata = {}
+        client_geodata = {
+                'city': None, 
+                'countrycode': 'ZZ',
+                'asn': 'AS0',
+                'ip': '127.0.0.1'
+        }
+
         if config.probe_ip.address and (config.privacy.includeip or \
                 config.privacy.includeasn or \
                 config.privacy.includecountry or \
                 config.privacy.includecity):
             log.msg("We will include some geo data in the report")
-            try:
-                client_geodata = geoip.IPToLocation(config.probe_ip.address)
-            except e.GeoIPDataFilesNotFound:
-                log.err("Unable to find the geoip data files")
-                client_geodata = {'city': None, 'countrycode': None, 'asn': None}
+            client_geodata = geoip.IPToLocation(config.probe_ip.address)
 
         if config.privacy.includeip:
             client_geodata['ip'] = config.probe_ip.address
@@ -257,21 +259,13 @@ class NetTestLoader(object):
 
         # Here we unset all the client geodata if the option to not include then
         # has been specified
-        if client_geodata and not config.privacy.includeasn:
+        if not config.privacy.includeasn:
             client_geodata['asn'] = 'AS0'
-        elif 'asn' in client_geodata:
-            # XXX this regexp should probably go inside of geodata
-            client_geodata['asn'] = client_geodata['asn']
-            log.msg("Your AS number is: %s" % client_geodata['asn'])
-        else:
-            client_geodata['asn'] = None
 
-        if (client_geodata and not config.privacy.includecity) \
-                or ('city' not in client_geodata):
+        if not config.privacy.includecity:
             client_geodata['city'] = None
 
-        if (client_geodata and not config.privacy.includecountry) \
-                or ('countrycode' not in client_geodata):
+        if not config.privacy.includecountry:
             client_geodata['countrycode'] = None
         
         input_file_hashes = []





More information about the tor-commits mailing list