Fixed port declaration in openvpn
[re6stnet.git] / db.py
1 import sqlite3, socket, xmlrpclib, time, os
2 import utils
3
4
5 class PeerManager:
6
7 # internal ip = temp arg/attribute
8 def __init__(self, db_dir_path, server, server_port, refresh_time, address,
9 internal_ip, prefix, manual, proto, port , db_size):
10 self._refresh_time = refresh_time
11 self._address = address
12 self._internal_ip = internal_ip
13 self._prefix = prefix
14 self._server = server
15 self._server_port = server_port
16 self._db_size = db_size
17 self._proto = proto
18 self._ovpn_port = port
19 self._manual = manual
20
21 self._proxy = xmlrpclib.ServerProxy('http://%s:%u'
22 % (server, server_port))
23
24 utils.log('Connectiong to peers database', 4)
25 self._db = sqlite3.connect(os.path.join(db_dir_path, 'peers.db'),
26 isolation_level=None)
27 utils.log('Preparing peers database', 4)
28 try:
29 self._db.execute("UPDATE peers SET used = 0")
30 except sqlite3.OperationalError, e:
31 if e.args[0] == 'no such table: peers':
32 raise RuntimeError
33
34 self.next_refresh = time.time()
35
36 def refresh(self):
37 utils.log('Refreshing the peers DB', 2)
38 try:
39 self._declare()
40 self._populate()
41 self.next_refresh = time.time() + self._refresh_time
42 except socket.error, e:
43 utils.log(str(e), 4)
44 utils.log('Connection to server failed, retrying in 30s', 2)
45 self.next_refresh = time.time() + 30
46
47 def _declare(self):
48 if self._address != None:
49 utils.log('Sending connection info to server', 3)
50 self._proxy.declare((self._internal_ip,
51 utils.address_list(self._address)))
52 else:
53 utils.log("Warning : couldn't send ip, unknown external config", 4)
54
55 def _populate(self):
56 utils.log('Populating the peers DB', 2)
57 new_peer_list = self._proxy.getPeerList(self._db_size,
58 self._internal_ip)
59 self._db.execute("""DELETE FROM peers WHERE used <= 0 ORDER BY used,
60 RANDOM() LIMIT MAX(0, ? + (SELECT COUNT(*)
61 FROM peers WHERE used <= 0))""",
62 (str(len(new_peer_list) - self._db_size),))
63 self._db.executemany("""INSERT OR IGNORE INTO peers (prefix, address)
64 VALUES (?,?)""", new_peer_list)
65 self._db.execute("DELETE FROM peers WHERE prefix = ?", (self._prefix,))
66 utils.log('New peers : %s' % ', '.join(map(str, new_peer_list)), 5)
67
68 def getUnusedPeers(self, peer_count):
69 return self._db.execute("""SELECT prefix, address FROM peers WHERE used
70 <= 0 ORDER BY used DESC,RANDOM() LIMIT ?""",
71 (peer_count,))
72
73 def usePeer(self, prefix):
74 utils.log('Updating peers database : using peer ' + str(prefix), 5)
75 self._db.execute("UPDATE peers SET used = 1 WHERE prefix = ?",
76 (prefix,))
77
78 def unusePeer(self, prefix):
79 utils.log('Updating peers database : unusing peer ' + str(prefix), 5)
80 self._db.execute("UPDATE peers SET used = 0 WHERE prefix = ?",
81 (prefix,))
82
83 def flagPeer(self, prefix):
84 utils.log('Updating peers database : flagging peer ' + str(prefix), 5)
85 self._db.execute("UPDATE peers SET used = -1 WHERE prefix = ?",
86 (prefix,))
87
88 def handle_message(self, msg):
89 script_type, arg = msg.split()
90 if script_type == 'client-connect':
91 utils.log('Incomming connection from %s' % (arg,), 3)
92 elif script_type == 'client-disconnect':
93 utils.log('%s has disconnected' % (arg,), 3)
94 elif script_type == 'route-up':
95 if not self._manual:
96 external_ip = arg
97 new_address = list([external_ip, self._ovpn_port, proto]
98 for proto in self._proto)
99 if self._address != new_address:
100 self._address = new_address
101 utils.log('Received new external configuration : %s:%s'
102 % (external_ip, external_port), 3)
103 self._declare()
104 else:
105 utils.log('Unknow message recieved from the openvpn pipe : '
106 + msg, 1)