Search code examples
pythonproxymitmproxy

Mitmproxy upstream server


Trying to set upstream server in mitmproxy.

The scheme is very simple: Browser -> Mitmproxy -> Proxy -> AnyServer

Here is the code: import mitmproxy from mitmproxy.models import HTTPResponse from netlib.http import Headers from mitmproxy import ctx

def request (flow):
    if flow.request.method == "CONNECT":
        return
    if flow.live:
        print (flow.request.host)
        address = ("92.247.125.246", 3128)
        flow.live.change_upstream_proxy_server(address)

After this a had the SetServerNotAllowedException:

enter image description here

I disabled HTTP/2 in my browser and now there is no errors, but on myip sites I see the IP of my MITM proxy server! So it is not using the last proxy, why? Any ideas please?


Solution

  • I found the answer.

    change_upstream_proxy_server is the function that can only CHANGE the upstream server. It can't enable it if the upstream server was not enabled earlier.

    This mean that if you want to change upstream server you need to start mitmproxy with -U parameter, for example: mitmdump -U http://127.0.0.1:9999 -s server.py

    After this you the change_upstream_proxy_server will work.