From dbafe9f87bb7b793ae2d84e01af5d39f034c78d4 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 23 Jul 2016 11:57:31 +1200 Subject: scripts: refactor some examples that keep global state We now have a better way to do this. --- examples/filt.py | 20 +++++++++++--------- examples/flowwriter.py | 25 +++++++++++++------------ examples/iframe_injector.py | 37 +++++++++++++++++++------------------ 3 files changed, 43 insertions(+), 39 deletions(-) (limited to 'examples') diff --git a/examples/filt.py b/examples/filt.py index 21744edd..9ccf9fa1 100644 --- a/examples/filt.py +++ b/examples/filt.py @@ -1,18 +1,20 @@ -# This scripts demonstrates how to use mitmproxy's filter pattern in inline scripts. +# This scripts demonstrates how to use mitmproxy's filter pattern in scripts. # Usage: mitmdump -s "filt.py FILTER" import sys from mitmproxy import filt -state = {} + +class Filter: + def __init__(self, spec): + self.filter = filt.parse(spec) + + def response(self, flow): + if flow.match(self.filter): + print("Flow matches filter:") + print(flow) def start(): if len(sys.argv) != 2: raise ValueError("Usage: -s 'filt.py FILTER'") - state["filter"] = filt.parse(sys.argv[1]) - - -def response(flow): - if flow.match(state["filter"]): - print("Flow matches filter:") - print(flow) + return Filter(sys.argv[1]) diff --git a/examples/flowwriter.py b/examples/flowwriter.py index 07c7ca20..df2e5a40 100644 --- a/examples/flowwriter.py +++ b/examples/flowwriter.py @@ -3,20 +3,21 @@ import sys from mitmproxy.flow import FlowWriter -state = {} + +class Writer: + def __init__(self, path): + if path == "-": + f = sys.stdout + else: + f = open(path, "wb") + self.w = FlowWriter(f) + + def response(self, flow): + if random.choice([True, False]): + self.w.add(flow) def start(): if len(sys.argv) != 2: raise ValueError('Usage: -s "flowriter.py filename"') - - if sys.argv[1] == "-": - f = sys.stdout - else: - f = open(sys.argv[1], "wb") - state["flow_writer"] = FlowWriter(f) - - -def response(flow): - if random.choice([True, False]): - state["flow_writer"].add(flow) + return Writer(sys.argv[1]) diff --git a/examples/iframe_injector.py b/examples/iframe_injector.py index 352c3c24..33d18bbd 100644 --- a/examples/iframe_injector.py +++ b/examples/iframe_injector.py @@ -3,26 +3,27 @@ import sys from bs4 import BeautifulSoup -iframe_url = None + +class Injector: + def __init__(self, iframe_url): + self.iframe_url = iframe_url + + def response(self, flow): + if flow.request.host in self.iframe_url: + return + html = BeautifulSoup(flow.response.content, "lxml") + if html.body: + iframe = html.new_tag( + "iframe", + src=self.iframe_url, + frameborder=0, + height=0, + width=0) + html.body.insert(0, iframe) + flow.response.content = str(html).encode("utf8") def start(): if len(sys.argv) != 2: raise ValueError('Usage: -s "iframe_injector.py url"') - global iframe_url - iframe_url = sys.argv[1] - - -def response(flow): - if flow.request.host in iframe_url: - return - html = BeautifulSoup(flow.response.content, "lxml") - if html.body: - iframe = html.new_tag( - "iframe", - src=iframe_url, - frameborder=0, - height=0, - width=0) - html.body.insert(0, iframe) - flow.response.content = str(html).encode("utf8") + return Injector(sys.argv[1]) -- cgit v1.2.3 From afda175e1ccebf072391432ad74d6118c7cbb407 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 23 Jul 2016 15:43:55 +1200 Subject: Addon iface: .configure(options) -> .configure(options, updated) --- examples/stub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/stub.py b/examples/stub.py index e5b4a39a..4f5061e2 100644 --- a/examples/stub.py +++ b/examples/stub.py @@ -11,7 +11,7 @@ def start(): mitmproxy.ctx.log("start") -def configure(options): +def configure(options, updated): """ Called once on script startup before any other events, and whenever options changes. """ -- cgit v1.2.3 From b78fea378429436e5e7b64b9da0037a31f9105fc Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Sat, 23 Jul 2016 14:02:54 -0700 Subject: add remote debug example --- examples/remote_debug.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 examples/remote_debug.py (limited to 'examples') diff --git a/examples/remote_debug.py b/examples/remote_debug.py new file mode 100644 index 00000000..c5839d2c --- /dev/null +++ b/examples/remote_debug.py @@ -0,0 +1,19 @@ +""" +This script enables remote debugging of the mitmproxy *UI* with PyCharm. +For general debugging purposes, it is easier to just debug mitmdump within PyCharm. + +Usage: + - pip install pydevd on the mitmproxy machine + - Open the Run/Debug Configuration dialog box in PyCharm, and select the Python Remote Debug configuration type. + - Debugging works in the way that mitmproxy connects to the debug server on startup. + Specify host and port that mitmproxy can use to reach your PyCharm instance on startup. + - Adjust this inline script accordingly. + - Start debug server in PyCharm + - Set breakpoints + - Start mitmproxy -s remote_debug.py +""" + + +def start(): + import pydevd + pydevd.settrace("localhost", port=5678, stdoutToServer=True, stderrToServer=True) \ No newline at end of file -- cgit v1.2.3 From fb238ad63e86fa0387ff0e923f86964736421ab2 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Sat, 23 Jul 2016 14:35:04 -0700 Subject: make the linter happy --- examples/remote_debug.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/remote_debug.py b/examples/remote_debug.py index c5839d2c..fb864f78 100644 --- a/examples/remote_debug.py +++ b/examples/remote_debug.py @@ -16,4 +16,4 @@ Usage: def start(): import pydevd - pydevd.settrace("localhost", port=5678, stdoutToServer=True, stderrToServer=True) \ No newline at end of file + pydevd.settrace("localhost", port=5678, stdoutToServer=True, stderrToServer=True) -- cgit v1.2.3