Commit b4c5fdd0 authored by Ulysse Beaugnon's avatar Ulysse Beaugnon

Merge branch 'master' of https://git.erp5.org/repos/vifibnet

parents 3309f6f5 cdd5c554
...@@ -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)
#!/usr/bin/env python #!/usr/bin/env python
import argparse, math, random, smtplib, sqlite3, string, time import argparse, math, random, smtplib, sqlite3, string, struct, socket, time
from email.mime.text import MIMEText from email.mime.text import MIMEText
from functools import wraps from functools import wraps
from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
...@@ -20,6 +20,10 @@ class main(object): ...@@ -20,6 +20,10 @@ class main(object):
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Peer discovery http server for vifibnet') description='Peer discovery http server for vifibnet')
_ = parser.add_argument _ = parser.add_argument
_('--prefix', required=True,
help='Prefix of the network deployed ( example : 2001:db8:42')
_('--prefix-len', required=True, type=int,
help='Prefix length')
_('--db', required=True, _('--db', required=True,
help='Path to database file') help='Path to database file')
_('--ca', required=True, _('--ca', required=True,
...@@ -140,15 +144,23 @@ class main(object): ...@@ -140,15 +144,23 @@ class main(object):
return crypto.dump_certificate(crypto.FILETYPE_PEM, self.ca) return crypto.dump_certificate(crypto.FILETYPE_PEM, self.ca)
def declare(self, handler, address): def declare(self, handler, address):
# guess prefix from handler.client_address client_address, _ = handler.client_address
ip1, ip2 = struct.unpack('>QQ', socket.inet_pton(socket.AF_INET6, handler.client_address))) # For Testing purposes only
prefix = bin(ip1)[2:] + bin(ip2)]2:] client_address = "2001:db8:42::"
assert(client_address.startswith(self.config.prefix))
ip1, ip2 = struct.unpack('>QQ', socket.inet_pton(socket.AF_INET6, client_address))
ip1 = bin(ip1)[2:].rjust(64, '0')
ip2 = bin(ip2)[2:].rjust(64, '0')
prefix = (ip1 + ip2)[self.config.prefix_len:]
prefix, = self.db.execute("SELECT prefix FROM vifib WHERE prefix <= ? ORDER BY prefix DESC", (prefix,)).next()
ip, port, proto = address ip, port, proto = address
self.db.execute("INSERT INTO peers VALUES (?,?,?,?)", (prefix, ip, port, proto)) self.db.execute("INSERT OR REPLACE INTO peers VALUES (?,?,?,?)", (prefix, ip, port, proto))
def getPeerList(self, handler, n, address): def getPeerList(self, handler, n, address):
assert 0 < n < 1000 assert 0 < n < 1000
print "declaring new node"
self.declare(handler, address) self.declare(handler, address)
print "sending peers"
return self.db.execute("SELECT ip, port, proto FROM peers ORDER BY random() LIMIT ?", (n,)).fetchall() return self.db.execute("SELECT ip, port, proto FROM peers ORDER BY random() LIMIT ?", (n,)).fetchall()
......
#!/usr/bin/env python #!/usr/bin/env python
import argparse, errno, os, select, sqlite3, subprocess, sys, time, xmlrpclib import argparse, errno, math, os, select, sqlite3, subprocess, sys, time, xmlrpclib
from OpenSSL import crypto
import traceback import traceback
import upnpigd import upnpigd
import openvpn import openvpn
import random import random
import log import log
VIFIB_NET = "2001:db8:42::/48" VIFIB_NET = "2001:db8:42:"
VIFIB_LEN = 48
connection_dict = {} # to remember current connections we made connection_dict = {} # to remember current connections we made
free_interface_set = set(('client1', 'client2', 'client3', 'client4', 'client5', 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))
...@@ -36,9 +39,11 @@ class PeersDB: ...@@ -36,9 +39,11 @@ class PeersDB:
self.populateDB(100) self.populateDB(100)
def populateDB(self, n): def populateDB(self, n):
log.log('Populating Peers DB', 2)
(ip, port) = upnpigd.GetExternalInfo(1194) (ip, port) = upnpigd.GetExternalInfo(1194)
proto = 'udp' proto = 'udp'
self.db.executemany("INSERT INTO peers (ip, port, proto) VALUES ?", self.proxy.getPeerList(n, port, proto)) new_peer_list = self.proxy.getPeerList(n, (ip, port, proto))
self.db.executemany("INSERT INTO peers (ip, port, proto) VALUES (?,?,?)", new_peer_list)
def getUnusedPeers(self, nPeers): def getUnusedPeers(self, nPeers):
return self.db.execute("SELECT id, ip, port, proto FROM peers WHERE used = 0 " return self.db.execute("SELECT id, ip, port, proto FROM peers WHERE used = 0 "
...@@ -52,19 +57,21 @@ class PeersDB: ...@@ -52,19 +57,21 @@ 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 = hew(int(prefix, 2))[2::] tmp = hex(int(prefix))[2:]
tmp = tmp.rjust(int((math.ceil(float(prefix_len) / 4))), '0')
ip = VIFIB_NET ip = VIFIB_NET
for i in xrange(0, len(ip), 4): for i in xrange(0, len(tmp), 4):
ip += tmp[i:i+4] + ':' ip += tmp[i:i+4] + ':'
ip += ':' return ip + ':'
def startBabel(**kw): def startBabel(**kw):
args = ['babeld', args = ['babeld',
'-C', 'redistribute local ip %s' % (config.ip), '-C', 'redistribute local ip %s' % (config.ip),
'-C', 'redistribute local deny', '-C', 'redistribute local deny',
# Route VIFIB ip adresses # Route VIFIB ip adresses
'-C', 'in ip %s' % VIFIB_NET, '-C', 'in ip %s:/%u' % (VIFIB_NET, VIFIB_LEN),
# Route only addresse in the 'local' network, # Route only addresse in the 'local' network,
# or other entire networks # or other entire networks
#'-C', 'in ip %s' % (config.ip), #'-C', 'in ip %s' % (config.ip),
...@@ -85,9 +92,9 @@ def getConfig(): ...@@ -85,9 +92,9 @@ def getConfig():
_ = parser.add_argument _ = parser.add_argument
_('--server', required=True, _('--server', required=True,
help='Address for peer discovery server') help='Address for peer discovery server')
_('--server-port', required=True, _('--server-port', required=True, type=int,
help='Peer discovery server port') help='Peer discovery server port')
_('--log-directory', default='/var/log', _('-l', '--log', default='/var/log',
help='Path to vifibnet logs directory') help='Path to vifibnet logs directory')
_('--client-count', default=2, type=int, _('--client-count', default=2, type=int,
help='Number of client connections') help='Number of client connections')
...@@ -109,32 +116,33 @@ def getConfig(): ...@@ -109,32 +116,33 @@ def getConfig():
_('--cert', required=True, _('--cert', required=True,
help='Path to the certificate file') help='Path to the certificate file')
# Temporary args - to be removed # Temporary args - to be removed
# Can be removed, should ip be a global variable ?
_('--ip', required=True, _('--ip', required=True,
help='IPv6 of the server') help='IPv6 of the server')
# Openvpn options # Openvpn options
_('openvpn_args', nargs=argparse.REMAINDER, _('openvpn_args', nargs=argparse.REMAINDER,
help="Common OpenVPN options (e.g. certificates)") help="Common OpenVPN options (e.g. certificates)")
openvpn.config = config = parser.parse_args() openvpn.config = config = parser.parse_args()
log.verbose = config.verbose
with open(config.cert, 'r') as f: with open(config.cert, 'r') as f:
cert = crypto.load_certificate(crypto.FILETYPE_PEM, f) cert = crypto.load_certificate(crypto.FILETYPE_PEM, f.read())
subject = cert.get_subject() subject = cert.get_subject()
prefix_txt, prefix_len_txt = subject.serialNumber.split('/') prefix, prefix_len = subject.serialNumber.split('/')
prefix = int(prefix_txt) ip = ipFromPrefix(prefix, int(prefix_len))
prefix_len = int(prefix_len_txt) log.log('Intranet ip : %s' % (ip,), 3)
ip = ipFromPrefix(prefix)
print ip
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)
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_directory, '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)
peers_db.usePeer(id) peers_db.usePeer(id)
...@@ -180,12 +188,15 @@ def refreshConnections(): ...@@ -180,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)
...@@ -193,7 +204,7 @@ def main(): ...@@ -193,7 +204,7 @@ def main():
# Get arguments # Get arguments
getConfig() getConfig()
log.verbose = config.verbose log.verbose = config.verbose
# TODO: get proto to use ? # TODO: how do we decide which protocol we use ?
(externalIp, externalPort) = upnpigd.GetExternalInfo(1194) (externalIp, externalPort) = upnpigd.GetExternalInfo(1194)
# Setup database # Setup database
...@@ -202,8 +213,7 @@ def main(): ...@@ -202,8 +213,7 @@ def main():
# Launch babel on all interfaces # Launch babel on all interfaces
log.log('Starting babel', 3) log.log('Starting babel', 3)
babel = startBabel(stdout=os.open('%s/babeld.log' % (config.log_directory,), os.O_WRONLY | os.O_CREAT | os.O_TRUNC), babel = startBabel(stdout=os.open(os.path.join(config.log, 'vifibnet.babeld.log'), os.O_WRONLY | os.O_CREAT | os.O_TRUNC), stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT)
# Create and open read_only pipe to get connect/disconnect events from openvpn # Create and open read_only pipe to get connect/disconnect events from openvpn
log.log('Creating pipe for openvpn events', 3) log.log('Creating pipe for openvpn events', 3)
...@@ -213,8 +223,8 @@ def main(): ...@@ -213,8 +223,8 @@ def main():
# Establish connections # Establish connections
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_directory, '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