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
|
2014-09-05 13:16:20 +00:00
|
|
|
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()
|
|
|
|
|
2014-09-08 14:02:31 +00:00
|
|
|
def handle_request(self, flow):
|
|
|
|
hid = (flow.request.host, flow.request.port)
|
|
|
|
if flow.request.headers["cookie"]:
|
|
|
|
self.stickyhosts[hid] = flow.request.headers["cookie"]
|
2010-02-16 04:09:07 +00:00
|
|
|
elif hid in self.stickyhosts:
|
2014-09-08 14:02:31 +00:00
|
|
|
flow.request.headers["cookie"] = self.stickyhosts[hid]
|
|
|
|
flow.reply()
|
|
|
|
|
|
|
|
def handle_response(self, flow):
|
|
|
|
hid = (flow.request.host, flow.request.port)
|
|
|
|
if flow.response.headers["set-cookie"]:
|
|
|
|
self.stickyhosts[hid] = flow.response.headers["set-cookie"]
|
|
|
|
flow.reply()
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
|
2012-02-20 23:32:56 +00:00
|
|
|
config = proxy.ProxyConfig(
|
2014-09-08 21:34:43 +00:00
|
|
|
port=8080,
|
2014-10-08 18:44:52 +00:00
|
|
|
default_ca=os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem")
|
2011-08-04 00:57:01 +00:00
|
|
|
)
|
2014-09-08 21:34:43 +00:00
|
|
|
server = ProxyServer(config)
|
2010-02-16 04:09:07 +00:00
|
|
|
m = StickyMaster(server)
|
|
|
|
m.run()
|