Repository: mitmproxy/mitmproxy

Issue:
tcp_message script not working
Hi,

I tried to execute the TCP message replace script from the doc but it seems is not working. I don't know if this is a issue with the doc script or with mitmproxy.

The script was unchanged.

##### Steps to reproduce the problem:

1. mitmdump --mode transparent --tcp-host ".*" -k -s examples/complex/tcp_message.py

Loading script: examples/tcp_message.py
Proxy server listening at http://*:8080
192.168.1.241:37604: clientconnect
::ffff:192.168.1.241:37604: Certificate verification error for None: hostname 'no-hostname' doesn't match either of '*.local.org', 'local.org'
::ffff:192.168.1.241:37604: Ignoring server verification error, continuing with connection
Addon error: Traceback (most recent call last):
  File "examples/tcp_message.py", line 16, in tcp_message
    modified_msg = tcp_msg.message.replace("foo", "bar")
AttributeError: 'TCPFlow' object has no attribute 'message'

192.168.1.241:37604 -> tcp -> 10.0.0.2:5443
Addon error: Traceback (most recent call last):
  File "examples/tcp_message.py", line 16, in tcp_message
    modified_msg = tcp_msg.message.replace("foo", "bar")
AttributeError: 'TCPFlow' object has no attribute 'message'

192.168.1.241:37604 <- tcp <- 10.0.0.2:5443

##### System information

<!-- Paste the output of "mitmproxy --version" here. -->

mitmdump --version
Mitmproxy: 3.0.4 
Python:    3.6.0
OpenSSL:   OpenSSL 1.1.0h  27 Mar 2018
Platform:  Linux-3.19.0-65-generic-x86_64-with-debian-jessie-sid

<!-- Please use the mitmproxy forums (https://discourse.mitmproxy.org/) for support/how-to questions. Thanks! :) -->



Discussion:
Thanks - looks like the example is a bit outdated.
I'll send a PR to update it in a few minutes.
Thanks, Is possible to post here an update example I really need one.

Resolving patch:
diff --git a/examples/complex/tcp_message.py b/examples/complex/tcp_message.py
--- a/examples/complex/tcp_message.py
+++ b/examples/complex/tcp_message.py
@@ -6,23 +6,22 @@
 * 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")
-
-    is_modified = False if modified_msg == tcp_msg.message else True
-    tcp_msg.message = modified_msg
+def tcp_message(flow: tcp.TCPFlow):
+    message = flow.messages[-1]
+    old_content = message.content
+    message.content = old_content.replace(b"foo", b"bar")
 
     ctx.log.info(
-        "[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))
+        "[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))
     )


Test patch: