Put all annexes python files into a subfolder
[re6stnet.git] / re6st / plib.py
1 import os, subprocess, logging
2 import utils
3
4 verbose = 0
5
6
7 def openvpn(hello_interval, *args, **kw):
8 args = ['openvpn',
9 '--dev-type', 'tap',
10 '--persist-tun',
11 '--persist-key',
12 '--script-security', '2',
13 '--user', 'nobody',
14 '--ping-exit', str(4 * hello_interval),
15 '--group', 'nogroup',
16 ] + list(args)
17 logging.trace('%s' % (args,))
18 return subprocess.Popen(args, **kw)
19
20
21 def server(server_ip, ip_length, max_clients, dh_path, pipe_fd, port, proto, hello_interval, *args, **kw):
22 logging.debug('Starting server...')
23 return openvpn(hello_interval,
24 '--tls-server',
25 '--mode', 'server',
26 '--up', 'ovpn-server %s/%u' % (server_ip, 64),
27 '--client-connect', 'ovpn-server ' + str(pipe_fd),
28 '--client-disconnect', 'ovpn-server ' + str(pipe_fd),
29 '--dh', dh_path,
30 '--max-clients', str(max_clients),
31 '--port', str(port),
32 '--proto', proto,
33 *args, **kw)
34
35
36 def client(server_address, pipe_fd, hello_interval, *args, **kw):
37 logging.debug('Starting client...')
38 remote = ['--nobind',
39 '--client',
40 '--up', 'ovpn-client',
41 '--route-up', 'ovpn-client ' + str(pipe_fd)]
42 try:
43 for ip, port, proto in utils.address_list(server_address):
44 remote += '--remote', ip, port, proto
45 except ValueError, e:
46 logging.warning('Error "%s" in unpacking address %s for openvpn client'
47 % (e, server_address,))
48 remote += args
49 return openvpn(hello_interval, *remote, **kw)
50
51
52 def router(network, internal_ip, interface_list,
53 wireless, hello_interval, state_path, **kw):
54 logging.info('Starting babel...')
55 args = ['babeld',
56 '-C', 'redistribute local ip %s' % (internal_ip),
57 '-C', 'redistribute local deny',
58 # Route VIFIB ip adresses
59 '-C', 'in ip %s::/%u' % (utils.ipFromBin(network), len(network)),
60 # Route only addresse in the 'local' network,
61 # or other entire networks
62 #'-C', 'in ip %s' % (config.internal_ip),
63 #'-C', 'in ip ::/0 le %s' % network_mask,
64 # Don't route other addresses
65 '-C', 'in deny',
66 '-d', str(verbose),
67 '-h', str(hello_interval),
68 '-H', str(hello_interval),
69 '-S', state_path,
70 '-s',
71 ]
72 if wireless:
73 args.append('-w')
74 args = args + interface_list
75 logging.trace('%s' % args)
76 return subprocess.Popen(args, **kw)