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
"""
This example builds on mitmproxy's base proxying infrastructure to
implement functionality similar to the "sticky cookies" option. This is at
a lower level than the Flow mechanism, so we're dealing directly with
request and response objects.
"""
2010-02-16 04:09:07 +00:00
from libmproxy import controller, proxy
2011-08-13 01:51:38 +00:00
import os
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()
def handle_request(self, msg):
hid = (msg.host, msg.port)
2011-08-04 00:57:01 +00:00
if msg.headers["cookie"]:
2010-02-16 04:09:07 +00:00
self.stickyhosts[hid] = msg.headers["cookie"]
elif hid in self.stickyhosts:
msg.headers["cookie"] = self.stickyhosts[hid]
2011-08-04 00:57:01 +00:00
msg._ack()
2010-02-16 04:09:07 +00:00
def handle_response(self, msg):
hid = (msg.request.host, msg.request.port)
2011-08-04 00:57:01 +00:00
if msg.headers["set-cookie"]:
2011-08-13 01:51:38 +00:00
self.stickyhosts[hid] = msg.headers["set-cookie"]
2011-08-04 00:57:01 +00:00
msg._ack()
2010-02-16 04:09:07 +00:00
2012-02-20 23:32:56 +00:00
config = proxy.ProxyConfig(
2011-08-13 01:51:38 +00:00
cacert = os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem")
2011-08-04 00:57:01 +00:00
)
2012-02-20 23:32:56 +00:00
server = proxy.ProxyServer(config, 8080)
2010-02-16 04:09:07 +00:00
m = StickyMaster(server)
m.run()