Commit d297a650 authored by Ulysse Beaugnon's avatar Ulysse Beaugnon

declare and populate are now separated so we can redeclare the external IP...

declare and populate are now separated so we can redeclare the external IP when it changes and get new peers when they are too few
parent f85a3905
......@@ -5,17 +5,16 @@ import utils
class PeerManager:
def __init__(self, dbPath, server, server_port, refresh_time, external_ip, internal_ip, port, proto, db_size):
self._server_port = server_port
self._refresh_time = refresh_time
self._external_ip = external_ip
self._internal_ip = internal_ip
self._external_port = port
self._proto = proto
self._db_size = db_size
self._proxy = xmlrpclib.ServerProxy('http://%s:%u' % (server, server_port))
utils.log('Connectiong to peers database', 4)
self._db = sqlite3.connect(dbPath, isolation_level=None)
self._server = server
utils.log('Preparing peers database', 4)
try:
self._db.execute("UPDATE peers SET used = 0")
......@@ -26,19 +25,21 @@ class PeerManager:
self.next_refresh = time.time()
def refresh(self):
utils.log('Refreshing the peers DB', 2)
self._declare()
self._populate()
self.next_refresh = time.time() + self._refresh_time
def _populate(self):
def _declare(self):
if self._external_ip != None:
address = (self._internal_ip, self._external_ip, self._external_port, self._proto)
utils.log('Declaring our connections info', 3)
self._proxy.declare((self._internal_ip, self._external_ip, self._external_port, self._proto))
else:
address = 0
utils.log('Connecting to remote server', 3)
self._proxy = xmlrpclib.ServerProxy('http://%s:%u' % (self._server, self._server_port))
utils.log('Updating peers database : populating', 2)
new_peer_list = self._proxy.getPeerList(self._db_size, address)
utils.log('New peers recieved from %s' % self._server, 5)
utils.log('Warning : could not declare the external ip because it is unknown', 4)
def _populate(self):
utils.log('Populating the peers DB', 2)
new_peer_list = self._proxy.getPeerList(self._db_size, self._internal_ip)
self._db.executemany("INSERT OR IGNORE INTO peers (ip, port, proto, used) VALUES (?,?,?,0)", new_peer_list)
if self._external_ip != None:
self._db.execute("DELETE FROM peers WHERE ip = ?", (self._external_ip,))
......@@ -63,7 +64,9 @@ class PeerManager:
elif script_type == 'client-disconnect':
utils.log('%s has disconnected' % (arg,), 3)
elif script_type == 'route-up':
utils.log('External Ip : ' + arg, 3)
self._external_ip = arg
if arg != self._external_ip:
self._external_ip = arg
utils.log('External Ip : ' + arg, 3)
self._declare()
else:
utils.log('Unknow message recieved from the openvpn pipe : ' + msg, 1)
......@@ -177,6 +177,7 @@ class main(object):
return ip, port, proto
def declare(self, handler, address):
print "declaring new node"
client_address, ip, port, proto = address
#client_address, _ = handler.client_address
client_ip = utils.binFromIp(client_address)
......@@ -190,15 +191,16 @@ class main(object):
print "Unauthorized connection from %s which does not start with %s" % (client_ip, self.network)
return False
def getPeerList(self, handler, n, address):
def getPeerList(self, handler, n, client_address):
assert 0 < n < 1000
print "declaring new node"
if address != 0:
if not self.declare(handler, address):
# TODO: do something intelligent
raise RuntimeError
print "sending peers"
return self.db.execute("SELECT ip, port, proto FROM peers ORDER BY random() LIMIT ?", (n,)).fetchall()
client_ip = utils.binFromIp(client_address)
if client_ip.startswith(self.network):
print "sending peers"
return self.db.execute("SELECT ip, port, proto FROM peers ORDER BY random() LIMIT ?", (n,)).fetchall()
else:
# TODO: use log + DO NOT PRINT BINARY IP
print "Unauthorized connection from %s which does not start with %s" % (client_ip, self.network)
raise RuntimeError
if __name__ == "__main__":
main()
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