Commit cdd5c554 authored by Guillaume Bury's avatar Guillaume Bury

Added openvpn ip discovery script

parent 8d07244c
...@@ -37,4 +37,5 @@ import os, sys ...@@ -37,4 +37,5 @@ import os, sys
'untrusted_port': '59345', 'untrusted_port': '59345',
'verb': '3'} 'verb': '3'}
open(sys.argv[2], 'w').write('push "setenv external_ip %s"\n' % os.environ[trusted_ip])
os.write(int(sys.argv[1]), '%(script_type)s %(common_name)s\n' % os.environ) os.write(int(sys.argv[1]), '%(script_type)s %(common_name)s\n' % os.environ)
...@@ -36,11 +36,12 @@ def server(ip, pipe_fd, *args, **kw): ...@@ -36,11 +36,12 @@ def server(ip, pipe_fd, *args, **kw):
'--max-clients', str(config.max_clients), '--max-clients', str(config.max_clients),
*args, **kw) *args, **kw)
def client(serverIp, *args, **kw): def client(serverIp, pipe_fd, *args, **kw):
return openvpn( return openvpn(
'--nobind', '--nobind',
'--tls-client', '--client',
'--remote', serverIp, '--remote', serverIp,
'--up', 'up-client', '--up', 'up-client',
'--ipchange', 'ipchange ' + str(pipe_fd),
*args, **kw) *args, **kw)
...@@ -14,7 +14,8 @@ free_interface_set = set(('client1', 'client2', 'client3', 'client4', 'client5', ...@@ -14,7 +14,8 @@ free_interface_set = set(('client1', 'client2', 'client3', 'client4', 'client5',
'client6', 'client7', 'client8', 'client9', 'client10')) 'client6', 'client7', 'client8', 'client9', 'client10'))
# TODO : flag in some way the peers that are connected to us so we don't connect to them # TODO : flag in some way the peers that are connected to us so we don't connect to them
# Or maybe we just don't care, # Or maybe we just don't care
class PeersDB: class PeersDB:
def __init__(self, dbPath): def __init__(self, dbPath):
self.proxy = xmlrpclib.ServerProxy('http://%s:%u' % (config.server, config.server_port)) self.proxy = xmlrpclib.ServerProxy('http://%s:%u' % (config.server, config.server_port))
...@@ -56,6 +57,7 @@ class PeersDB: ...@@ -56,6 +57,7 @@ class PeersDB:
log.log('Updating peers database : unusing peer ' + str(id), 5) log.log('Updating peers database : unusing peer ' + str(id), 5)
self.db.execute("UPDATE peers SET used = 0 WHERE id = ?", (id,)) self.db.execute("UPDATE peers SET used = 0 WHERE id = ?", (id,))
# TODO: do everything using 'binary' strings
def ipFromPrefix(prefix, prefix_len): def ipFromPrefix(prefix, prefix_len):
tmp = hex(int(prefix))[2:] tmp = hex(int(prefix))[2:]
tmp = tmp.rjust(int((math.ceil(float(prefix_len) / 4))), '0') tmp = tmp.rjust(int((math.ceil(float(prefix_len) / 4))), '0')
...@@ -127,20 +129,19 @@ def getConfig(): ...@@ -127,20 +129,19 @@ def getConfig():
subject = cert.get_subject() subject = cert.get_subject()
prefix, prefix_len = subject.serialNumber.split('/') prefix, prefix_len = subject.serialNumber.split('/')
ip = ipFromPrefix(prefix, int(prefix_len)) ip = ipFromPrefix(prefix, int(prefix_len))
print ip log.log('Intranet ip : %s' % (ip,), 3)
log.log('Intranet ip : %s' % (ip,), 4)
if config.openvpn_args[0] == "--": if config.openvpn_args[0] == "--":
del config.openvpn_args[0] del config.openvpn_args[0]
config.openvpn_args.append('--cert') config.openvpn_args.append('--cert')
config.openvpn_args.append(config.cert) config.openvpn_args.append(config.cert)
log.log("Configuration completed", 1) log.log("Configuration completed", 1)
def startNewConnection(n): def startNewConnection(n, write_pipe):
try: try:
for id, ip, port, proto in peers_db.getUnusedPeers(n): for id, ip, port, proto in peers_db.getUnusedPeers(n):
log.log('Establishing a connection with id %s (%s:%s)' % (id,ip,port), 2) log.log('Establishing a connection with id %s (%s:%s)' % (id,ip,port), 2)
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, write_pipe, '--dev', iface, '--proto', proto, '--rport', str(port),
stdout=os.open(os.path.join(config.log, 'vifibnet.client.%s.log' % (id,)), stdout=os.open(os.path.join(config.log, 'vifibnet.client.%s.log' % (id,)),
os.O_WRONLY|os.O_CREAT|os.O_TRUNC) ), os.O_WRONLY|os.O_CREAT|os.O_TRUNC) ),
iface) iface)
...@@ -187,12 +188,15 @@ def refreshConnections(): ...@@ -187,12 +188,15 @@ def refreshConnections():
startNewConnection(config.client_count - len(connection_dict)) startNewConnection(config.client_count - len(connection_dict))
def handle_message(msg): def handle_message(msg):
script_type, common_name = msg.split() script_type, arg = msg.split()
if script_type == 'client-connect': if script_type == 'client-connect':
log.log('Incomming connection from %s' % (common_name,), 3) log.log('Incomming connection from %s' % (arg,), 3)
# TODO : check if we are not already connected to it # TODO : check if we are not already connected to it
elif script_type == 'client-disconnect': elif script_type == 'client-disconnect':
log.log('%s has disconnected' % (common_name,), 3) log.log('%s has disconnected' % (arg,), 3)
elif script_type == 'ipchange':
# TODO: save the external ip received
log.log('External Ip : ' + arg, 3)
else: else:
log.log('Unknow message recieved from the openvpn pipe : ' + msg, 1) log.log('Unknow message recieved from the openvpn pipe : ' + msg, 1)
...@@ -220,7 +224,7 @@ def main(): ...@@ -220,7 +224,7 @@ def main():
log.log('Starting openvpn server', 3) log.log('Starting openvpn server', 3)
serverProcess = openvpn.server(config.ip, write_pipe, '--dev', 'vifibnet', serverProcess = openvpn.server(config.ip, write_pipe, '--dev', 'vifibnet',
stdout=os.open(os.path.join(config.log, 'vifibnet.server.log'), os.O_WRONLY | os.O_CREAT | os.O_TRUNC)) stdout=os.open(os.path.join(config.log, 'vifibnet.server.log'), os.O_WRONLY | os.O_CREAT | os.O_TRUNC))
startNewConnection(config.client_count) startNewConnection(config.client_count, write_pipe)
# Timed refresh initializing # Timed refresh initializing
next_refresh = time.time() + config.refresh_time next_refresh = time.time() + config.refresh_time
......
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