prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <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! :) -->
  
  
  </issue>
  <code>
  [start of examples/complex/tcp_message.py]
  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 of examples/complex/tcp_message.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: 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))
       )
  
verification_info: {"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"}]}