I am trying to build a reverse proxy (with twisted). The reverse proxy listens on ports 6000-6099 and should map those ports to different internal IP addresses. Once a connection is made to a port, it should do some pre-checks, like starting a virtual machine in a cluster.
Example:
PublicIP:6000 -> do pre-check -> forward traffic to InternalIP-1:6800
PublicIP:6001 -> do pre-check -> forward traffic to InternalIP-2:6800
...
I modified an example I found here (section 'Proxies and reverse proxies'). But I can't get it to work. Can anybody help?
from twisted.web import proxy, http
from twisted.internet import reactor
from twisted.python import log
import sys
log.startLogging(sys.stdout)
machines = {}
class ProxyFactory(http.HTTPFactory):
protocol = proxy.ReverseProxy
def connectionMade(self):
if not machines.has_key(self.request.port): # self.request.port?!
# start new machine in cluster
# machines[self.request.port] = new_machine_ip
# reverse proxy to machines[self.request.port] on port 6800
# return proxy.ReverseProxyResource(machines[self.request.port], 6800, '/')
for port in range(6000,6100):
reactor.listenTCP(port, ProxyFactory())
reactor.run()
Edit:
- How can I get the port of the current request?
- How to pass traffic to the internal IP?