From b0cfeff06d9dd99a16dfae19c5df3c73c5864fb9 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 3 Sep 2014 16:57:56 +0200 Subject: fix #341 - work on flows instead of request/response internally. --- examples/flowbasic | 12 ++++++------ examples/proxapp | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/flowbasic b/examples/flowbasic index b8184262..8dbe2f28 100755 --- a/examples/flowbasic +++ b/examples/flowbasic @@ -16,16 +16,16 @@ class MyMaster(flow.FlowMaster): except KeyboardInterrupt: self.shutdown() - def handle_request(self, r): - f = flow.FlowMaster.handle_request(self, r) + def handle_request(self, f): + f = flow.FlowMaster.handle_request(self, f) if f: - r.reply() + f.reply() return f - def handle_response(self, r): - f = flow.FlowMaster.handle_response(self, r) + def handle_response(self, f): + f = flow.FlowMaster.handle_response(self, f) if f: - r.reply() + f.reply() print f return f diff --git a/examples/proxapp b/examples/proxapp index 3a94cd55..9f299d25 100755 --- a/examples/proxapp +++ b/examples/proxapp @@ -20,16 +20,16 @@ class MyMaster(flow.FlowMaster): except KeyboardInterrupt: self.shutdown() - def handle_request(self, r): - f = flow.FlowMaster.handle_request(self, r) + def handle_request(self, f): + f = flow.FlowMaster.handle_request(self, f) if f: - r.reply() + f.reply() return f - def handle_response(self, r): - f = flow.FlowMaster.handle_response(self, r) + def handle_response(self, f): + f = flow.FlowMaster.handle_response(self, f) if f: - r.reply() + f.reply() print f return f -- cgit v1.2.3 From 2f44b26b4cd014e03dd62a125d79af9b81663a93 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 3 Sep 2014 23:44:54 +0200 Subject: improve HTTPRequest syntax --- examples/modify_form.py | 4 ++-- examples/modify_querystring.py | 4 ++-- examples/redirect_requests.py | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'examples') diff --git a/examples/modify_form.py b/examples/modify_form.py index 2d839aed..cb12ee0f 100644 --- a/examples/modify_form.py +++ b/examples/modify_form.py @@ -1,8 +1,8 @@ def request(context, flow): if "application/x-www-form-urlencoded" in flow.request.headers["content-type"]: - frm = flow.request.get_form_urlencoded() + frm = flow.request.form_urlencoded frm["mitmproxy"] = ["rocks"] - flow.request.set_form_urlencoded(frm) + flow.request.form_urlencoded = frm diff --git a/examples/modify_querystring.py b/examples/modify_querystring.py index b1abcc1e..7e3a068a 100644 --- a/examples/modify_querystring.py +++ b/examples/modify_querystring.py @@ -1,7 +1,7 @@ def request(context, flow): - q = flow.request.get_query() + q = flow.request.query if q: q["mitmproxy"] = ["rocks"] - flow.request.set_query(q) + flow.request.query = q diff --git a/examples/redirect_requests.py b/examples/redirect_requests.py index a9a7e795..b57df2b2 100644 --- a/examples/redirect_requests.py +++ b/examples/redirect_requests.py @@ -7,12 +7,12 @@ This example shows two ways to redirect flows to other destinations. def request(context, flow): - if flow.request.get_host(hostheader=True).endswith("example.com"): + if flow.request.pretty_host(hostheader=True).endswith("example.com"): resp = HTTPResponse( [1, 1], 200, "OK", ODictCaseless([["Content-Type", "text/html"]]), "helloworld") flow.request.reply(resp) - if flow.request.get_host(hostheader=True).endswith("example.org"): + if flow.request.pretty_host(hostheader=True).endswith("example.org"): flow.request.host = "mitmproxy.org" - flow.request.headers["Host"] = ["mitmproxy.org"] + flow.request.update_host_header() -- cgit v1.2.3 From 649e63ff3c868397f493e1dabdc1c63d572aedd8 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Thu, 4 Sep 2014 00:10:01 +0200 Subject: fix some leftovers --- examples/redirect_requests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/redirect_requests.py b/examples/redirect_requests.py index a9a7e795..530da200 100644 --- a/examples/redirect_requests.py +++ b/examples/redirect_requests.py @@ -12,7 +12,7 @@ def request(context, flow): [1, 1], 200, "OK", ODictCaseless([["Content-Type", "text/html"]]), "helloworld") - flow.request.reply(resp) + flow.reply(resp) if flow.request.get_host(hostheader=True).endswith("example.org"): flow.request.host = "mitmproxy.org" flow.request.headers["Host"] = ["mitmproxy.org"] -- cgit v1.2.3 From f2570c773aa18e4ac236b1cf7f43acfb4ca080dd Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 5 Sep 2014 15:05:44 +0200 Subject: iframe injector example: use inline script --- examples/iframe_injector | 50 --------------------------------------------- examples/iframe_injector.py | 18 ++++++++++++++++ 2 files changed, 18 insertions(+), 50 deletions(-) delete mode 100755 examples/iframe_injector create mode 100644 examples/iframe_injector.py (limited to 'examples') diff --git a/examples/iframe_injector b/examples/iframe_injector deleted file mode 100755 index 8b1e02f1..00000000 --- a/examples/iframe_injector +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env python -""" - Zap encoding in requests and inject iframe after body tag in html responses. - Usage: - iframe_injector http://someurl/somefile.html -""" -from libmproxy import controller, proxy -import os -import sys - - -class InjectingMaster(controller.Master): - def __init__(self, server, iframe_url): - controller.Master.__init__(self, server) - self._iframe_url = iframe_url - - def run(self): - try: - return controller.Master.run(self) - except KeyboardInterrupt: - self.shutdown() - - def handle_request(self, msg): - if 'Accept-Encoding' in msg.headers: - msg.headers["Accept-Encoding"] = 'none' - msg.reply() - - def handle_response(self, msg): - if msg.content: - c = msg.replace('', '' % self._iframe_url) - if c > 0: - print 'Iframe injected!' - msg.reply() - - -def main(argv): - if len(argv) != 2: - print "Usage: %s IFRAME_URL" % argv[0] - sys.exit(1) - iframe_url = argv[1] - config = proxy.ProxyConfig( - cacert = os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem") - ) - server = proxy.ProxyServer(config, 8080) - print 'Starting proxy...' - m = InjectingMaster(server, iframe_url) - m.run() - -if __name__ == '__main__': - main(sys.argv) diff --git a/examples/iframe_injector.py b/examples/iframe_injector.py new file mode 100644 index 00000000..7042dbab --- /dev/null +++ b/examples/iframe_injector.py @@ -0,0 +1,18 @@ +# Usage: mitmdump -s "iframe_injector.py url" +# (this script works best with --anticache) +from libmproxy.protocol.http import decoded + + +def start(ctx, argv): + if len(argv) != 2: + raise ValueError('Usage: -s "iframe_injector.py url"') + ctx.iframe_url = argv[1] + + +def handle_response(ctx, flow): + with decoded(flow.response): # Remove content encoding (gzip, ...) + c = flow.response.replace( + '', + '' % ctx.iframe_url) + if c > 0: + ctx.log("Iframe injected!") \ No newline at end of file -- cgit v1.2.3 From 2a6337343a14f7f72c28d8bf5f24220f6d9ca6d0 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 5 Sep 2014 15:16:20 +0200 Subject: update docs, mostly revert 2f44b26b4cd014e03dd62a125d79af9b81663a93 --- examples/add_header.py | 4 ++-- examples/dup_and_replay.py | 6 +++--- examples/flowbasic | 9 ++++++--- examples/modify_form.py | 10 ++++------ examples/modify_querystring.py | 7 +++---- examples/nonblocking.py | 3 ++- examples/redirect_requests.py | 4 +++- examples/stickycookies | 8 +++++--- examples/stub.py | 6 +++--- examples/upsidedownternet.py | 2 +- 10 files changed, 32 insertions(+), 27 deletions(-) (limited to 'examples') diff --git a/examples/add_header.py b/examples/add_header.py index 0c0593d1..b9c8c1c6 100644 --- a/examples/add_header.py +++ b/examples/add_header.py @@ -1,2 +1,2 @@ -def response(context, flow): - flow.response.headers["newheader"] = ["foo"] +def response(ctx, flow): + flow.response.headers["newheader"] = ["foo"] \ No newline at end of file diff --git a/examples/dup_and_replay.py b/examples/dup_and_replay.py index 9c58d3a4..b38c2b7e 100644 --- a/examples/dup_and_replay.py +++ b/examples/dup_and_replay.py @@ -1,4 +1,4 @@ def request(ctx, flow): - f = ctx.duplicate_flow(flow) - f.request.path = "/changed" - ctx.replay_request(f) + f = ctx.duplicate_flow(flow) + f.request.path = "/changed" + ctx.replay_request(f) \ No newline at end of file diff --git a/examples/flowbasic b/examples/flowbasic index 8dbe2f28..2b44be3f 100755 --- a/examples/flowbasic +++ b/examples/flowbasic @@ -3,11 +3,14 @@ This example shows how to build a proxy based on mitmproxy's Flow primitives. + Heads Up: In the majority of cases, you want to use inline scripts. + Note that request and response messages are not automatically replied to, so we need to implement handlers to do this. """ import os -from libmproxy import proxy, flow +from libmproxy import flow, proxy +from libmproxy.proxy.server import ProxyServer class MyMaster(flow.FlowMaster): def run(self): @@ -31,9 +34,9 @@ class MyMaster(flow.FlowMaster): config = proxy.ProxyConfig( - cacert = os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem") + ca_file = os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem") ) state = flow.State() -server = proxy.ProxyServer(config, 8080) +server = ProxyServer(config, 8080) m = MyMaster(server, state) m.run() diff --git a/examples/modify_form.py b/examples/modify_form.py index cb12ee0f..6d651b19 100644 --- a/examples/modify_form.py +++ b/examples/modify_form.py @@ -1,8 +1,6 @@ -def request(context, flow): +def request(ctx, flow): if "application/x-www-form-urlencoded" in flow.request.headers["content-type"]: - frm = flow.request.form_urlencoded - frm["mitmproxy"] = ["rocks"] - flow.request.form_urlencoded = frm - - + form = flow.request.get_form_urlencoded() + form["mitmproxy"] = ["rocks"] + flow.request.set_form_urlencoded(form) \ No newline at end of file diff --git a/examples/modify_querystring.py b/examples/modify_querystring.py index 7e3a068a..56fbbb32 100644 --- a/examples/modify_querystring.py +++ b/examples/modify_querystring.py @@ -1,7 +1,6 @@ -def request(context, flow): - q = flow.request.query +def request(ctx, flow): + q = flow.request.get_query() if q: q["mitmproxy"] = ["rocks"] - flow.request.query = q - + flow.request.set_query(q) \ No newline at end of file diff --git a/examples/nonblocking.py b/examples/nonblocking.py index 9a131b32..1396742a 100644 --- a/examples/nonblocking.py +++ b/examples/nonblocking.py @@ -1,8 +1,9 @@ import time from libmproxy.script import concurrent + @concurrent def request(context, flow): print "handle request: %s%s" % (flow.request.host, flow.request.path) time.sleep(5) - print "start request: %s%s" % (flow.request.host, flow.request.path) + print "start request: %s%s" % (flow.request.host, flow.request.path) \ No newline at end of file diff --git a/examples/redirect_requests.py b/examples/redirect_requests.py index cc642039..c5561839 100644 --- a/examples/redirect_requests.py +++ b/examples/redirect_requests.py @@ -6,7 +6,9 @@ This example shows two ways to redirect flows to other destinations. """ -def request(context, flow): +def request(ctx, flow): + # pretty_host(hostheader=True) takes the Host: header of the request into account, + # which is useful in transparent mode where we usually only have the IP otherwise. if flow.request.pretty_host(hostheader=True).endswith("example.com"): resp = HTTPResponse( [1, 1], 200, "OK", diff --git a/examples/stickycookies b/examples/stickycookies index 17cd6019..2aab31d6 100755 --- a/examples/stickycookies +++ b/examples/stickycookies @@ -5,8 +5,10 @@ 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 +from libmproxy import controller, proxy +from libmproxy.proxy.server import ProxyServer + class StickyMaster(controller.Master): def __init__(self, server): @@ -35,8 +37,8 @@ class StickyMaster(controller.Master): config = proxy.ProxyConfig( - cacert = os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem") + ca_file = os.path.expanduser("~/.mitmproxy/mitmproxy-ca.pem") ) -server = proxy.ProxyServer(config, 8080) +server = ProxyServer(config, 8080) m = StickyMaster(server) m.run() diff --git a/examples/stub.py b/examples/stub.py index 0cf67db7..5976dd76 100644 --- a/examples/stub.py +++ b/examples/stub.py @@ -7,14 +7,14 @@ def start(ctx, argv): """ ctx.log("start") -def clientconnect(ctx, client_connect): +def clientconnect(ctx, conn_handler): """ Called when a client initiates a connection to the proxy. Note that a connection can correspond to multiple HTTP requests """ ctx.log("clientconnect") -def serverconnect(ctx, server_connection): +def serverconnect(ctx, conn_handler): """ Called when the proxy initiates a connection to the target server. Note that a connection can correspond to multiple HTTP requests @@ -50,7 +50,7 @@ def error(ctx, flow): """ ctx.log("error") -def clientdisconnect(ctx, client_disconnect): +def clientdisconnect(ctx, conn_handler): """ Called when a client disconnects from the proxy. """ diff --git a/examples/upsidedownternet.py b/examples/upsidedownternet.py index 181a40c2..a52b6d30 100644 --- a/examples/upsidedownternet.py +++ b/examples/upsidedownternet.py @@ -1,7 +1,7 @@ import cStringIO from PIL import Image -def response(context, flow): +def response(ctx, flow): if flow.response.headers["content-type"] == ["image/png"]: s = cStringIO.StringIO(flow.response.content) img = Image.open(s).rotate(180) -- cgit v1.2.3