Fixing config
[re6stnet.git] / vifibnet.py
1 #!/usr/bin/env python
2 import argparse, errno, math, os, select, subprocess, sys, time, traceback
3 from OpenSSL import crypto
4 import db, plib, upnpigd, utils, tunnelmanager
5
6 def getConfig():
7 parser = argparse.ArgumentParser(
8 description='Resilient virtual private network application')
9 _ = parser.add_argument
10 # Server address MUST be a vifib address ( else requests will be denied )
11 _('--server', required=True,
12 help='Address for peer discovery server')
13 _('--server-port', required=True, type=int,
14 help='Peer discovery server port')
15 _('-l', '--log', default='/var/log',
16 help='Path to vifibnet logs directory')
17 _('--client-count', default=2, type=int,
18 help='Number of client connections')
19 # TODO: use maxpeer
20 _('--max-clients', default=10, type=int,
21 help='the number of peers that can connect to the server')
22 _('--refresh-time', default=300, type=int,
23 help='the time (seconds) to wait before changing the connections')
24 _('--refresh-count', default=1, type=int,
25 help='The number of connections to drop when refreshing the connections')
26 _('--db', default='/var/lib/vifibnet/peers.db',
27 help='Path to peers database')
28 _('--dh', required=True,
29 help='Path to dh file')
30 _('--babel-state', default='/var/lib/vifibnet/babel_state',
31 help='Path to babeld state-file')
32 _('--verbose', '-v', default=0, type=int,
33 help='Defines the verbose level')
34 _('--ca', required=True,
35 help='Path to the certificate authority file')
36 _('--cert', required=True,
37 help='Path to the certificate file')
38 _('--ip', required=True, dest='external_ip',
39 help='Ip address of the machine on the internet')
40 # Openvpn options
41 _('openvpn_args', nargs=argparse.REMAINDER,
42 help="Common OpenVPN options (e.g. certificates)")
43 return parser.parse_args()
44
45 def main():
46 # Get arguments
47 config = getConfig()
48
49 # Launch babel on all interfaces. WARNING : you have to be root to start babeld
50 babel = plib.babel(stdout=os.open(os.path.join(utils.config.log, 'vifibnet.babeld.log'),
51 os.O_WRONLY | os.O_CREAT | os.O_TRUNC), stderr=subprocess.STDOUT)
52
53 # Create and open read_only pipe to get connect/disconnect events from openvpn
54 utils.log('Creating pipe for server events', 3)
55 r_pipe, write_pipe = os.pipe()
56 read_pipe = os.fdopen(r_pipe)
57
58 # Setup the tunnel manager
59 peers_db = db.PeersDB(utils.config.db)
60 tunnelManager = tunnelmanager.TunnelManager(write_pipe, peers_db, utils.config.client_count, utils.config.refresh_count)
61
62 # Establish connections
63 serverProcess = plib.server(utils.config.internal_ip, write_pipe, '--dev', 'vifibnet',
64 stdout=os.open(os.path.join(utils.config.log, 'vifibnet.server.log'), os.O_WRONLY | os.O_CREAT | os.O_TRUNC))
65 tunnelManager.refresh()
66
67 # Timed refresh initializing
68 next_refresh = time.time() + utils.config.refresh_time
69
70 # main loop
71 try:
72 while True:
73 ready, tmp1, tmp2 = select.select([read_pipe], [], [],
74 max(0, next_refresh - timhttp://blogs.lesechos.fr/dominique-seux/de-mondialiser-les-telecoms-a11339.htmle.time()))
75 if ready:
76 tunnelManager.handle_message(read_pipe.readline())
77 if time.time() >= next_refresh:
78 peers_db.populate(10)
79 tunnelManager.refresh()
80 next_refresh = time.time() + utils.config.refresh_time
81 except KeyboardInterrupt:
82 return 0
83
84 if __name__ == "__main__":
85 main()
86