Merge pull request #29 from hessu/master

Reverse proxy mode for mitmproxy
This commit is contained in:
Aldo Cortesi 2012-02-16 22:57:58 -08:00
commit fcc874fa18
2 changed files with 18 additions and 2 deletions

View File

@ -64,6 +64,11 @@ def common_options(parser):
action="store", type = "int", dest="port", default=8080,
help = "Proxy service port."
)
parser.add_option(
"-R",
action="store", dest="reverse_upstream", default=None,
help="Reverse proxy: upstream server host:port"
)
parser.add_option(
"-q",
action="store_true", dest="quiet",

View File

@ -22,13 +22,14 @@ class ProxyError(Exception):
class ProxyConfig:
def __init__(self, certfile = None, ciphers = None, cacert = None, cert_wait_time=0, body_size_limit = None):
def __init__(self, certfile = None, ciphers = None, cacert = None, cert_wait_time=0, body_size_limit = None, reverse_upstream=None):
self.certfile = certfile
self.ciphers = ciphers
self.cacert = cacert
self.certdir = None
self.cert_wait_time = cert_wait_time
self.body_size_limit = body_size_limit
self.reverse_upstream = reverse_upstream
def read_chunked(fp, limit):
@ -347,6 +348,15 @@ class ProxyHandler(SocketServer.StreamRequestHandler):
self.rfile = FileLike(self.connection)
self.wfile = FileLike(self.connection)
method, scheme, host, port, path, httpminor = parse_request_line(self.rfile.readline())
# If we're in reverse proxy mode, we only get the path and
# version in the request and need to fill up host and port
# from the configuration. This still assumes that the client will
# provide the correct Host: header and we do not need to tamper
# with that (or will tamper using other means).
if self.config.reverse_upstream:
scheme = 'http'
host, port = self.config.reverse_upstream.split(':')
port = int(port)
if scheme is None:
scheme = "https"
headers = flow.Headers()
@ -488,7 +498,8 @@ def process_proxy_options(parser, options):
cacert = cacert,
ciphers = options.ciphers,
cert_wait_time = options.cert_wait_time,
body_size_limit = body_size_limit
body_size_limit = body_size_limit,
reverse_upstream = options.reverse_upstream
)