Commit 213e2d91 authored by Joanne Hugé's avatar Joanne Hugé

Remove geoip2

parent cbf3d89e
......@@ -249,6 +249,12 @@ class Cache(object):
return prefix, address
logging.warning('Buggy registry sent us our own address')
def removePeer(self, prefix):
logging.debug('Removing peer %s', prefix)
with self._db as db:
db.execute("DELETE FROM peer WHERE prefix=?",
(prefix,))
def addPeer(self, prefix, address, set_preferred=False):
logging.debug('Adding peer %s: %s', prefix, address)
with self._db:
......
......@@ -195,7 +195,6 @@ class BaseTunnelManager(object):
'babel_hmac_sign', 'encrypt',
'hello', 'ipv4', 'ipv4_sublen'))
_geoiplookup = None
_forward = None
_next_rina = True
......@@ -231,23 +230,10 @@ class BaseTunnelManager(object):
else:
del cache.my_address
db = os.getenv('GEOIP2_MMDB')
if db:
from geoip2 import database, errors
country = database.Reader(db).country
def geoiplookup(ip):
try:
return country(ip).country.iso_code.encode()
except errors.AddressNotFoundError:
return
self._geoiplookup = geoiplookup
if cache.same_country:
self._country = {}
address_dict = {family: self._updateCountry(address)
for family, address in address_dict.iteritems()}
elif cache.same_country:
sys.exit("Can not respect 'same_country' network configuration"
" (GEOIP2_MMDB not set)")
self._address = {family: utils.dump_address(address)
for family, address in address_dict.iteritems()
if address}
......@@ -465,6 +451,24 @@ class BaseTunnelManager(object):
elif code == 1: # address
if msg:
if peer:
# Ask registry for the country if we didn't get one
def updateMsg():
address_list = utils.parse_address(msg)
for a in address_list:
if len(a) > 3:
return msg
family, ip = resolve(*a[:3])
for ip in ip:
try:
country = self.cache._registry.getCountry(self._prefix, ip)
except (socket.error, subprocess.CalledProcessError, ValueError), e:
logging.warning('Failed to get country (%s)', ip)
else:
return utils.dump_address([a[:3] + (country,) for a in address_list])
msg = updateMsg()
if not msg:
return
self.cache.addPeer(peer, msg)
try:
self._connecting.remove(peer)
......@@ -895,9 +899,15 @@ class TunnelManager(BaseTunnelManager):
family, ip = resolve(*x[:3])
my_country = self._country.get(family, self._conf_country)
if my_country:
if len(x) <= 3:
self.cache.removePeer(prefix)
if self._disconnected:
return False
self.sendto(peer, '\1')
self._connecting.add(peer)
return True
for ip in ip:
# Use geoip if there is no country in the address
country = x[3] if len(x) > 3 else self._geoiplookup(ip)
country = x[3]
if country and (country != my_country
if my_country in same_country else
country in same_country):
......@@ -1040,9 +1050,10 @@ class TunnelManager(BaseTunnelManager):
logging.info("ignore route_up notification for %s %r",
common_name, tuple(self._connection_dict))
if self._ip_changed:
family, address = self._ip_changed(ip)
if address:
if self._geoiplookup or self._conf_country:
if self.cache.same_country:
address = self._updateCountry(address)
self._address[family] = utils.dump_address(address)
self.cache.my_address = ';'.join(self._address.itervalues())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment