views:

30

answers:

0

Hi I'm having trouble trying to write a connection manager for OpenVPN in python. My needs are to have a script that listens on port 1195 UDP and when a connections is established, it should forward all the packages to the server instance of OpenVPN. The reason I need to do this is that I will have 4 instances of OpenVPN running in the same server. I need this so that I can do some load balancing. I know that OpenVPN offers a solution on load balancing itself, but my boss wants me to implement it this way. So far I have managed to create a connection between client and the script; and the server and the script, but I can't get the script to deliver the right packets to client and server. The code I have is as follows. Any help is truly appreciated!

import socket

# user-accessible port
PORT = 1195

#OpenVPN Server address
SIP = "127.0.0.1"

# establish server
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
client.bind((SIP, PORT))

#wait for connections
print ('Server started! Listening on port:', PORT)
while 1:
 # serve forever
 data, recv = client.recvfrom(1024)
 print ("Connection attempt from", recv[0])
 print ("with data", data)
 client.sendto(data, (SIP, 1194)) # send
 data2, serv = client.recvfrom(1024)
 print ("server data", data2, serv)
 client.sendto(data2, recv)