various fixes

This commit is contained in:
Maximilian Hils 2015-08-15 16:26:12 +02:00
parent 747699b126
commit 0dd243c5e4
4 changed files with 36 additions and 24 deletions

View File

@ -5,7 +5,6 @@ from ..exceptions import InvalidCredentials, HttpException, ProtocolException
from .layer import Layer, ServerConnectionMixin from .layer import Layer, ServerConnectionMixin
from libmproxy import utils from libmproxy import utils
from .messages import ChangeServer, Connect, Reconnect, Kill from .messages import ChangeServer, Connect, Reconnect, Kill
from .http_proxy import HttpProxy, HttpUpstreamProxy
from libmproxy.protocol import KILL from libmproxy.protocol import KILL
from libmproxy.protocol.http import HTTPFlow from libmproxy.protocol.http import HTTPFlow
@ -66,13 +65,18 @@ def make_connect_response(httpversion):
) )
class HttpLayer(Layer, ServerConnectionMixin): class HttpLayer(Layer):
""" """
HTTP 1 Layer HTTP 1 Layer
""" """
def __init__(self, ctx): def __init__(self, ctx):
super(HttpLayer, self).__init__(ctx) super(HttpLayer, self).__init__(ctx)
# FIXME: Imports
from .http_proxy import HttpProxy, HttpUpstreamProxy
if any(isinstance(l, HttpProxy) for l in self.layers): if any(isinstance(l, HttpProxy) for l in self.layers):
self.mode = "regular" self.mode = "regular"
elif any(isinstance(l, HttpUpstreamProxy) for l in self.layers): elif any(isinstance(l, HttpUpstreamProxy) for l in self.layers):
@ -199,7 +203,7 @@ class HttpLayer(Layer, ServerConnectionMixin):
self.send_to_server(flow.request) self.send_to_server(flow.request)
flow.response = HTTP1.read_response( flow.response = HTTP1.read_response(
self.server_conn.protocol, self.server_conn,
flow.request.method, flow.request.method,
body_size_limit=self.config.body_size_limit, body_size_limit=self.config.body_size_limit,
include_body=False, include_body=False,
@ -215,6 +219,7 @@ class HttpLayer(Layer, ServerConnectionMixin):
flow.response.content = CONTENT_MISSING flow.response.content = CONTENT_MISSING
else: else:
flow.response.content = HTTP1.read_http_body( flow.response.content = HTTP1.read_http_body(
self.server_conn,
flow.response.headers, flow.response.headers,
self.config.body_size_limit, self.config.body_size_limit,
flow.request.method, flow.request.method,
@ -303,7 +308,7 @@ class HttpLayer(Layer, ServerConnectionMixin):
expected_request_forms = { expected_request_forms = {
"regular": ("absolute",), # an authority request would already be handled. "regular": ("absolute",), # an authority request would already be handled.
"upstream": ("authority", "absolute"), "upstream": ("authority", "absolute"),
"transparent": ("regular",) "transparent": ("relative",)
} }
allowed_request_forms = expected_request_forms[self.mode] allowed_request_forms = expected_request_forms[self.mode]
@ -314,6 +319,9 @@ class HttpLayer(Layer, ServerConnectionMixin):
self.send_to_client(make_error_response(400, err_message)) self.send_to_client(make_error_response(400, err_message))
raise HttpException(err_message) raise HttpException(err_message)
if self.mode == "regular":
request.form_out = "relative"
def authenticate(self, request): def authenticate(self, request):
if self.config.authenticator: if self.config.authenticator:
if self.config.authenticator.authenticate(request.headers): if self.config.authenticator.authenticate(request.headers):
@ -327,10 +335,10 @@ class HttpLayer(Layer, ServerConnectionMixin):
raise InvalidCredentials("Proxy Authentication Required") raise InvalidCredentials("Proxy Authentication Required")
def send_to_server(self, message): def send_to_server(self, message):
self.server_conn.wfile.wrie(message) self.server_conn.send(HTTP1.assemble(message))
def send_to_client(self, message): def send_to_client(self, message):
# FIXME # FIXME
# - possibly do some http2 stuff here # - possibly do some http2 stuff here
# - fix message assembly. self.client_conn.send(HTTP1.assemble(message))
self.client_conn.wfile.write(message)

View File

@ -1,6 +1,7 @@
""" """
Temporary mock to sort out API discrepancies Temporary mock to sort out API discrepancies
""" """
from libmproxy.protocol.http_wrappers import HTTPResponse, HTTPRequest
from netlib.http.http1 import HTTP1Protocol from netlib.http.http1 import HTTP1Protocol
@ -10,14 +11,14 @@ class HTTP1(object):
""" """
:type connection: object :type connection: object
""" """
return HTTP1Protocol(connection).read_request(*args, **kwargs) return HTTPRequest.wrap(HTTP1Protocol(connection).read_request(*args, **kwargs))
@staticmethod @staticmethod
def read_response(connection, *args, **kwargs): def read_response(connection, *args, **kwargs):
""" """
:type connection: object :type connection: object
""" """
return HTTP1Protocol(connection).read_response(*args, **kwargs) return HTTPResponse.wrap(HTTP1Protocol(connection).read_response(*args, **kwargs))
@staticmethod @staticmethod
def read_http_body(connection, *args, **kwargs): def read_http_body(connection, *args, **kwargs):
@ -28,19 +29,13 @@ class HTTP1(object):
@staticmethod @staticmethod
def _assemble_response_first_line(connection, *args, **kwargs): def _assemble_response_first_line(*args, **kwargs):
""" return HTTP1Protocol()._assemble_response_first_line(*args, **kwargs)
:type connection: object
"""
return HTTP1Protocol(connection)._assemble_response_first_line(*args, **kwargs)
@staticmethod @staticmethod
def _assemble_response_headers(connection, *args, **kwargs): def _assemble_response_headers(*args, **kwargs):
""" return HTTP1Protocol()._assemble_response_headers(*args, **kwargs)
:type connection: object
"""
return HTTP1Protocol(connection)._assemble_response_headers(*args, **kwargs)
@staticmethod @staticmethod
@ -48,4 +43,8 @@ class HTTP1(object):
""" """
:type connection: object :type connection: object
""" """
return HTTP1Protocol(connection).read_http_body_chunked(*args, **kwargs) return HTTP1Protocol(connection).read_http_body_chunked(*args, **kwargs)
@staticmethod
def assemble(*args, **kwargs):
return HTTP1Protocol().assemble(*args, **kwargs)

View File

@ -45,6 +45,7 @@ class _LayerCodeCompletion(object):
""" """
def __init__(self): def __init__(self):
super(_LayerCodeCompletion, self).__init__()
if True: if True:
return return
self.config = None self.config = None
@ -94,7 +95,7 @@ class Layer(_LayerCodeCompletion):
return [self] + self.ctx.layers return [self] + self.ctx.layers
def __repr__(self): def __repr__(self):
return "%s\r\n %s" % (self.__class__.name__, repr(self.ctx)) return type(self).__name__
class ServerConnectionMixin(object): class ServerConnectionMixin(object):
@ -103,6 +104,7 @@ class ServerConnectionMixin(object):
""" """
def __init__(self): def __init__(self):
super(ServerConnectionMixin, self).__init__()
self._server_address = None self._server_address = None
self.server_conn = None self.server_conn = None
@ -114,8 +116,11 @@ class ServerConnectionMixin(object):
elif message == Connect: elif message == Connect:
self._connect() self._connect()
return True return True
elif message == ChangeServer: elif message == ChangeServer and message.depth == 1:
raise NotImplementedError if self.server_conn:
self._disconnect()
self.server_address = message.address
return True
elif message == Kill: elif message == Kill:
self._disconnect() self._disconnect()

View File

@ -80,7 +80,7 @@ class ConnectionHandler2:
self.config, self.config,
self.channel self.channel
) )
root_layer = protocol2.Socks5Proxy(root_context) root_layer = protocol2.HttpProxy(root_context)
try: try:
for message in root_layer(): for message in root_layer():