diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/addons/events.py | 13 | ||||
-rw-r--r-- | examples/complex/dup_and_replay.py | 14 | ||||
-rw-r--r-- | examples/complex/har_dump.py | 14 | ||||
-rw-r--r-- | examples/complex/nonblocking.py | 5 | ||||
-rw-r--r-- | examples/complex/sslstrip.py | 2 | ||||
-rw-r--r-- | examples/complex/tcp_message.py | 26 | ||||
-rwxr-xr-x | examples/complex/xss_scanner.py | 4 | ||||
-rw-r--r-- | examples/simple/README.md | 2 | ||||
-rw-r--r-- | examples/simple/filter_flows.py | 6 | ||||
-rw-r--r-- | examples/simple/io_write_dumpfile.py | 2 | ||||
-rw-r--r-- | examples/simple/log_events.py | 5 |
11 files changed, 51 insertions, 42 deletions
diff --git a/examples/addons/events.py b/examples/addons/events.py index 674ab4c8..f83c8f11 100644 --- a/examples/addons/events.py +++ b/examples/addons/events.py @@ -142,8 +142,12 @@ class Events: def done(self): """ - Called when the addon shuts down, either by being removed from the - mitmproxy instance, or when mitmproxy itself shuts down. + Called when the addon shuts down, either by being removed from + the mitmproxy instance, or when mitmproxy itself shuts down. On + shutdown, this event is called after the event loop is + terminated, guaranteeing that it will be the final event an addon + sees. Note that log handlers are shut down at this point, so + calls to log functions will produce no output. """ def load(self, entry: mitmproxy.addonmanager.Loader): @@ -167,11 +171,6 @@ class Events: loaded. """ - def tick(self): - """ - A regular ticker - called approximately once every 100ms. - """ - def update(self, flows: typing.Sequence[mitmproxy.flow.Flow]): """ Update is called when one or more flow objects have been modified, diff --git a/examples/complex/dup_and_replay.py b/examples/complex/dup_and_replay.py index 2baa1ea6..adcebff3 100644 --- a/examples/complex/dup_and_replay.py +++ b/examples/complex/dup_and_replay.py @@ -2,7 +2,13 @@ from mitmproxy import ctx def request(flow): - f = flow.copy() - ctx.master.view.add(f) - f.request.path = "/changed" - ctx.master.replay_request(f, block=True) + # Avoid an infinite loop by not replaying already replayed requests + if flow.request.is_replay: + return + flow = flow.copy() + # Only interactive tools have a view. If we have one, add a duplicate entry + # for our flow. + if "view" in ctx.master.addons: + ctx.master.commands.call("view.add", [flow]) + flow.request.path = "/changed" + ctx.master.commands.call("replay.client", [flow]) diff --git a/examples/complex/har_dump.py b/examples/complex/har_dump.py index 9e287a19..33a2f79f 100644 --- a/examples/complex/har_dump.py +++ b/examples/complex/har_dump.py @@ -1,5 +1,11 @@ """ This inline script can be used to dump flows as HAR files. + +example cmdline invocation: +mitmdump -s ./har_dump.py --set hardump=./dump.har + +filename endwith '.zhar' will be compressed: +mitmdump -s ./har_dump.py --set hardump=./dump.zhar """ @@ -20,11 +26,11 @@ from mitmproxy import ctx from mitmproxy.utils import strutils from mitmproxy.net.http import cookies -HAR = {} # type: typing.Dict +HAR: typing.Dict = {} # A list of server seen till now is maintained so we can avoid # using 'connect' time for entries that use an existing connection. -SERVERS_SEEN = set() # type: typing.Set[connections.ServerConnection] +SERVERS_SEEN: typing.Set[connections.ServerConnection] = set() def load(l): @@ -155,12 +161,12 @@ def done(): Called once on script shutdown, after any other events. """ if ctx.options.hardump: - json_dump = json.dumps(HAR, indent=2) # type: str + json_dump: str = json.dumps(HAR, indent=2) if ctx.options.hardump == '-': mitmproxy.ctx.log(json_dump) else: - raw = json_dump.encode() # type: bytes + raw: bytes = json_dump.encode() if ctx.options.hardump.endswith('.zhar'): raw = zlib.compress(raw, 9) diff --git a/examples/complex/nonblocking.py b/examples/complex/nonblocking.py index 264a1fdb..72c9c0ab 100644 --- a/examples/complex/nonblocking.py +++ b/examples/complex/nonblocking.py @@ -1,11 +1,12 @@ import time from mitmproxy.script import concurrent +from mitmproxy import ctx @concurrent # Remove this and see what happens def request(flow): # You don't want to use mitmproxy.ctx from a different thread - print("handle request: %s%s" % (flow.request.host, flow.request.path)) + ctx.log.info("handle request: %s%s" % (flow.request.host, flow.request.path)) time.sleep(5) - print("start request: %s%s" % (flow.request.host, flow.request.path)) + ctx.log.info("start request: %s%s" % (flow.request.host, flow.request.path)) diff --git a/examples/complex/sslstrip.py b/examples/complex/sslstrip.py index c3f8c4f7..c862536f 100644 --- a/examples/complex/sslstrip.py +++ b/examples/complex/sslstrip.py @@ -9,7 +9,7 @@ import typing # noqa from mitmproxy import http # set of SSL/TLS capable hosts -secure_hosts = set() # type: typing.Set[str] +secure_hosts: typing.Set[str] = set() def request(flow: http.HTTPFlow) -> None: diff --git a/examples/complex/tcp_message.py b/examples/complex/tcp_message.py index d7c9c42e..b1311d08 100644 --- a/examples/complex/tcp_message.py +++ b/examples/complex/tcp_message.py @@ -6,22 +6,22 @@ tcp_message Inline Script Hook API Demonstration * prints various details for each packet. example cmdline invocation: -mitmdump -T --host --tcp ".*" -q -s examples/tcp_message.py +mitmdump --rawtcp --tcp-host ".*" -s examples/complex/tcp_message.py """ from mitmproxy.utils import strutils +from mitmproxy import ctx +from mitmproxy import tcp -def tcp_message(tcp_msg): - modified_msg = tcp_msg.message.replace("foo", "bar") +def tcp_message(flow: tcp.TCPFlow): + message = flow.messages[-1] + old_content = message.content + message.content = old_content.replace(b"foo", b"bar") - is_modified = False if modified_msg == tcp_msg.message else True - tcp_msg.message = modified_msg - - print( - "[tcp_message{}] from {} {} to {} {}:\r\n{}".format( - " (modified)" if is_modified else "", - "client" if tcp_msg.sender == tcp_msg.client_conn else "server", - tcp_msg.sender.address, - "server" if tcp_msg.receiver == tcp_msg.server_conn else "client", - tcp_msg.receiver.address, strutils.bytes_to_escaped_str(tcp_msg.message)) + ctx.log.info( + "[tcp_message{}] from {} to {}:\n{}".format( + " (modified)" if message.content != old_content else "", + "client" if message.from_client else "server", + "server" if message.from_client else "client", + strutils.bytes_to_escaped_str(message.content)) ) diff --git a/examples/complex/xss_scanner.py b/examples/complex/xss_scanner.py index 0c0dd0f3..55fc2fe7 100755 --- a/examples/complex/xss_scanner.py +++ b/examples/complex/xss_scanner.py @@ -95,7 +95,7 @@ def find_unclaimed_URLs(body: str, requestUrl: bytes) -> None: return None class ScriptURLExtractor(HTMLParser): - script_URLs = [] # type: List[str] + script_URLs: List[str] = [] def handle_starttag(self, tag, attrs): if (tag == "script" or tag == "iframe") and "src" in [name for name, value in attrs]: @@ -254,7 +254,7 @@ def paths_to_text(html: str, string: str) -> List[str]: class PathHTMLParser(HTMLParser): currentPath = "" - paths = [] # type: List[str] + paths: List[str] = [] def handle_starttag(self, tag, attrs): self.currentPath += ("/" + tag) diff --git a/examples/simple/README.md b/examples/simple/README.md index d140a84c..2fafdd5a 100644 --- a/examples/simple/README.md +++ b/examples/simple/README.md @@ -14,5 +14,5 @@ | modify_querystring.py | Modify HTTP query strings. | | redirect_requests.py | Redirect a request to a different server. | | send_reply_from_proxy.py | Send a HTTP response directly from the proxy. | -| upsidedownternet.py | Turn all images upside down. | +| internet_in_mirror.py | Turn all images upside down. | | wsgi_flask_app.py | Embed a WSGI app into mitmproxy. | diff --git a/examples/simple/filter_flows.py b/examples/simple/filter_flows.py index 70979591..aba240de 100644 --- a/examples/simple/filter_flows.py +++ b/examples/simple/filter_flows.py @@ -7,7 +7,7 @@ from mitmproxy import ctx, http class Filter: def __init__(self): - self.filter = None # type: flowfilter.TFilter + self.filter: flowfilter.TFilter = None def configure(self, updated): self.filter = flowfilter.parse(ctx.options.flowfilter) @@ -19,8 +19,8 @@ class Filter: def response(self, flow: http.HTTPFlow) -> None: if flowfilter.match(self.filter, flow): - print("Flow matches filter:") - print(flow) + ctx.log.info("Flow matches filter:") + ctx.log.info(flow) addons = [Filter()] diff --git a/examples/simple/io_write_dumpfile.py b/examples/simple/io_write_dumpfile.py index be6e4121..c8c59c62 100644 --- a/examples/simple/io_write_dumpfile.py +++ b/examples/simple/io_write_dumpfile.py @@ -13,7 +13,7 @@ import typing # noqa class Writer: def __init__(self, path: str) -> None: - self.f = open(path, "wb") # type: typing.IO[bytes] + self.f: typing.IO[bytes] = open(path, "wb") self.w = io.FlowWriter(self.f) def response(self, flow: http.HTTPFlow) -> None: diff --git a/examples/simple/log_events.py b/examples/simple/log_events.py index b9aa2c1f..4f70e340 100644 --- a/examples/simple/log_events.py +++ b/examples/simple/log_events.py @@ -1,10 +1,7 @@ -""" -It is recommended to use `ctx.log` for logging within a script. -print() statements are equivalent to ctx.log.warn(). -""" from mitmproxy import ctx def load(l): ctx.log.info("This is some informative text.") + ctx.log.warn("This is a warning.") ctx.log.error("This is an error.") |