summaryrefslogtreecommitdiff
path: root/src/leap/util
diff options
context:
space:
mode:
authorkali <kali@leap.se>2013-01-17 08:07:45 +0900
committerkali <kali@leap.se>2013-01-17 08:07:45 +0900
commit6fb952397573f4bc90f4cd9e72b49fcf6256e95c (patch)
tree9e3def6f94884a0517022f61874d1a31f0d1938b /src/leap/util
parent97f4324be1be58e7d0c38da8bdc6474af1aae78f (diff)
localize exit country if we can
only if we can find the geoip database, which comes with geoip-database in debian. we will have to think more about this in the future but it's nice to have now for testing.
Diffstat (limited to 'src/leap/util')
-rw-r--r--src/leap/util/__init__.py9
-rw-r--r--src/leap/util/geo.py32
2 files changed, 41 insertions, 0 deletions
diff --git a/src/leap/util/__init__.py b/src/leap/util/__init__.py
index e69de29b..a70a9a8b 100644
--- a/src/leap/util/__init__.py
+++ b/src/leap/util/__init__.py
@@ -0,0 +1,9 @@
+import logging
+logger = logging.getLogger(__name__)
+
+try:
+ import pygeoip
+ HAS_GEOIP = True
+except ImportError:
+ logger.debug('PyGeoIP not found. Disabled Geo support.')
+ HAS_GEOIP = False
diff --git a/src/leap/util/geo.py b/src/leap/util/geo.py
new file mode 100644
index 00000000..54b29596
--- /dev/null
+++ b/src/leap/util/geo.py
@@ -0,0 +1,32 @@
+"""
+experimental geo support.
+not yet a feature.
+in debian, we rely on the (optional) geoip-database
+"""
+import os
+import platform
+
+from leap.util import HAS_GEOIP
+
+GEOIP = None
+
+if HAS_GEOIP:
+ import pygeoip # we know we can :)
+
+ GEOIP_PATH = None
+
+ if platform.system() == "Linux":
+ PATH = "/usr/share/GeoIP/GeoIP.dat"
+ if os.path.isfile(PATH):
+ GEOIP_PATH = PATH
+ GEOIP = pygeoip.GeoIP(GEOIP_PATH, pygeoip.MEMORY_CACHE)
+
+
+def get_country_name(ip):
+ if not GEOIP:
+ return
+ try:
+ country = GEOIP.country_name_by_addr(ip)
+ except pygeoip.GeoIPError:
+ country = None
+ return country if country else "-"