aboutsummaryrefslogtreecommitdiffstats
path: root/examples/complex
diff options
context:
space:
mode:
Diffstat (limited to 'examples/complex')
-rw-r--r--examples/complex/dup_and_replay.py14
-rw-r--r--examples/complex/har_dump.py14
-rw-r--r--examples/complex/nonblocking.py5
-rw-r--r--examples/complex/sslstrip.py2
-rw-r--r--examples/complex/tcp_message.py26
-rwxr-xr-xexamples/complex/xss_scanner.py4
6 files changed, 39 insertions, 26 deletions
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)