mitmproxy/examples/stickycookies

45 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
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.
2011-08-13 01:51:38 +00:00
"""
import os
from libmproxy import controller, proxy
from libmproxy.proxy.server import ProxyServer
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]
msg.reply()
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"]
msg.reply()
2010-02-16 04:09:07 +00:00
2012-02-20 23:32:56 +00:00
config = proxy.ProxyConfig(
ca_file = os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem")
2011-08-04 00:57:01 +00:00
)
server = ProxyServer(config, 8080)
2010-02-16 04:09:07 +00:00
m = StickyMaster(server)
m.run()