From 25f12b0e5debc7b052e0a5d002a88b045d8f6f6a Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 13 Aug 2011 13:51:38 +1200 Subject: [PATCH] Add a basic Flow processor example. --- examples/flowbasic | 39 +++++++++++++++++++++++++++++++++++++++ examples/stickycookies | 11 +++++++++-- libmproxy/dump.py | 1 - libmproxy/proxy.py | 2 +- 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100755 examples/flowbasic diff --git a/examples/flowbasic b/examples/flowbasic new file mode 100755 index 000000000..d39813296 --- /dev/null +++ b/examples/flowbasic @@ -0,0 +1,39 @@ +#!/usr/bin/env python +""" + This example shows how to build a proxy based on mitmproxy's Flow + primitives. + + Note that request and response messages are not automatically acked, so we + need to implement handlers to do this. +""" +import os +from libmproxy import proxy, flow + +class MyMaster(flow.FlowMaster): + def run(self): + try: + flow.FlowMaster.run(self) + except KeyboardInterrupt: + self.shutdown() + + def handle_request(self, r): + f = flow.FlowMaster.handle_request(self, r) + if f: + r._ack() + return f + + def handle_response(self, r): + f = flow.FlowMaster.handle_response(self, r) + if f: + r._ack() + print f + return f + + +ssl_config = proxy.SSLConfig( + cacert = os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem") +) +state = flow.State() +server = proxy.ProxyServer(ssl_config, 8080) +m = MyMaster(server, state) +m.run() diff --git a/examples/stickycookies b/examples/stickycookies index 1ae0e113b..b290f2fd1 100755 --- a/examples/stickycookies +++ b/examples/stickycookies @@ -1,5 +1,12 @@ #!/usr/bin/env python +""" + 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. +""" from libmproxy import controller, proxy +import os class StickyMaster(controller.Master): def __init__(self, server): @@ -23,12 +30,12 @@ class StickyMaster(controller.Master): def handle_response(self, msg): hid = (msg.request.host, msg.request.port) if msg.headers["set-cookie"]: - self.stickyhosts[hid] = f.response.headers["set-cookie"] + self.stickyhosts[hid] = msg.headers["set-cookie"] msg._ack() ssl_config = proxy.SSLConfig( - "~/.mitmproxy/cert.pem" + cacert = os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem") ) server = proxy.ProxyServer(ssl_config, 8080) m = StickyMaster(server) diff --git a/libmproxy/dump.py b/libmproxy/dump.py index 21ea038e5..d1bdfb3fc 100644 --- a/libmproxy/dump.py +++ b/libmproxy/dump.py @@ -187,7 +187,6 @@ class DumpMaster(flow.FlowMaster): def handle_error(self, msg): f = flow.FlowMaster.handle_error(self, msg) if f: - msg._ack() self._process_flow(f) return f diff --git a/libmproxy/proxy.py b/libmproxy/proxy.py index 0ef5e225b..54becb393 100644 --- a/libmproxy/proxy.py +++ b/libmproxy/proxy.py @@ -22,7 +22,7 @@ class ProxyError(Exception): class SSLConfig: - def __init__(self, certfile = None, ciphers = None, cacert = None, cert_wait_time=None): + def __init__(self, certfile = None, ciphers = None, cacert = None, cert_wait_time=0): self.certfile = certfile self.ciphers = ciphers self.cacert = cacert