使用Python在Mitmproxy中加载和卸载脚本
我正在按照Mitmproxy的GitHub上建议的方式运行一个代理,具体可以参考示例:
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()
我想处理每个请求和响应,而不影响其他的请求和响应。为此,我需要使用并发装饰器和脚本。
我的问题是:在这种配置下,我该如何加载和卸载代理中的脚本呢?
2 个回答
0
你基本上需要照着 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
3
你可以在加载脚本的时候使用并发模式。
这里有一个示例,展示了这种用法。
我更喜欢在流程层面实现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)