Commit bdc04ba6 authored by Guillaume Bury's avatar Guillaume Bury

First Commit

parents
vifibnet is a daemon setting up a resilient virtual private network over the internet
CaPath = '/root/overnet/keys/ca.crt'
CertPath = '/root/overnet/keys/server.crt'
KeyPath = '/root/overnet/keys/server.key'
DhPath = '/root/overnet/keys/dh1024.pem'
Subnet = '10.8.0.0'
SubnetMask = '255.255.255.0'
Debug = True
\ No newline at end of file
def CheckVarExists(varName):
return varName in config.__dict__
def FailIfNotExists(varName):
if not CheckVarExists(varName):
print 'Entry ' + varName + ' not found in config.py'
sys.exit(-1)
def SetIfNotExists(varName, value):
if not CheckVarExists(varName):
config.__dict__[varName] = value
# Check that the file config.py exist and is valid
try:
import config
except ImportError:
print 'Unable to read the config file config.py'
sys.exit(-1)
# Check vars existence
FailIfNotExists('CaPath')
FailIfNotExists('CertPath')
FailIfNotExists('KeyPath')
FailIfNotExists('DhPath')
FailIfNotExists('Subnet')
FailIfNotExists('SubnetMask')
SetIfNotExists('Debug', False)
SetIfNotExists('MandatoryConnections', [])
\ No newline at end of file
import upnpigd
from subprocess import call
from configuration import *
# Call == bad !!
# TODO : use subprocess module
def LaunchOpenVpnClient(serverAddress, serverPort):
call(['openvpn',
'--client',
'--dev', 'tun',
'--proto', 'udp',
'--remote', serverAddress, str(serverPort),
'--nobind',
'--persist-key',
'--persist-tun',
'--ca', config.CaPath,
'--cert', config.CertPath,
'--key', config.KeyPath,
'--ns-cert-type', 'server',
'--comp-lzo',
'--verb', '3',
'--daemon', 'openVpnClient(' + serverAddress + ')' ])
def LaunchOpenVpnServer(port):
call(['openvpn',
'--dev', 'tun',
'--proto', 'udp',
'--ca', config.CaPath,
'--cert', config.CertPath,
'--key', config.KeyPath,
'--dh', config.DhPath,
'--server', config.Subnet, config.SubnetMask,
'--port', str(port),
'--ifconfig-pool-persist', 'ipp.txt',
'--comp-lzo',
'--keepalive', '10', '120',
'--persist-tun',
'--persist-key',
'--verb', '3'])
import miniupnpc
import socket
# return (address, port)
def ForwardViaUPnP(localPort):
u = miniupnpc.UPnP()
u.discoverdelay = 200
u.discover()
u.selectigd()
externalPort = 1194
while True:
while u.getspecificportmapping(externalPort, 'TCP') != None:
externalPort = max(externalPort + 1, 49152)
if externalPort == 65536:
raise Exception
if u.addportmapping(externalPort, 'UDP', u.lanaddr, localPort, 'Vifib openvpn server', ''):
return (u.externalipaddress(), externalPort)
# TODO : specify a lease duration
# TODO : use more precises exceptions
# TODO : be sure that GetLocalIp do not bug
def GetLocalIp():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 0))
return s.getsockname()[0]
def GetExternalInfo(localPort):
try:
return ForwardViaUPnP(localPort)
except Exception:
return (GetLocalIp(), localPort)
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