2011-08-04 22:23:32 +00:00
|
|
|
#!/usr/bin/env python
|
2011-08-13 01:51:38 +00:00
|
|
|
"""
|
2013-05-05 01:18:52 +00:00
|
|
|
This example builds on mitmproxy's base proxying infrastructure to
|
2014-09-08 14:02:31 +00:00
|
|
|
implement functionality similar to the "sticky cookies" option.
|
|
|
|
|
|
|
|
Heads Up: In the majority of cases, you want to use inline scripts.
|
2011-08-13 01:51:38 +00:00
|
|
|
"""
|
|
|
|
import os
|
2016-02-16 19:49:10 +00:00
|
|
|
from mitmproxy import controller, proxy
|
|
|
|
from mitmproxy.proxy.server import ProxyServer
|
2014-09-05 13:16:20 +00:00
|
|
|
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
class StickyMaster(controller.Master):
|
|
|
|
def __init__(self, server):
|
|
|
|
controller.Master.__init__(self, server)
|
|
|
|
self.stickyhosts = {}
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
return controller.Master.run(self)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
self.shutdown()
|
|
|
|
|
2014-09-08 14:02:31 +00:00
|
|
|
def handle_request(self, flow):
|
|
|
|
hid = (flow.request.host, flow.request.port)
|
2015-09-05 18:45:58 +00:00
|
|
|
if "cookie" in flow.request.headers:
|
|
|
|
self.stickyhosts[hid] = flow.request.headers.get_all("cookie")
|
2010-02-16 04:09:07 +00:00
|
|
|
elif hid in self.stickyhosts:
|
2015-09-05 18:45:58 +00:00
|
|
|
flow.request.headers.set_all("cookie", self.stickyhosts[hid])
|
2014-09-08 14:02:31 +00:00
|
|
|
flow.reply()
|
|
|
|
|
|
|
|
def handle_response(self, flow):
|
|
|
|
hid = (flow.request.host, flow.request.port)
|
2015-09-05 18:45:58 +00:00
|
|
|
if "set-cookie" in flow.response.headers:
|
|
|
|
self.stickyhosts[hid] = flow.response.headers.get_all("set-cookie")
|
2014-09-08 14:02:31 +00:00
|
|
|
flow.reply()
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
|
2014-10-18 13:26:10 +00:00
|
|
|
config = proxy.ProxyConfig(port=8080)
|
2014-09-08 21:34:43 +00:00
|
|
|
server = ProxyServer(config)
|
2010-02-16 04:09:07 +00:00
|
|
|
m = StickyMaster(server)
|
|
|
|
m.run()
|