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