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