Commit dcb0694e authored by Guillaume Bury's avatar Guillaume Bury

Added exception catching in db, and modified utils.log so tht it can be used...

Added exception catching in db, and modified utils.log so tht it can be used with anything ( and not only a string )
parent 0c4db51f
......@@ -33,29 +33,47 @@ class PeerManager:
flag INTEGER NOT NULL)""")
self._db.execute("""CREATE INDEX IF NOT EXISTS
blacklist_flag ON blacklist(flag)""")
utils.log('Database prepared', 5)
except sqlite3.OperationalError, e:
if e.args[0] == 'no such table: peers':
raise RuntimeError
utils.log('Database prepared', 5)
else:
utils.log(str(e), 1)
except sqlite3.Error, e:
utils.log(str(e), 1)
self.next_refresh = time.time()
def clear_blacklist(self, flag):
utils.log('Clearing blacklist from flag %u' % (flag,), 3)
self._db.execute("DELETE FROM blacklist WHERE flag = ?", (flag,))
utils.log('Blacklist cleared', 5)
try:
with self._db:
self._db.execute("DELETE FROM blacklist WHERE flag = ?",
(flag,))
utils.log('Blacklist cleared', 5)
except sqlite3.Error, e:
utils.log(str(e), 1)
def blacklist(self, prefix, flag):
utils.log('Blacklisting %s' % (prefix,), 4)
self._db.execute("DELETE FROM peers WHERE prefix = ?", (prefix,))
self._db.execute("INSERT OR REPLACE INTO blacklist VALUES (?,?)",
(prefix, flag))
utils.log('%s blacklisted' % (prefix,), 5)
try:
with self._db:
self._db.execute("DELETE FROM peers WHERE prefix = ?",
(prefix,))
self._db.execute("""INSERT OR REPLACE INTO blacklist
VALUES (?,?)""", (prefix, flag))
utils.log('%s blacklisted' % (prefix,), 5)
except sqlite3.Error, e:
utils.log(str(e), 1)
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)
try:
with self._db:
self._db.execute("DELETE FROM blacklist WHERE prefix = ?", (prefix,))
utils.log('%s whitelisted' % (prefix,), 5)
except sqlite3.Error, e:
utils.log(str(e), 1)
def refresh(self):
utils.log('Refreshing the peers DB...', 2)
......@@ -73,7 +91,7 @@ class PeerManager:
if self._address != None:
utils.log('Sending connection info to server...', 3)
self._proxy.declare((self._internal_ip,
utils.address_list(self._address)))
utils.address_str(self._address)))
utils.log('Info sent', 5)
else:
utils.log("Warning : couldn't send ip, unknown external config", 4)
......@@ -82,39 +100,59 @@ class PeerManager:
utils.log('Populating the peers DB...', 2)
new_peer_list = self._proxy.getPeerList(self._db_size,
self._internal_ip)
self._db.execute("""DELETE FROM peers WHERE used <= 0 ORDER BY used,
RANDOM() LIMIT MAX(0, ? + (SELECT COUNT(*)
FROM peers WHERE used <= 0))""",
(str(len(new_peer_list) - self._db_size),))
self._db.executemany("""INSERT OR IGNORE INTO peers (prefix, address)
VALUES (?,?)""", new_peer_list)
self._db.execute("""DELETE FROM peers WHERE prefix IN
(SELECT prefix FROM blacklist)""")
utils.log('DB populated', 3)
utils.log('New peers : %s' % ', '.join(map(str, new_peer_list)), 5)
try:
with self._db:
self._db.execute("""DELETE FROM peers WHERE used <= 0 ORDER BY
used,random() LIMIT MAX(0, ? + (SELECT
COUNT(*) FROM peers WHERE used <= 0))""",
(str(len(new_peer_list) - self._db_size),))
self._db.executemany("""INSERT OR IGNORE INTO peers (prefix, address)
VALUES (?,?)""", new_peer_list)
self._db.execute("""DELETE FROM peers WHERE prefix IN
(SELECT prefix FROM blacklist)""")
utils.log('DB populated', 3)
utils.log('New peers : %s' % ', '.join(map(str, new_peer_list)), 5)
except sqlite3.Error, e:
utils.log(str(e), 1)
def getUnusedPeers(self, peer_count):
return self._db.execute("""SELECT prefix, address FROM peers WHERE used
<= 0 ORDER BY used DESC,RANDOM() LIMIT ?""",
(peer_count,))
try:
return self._db.execute("""SELECT prefix, address FROM peers WHERE used
<= 0 ORDER BY used DESC,RANDOM() LIMIT ?""",
(peer_count,))
except sqlite3.Error, e:
utils.log(str(e), 1)
return [('0', '')]
def usePeer(self, prefix):
utils.log('Updating peers database : using peer ' + str(prefix), 5)
self._db.execute("UPDATE peers SET used = 1 WHERE prefix = ?",
(prefix,))
utils.log('DB updated', 5)
try:
with self._db:
self._db.execute("UPDATE peers SET used = 1 WHERE prefix = ?",
(prefix,))
utils.log('DB updated', 5)
except sqlite3.Error, e:
utils.log(str(e), 1)
def unusePeer(self, prefix):
utils.log('Updating peers database : unusing peer ' + str(prefix), 5)
self._db.execute("UPDATE peers SET used = 0 WHERE prefix = ?",
(prefix,))
utils.log('DB updated', 5)
try:
with self._db:
self._db.execute("UPDATE peers SET used = 0 WHERE prefix = ?",
(prefix,))
utils.log('DB updated', 5)
except sqlite3.Error, e:
utils.log(str(e), 1)
def flagPeer(self, prefix):
utils.log('Updating peers database : flagging peer ' + str(prefix), 5)
self._db.execute("UPDATE peers SET used = -1 WHERE prefix = ?",
(prefix,))
utils.log('DB updated', 5)
try:
with self._db:
self._db.execute("UPDATE peers SET used = -1 WHERE prefix = ?",
(prefix,))
utils.log('DB updated', 5)
except sqlite3.Error, e:
utils.log(str(e), 1)
def handle_message(self, msg):
script_type, arg = msg.split()
......
......@@ -33,12 +33,17 @@ def server(server_ip, ip_length, max_clients, dh_path, pipe_fd, port, proto, hel
def client(server_address, pipe_fd, hello_interval, *args, **kw):
utils.log('Starting client...', 5)
remote= ['--nobind',
'--client',
'--up', 'ovpn-client',
'--route-up', 'ovpn-client ' + str(pipe_fd) ]
for ip, port, proto in utils.address_set(server_address):
remote += '--remote', ip, port, proto
remote = ['--nobind',
'--client',
'--up', 'ovpn-client',
'--route-up', 'ovpn-client ' + str(pipe_fd) ]
try:
for ip, port, proto in utils.address_list(server_address):
remote += '--remote', ip, port, proto
except ValueError, e:
utils.log(e, 1)
utils.log('Error in unpacking address %s for openvpn client'
% (server_address,), 1)
remote += args
return openvpn(hello_interval, *remote, **kw)
......
......@@ -38,9 +38,9 @@ class Connection:
return True
# Unused for now. By killing tunnels with significantly lower trafic
# in comparison to other tunnels, we hope to connect to nodes with
# better bandwith, in order to improve connectivity with destinations
# we are really interested in.
# in comparison to other tunnels, we hope to connect to nodes with
# better bandwith, in order to improve connectivity with destinations
# we are really interested in.
def _updateBandwidth(self):
try:
f_rx = open('/sys/class/net/%s/statistics/rx_bytes' %
......
......@@ -5,7 +5,8 @@ verbose = 0
def log(message, verbose_level):
if verbose >= verbose_level:
print time.strftime("%d-%m-%Y %H:%M:%S : " + message)
print time.strftime("%d-%m-%Y %H:%M:%S :"),
print message
def binFromIp(ip):
ip1, ip2 = struct.unpack('>QQ', socket.inet_pton(socket.AF_INET6, ip))
......@@ -37,9 +38,9 @@ def ipFromCert(network, cert_path):
prefix, prefix_len = subject.CN.split('/')
return ipFromPrefix(network, prefix, int(prefix_len))
def address_list(address_set):
def address_str(address_set):
return ';'.join(map(','.join, address_set))
def address_set(address_list):
def address_list(address_list):
return list(tuple(address.split(','))
for address in address_list.split(';'))
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