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