Commit 3b161a08 authored by Guillaume Bury's avatar Guillaume Bury

Added fifo to get connect openvpn events

parent 08ce480b
#!/usr/bin/python -S
import os, sys
os.write(int(sys.argv[1]), 'hello !\n')
#!/usr/bin/env python #!/usr/bin/env python
import argparse, errno, os, sqlite3, subprocess, sys, time import argparse, errno, os, select, sqlite3, subprocess, sys, time
import traceback import traceback
import upnpigd import upnpigd
import openvpn import openvpn
...@@ -80,7 +80,7 @@ def startNewConnection(n): ...@@ -80,7 +80,7 @@ def startNewConnection(n):
iface = free_interface_set.pop() iface = free_interface_set.pop()
connection_dict[id] = ( openvpn.client( ip, '--dev', iface, '--proto', proto, '--rport', str(port), connection_dict[id] = ( openvpn.client( ip, '--dev', iface, '--proto', proto, '--rport', str(port),
stdout=os.open(config.client_log + 'vifibnet.client.' + str(id) + '.log', os.O_RDONLY|os.O_CREAT) ) , iface) stdout=os.open(config.client_log + 'vifibnet.client.' + str(id) + '.log', os.O_RDONLY|os.O_CREAT) ) , iface)
log_message('Updating peers database', 3) log_message('Updating peers database', 5)
peer_db.execute("UPDATE peers SET used = 1 WHERE id = ?", (id,)) peer_db.execute("UPDATE peers SET used = 1 WHERE id = ?", (id,))
except KeyError: except KeyError:
log_message("Can't establish connection with %s : no available interface" % ip, 2) log_message("Can't establish connection with %s : no available interface" % ip, 2)
...@@ -94,7 +94,7 @@ def killConnection(id): ...@@ -94,7 +94,7 @@ def killConnection(id):
p, iface = connection_dict.pop(id) p, iface = connection_dict.pop(id)
p.kill() p.kill()
free_interface_set.add(iface) free_interface_set.add(iface)
log_message('Updating peers database', 3) log_message('Updating peers database', 5)
peer_db.execute("UPDATE peers SET used = 0 WHERE id = ?", (id,)) peer_db.execute("UPDATE peers SET used = 0 WHERE id = ?", (id,))
except KeyError: except KeyError:
log_message("Can't kill connection to " + peer + ": no existing connection", 1) log_message("Can't kill connection to " + peer + ": no existing connection", 1)
...@@ -134,17 +134,23 @@ def main(): ...@@ -134,17 +134,23 @@ def main():
peer_db.execute("CREATE INDEX IF NOT EXISTS _peers_used ON peers(used)") peer_db.execute("CREATE INDEX IF NOT EXISTS _peers_used ON peers(used)")
peer_db.execute("UPDATE peers SET used = 0") peer_db.execute("UPDATE peers SET used = 0")
# Create and open read_only pipe to get connect/disconnect events from openvpn
log_message('Creating pipe for openvpn events', 3)
r_pipe, write_pipe = os.pipe()
read_pipe = os.fdopen(r_pipe)
# Establish connections # Establish connections
log_message('Starting openvpn server', 3) log_message('Starting openvpn server', 3)
serverProcess = openvpn.server(config.ip, serverProcess = openvpn.server(config.ip, write_pipe,
'--dev', 'vifibnet', stdout=os.open(config.server_log, os.O_RDONLY|os.O_CREAT)) '--dev', 'vifibnet', stdout=os.open(config.server_log, os.O_RDONLY|os.O_CREAT))
startNewConnection(config.client_count) startNewConnection(config.client_count)
# main loop # main loop
try: try:
while True: while True:
# TODO : use select to get openvpn events from pipes ready, tmp1, tmp2 = select.select([read_pipe], [], [], float(config.refresh_time))
time.sleep(float(config.refresh_time)) if ready:
log_message(read_pipe.readline(), 0)
refreshConnections() refreshConnections()
except KeyboardInterrupt: except KeyboardInterrupt:
return 0 return 0
......
...@@ -18,13 +18,14 @@ def openvpn(*args, **kw): ...@@ -18,13 +18,14 @@ def openvpn(*args, **kw):
# TODO : set iface up when creating a server/client # TODO : set iface up when creating a server/client
# ! check working directory before launching up script ? # ! check working directory before launching up script ?
def server(ip, *args, **kw): def server(ip, pipe_fd, *args, **kw):
return openvpn( return openvpn(
'--tls-server', '--tls-server',
'--keepalive', '10', '60', '--keepalive', '10', '60',
'--mode', 'server', '--mode', 'server',
'--duplicate-cn', # XXX : to be removed '--duplicate-cn', # XXX : to be removed
'--up', 'up-server ' + ip, '--up', 'up-server ' + ip,
'--client-connect', 'client-connect ' + str(pipe_fd),
'--dh', config.dh, '--dh', config.dh,
*args, **kw) *args, **kw)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment