Big TODO update
[re6stnet.git] / plib.py
1 import os, subprocess
2 import utils
3
4 verbose = None
5
6 def openvpn(hello_interval, *args, **kw):
7 args = ['openvpn',
8 '--dev-type', 'tap',
9 '--persist-tun',
10 '--persist-key',
11 '--script-security', '2',
12 '--user', 'nobody',
13 '--ping-exit', str(4 * hello_interval),
14 '--group', 'nogroup',
15 '--verb', str(verbose),
16 ] + list(args)
17 utils.log(str(args), 5)
18 return subprocess.Popen(args, **kw)
19
20 def server(server_ip, network, max_clients, dh_path, pipe_fd, port, proto, hello_interval, *args, **kw):
21 utils.log('Starting server', 3)
22 return openvpn(hello_interval,
23 '--tls-server',
24 '--mode', 'server',
25 '--up', 'ovpn-server %s/%u' % (server_ip, len(network)),
26 '--client-connect', 'ovpn-server ' + str(pipe_fd),
27 '--client-disconnect', 'ovpn-server ' + str(pipe_fd),
28 '--dh', dh_path,
29 '--max-clients', str(max_clients),
30 '--port', str(port),
31 '--proto', proto,
32 *args, **kw)
33
34 def client(server_ip, pipe_fd, hello_interval, *args, **kw):
35 utils.log('Starting client', 5)
36 return openvpn(hello_interval,
37 '--nobind',
38 '--client',
39 '--remote', server_ip,
40 '--up', 'ovpn-client',
41 '--route-up', 'ovpn-client ' + str(pipe_fd),
42 *args, **kw)
43
44 def router(network, internal_ip, interface_list,
45 wireless, hello_interval, **kw):
46 utils.log('Starting babel', 3)
47 args = ['babeld',
48 '-C', 'redistribute local ip %s' % (internal_ip),
49 '-C', 'redistribute local deny',
50 # Route VIFIB ip adresses
51 '-C', 'in ip %s::/%u' % (utils.ipFromBin(network), len(network)),
52 # Route only addresse in the 'local' network,
53 # or other entire networks
54 #'-C', 'in ip %s' % (config.internal_ip),
55 #'-C', 'in ip ::/0 le %s' % network_mask,
56 # Don't route other addresses
57 '-C', 'in deny',
58 '-d', str(verbose),
59 '-h', str(hello_interval),
60 '-H', str(hello_interval),
61 '-s',
62 ]
63 #if utils.config.babel_state:
64 # args += '-S', utils.config.babel_state
65 if wireless:
66 args.append('-w')
67 args = args + interface_list
68 utils.log(str(args), 5)
69 return subprocess.Popen(args, **kw)
70
71 def watch(interface):
72 return ( subprocess.call(['ip6tables', '-I', 'INPUT', '-i', interface]) and
73 subprocess.call(['ip6tables', '-I', 'OUTPUT', '-o', interface]))
74
75 def unwatch(interface):
76 return ( subprocess.call(['ip6tables', '-D', 'INPUT', '-i', interface]) and
77 subprocess.call(['ip6tables', '-D', 'OUTPUT', '-o', interface]))
78