Commit a00f6201 authored by Ulysse Beaugnon's avatar Ulysse Beaugnon

A peer now advertise itself more at the begining to be as present in the local...

A peer now advertise itself more at the begining to be as present in the local db of other peers than the average
parent 7c638d8d
......@@ -2,16 +2,8 @@ Bug :
possible bug in the upnp refresh, when refreshing after a too long time
To be done :
Choose the db size and how peers are deleted
find out who to have an automatic recovery without declare
Test the package
at the beggining a node tries to find 10 other nodes so ask for 10 bootsrap nodes
This slow the beginning and might overload the server
If their is too many nodes flaged as bad, the node never ask for some bootstrap peer
When a node arrive it is in nobody DB => very slow beginning. It should advertise itself more.
check the ips attributed
remove decalre
how do we make sure that none inject some false packets ?
......
import logging, sqlite3, socket, subprocess, xmlrpclib, time
import utils
# used = 0 : fresh node
# used = 1 : previously used peer
# used = 2 : curently in use
class PeerManager:
......@@ -11,7 +15,7 @@ class PeerManager:
self.address = address
self._internal_ip = internal_ip
self._prefix = prefix
self._db_size = db_size
self.db_size = db_size
self._registry = registry
self._key_path = key_path
self._pp = pp
......@@ -28,7 +32,7 @@ class PeerManager:
address TEXT NOT NULL,
used INTEGER NOT NULL DEFAULT 0,
date INTEGER DEFAULT (strftime('%s', 'now')))""")
self._db.execute("UPDATE peers SET used = 0")
self._db.execute("UPDATE peers SET used = 1 WHERE used = 2")
self._db.execute("""CREATE INDEX IF NOT EXISTS
_peers_used ON peers(used)""")
self._db.execute("""CREATE TABLE IF NOT EXISTS config (
......@@ -73,30 +77,28 @@ class PeerManager:
def refresh(self):
logging.info('Refreshing the peers DB...')
try:
self.next_refresh = time.time() + 30
self._declare()
self.next_refresh = time.time() + self._refresh_time
except socket.error, e:
logging.info('Connection to server failed, re-bootstraping')
logging.info('Connection to server failed, re-bootstraping and retrying in 30s')
try:
self._bootstrap()
self.next_refresh = time.time() + self._refresh_time
except socket.error, e:
logging.debug('socket.error : %s' % e)
logging.info('Connection to server failed, retrying in 30s')
self.next_refresh = time.time() + 30
def _declare(self):
if self.address != None:
logging.info('Sending connection info to server...')
self._proxy.declare(utils.address_str(self.address))
self.next_refresh = time.time() + self._refresh_time
logging.debug('Info sent')
else:
logging.warning("Warning : couldn't send ip, unknown external config")
logging.warning("Warning : couldn't send ip, unknown external config. retrying in 30s")
def getUnusedPeers(self, peer_count):
for populate in self._bootstrap, bool:
peer_list = self._db.execute("""SELECT prefix, address FROM peers WHERE used
<= 0 ORDER BY used DESC,RANDOM() LIMIT ?""",
<> 2 ORDER BY used ASC, RANDOM() LIMIT ?""",
(peer_count,)).fetchall()
if peer_list:
return peer_list
......@@ -125,19 +127,13 @@ class PeerManager:
def usePeer(self, prefix):
logging.trace('Updating peers database : using peer %s' % prefix)
self._db.execute("UPDATE peers SET used = 1 WHERE prefix = ?",
self._db.execute("UPDATE peers SET used = 2 WHERE prefix = ?",
(prefix,))
logging.debug('DB updated')
def unusePeer(self, prefix):
logging.trace('Updating peers database : unusing peer %s' % prefix)
self._db.execute("UPDATE peers SET used = 0 WHERE prefix = ?",
(prefix,))
logging.debug('DB updated')
def flagPeer(self, prefix):
logging.trace('Updating peers database : flagging peer %s' % prefix)
self._db.execute("UPDATE peers SET used = -1 WHERE prefix = ?",
self._db.execute("UPDATE peers SET used = 1 WHERE prefix = ?",
(prefix,))
logging.debug('DB updated')
......@@ -164,8 +160,7 @@ class PeerManager:
self._declare()
except socket.error, e:
logging.debug('socket.error : %s' % e)
logging.info('''Connection to server failed while
declaring external infos''')
logging.info("""Connection to server failed while declaring external infos""")
else:
logging.debug('Unknow message recieved from the openvpn pipe : %s'
% msg)
......@@ -182,9 +177,10 @@ class PeerManager:
if int(self._db.execute("""SELECT COUNT(*) FROM blacklist.flag WHERE prefix = ?""", (peer[0],)).next()[0]) > 0:
logging.info('Peer is blacklisted')
return False
self._db.execute("""DELETE FROM peers WHERE used <= 0 ORDER BY used,
RANDOM() LIMIT MAX(0, (SELECT COUNT(*) FROM peers
WHERE used <= 0) - ?)""", (str(self._db_size),))
self._db.execute("""DELETE FROM peers WHERE used <> 2 ORDER BY used DESC, date DESC
LIMIT MAX(0, (SELECT COUNT(*) FROM peers
WHERE used <> 2) - ?)""", (str(self.db_size),))
self._db.execute("UPDATE peers SET address = ?, used = 0, date = strftime('%s','now') WHERE used = 1 and prefix = ?", (peer[1], peer[0],))
self._db.execute("INSERT OR IGNORE INTO peers (prefix, address) VALUES (?,?)", peer)
logging.debug('Peer added')
return True
......@@ -51,6 +51,7 @@ class TunnelManager:
self._prefix = prefix
self._nSend = nSend
self._encrypt = encrypt
self._fast_start_done = False
self.next_refresh = time.time()
self._next_tunnel_refresh = time.time()
......@@ -75,7 +76,6 @@ class TunnelManager:
for prefix in self._connection_dict.keys():
if not self._connection_dict[prefix].refresh():
self._kill(prefix)
self._peer_db.flagPeer(prefix)
def _removeSomeTunnels(self):
# Get the candidates to killing
......@@ -149,7 +149,11 @@ class TunnelManager:
self._peer_db.blacklist(prefix, 0)
possiblePeers.add(line[0])
for ip in random.sample(possiblePeers, min(3, len(possiblePeers))):
if not self._fast_start_done and len(possiblePeers) > 4:
nSend = min(self._peer_db.db_size, len(possiblePeers))
else:
nSend = min(4, len(possiblePeers))
for ip in random.sample(possiblePeers, nSend):
self._notifyPeer(ip)
logging.debug("Routes have been counted")
......
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