[#2861] [Core] Switch to using python-geoip for geoip lookups

* libtorrent >= 1.1 dropped support for GeoIP so this adds support
   again using MaxMind GeoIP Legacy Python Extension API.
   For reference it is known by the following package names:
       * Maxmind: geoip-api-python
       * Linux: python-geoip
       * PyPi: GeoIP
This commit is contained in:
Calum Lind 2016-10-19 12:42:57 +01:00
parent d77666cd3e
commit ca7cbd291f
5 changed files with 30 additions and 29 deletions

View File

@ -1,5 +1,5 @@
=== Core === === Core ===
* libtorrent (rasterbar) >= 0.16.7 * libtorrent (rasterbar) >= 1.0.7
* python >= 2.6 * python >= 2.6
* setuptools * setuptools
* twisted >= 11.1 * twisted >= 11.1
@ -7,6 +7,7 @@
* pyxdg * pyxdg
* chardet * chardet
* gettext * gettext
* python-geoip (optional)
* geoip-database (optional) * geoip-database (optional)
* setproctitle (optional) * setproctitle (optional)
* pillow (optional) * pillow (optional)

View File

@ -96,6 +96,9 @@ class Core(component.Component):
self.external_ip = None self.external_ip = None
self.eventmanager.register_event_handler("ExternalIPEvent", self._on_external_ip_event) self.eventmanager.register_event_handler("ExternalIPEvent", self._on_external_ip_event)
# GeoIP instance with db loaded
self.geoip_instance = None
# Get the core config # Get the core config
self.config = ConfigManager("core.conf") self.config = ConfigManager("core.conf")
self.config.save() self.config.save()

View File

@ -21,6 +21,11 @@ import deluge.configmanager
from deluge._libtorrent import lt from deluge._libtorrent import lt
from deluge.event import ConfigValueChangedEvent from deluge.event import ConfigValueChangedEvent
try:
import GeoIP
except ImportError:
GeoIP = None
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
DEFAULT_PREFS = { DEFAULT_PREFS = {
@ -436,27 +441,19 @@ class PreferencesManager(component.Component):
log.debug("%s: %s", key, value) log.debug("%s: %s", key, value)
self.session_set_setting("anonymous_mode", value) self.session_set_setting("anonymous_mode", value)
def _on_set_geoip_db_location(self, key, value): def _on_set_geoip_db_location(self, key, geoip_db):
log.debug("%s: %s", key, value) log.debug("%s: %s", key, geoip_db)
# Load the GeoIP DB for country look-ups if available # Load the GeoIP DB for country look-ups if available
geoip_db = "" deluge_geoip_db = deluge.common.resource_filename("deluge", os.path.join("data", "GeoIP.dat"))
if os.path.exists(value): for geoip_path in (geoip_db, deluge_geoip_db):
geoip_db = value if os.path.exists(geoip_path):
elif os.path.exists(deluge.common.resource_filename("deluge", os.path.join("data", "GeoIP.dat"))): try:
geoip_db = deluge.common.resource_filename( self.core.geoip_instance = GeoIP.open(geoip_path, GeoIP.GEOIP_STANDARD)
"deluge", os.path.join("data", "GeoIP.dat") except AttributeError:
) log.warning("GeoIP Unavailable")
break
else: else:
log.warning("Unable to find GeoIP database file!") log.warning("Unable to find GeoIP database file: %s")
if geoip_db:
try:
self.session.load_country_db(str(geoip_db))
except RuntimeError as ex:
log.error("Unable to load geoip database!")
log.exception(ex)
except AttributeError:
log.warning("GeoIP Unavailable")
def _on_set_cache_size(self, key, value): def _on_set_cache_size(self, key, value):
log.debug("%s: %s", key, value) log.debug("%s: %s", key, value)

View File

@ -791,14 +791,14 @@ class Torrent(object):
if peer.flags & peer.connecting or peer.flags & peer.handshake: if peer.flags & peer.connecting or peer.flags & peer.handshake:
continue continue
client = decode_string(str(peer.client)) client = decode_string(peer.client)
# Make country a proper string
country = str() try:
for char in peer.country: country = component.get("Core").geoip_instance.country_code_by_addr(peer.ip[0])
if not char.isalpha(): except AttributeError:
country += " " country = ""
else: else:
country += char country = "".join([char if char.isalpha() else " " for char in country])
ret.append({ ret.append({
"client": client, "client": client,

View File

@ -214,7 +214,7 @@ class PeersTab(Tab):
component.get("SessionProxy").get_torrent_status(torrent_id, ["peers"]).addCallback(self._on_get_torrent_status) component.get("SessionProxy").get_torrent_status(torrent_id, ["peers"]).addCallback(self._on_get_torrent_status)
def get_flag_pixbuf(self, country): def get_flag_pixbuf(self, country):
if country == " ": if not country.strip():
return None return None
if country not in self.cached_flag_pixbufs: if country not in self.cached_flag_pixbufs: