How can I switch mitmproxy mode based on attributes of the proxied request

放肆的年华 提交于 2021-02-10 14:51:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!