Commit eee67f4e authored by Guillaume Bury's avatar Guillaume Bury

Blacklist is now a sql table

parent 44eab543
...@@ -28,6 +28,11 @@ class PeerManager: ...@@ -28,6 +28,11 @@ class PeerManager:
utils.log('Preparing peers database...', 4) utils.log('Preparing peers database...', 4)
try: try:
self._db.execute("UPDATE peers SET used = 0") self._db.execute("UPDATE peers SET used = 0")
self._db.execute("""CREATE TABLE IF NOT EXISTS blacklist (
prefix TEXT PRIMARY KEY,
flag INTEGER NOT NULL)""")
self._db.execute("""CREATE INDEX IF NOT EXISTS
blacklist_flag ON blacklist(flag)""")
except sqlite3.OperationalError, e: except sqlite3.OperationalError, e:
if e.args[0] == 'no such table: peers': if e.args[0] == 'no such table: peers':
raise RuntimeError raise RuntimeError
...@@ -35,13 +40,22 @@ class PeerManager: ...@@ -35,13 +40,22 @@ class PeerManager:
self.next_refresh = time.time() self.next_refresh = time.time()
def reset_blacklist(self): def clear_blacklist(self, flag):
self._blacklist = [(self._prefix)] utils.log('Clearing blacklist from flag %u' % (flag,), 3)
self._db.execute("DELETE FROM blacklist WHERE flag = ?", (flag,))
utils.log('Blacklist cleared', 5)
def blacklist(self, prefix): def blacklist(self, prefix, flag):
utils.log('Blacklisting %s' % (prefix,), 4) utils.log('Blacklisting %s' % (prefix,), 4)
self._db.execute("DELETE FROM peers WHERE prefix = ?", (prefix,)) self._db.execute("DELETE FROM peers WHERE prefix = ?", (prefix,))
self._blacklist = list(set(self._blacklist + [(prefix,)])) self._db.execute("INSERT OR REPLACE INTO blacklist VALUES (?,?)",
(prefix, flag))
utils.log('%s blacklisted' % (prefix,), 5)
def whitelist(self, prefix):
utils.log('Unblacklisting %s' % (prefix,), 4)
self._db.execute("DELETE FROM blacklist WHERE prefix = ?", (prefix,))
utils.log('%s whitelisted' % (prefix,), 5)
def refresh(self): def refresh(self):
utils.log('Refreshing the peers DB...', 2) utils.log('Refreshing the peers DB...', 2)
...@@ -74,8 +88,8 @@ class PeerManager: ...@@ -74,8 +88,8 @@ class PeerManager:
(str(len(new_peer_list) - self._db_size),)) (str(len(new_peer_list) - self._db_size),))
self._db.executemany("""INSERT OR IGNORE INTO peers (prefix, address) self._db.executemany("""INSERT OR IGNORE INTO peers (prefix, address)
VALUES (?,?)""", new_peer_list) VALUES (?,?)""", new_peer_list)
self._db.executemany("DELETE FROM peers WHERE prefix = ?", self._db.execute("""DELETE FROM peers WHERE prefix IN
self._blacklist) (SELECT prefix FROM blacklist)""")
utils.log('DB populated', 3) utils.log('DB populated', 3)
utils.log('New peers : %s' % ', '.join(map(str, new_peer_list)), 5) utils.log('New peers : %s' % ', '.join(map(str, new_peer_list)), 5)
......
...@@ -78,7 +78,6 @@ class TunnelManager: ...@@ -78,7 +78,6 @@ class TunnelManager:
self._network = network self._network = network
self._net_len = len(network) self._net_len = len(network)
self._iface_list = iface_list self._iface_list = iface_list
self.__indirect_connect = []
self.free_interface_set = set(('client1', 'client2', 'client3', self.free_interface_set = set(('client1', 'client2', 'client3',
'client4', 'client5', 'client6', 'client4', 'client5', 'client6',
'client7', 'client8', 'client9', 'client7', 'client8', 'client9',
...@@ -150,7 +149,7 @@ class TunnelManager: ...@@ -150,7 +149,7 @@ class TunnelManager:
def _countRoutes(self): def _countRoutes(self):
utils.log('Starting to count the routes on each interface...', 3) utils.log('Starting to count the routes on each interface...', 3)
self._indirect_connect = [] self._peer_db.clear_blacklist(0)
for iface in self._iface_to_prefix.keys(): for iface in self._iface_to_prefix.keys():
self._connection_dict[self._iface_to_prefix[iface]].routes = 0 self._connection_dict[self._iface_to_prefix[iface]].routes = 0
f = open('/proc/net/ipv6_route', 'r') f = open('/proc/net/ipv6_route', 'r')
...@@ -170,7 +169,7 @@ class TunnelManager: ...@@ -170,7 +169,7 @@ class TunnelManager:
prefix = ip[self._net_len:subnet_size] prefix = ip[self._net_len:subnet_size]
utils.log('A route to %s has been discovered on the LAN' utils.log('A route to %s has been discovered on the LAN'
% (prefix,), 3) % (prefix,), 3)
self._peer_db.blacklist(prefix) self._peer_db.blacklist(prefix, 0)
utils.log("Routes have been counted", 3) utils.log("Routes have been counted", 3)
for p in self._connection_dict.keys(): for p in self._connection_dict.keys():
......
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