Adapt examples

This commit is contained in:
Aldo Cortesi
2016-05-29 12:54:52 +12:00
parent 0a875421c5
commit 0176f50e4f
3 changed files with 17 additions and 18 deletions

11
examples/flowbasic Normal file → Executable file
View File

@@ -8,7 +8,7 @@
Note that request and response messages are not automatically replied to,
so we need to implement handlers to do this.
"""
from mitmproxy import flow
from mitmproxy import flow, controller
from mitmproxy.proxy import ProxyServer, ProxyConfig
@@ -19,18 +19,15 @@ class MyMaster(flow.FlowMaster):
except KeyboardInterrupt:
self.shutdown()
@controller.handler
def handle_request(self, f):
f = flow.FlowMaster.handle_request(self, f)
if f:
f.reply()
return f
print(f)
@controller.handler
def handle_response(self, f):
f = flow.FlowMaster.handle_response(self, f)
if f:
f.reply()
print(f)
return f
config = ProxyConfig(

8
examples/stickycookies Normal file → Executable file
View File

@@ -21,19 +21,19 @@ class StickyMaster(controller.Master):
except KeyboardInterrupt:
self.shutdown()
def handle_request(self, flow):
@controller.handler
def request(self, flow):
hid = (flow.request.host, flow.request.port)
if "cookie" in flow.request.headers:
self.stickyhosts[hid] = flow.request.headers.get_all("cookie")
elif hid in self.stickyhosts:
flow.request.headers.set_all("cookie", self.stickyhosts[hid])
flow.reply()
def handle_response(self, flow):
@controller.handler
def response(self, flow):
hid = (flow.request.host, flow.request.port)
if "set-cookie" in flow.response.headers:
self.stickyhosts[hid] = flow.response.headers.get_all("set-cookie")
flow.reply()
config = proxy.ProxyConfig(port=8080)

View File

@@ -36,10 +36,18 @@ class Master(object):
"""
The master handles mitmproxy's main event loop.
"""
def __init__(self):
def __init__(self, *servers):
self.event_queue = queue.Queue()
self.should_exit = threading.Event()
self.servers = []
for i in servers:
self.add_server(i)
def add_server(self, server):
# We give a Channel to the server which can be used to communicate with the master
channel = Channel(self.event_queue, self.should_exit)
server.set_channel(channel)
self.servers.append(server)
def start(self):
self.should_exit.clear()
@@ -87,12 +95,6 @@ class Master(object):
server.shutdown()
self.should_exit.set()
def add_server(self, server):
# We give a Channel to the server which can be used to communicate with the master
channel = Channel(self.event_queue, self.should_exit)
server.set_channel(channel)
self.servers.append(server)
class ServerThread(threading.Thread):
def __init__(self, server):