Commit 61ca38b5 authored by Julien Muchembled's avatar Julien Muchembled

re6st-geo: quick'n dirty update to at least in which countries nodes are spread

parent 111a5e4d
...@@ -7,9 +7,20 @@ if 're6st' not in sys.modules: ...@@ -7,9 +7,20 @@ if 're6st' not in sys.modules:
import os; sys.path[0] = os.path.dirname(sys.path[0]) import os; sys.path[0] = os.path.dirname(sys.path[0])
from re6st import ctl, tunnel, utils from re6st import ctl, tunnel, utils
from re6st.registry import RegistryServer
@apply
class proxy(object):
def __init__(self):
self.sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
recv = RegistryServer.recv.__func__
sendto = RegistryServer.sendto.__func__
def cmd_update(db, config): def cmd_update(db, config):
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) s = proxy.sock,
q = db.execute q = db.execute
ip, n = config.network.split('/') ip, n = config.network.split('/')
network = utils.binFromIp(ip)[:int(n)] network = utils.binFromIp(ip)[:int(n)]
...@@ -29,16 +40,9 @@ def cmd_update(db, config): ...@@ -29,16 +40,9 @@ def cmd_update(db, config):
break break
p = peers.copy() p = peers.copy()
while True: while True:
r, w, _ = select.select([s], [s] if p else [], [], 1) r, w, _ = select.select(s, s if p else (), (), 1)
if r: if r:
x, a = s.recvfrom(1<<16) prefix, address = proxy.recv(1)
if x[0] == '\1':
try:
prefix, address = x[1:x.index('\n')].split()
except ValueError:
pass
else:
if utils.binFromIp(a[0]).startswith(network + prefix):
peers.discard(prefix) peers.discard(prefix)
ip = None ip = None
for ip, _, _ in utils.parse_address(address): for ip, _, _ in utils.parse_address(address):
...@@ -51,8 +55,7 @@ def cmd_update(db, config): ...@@ -51,8 +55,7 @@ def cmd_update(db, config):
except socket.error: except socket.error:
pass pass
else: else:
if bin(*struct.unpack('>I', a))[2:] \ if bin(*struct.unpack('>I', a))[2:].startswith((
.startswith((
'10100000', '11111110', '10100000', '11111110',
'101011000001', '101011000001',
'1100000010101000')): '1100000010101000')):
...@@ -61,11 +64,7 @@ def cmd_update(db, config): ...@@ -61,11 +64,7 @@ def cmd_update(db, config):
q("UPDATE ip SET ip=? WHERE prefix=?", q("UPDATE ip SET ip=? WHERE prefix=?",
(ip, prefix)) (ip, prefix))
if w: if w:
x = utils.ipFromBin(network + p.pop()) proxy.sendto(p.pop(), 1)
try:
s.sendto('\2', (x, tunnel.PORT))
except socket.error:
pass
elif not r: elif not r:
break break
db.commit() db.commit()
...@@ -84,6 +83,15 @@ def cmd_ip(db, config): ...@@ -84,6 +83,15 @@ def cmd_ip(db, config):
db.commit() db.commit()
def geo_geolite2():
from geoip2 import database, errors
country = database.Reader(os.environ['GEOIP2_MMDB']).country
def geo(ip):
x = country(ip).country
return None, None, '%s, %s' % (x.iso_code, x.name)
return geo
def geo_freegeoip(): def geo_freegeoip():
import json import json
host = 'freegeoip.net' host = 'freegeoip.net'
...@@ -133,7 +141,7 @@ def cmd_geoip(db, config): ...@@ -133,7 +141,7 @@ def cmd_geoip(db, config):
loc = cache_dict[ip] loc = cache_dict[ip]
except KeyError: except KeyError:
if mode in (None, 'auto'): if mode in (None, 'auto'):
mode = 'freegeoip' mode = 'geolite2'
try: try:
geo = mode_dict[mode] geo = mode_dict[mode]
except KeyError: except KeyError:
...@@ -212,7 +220,8 @@ def main(): ...@@ -212,7 +220,8 @@ def main():
help="Query all running nodes to fetch their tunnel IP." help="Query all running nodes to fetch their tunnel IP."
" CN marked for manual update with 'ip' subcommand are skipped." " CN marked for manual update with 'ip' subcommand are skipped."
).add_argument ).add_argument
_('--control-socket', metavar='CTL_SOCK', default=ctl.SOCK_PATH, _('--control-socket', metavar='CTL_SOCK',
default='/var/run/re6stnet/babeld.sock',
help="Socket path to use for communication between re6stnet and babeld" help="Socket path to use for communication between re6stnet and babeld"
" (option -R of Babel).") " (option -R of Babel).")
_('network') _('network')
......
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