Fixed some typos
[re6stnet.git] / plib.py
1 #!/usr/bin/env python
2 import os, subprocess
3 import utils
4
5 verbose = None
6
7 def openvpn(*args, **kw):
8 args = ['openvpn',
9 '--dev-type', 'tap',
10 '--persist-tun',
11 '--persist-key',
12 '--script-security', '2',
13 '--user', 'nobody',
14 # I don't kown how Babel works, but if it test the
15 # connection often, the ping directive might not be needed
16 # if it test the connection very often, we could also decrease
17 # ping-exit to 1 sec
18 # '--ping', '1',
19 # '--ping-exit', '3',
20 '--group', 'nogroup',
21 '--verb', str(verbose),
22 ] + list(args)
23 utils.log(str(args), 5)
24 return subprocess.Popen(args, **kw)
25
26 # TODO : set iface up when creating a server/client
27 # ! check working directory before launching up script ?
28
29 def server(serverIp, network, max_clients, dh_path, pipe_fd, *args, **kw):
30 utils.log('Starting server', 3)
31 return openvpn(
32 '--tls-server',
33 '--mode', 'server',
34 '--up', 'ovpn-server %s/%u' % (serverIp, len(network)),
35 '--client-connect', 'ovpn-server ' + str(pipe_fd),
36 '--client-disconnect', 'ovpn-server ' + str(pipe_fd),
37 '--dh', dh_path,
38 '--max-clients', str(max_clients),
39 *args, **kw)
40
41 def client(serverIp, pipe_fd, *args, **kw):
42 utils.log('Starting client', 5)
43 return openvpn(
44 '--nobind',
45 '--client',
46 '--remote', serverIp,
47 '--up', 'ovpn-client',
48 '--route-up', 'ovpn-client ' + str(pipe_fd),
49 *args, **kw)
50
51 def router(network, internal_ip, interface_list, **kw):
52 utils.log('Starting babel', 3)
53 args = ['babeld',
54 '-C', 'redistribute local ip %s' % (internal_ip),
55 '-C', 'redistribute local deny',
56 # Route VIFIB ip adresses
57 '-C', 'in ip %s::/%u' % (utils.ipFromBin(network), len(network)),
58 # Route only addresse in the 'local' network,
59 # or other entire networks
60 #'-C', 'in ip %s' % (config.internal_ip),
61 #'-C', 'in ip ::/0 le %s' % network_mask,
62 # Don't route other addresses
63 '-C', 'in deny',
64 '-d', str(verbose),
65 '-s',
66 ]
67 #if utils.config.babel_state:
68 # args += '-S', utils.config.babel_state
69 args = args + interface_list
70 utils.log(str(args), 5)
71 return subprocess.Popen(args, **kw)
72