mitmproxy/examples/stickycookies

43 lines
1.3 KiB
Plaintext
Raw Normal View History

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
from mitmproxy import controller, proxy
from mitmproxy.proxy.server import ProxyServer
2010-02-16 04:09:07 +00:00
class StickyMaster(master.Master):
2010-02-16 04:09:07 +00:00
def __init__(self, server):
master.Master.__init__(self, server)
2010-02-16 04:09:07 +00:00
self.stickyhosts = {}
def run(self):
try:
return master.Master.run(self)
2010-02-16 04:09:07 +00:00
except KeyboardInterrupt:
self.shutdown()
2016-05-29 00:54:52 +00:00
@controller.handler
def request(self, flow):
2014-09-08 14:02:31 +00:00
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
2016-05-29 00:54:52 +00:00
@controller.handler
def response(self, flow):
2014-09-08 14:02:31 +00:00
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")
2010-02-16 04:09:07 +00:00
2014-10-18 13:26:10 +00:00
config = proxy.ProxyConfig(port=8080)
server = ProxyServer(config)
2010-02-16 04:09:07 +00:00
m = StickyMaster(server)
m.run()