Implemented reverse proxy mode: -R upstreamhost:port makes the

proxy accept a 'GET / HTTP/1.0' request and fill up the destination
host and port from the ones given with -R (for example,
"-R localhost:80").
This commit is contained in:
Heikki Hannikainen 2012-02-16 16:33:27 +02:00
parent f25156a637
commit a82ac9eaf0
2 changed files with 15 additions and 2 deletions

View File

@ -64,6 +64,11 @@ def common_options(parser):
action="store", type = "int", dest="port", default=8080, action="store", type = "int", dest="port", default=8080,
help = "Proxy service port." 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( parser.add_option(
"-q", "-q",
action="store_true", dest="quiet", action="store_true", dest="quiet",

View File

@ -22,13 +22,14 @@ class ProxyError(Exception):
class ProxyConfig: 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.certfile = certfile
self.ciphers = ciphers self.ciphers = ciphers
self.cacert = cacert self.cacert = cacert
self.certdir = None self.certdir = None
self.cert_wait_time = cert_wait_time self.cert_wait_time = cert_wait_time
self.body_size_limit = body_size_limit self.body_size_limit = body_size_limit
self.reverse_upstream = reverse_upstream
def read_chunked(fp, limit): def read_chunked(fp, limit):
@ -347,6 +348,12 @@ class ProxyHandler(SocketServer.StreamRequestHandler):
self.rfile = FileLike(self.connection) self.rfile = FileLike(self.connection)
self.wfile = FileLike(self.connection) self.wfile = FileLike(self.connection)
method, scheme, host, port, path, httpminor = parse_request_line(self.rfile.readline()) 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
if self.config.reverse_upstream:
scheme = 'http'
host, port = self.config.reverse_upstream.split(':')
port = int(port)
if scheme is None: if scheme is None:
scheme = "https" scheme = "https"
headers = flow.Headers() headers = flow.Headers()
@ -488,7 +495,8 @@ def process_proxy_options(parser, options):
cacert = cacert, cacert = cacert,
ciphers = options.ciphers, ciphers = options.ciphers,
cert_wait_time = options.cert_wait_time, cert_wait_time = options.cert_wait_time,
body_size_limit = body_size_limit body_size_limit = body_size_limit,
reverse_upstream = options.reverse_upstream
) )