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