发生Http错误时无法切换上游代理 问题描述重现行为的步骤:当前结果: Watcher.py 系统信息

问题描述

问题描述

当我尝试使用flow.live.change_upstream_proxy_server(address)Error()方法中的上游代理从HttpEvents切换时,上游代理保持不变且不变。
我的代码基于this示例。

重现行为的步骤:

  1. 使用上游和脚本启动Mitm: mitmdump --mode upstream:182.52.74.76:34084 --ssl-insecure -s scripts/watcher.py
  2. 等待上游代理失败
  3. 让watcher.py续订上游代理

当前结果:

(由于缺乏声誉而无法直接发布图片
https://i.imgur.com/vyz2bq3.png

在这一点上,我们假设上游代理现在已更改为103.199.84.54:8080,但是当我们续订请求时,我们可以在错误消息中看到该代理仍然相同(182.52.74.76:34084)而不是103.199.84.54:8080

https://i.imgur.com/WkWFyBJ.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()
]

系统信息

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

感谢您的帮助,现在这个问题真的使我很恼火!

解决方法

事实证明,Mitproxy本身目前不支持重试请求。
参见this