问题
I'm running mitmproxy in upstream mode. In my script I'm inspecting for a header, X-Direct
, which indicates that the request should not be sent to the upstream proxy and instead be sent directly to the server.
I figured out how to modify the proxy mode on a per-request basis using the next_layer()
hook:
def next_layer(next_layer):
ctx.log.info(f'next_layer = {repr(next_layer)}')
if isinstance(next_layer, Http1Layer):
next_layer.mode = HTTPMode.regular
This hook executes too early and I can't (or don't know how) to inspect request headers in that context. Hooking request
allows me to inspect flow.request
but by that point the server connection (via the upstream proxy) has already been established.
I tried just closing the server connection and instantiating a mitmproxy.connections.ServerConnection
to replace flow.server_conn
. This didn't work because, I think, other layers referenced the old upstream proxy connection.
What is the most elegant way to, based on the contents of flow.request.headers
, switch from upstream to regular proxy mode for just that flow?
来源:https://stackoverflow.com/questions/61702363/how-can-i-switch-mitmproxy-mode-based-on-attributes-of-the-proxied-request