remove depth attribute from set_server

This commit is contained in:
Maximilian Hils 2015-09-03 18:25:36 +02:00
parent 8da683a638
commit 99126f62ed
4 changed files with 36 additions and 39 deletions

View File

@ -4,7 +4,6 @@
# Usage: mitmdump -U http://default-upstream-proxy.local:8080/ -s change_upstream_proxy.py # Usage: mitmdump -U http://default-upstream-proxy.local:8080/ -s change_upstream_proxy.py
# #
# If you want to change the target server, you should modify flow.request.host and flow.request.port # If you want to change the target server, you should modify flow.request.host and flow.request.port
# flow.live.set_server should only be used by inline scripts to change the upstream proxy.
def proxy_address(flow): def proxy_address(flow):
@ -22,13 +21,4 @@ def request(context, flow):
return return
address = proxy_address(flow) address = proxy_address(flow)
if flow.live: if flow.live:
if flow.request.scheme == "http": flow.live.change_upstream_proxy_server(address)
# For a normal HTTP request, we just change the proxy server and we're done!
if address != flow.live.server_conn.address:
flow.live.set_server(address, depth=1)
else:
# If we have CONNECTed (and thereby established "destination state"), the story is
# a bit more complex. Now we don't want to change the top level address (which is
# the connect destination) but the address below that. (Notice the `.via` and depth=2).
if address != flow.live.server_conn.via.address:
flow.live.set_server(address, depth=2)

View File

@ -116,19 +116,16 @@ class ServerConnectionMixin(object):
"The proxy shall not connect to itself.".format(repr(address)) "The proxy shall not connect to itself.".format(repr(address))
) )
def set_server(self, address, server_tls=None, sni=None, depth=1): def set_server(self, address, server_tls=None, sni=None):
if depth == 1: if self.server_conn:
if self.server_conn: self.disconnect()
self.disconnect() self.log("Set new server address: " + repr(address), "debug")
self.log("Set new server address: " + repr(address), "debug") self.server_conn.address = address
self.server_conn.address = address self.__check_self_connect()
self.__check_self_connect() if server_tls:
if server_tls: raise ProtocolException(
raise ProtocolException( "Cannot upgrade to TLS, no TLS layer on the protocol stack."
"Cannot upgrade to TLS, no TLS layer on the protocol stack." )
)
else:
self.ctx.set_server(address, server_tls, sni, depth - 1)
def disconnect(self): def disconnect(self):
""" """

View File

@ -304,16 +304,22 @@ class UpstreamConnectLayer(Layer):
else: else:
pass # swallow the message pass # swallow the message
def set_server(self, address, server_tls=None, sni=None, depth=1): def change_upstream_proxy_server(self, address):
if depth == 1: if address != self.server_conn.via.address:
if self.ctx.server_conn: self.ctx.set_server(address)
self.ctx.disconnect()
address = Address.wrap(address) def set_server(self, address, server_tls=None, sni=None):
self.connect_request.host = address.host if self.ctx.server_conn:
self.connect_request.port = address.port self.ctx.disconnect()
self.server_conn.address = address address = Address.wrap(address)
else: self.connect_request.host = address.host
self.ctx.set_server(address, server_tls, sni, depth - 1) self.connect_request.port = address.port
self.server_conn.address = address
if server_tls:
raise ProtocolException(
"Cannot upgrade to TLS, no TLS layer on the protocol stack."
)
class HttpLayer(Layer): class HttpLayer(Layer):
@ -388,6 +394,12 @@ class HttpLayer(Layer):
finally: finally:
flow.live = False flow.live = False
def change_upstream_proxy_server(self, address):
# Make set_upstream_proxy_server always available,
# even if there's no UpstreamConnectLayer
if address != self.server_conn.address:
return self.set_server(address)
def handle_regular_mode_connect(self, request): def handle_regular_mode_connect(self, request):
self.set_server((request.host, request.port)) self.set_server((request.host, request.port))
self.send_response(make_connect_response(request.httpversion)) self.send_response(make_connect_response(request.httpversion))

View File

@ -338,13 +338,11 @@ class TlsLayer(Layer):
if self._server_tls and not self.server_conn.tls_established: if self._server_tls and not self.server_conn.tls_established:
self._establish_tls_with_server() self._establish_tls_with_server()
def set_server(self, address, server_tls=None, sni=None, depth=1): def set_server(self, address, server_tls=None, sni=None):
if depth == 1 and server_tls is not None: if server_tls is not None:
self.ctx.set_server(address, None, None, 1)
self._sni_from_server_change = sni self._sni_from_server_change = sni
self._server_tls = server_tls self._server_tls = server_tls
else: self.ctx.set_server(address, None, None)
self.ctx.set_server(address, server_tls, sni, depth)
@property @property
def sni_for_server_connection(self): def sni_for_server_connection(self):