Saturday, March 7, 2020

A simple http proxy:
Software applications are increasingly deployed on linux containers some of which don’t have external connectivity except to their host. These applications require traffic to come and go via their host. Often this involves tweaking the IP routing table or port forwarding between the container and the host. This article acknowledges the fact that http traffic can transcend the networking divide by merely requiring a proxy in the middle.
A sample HTTP proxy program would look something like this:

#! /bin/python
import SocketServer
import SimpleHTTPServer
import urllib

PORT = 9000

class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
         remote_url = 'http://' +
                destination_ip+':'+destination_port+self.path
        self.copyfile(urllib.urlopen(remote_url), self.wfile)

    def do_POST(self):
        remote_url = 'http://' +
                destination_ip+':'+destination_port+self.path
        self.copyfile(urllib.urlopen(remote_url, self.request.data), self.wfile)

httpd = SocketServer.ForkingTCPServer(('', PORT), Proxy)
print "serving at port", PORT
httpd.serve_forever()

The above program is a sample for reading purposes only. It could be wrapped in a Django server such as this one. It is also easy to modify the program above to replace all destination address so that the proxy does not disclose the backend. A truly transparent proxy is also possible with the help of http headers. This http proxy does the same relaying as a one-armed router except that this is at http layer instead of the IP layer.
Other benefits of http proxy involve troubleshooting, client and destination statistics, and several gateway rules and classifiers.

No comments:

Post a Comment