Commit bc5d0fc1 authored by Ulysse Beaugnon's avatar Ulysse Beaugnon

Vifibnet now establish a few random connection from a list a given peers and...

Vifibnet now establish a few random connection from a list a given peers and change some evry 2 minutes
parent cb4cb822
......@@ -2,6 +2,7 @@
import argparse, errno, os, subprocess, sys, time
import upnpigd
import openvpn
import random
VIFIB_NET = "2001:db8:42::/48"
......@@ -37,19 +38,78 @@ def getConfig():
_('--ca', required=True, help='Path to the certificate authority')
_('--key', required=True, help='Path to the rsa_key')
_('--cert', required=True, help='Pah to the certificate')
# connections establishement
_('--max-peer', help='the number of peers that can connect to the server', default='10')
# TODO : use it
_('--client-count', help='the number servers the peers try to connect to', default = '2')
_('--refresh-time', help='the time (seconds) to wait before changing the connections', default = '20')
# TODO : use it
_('--refresh-count', help='The number of connections to drop when refreshing the connections', default='1')
# TODO : use it
# Temporary args
_('--ip', required=True, help='IPv6 of the server')
config = parser.parse_args()
def startNewConnection():
try:
peer = random.choice(avalaiblePeers.keys())
if config.verbose > 2:
print 'Establishing a connection with ' + peer
del avalaiblePeers[peer]
connections[peer] = openvpn.client(config, peer)
except Exception:
pass
# TODO :
def killConnection(peer):
if config.verbose > 2:
print 'Killing the connection with ' + peer
subprocess.Popen.kill(connections[peer])
del connections[peer]
avalaiblePeers[peer] = 1194 # TODO : give the real port
def refreshConnections():
try:
for i in range(0, int(config.refresh_count)):
peer = random.choice(connections.keys())
killConnection(peer)
except Exception:
pass
for i in range(len(connections), int(config.client_count)):
startNewConnection()
def main():
# init variables
global connections
global avalaiblePeers # the list of peers we can connect to
avalaiblePeers = { '10.1.4.2' : 1194, '10.1.4.3' : 1194, '10.1.3.2' : 1194 }
connections = {} # to remember current connections
getConfig()
if config.ip != 'none':
serverProcess = openvpn.server(config, config.ip)
else:
client1Process = openvpn.client(config, '10.1.4.2')
(externalIp, externalPort) = upnpigd.GetExternalInfo(1194)
try:
del avalaiblePeers[externalIp]
except Exception:
pass
# establish connections
serverProcess = openvpn.server(config, config.ip)
for i in range(0, int(config.client_count)):
startNewConnection()
# main loop
try:
while True:
time.sleep(float(config.refresh_time))
refreshConnections()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()
# TODO : pass the remote port as an argument to openvpn
# TODO : remove incomming connections from avalaible peers
......@@ -29,8 +29,8 @@ def server(config, ip):
def client(config, serverIp):
return openvpn(config,
'--nobind',
'--tls-client',
'--remote', serverIp,
'--up', 'up-client')
'--nobind',
'--tls-client',
'--remote', serverIp,
'--up', 'up-client')
......@@ -22,7 +22,7 @@ def ForwardViaUPnP(localPort):
def GetLocalIp():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 0))
s.connect(('10.8.8.8', 0))
return s.getsockname()[0]
......
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