【问题标题】:Mitmproxy load and unload scripts with pythonMitmproxy 使用 python 加载和卸载脚本
【发布时间】:2014-06-11 07:09:26
【问题描述】:

我正在按照 Mitmproxy github examples 中的建议运行代理:

from libmproxy import proxy, flow

class MitmProxy(flow.FlowMaster):
    def run(self):
        try:
            flow.FlowMaster.run(self)
        except KeyboardInterrupt:
            self.shutdown()

  
    def handle_request(self, r):
        f = flow.FlowMaster.handle_request(self, r)

        if f:
            r.reply()
        return f

    def handle_response(self, r):
        f = flow.FlowMaster.handle_response(self, r)
 
        if f:
            r.reply()
        return f



config = proxy.ProxyConfig(
    cacert = os.path.expanduser("~/.ssl/mitmproxy.pem")
)
state = flow.State()
server = proxy.ProxyServer(config, 8083)
m = MitmProxy(server, state)
try:
    m.run()
except Exception, e:
    print e.message
    m.shutdown()

我想在不阻塞其他请求/响应的情况下处理每个请求/响应, 为此我需要使用并发装饰器和脚本

我的问题是:如何将脚本加载和卸载到在此配置中运行的代理?

【问题讨论】:

    标签: python concurrency proxy mitmproxy libmproxy


    【解决方案1】:

    您可以在脚本加载中使用并发模式。
    这是用于这种用法的example

    我更喜欢在流级别实现 mitmproxy 逻辑。
    您可以使用此代码

    def handle_response(self, r):
        reply = f.response.reply
            f.response.reply = controller.DummyReply()
            if hasattr(reply, "q"):
                f.response.reply.q = reply.q
            def run(): 
                pass
            threading.Thread(target=run)
    

    【讨论】:

    • tnx,这对我帮助很大。我还尝试通过执行从代码中加载脚本。 m.load_script(path to script) 但它给了我一些错误。经过进一步挖掘,我发现有人将其报告为错误,并且开发人员修复了它,这是问题的链接:https://github.com/mitmproxy/mitmproxy/issues/267
    【解决方案2】:

    你基本上必须复制libmproxy.script中handle_concurrent_reply的工作方式

    f = flow.FlowMaster.handle_request(self,r)
    if f:
            def run():
               request.reply() #if you forget this you'll end up in a loop and never reply
    threading.Thread(target=run).start()  #this will start run
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多