Search code examples
pythonmitmproxy

Can't switch Upstream Proxy when Http Error occur


Problem Description

When I try to use flow.live.change_upstream_proxy_server(address) to switch the upstream Proxy in Error() method from HttpEvents, the upstream proxy stay the same and does not change.
I based my code on this example.

Steps to reproduce the behavior:

  1. launch Mitm with an upstream and script: mitmdump --mode upstream:182.52.74.76:34084 --ssl-insecure -s scripts/watcher.py
  2. wait for upstream proxy to fail
  3. let watcher.py renew the upstream proxy

Current result:

(can't post image directly due to lack of reputation)
https://i.sstatic.net/YUpRB.png

At this point we assume that the upstream proxy is now changed to 103.199.84.54:8080, but when we renew a request, we can see in the error message that the proxy is still the same (182.52.74.76:34084) instead of 103.199.84.54:8080 :

https://i.sstatic.net/Ko95D.png

Watcher.py

from mitmproxy import http
from mitmproxy import ctx

import requests
import json
import typing

#***** Some fetching to get new proxy from my API*****

def getNewProxy() -> typing.Tuple[str, int]:
    id = str(getRandID())
    params = { 'id': 'eq.' + id, 'select': 'proxy'}
    response = str(requests.get(url = url, params = params).json()[0]['proxy'])[7:].split(':')
    proxIP = (response[0], int(response[1]))
    return (proxIP)

class Events:
    
    def error(self, flow: http.HTTPFlow):
        ctx.log.info(str(flow.server_conn.address))
        pAddr = getNewProxy()
        ctx.log.info("Switching upstream proxy to: " + pAddr[0] + ":" + str(pAddr[1]))
        flow.live.change_upstream_proxy_server(pAddr)

addons = [
    Events()
]

System Information

mitmproxy --version:

$ mitmdump --version
Mitmproxy: 5.2
Python:    3.7.3
OpenSSL:   OpenSSL 1.1.1g  21 Apr 2020
Platform:  Linux-4.19.0-9-amd64-x86_64-with-debian-10.4

Thanks for your help, really annoyed by this problem right now !


Solution

  • It turns out that Mitproxy itself does not support retrying requests at the moment.
    See this.