2014-01-05 00:03:55 +00:00
|
|
|
from libmproxy.proxy import ProxyError, ConnectionHandler
|
|
|
|
from netlib import http
|
|
|
|
|
2014-01-07 01:29:10 +00:00
|
|
|
|
2014-01-05 00:03:55 +00:00
|
|
|
def handle_messages(conntype, connection_handler):
|
|
|
|
handler = None
|
|
|
|
if conntype == "http":
|
|
|
|
handler = HTTPHandler(connection_handler)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
return handler.handle_messages()
|
|
|
|
|
|
|
|
|
2014-01-07 01:29:10 +00:00
|
|
|
class ConnectionTypeChange(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-01-05 00:03:55 +00:00
|
|
|
class ProtocolHandler(object):
|
|
|
|
def __init__(self, c):
|
|
|
|
self.c = c
|
|
|
|
|
2014-01-07 01:29:10 +00:00
|
|
|
|
2014-01-05 00:03:55 +00:00
|
|
|
class HTTPHandler(ProtocolHandler):
|
|
|
|
|
|
|
|
def handle_messages(self):
|
|
|
|
while self.handle_request():
|
|
|
|
pass
|
|
|
|
self.c.close = True
|
|
|
|
|
|
|
|
def handle_request(self):
|
|
|
|
request = self.read_request()
|
|
|
|
if request is None:
|
|
|
|
return
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def read_request(self):
|
|
|
|
self.c.client_conn.rfile.reset_timestamps()
|
|
|
|
|
|
|
|
request_line = self.get_line(self.c.client_conn.rfile)
|
|
|
|
method, path, httpversion = http.parse_init(request_line)
|
|
|
|
headers = self.read_headers(authenticate=True)
|
|
|
|
|
2014-01-07 01:29:10 +00:00
|
|
|
if self.c.mode == "regular":
|
2014-01-05 00:03:55 +00:00
|
|
|
if method == "CONNECT":
|
|
|
|
r = http.parse_init_connect(request_line)
|
|
|
|
if not r:
|
|
|
|
raise ProxyError(400, "Bad HTTP request line: %s"%repr(request_line))
|
|
|
|
host, port, _ = r
|
2014-01-07 01:29:10 +00:00
|
|
|
if self.c.config.forward_proxy:
|
|
|
|
#FIXME: Treat as request, no custom handling
|
|
|
|
self.c.server_conn.wfile.write(request_line)
|
2014-01-05 00:03:55 +00:00
|
|
|
for key, value in headers.items():
|
2014-01-07 01:29:10 +00:00
|
|
|
self.c.server_conn.wfile.write("%s: %s\r\n"%(key, value))
|
|
|
|
self.c.server_conn.wfile.write("\r\n")
|
2014-01-05 00:03:55 +00:00
|
|
|
else:
|
2014-01-07 01:29:10 +00:00
|
|
|
self.c.server_address = (host, port)
|
|
|
|
self.c.establish_server_connection()
|
2014-01-05 00:03:55 +00:00
|
|
|
|
2014-01-07 01:29:10 +00:00
|
|
|
self.c.handle_ssl()
|
|
|
|
self.c.determine_conntype("transparent", host, port)
|
|
|
|
raise ConnectionTypeChange
|
2014-01-05 00:03:55 +00:00
|
|
|
else:
|
|
|
|
r = http.parse_init_proxy(request_line)
|
|
|
|
if not r:
|
|
|
|
raise ProxyError(400, "Bad HTTP request line: %s"%repr(request_line))
|
|
|
|
method, scheme, host, port, path, httpversion = r
|
2014-01-07 01:29:10 +00:00
|
|
|
if not self.c.config.forward_proxy:
|
|
|
|
if (not self.c.server_conn) or (self.c.server_address != (host, port)):
|
|
|
|
self.c.server_address = (host, port)
|
|
|
|
self.c.establish_server_connection()
|
2014-01-05 00:03:55 +00:00
|
|
|
|
|
|
|
def get_line(self, fp):
|
|
|
|
"""
|
|
|
|
Get a line, possibly preceded by a blank.
|
|
|
|
"""
|
|
|
|
line = fp.readline()
|
|
|
|
if line == "\r\n" or line == "\n": # Possible leftover from previous message
|
|
|
|
line = fp.readline()
|
|
|
|
return line
|
|
|
|
|
|
|
|
def read_headers(self, authenticate=False):
|
2014-01-07 01:29:10 +00:00
|
|
|
headers = http.read_headers(self.c.client_conn.rfile)
|
2014-01-05 00:03:55 +00:00
|
|
|
if headers is None:
|
|
|
|
raise ProxyError(400, "Invalid headers")
|
2014-01-07 01:29:10 +00:00
|
|
|
if authenticate and self.c.config.authenticator:
|
|
|
|
if self.c.config.authenticator.authenticate(headers):
|
|
|
|
self.c.config.authenticator.clean(headers)
|
2014-01-05 00:03:55 +00:00
|
|
|
else:
|
|
|
|
raise ProxyError(
|
|
|
|
407,
|
|
|
|
"Proxy Authentication Required",
|
2014-01-07 01:29:10 +00:00
|
|
|
self.c.config.authenticator.auth_challenge_headers()
|
2014-01-05 00:03:55 +00:00
|
|
|
)
|
|
|
|
return headers
|