aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2011-08-13 13:51:38 +1200
committerAldo Cortesi <aldo@nullcube.com>2011-08-13 13:51:38 +1200
commit25f12b0e5debc7b052e0a5d002a88b045d8f6f6a (patch)
tree2c5274591d4ae2473fc2b994582b7b579ca7803c /examples
parent4d02ae0582dead00ad24a644ba712c79f56d04fd (diff)
downloadmitmproxy-25f12b0e5debc7b052e0a5d002a88b045d8f6f6a.tar.gz
mitmproxy-25f12b0e5debc7b052e0a5d002a88b045d8f6f6a.tar.bz2
mitmproxy-25f12b0e5debc7b052e0a5d002a88b045d8f6f6a.zip
Add a basic Flow processor example.
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/flowbasic39
-rwxr-xr-xexamples/stickycookies11
2 files changed, 48 insertions, 2 deletions
diff --git a/examples/flowbasic b/examples/flowbasic
new file mode 100755
index 00000000..d3981329
--- /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 1ae0e113..b290f2fd 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)