2014-06-11 48 views
1

我運行一個代理腳本,在Mitmproxy github上examples建議:Mitmproxy裝載和卸載與Python

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() 

我想要處理不阻塞他人, 的,我需要每個請求/響應使用併發修飾器和腳本

我的問題是:我如何加載和卸載腳本到在此配置中運行的代理?

回答

3

您可以使用腳本加載的併發模式。
這是用於這種用法的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) 
+0

TNX工作,幫助我很多。我也嘗試從代碼中加載腳本。 m.load_script(腳本的路徑),但它給了我一些錯誤。進一步挖掘後,我發現有人報告它是一個錯誤,而開發人員修復它,這裏是問題的鏈接:[https://github.com/mitmproxy/mitmproxy/issues/267](https://github。 COM/mitmproxy/mitmproxy /問題/ 267) – Urban48

0

你基本上覆制如何handle_concurrent_reply在libmproxy.script

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