Add a basic Flow processor example.

This commit is contained in:
Aldo Cortesi 2011-08-13 13:51:38 +12:00
parent 4d02ae0582
commit 25f12b0e5d
4 changed files with 49 additions and 4 deletions

39
examples/flowbasic Executable file
View File

@ -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()

View File

@ -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)

View File

@ -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

View File

@ -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