We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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! :) -->


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `examples/complex/tcp_message.py`
Content:
```
1 """
2 tcp_message Inline Script Hook API Demonstration
3 ------------------------------------------------
4 
5 * modifies packets containing "foo" to "bar"
6 * prints various details for each packet.
7 
8 example cmdline invocation:
9 mitmdump -T --host --tcp ".*" -q -s examples/tcp_message.py
10 """
11 from mitmproxy.utils import strutils
12 from mitmproxy import ctx
13 
14 
15 def tcp_message(tcp_msg):
16     modified_msg = tcp_msg.message.replace("foo", "bar")
17 
18     is_modified = False if modified_msg == tcp_msg.message else True
19     tcp_msg.message = modified_msg
20 
21     ctx.log.info(
22         "[tcp_message{}] from {} {} to {} {}:\r\n{}".format(
23             " (modified)" if is_modified else "",
24             "client" if tcp_msg.sender == tcp_msg.client_conn else "server",
25             tcp_msg.sender.address,
26             "server" if tcp_msg.receiver == tcp_msg.server_conn else "client",
27             tcp_msg.receiver.address, strutils.bytes_to_escaped_str(tcp_msg.message))
28     )
29
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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))
     )

{"golden_diff": "diff --git a/examples/complex/tcp_message.py b/examples/complex/tcp_message.py\n--- a/examples/complex/tcp_message.py\n+++ b/examples/complex/tcp_message.py\n@@ -6,23 +6,22 @@\n * prints various details for each packet.\n \n example cmdline invocation:\n-mitmdump -T --host --tcp \".*\" -q -s examples/tcp_message.py\n+mitmdump --rawtcp --tcp-host \".*\" -s examples/complex/tcp_message.py\n \"\"\"\n from mitmproxy.utils import strutils\n from mitmproxy import ctx\n+from mitmproxy import tcp\n \n \n-def tcp_message(tcp_msg):\n-    modified_msg = tcp_msg.message.replace(\"foo\", \"bar\")\n-\n-    is_modified = False if modified_msg == tcp_msg.message else True\n-    tcp_msg.message = modified_msg\n+def tcp_message(flow: tcp.TCPFlow):\n+    message = flow.messages[-1]\n+    old_content = message.content\n+    message.content = old_content.replace(b\"foo\", b\"bar\")\n \n     ctx.log.info(\n-        \"[tcp_message{}] from {} {} to {} {}:\\r\\n{}\".format(\n-            \" (modified)\" if is_modified else \"\",\n-            \"client\" if tcp_msg.sender == tcp_msg.client_conn else \"server\",\n-            tcp_msg.sender.address,\n-            \"server\" if tcp_msg.receiver == tcp_msg.server_conn else \"client\",\n-            tcp_msg.receiver.address, strutils.bytes_to_escaped_str(tcp_msg.message))\n+        \"[tcp_message{}] from {} to {}:\\n{}\".format(\n+            \" (modified)\" if message.content != old_content else \"\",\n+            \"client\" if message.from_client else \"server\",\n+            \"server\" if message.from_client else \"client\",\n+            strutils.bytes_to_escaped_str(message.content))\n     )\n", "issue": "tcp_message script not working\nHi,\r\n\r\nI 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.\r\n\r\nThe script was unchanged.\r\n\r\n##### Steps to reproduce the problem:\r\n\r\n1. mitmdump --mode transparent --tcp-host \".*\" -k -s examples/complex/tcp_message.py\r\n\r\nLoading script: examples/tcp_message.py\r\nProxy server listening at http://*:8080\r\n192.168.1.241:37604: clientconnect\r\n::ffff:192.168.1.241:37604: Certificate verification error for None: hostname 'no-hostname' doesn't match either of '*.local.org', 'local.org'\r\n::ffff:192.168.1.241:37604: Ignoring server verification error, continuing with connection\r\nAddon error: Traceback (most recent call last):\r\n  File \"examples/tcp_message.py\", line 16, in tcp_message\r\n    modified_msg = tcp_msg.message.replace(\"foo\", \"bar\")\r\nAttributeError: 'TCPFlow' object has no attribute 'message'\r\n\r\n192.168.1.241:37604 -> tcp -> 10.0.0.2:5443\r\nAddon error: Traceback (most recent call last):\r\n  File \"examples/tcp_message.py\", line 16, in tcp_message\r\n    modified_msg = tcp_msg.message.replace(\"foo\", \"bar\")\r\nAttributeError: 'TCPFlow' object has no attribute 'message'\r\n\r\n192.168.1.241:37604 <- tcp <- 10.0.0.2:5443\r\n\r\n##### System information\r\n\r\n<!-- Paste the output of \"mitmproxy --version\" here. -->\r\n\r\nmitmdump --version\r\nMitmproxy: 3.0.4 \r\nPython:    3.6.0\r\nOpenSSL:   OpenSSL 1.1.0h  27 Mar 2018\r\nPlatform:  Linux-3.19.0-65-generic-x86_64-with-debian-jessie-sid\r\n\r\n<!-- Please use the mitmproxy forums (https://discourse.mitmproxy.org/) for support/how-to questions. Thanks! :) -->\r\n\n", "before_files": [{"content": "\"\"\"\ntcp_message Inline Script Hook API Demonstration\n------------------------------------------------\n\n* modifies packets containing \"foo\" to \"bar\"\n* prints various details for each packet.\n\nexample cmdline invocation:\nmitmdump -T --host --tcp \".*\" -q -s examples/tcp_message.py\n\"\"\"\nfrom mitmproxy.utils import strutils\nfrom mitmproxy import ctx\n\n\ndef tcp_message(tcp_msg):\n    modified_msg = tcp_msg.message.replace(\"foo\", \"bar\")\n\n    is_modified = False if modified_msg == tcp_msg.message else True\n    tcp_msg.message = modified_msg\n\n    ctx.log.info(\n        \"[tcp_message{}] from {} {} to {} {}:\\r\\n{}\".format(\n            \" (modified)\" if is_modified else \"\",\n            \"client\" if tcp_msg.sender == tcp_msg.client_conn else \"server\",\n            tcp_msg.sender.address,\n            \"server\" if tcp_msg.receiver == tcp_msg.server_conn else \"client\",\n            tcp_msg.receiver.address, strutils.bytes_to_escaped_str(tcp_msg.message))\n    )\n", "path": "examples/complex/tcp_message.py"}], "after_files": [{"content": "\"\"\"\ntcp_message Inline Script Hook API Demonstration\n------------------------------------------------\n\n* modifies packets containing \"foo\" to \"bar\"\n* prints various details for each packet.\n\nexample cmdline invocation:\nmitmdump --rawtcp --tcp-host \".*\" -s examples/complex/tcp_message.py\n\"\"\"\nfrom mitmproxy.utils import strutils\nfrom mitmproxy import ctx\nfrom mitmproxy import tcp\n\n\ndef tcp_message(flow: tcp.TCPFlow):\n    message = flow.messages[-1]\n    old_content = message.content\n    message.content = old_content.replace(b\"foo\", b\"bar\")\n\n    ctx.log.info(\n        \"[tcp_message{}] from {} to {}:\\n{}\".format(\n            \" (modified)\" if message.content != old_content else \"\",\n            \"client\" if message.from_client else \"server\",\n            \"server\" if message.from_client else \"client\",\n            strutils.bytes_to_escaped_str(message.content))\n    )\n", "path": "examples/complex/tcp_message.py"}]}