Cleaner server connection
[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_address, pipe_fd, hello_interval, *args, **kw):
35 utils.log('Starting client', 5)
36 remote= ['--nobind',
37 '--client',
38 '--up', 'ovpn-client',
39 '--route-up', 'ovpn-client ' + str(pipe_fd) ]
40 for ip, port, proto in utils.address_set(server_address):
41 remote += '--remote', ip, port, proto
42 remote += args
43 return openvpn(hello_interval, *remote, **kw)
44
45 def router(network, internal_ip, interface_list,
46 wireless, hello_interval, **kw):
47 utils.log('Starting babel', 3)
48 args = ['babeld',
49 '-C', 'redistribute local ip %s' % (internal_ip),
50 '-C', 'redistribute local deny',
51 # Route VIFIB ip adresses
52 '-C', 'in ip %s::/%u' % (utils.ipFromBin(network), len(network)),
53 # Route only addresse in the 'local' network,
54 # or other entire networks
55 #'-C', 'in ip %s' % (config.internal_ip),
56 #'-C', 'in ip ::/0 le %s' % network_mask,
57 # Don't route other addresses
58 '-C', 'in deny',
59 '-d', str(verbose),
60 '-h', str(hello_interval),
61 '-H', str(hello_interval),
62 '-s',
63 ]
64 #if utils.config.babel_state:
65 # args += '-S', utils.config.babel_state
66 if wireless:
67 args.append('-w')
68 args = args + interface_list
69 utils.log(str(args), 5)
70 return subprocess.Popen(args, **kw)
71