Commit f475f2ca authored by Guillaume Bury's avatar Guillaume Bury

Proper db refresh & bootstrap

parent 0ee76b06
...@@ -137,7 +137,7 @@ OPTIONS : VIFIBNET.PY ...@@ -137,7 +137,7 @@ OPTIONS : VIFIBNET.PY
Default : 0 Default : 0
--registry address --registry address
Complete publi ( reachable from the internet ) address of the machine Complete public ( reachable from the internet ) address of the machine
running a registry. Will be used to get the pirvate address of the running a registry. Will be used to get the pirvate address of the
registry and/or bootstrap peers registry and/or bootstrap peers
......
import sqlite3, socket, xmlrpclib, time, os import sqlite3, socket, subprocess, xmlrpclib, time, os
import utils import utils
class PeerManager: class PeerManager:
# internal ip = temp arg/attribute # internal ip = temp arg/attribute
...@@ -41,38 +40,16 @@ class PeerManager: ...@@ -41,38 +40,16 @@ class PeerManager:
name text primary key, name text primary key,
value text)""") value text)""")
try: try:
a = self._db.execute("SELECT value FROM config WHERE name='registry'").next() a, = self._db.execute("SELECT value FROM config WHERE name='registry'").next()
except StopIteration: except StopIteration:
proxy = xmlrpclib.ServerProxy(registry) proxy = xmlrpclib.ServerProxy(registry)
a = proxy.getPrivateAddress() a = proxy.getPrivateAddress()
self._db.execute("INSERT INTO config VALUES ('registry',?)", a) self._db.execute("INSERT INTO config VALUES ('registry',?)", (a,))
self._proxy = xmlrpclib.ServerProxy(a) self._proxy = xmlrpclib.ServerProxy(a)
utils.log('Database prepared', 5) utils.log('Database prepared', 5)
self.next_refresh = time.time() self.next_refresh = time.time()
def _boot(self):
utils.log('Getting Boot peer...', 3)
try:
utils.log("Contacting registry's private address", 5)
bootpeer = self._proxy.getBootstrapPeer(self._prefix).data
except socket.error, e:
utils.log("""Registry's private address unreachable,
trying public address""")
proxy = xmlrpclib.ServerProxy(self._registry)
bootpeer = proxy.getBootstrapPeer(self._prefix).data
utils.log('Boot peer received from server', 4)
p = subprocess.Popen(('openssl', 'rsautl', '-decrypt', '-inkey', self._key_path),
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
try:
prefix, address = p.communicate(bootpeer).split()
except ValueError:
# DO something
pass
self.db.execute("INSERT INTO peers (prefix, address) VALUES (?,?)",
(prefix, address))
utils.log('Boot peer added', 4)
def clear_blacklist(self, flag): def clear_blacklist(self, flag):
utils.log('Clearing blacklist from flag %u' % (flag,), 3) utils.log('Clearing blacklist from flag %u' % (flag,), 3)
self._db.execute("DELETE FROM blacklist WHERE flag = ?", self._db.execute("DELETE FROM blacklist WHERE flag = ?",
...@@ -98,10 +75,12 @@ class PeerManager: ...@@ -98,10 +75,12 @@ class PeerManager:
self._populate() self._populate()
utils.log('DB refreshed', 3) utils.log('DB refreshed', 3)
self.next_refresh = time.time() + self._refresh_time self.next_refresh = time.time() + self._refresh_time
return True
except socket.error, e: except socket.error, e:
utils.log(e, 4) utils.log(e, 4)
utils.log('Connection to server failed, retrying in 30s', 2) utils.log('Connection to server failed, retrying in 30s', 2)
self.next_refresh = time.time() + 30 self.next_refresh = time.time() + 30
return False
def _declare(self): def _declare(self):
if self._address != None: if self._address != None:
...@@ -129,9 +108,32 @@ class PeerManager: ...@@ -129,9 +108,32 @@ class PeerManager:
utils.log('New peers : %s' % ', '.join(map(str, new_peer_list)), 5) utils.log('New peers : %s' % ', '.join(map(str, new_peer_list)), 5)
def getUnusedPeers(self, peer_count): def getUnusedPeers(self, peer_count):
return self._db.execute("""SELECT prefix, address FROM peers WHERE used for populate in self.refresh, self._bootstrap, bool:
<= 0 ORDER BY used DESC,RANDOM() LIMIT ?""", peer_list = self._db.execute("""SELECT prefix, address FROM peers WHERE used
(peer_count,)) <= 0 ORDER BY used DESC,RANDOM() LIMIT ?""",
(peer_count,)).fetchall()
if peer_list or populate():
return peer_list
def _bootstrap(self):
utils.log('Getting Boot peer...', 3)
proxy = xmlrpclib.ServerProxy(self._registry)
try:
bootpeer = proxy.getBootstrapPeer(self._prefix).data
utils.log('Boot peer received from server', 4)
p = subprocess.Popen(('openssl', 'rsautl', '-decrypt', '-inkey', self._key_path),
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
bootpeer = p.communicate(bootpeer).split()
self.db.execute("INSERT INTO peers (prefix, address) VALUES (?,?)", bootpeer)
utils.log('Boot peer added', 4)
return True
except socket.error:
pass
except sqlite3.IntegrityError, e:
import pdb; pdb.set_trace()
if e.args[0] != '':
raise
return False
def usePeer(self, prefix): def usePeer(self, prefix):
utils.log('Updating peers database : using peer ' + str(prefix), 5) utils.log('Updating peers database : using peer ' + str(prefix), 5)
......
#!/usr/bin/env python #!/usr/bin/env python
import argparse, math, random, select, smtplib, sqlite3, string, socket, time, import argparse, math, random, select, smtplib, sqlite3, string, socket
import subprocess, threading, traceback, errno import subprocess, time, threading, traceback, errno
from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
from email.mime.text import MIMEText from email.mime.text import MIMEText
from OpenSSL import crypto from OpenSSL import crypto
......
...@@ -19,7 +19,7 @@ class ArgParser(ArgumentParser): ...@@ -19,7 +19,7 @@ class ArgParser(ArgumentParser):
def ovpnArgs(optional_args, ca_path, cert_path, key_path): def ovpnArgs(optional_args, ca_path, cert_path, key_path):
# Treat openvpn arguments # Treat openvpn arguments
if optional_args[0] == "--": if optional_args and optional_args[0] == "--":
del optional_args[0] del optional_args[0]
optional_args.append('--ca') optional_args.append('--ca')
optional_args.append(ca_path) optional_args.append(ca_path)
...@@ -122,7 +122,7 @@ def main(): ...@@ -122,7 +122,7 @@ def main():
except upnpigd.NoUPnPDevice: except upnpigd.NoUPnPDevice:
utils.log('No upnp device found', 4) utils.log('No upnp device found', 4)
peer_db = db.PeerManager(config.state, config.registry, peer_db = db.PeerManager(config.state, config.registry, config.key,
config.peers_db_refresh, config.address, internal_ip, prefix, config.peers_db_refresh, config.address, internal_ip, prefix,
manual, config.pp, 200) manual, config.pp, 200)
tunnel_manager = tunnel.TunnelManager(write_pipe, peer_db, openvpn_args, tunnel_manager = tunnel.TunnelManager(write_pipe, peer_db, openvpn_args,
......
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