prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  allow_remote=false does not prevent remote access
  ##### Steps to reproduce the problem:
  
  1. Run mitmproxy on a publicly routable host, with default configuration including `listen_host=""`, `listen_port=8080`, and `allow_remote=false`.
  2. From a host on a different network, send a request through that instance of mitmproxy, e.g. with `curl --proxy http://your-host.example:8080`
  
  The default `allow_remote=false` should prevent this request from succeeding. However, it is served by mitmproxy just fine.
  
  ##### Any other comments? What have you tried so far?
  
  I have a laptop sitting in the “DMZ” of a home router, which is globally IPv4 routable. I also have a VPS which is globally IPv4 routable. Both the laptop and the VPS are running Ubuntu 16.04 “Xenial Xerus”. I can reproduce the problem with mitmproxy running on the VPS and curl on the laptop, as well as vice-versa.
  
  Both tcpdump and mitmproxy’s own Details pane show the request as originating from a remote network.
  
  I only noticed this because I saw strange flows in a mitmproxy instance that I spun up on the laptop.
  
  ##### System information
  
  Mitmproxy: 3.0.3 binary
  Python:    3.5.2
  OpenSSL:   OpenSSL 1.1.0g  2 Nov 2017
  Platform:  Linux-4.4.0-116-generic-x86_64-with-debian-stretch-sid
  
  
  </issue>
  <code>
  [start of mitmproxy/addons/allowremote.py]
  1 import ipaddress
  2 from mitmproxy import ctx
  3 
  4 
  5 class AllowRemote:
  6     def load(self, loader):
  7         loader.add_option(
  8             "allow_remote", bool, False,
  9             """
  10             Allow remote clients to connect to proxy. If set to false,
  11             client will not be able to connect to proxy unless it is on the same network
  12             or the proxyauth option is set
  13             """
  14         )
  15 
  16     def clientconnect(self, layer):
  17         address = layer.client_conn.address
  18 
  19         accept_connection = (
  20             ctx.options.allow_remote or
  21             ipaddress.ip_address(address[0]).is_private or
  22             ctx.options.proxyauth is not None
  23         )
  24 
  25         if not accept_connection:
  26             layer.reply.kill()
  27             ctx.log.warn("Client connection was killed because allow_remote option is set to false, "
  28                          "client IP was not a private IP and proxyauth was not set.\n"
  29                          "To allow remote connections set allow_remote option to true or set proxyauth option.")
  30
  [end of mitmproxy/addons/allowremote.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/mitmproxy/addons/allowremote.py b/mitmproxy/addons/allowremote.py
  --- a/mitmproxy/addons/allowremote.py
  +++ b/mitmproxy/addons/allowremote.py
  @@ -14,11 +14,13 @@
           )
   
       def clientconnect(self, layer):
  -        address = layer.client_conn.address
  +        address = ipaddress.ip_address(layer.client_conn.address[0])
  +        if isinstance(address, ipaddress.IPv6Address):
  +            address = address.ipv4_mapped or address
   
           accept_connection = (
               ctx.options.allow_remote or
  -            ipaddress.ip_address(address[0]).is_private or
  +            ipaddress.ip_address(address).is_private or
               ctx.options.proxyauth is not None
           )
  
verification_info: {"golden_diff": "diff --git a/mitmproxy/addons/allowremote.py b/mitmproxy/addons/allowremote.py\n--- a/mitmproxy/addons/allowremote.py\n+++ b/mitmproxy/addons/allowremote.py\n@@ -14,11 +14,13 @@\n         )\n \n     def clientconnect(self, layer):\n-        address = layer.client_conn.address\n+        address = ipaddress.ip_address(layer.client_conn.address[0])\n+        if isinstance(address, ipaddress.IPv6Address):\n+            address = address.ipv4_mapped or address\n \n         accept_connection = (\n             ctx.options.allow_remote or\n-            ipaddress.ip_address(address[0]).is_private or\n+            ipaddress.ip_address(address).is_private or\n             ctx.options.proxyauth is not None\n         )\n", "issue": "allow_remote=false does not prevent remote access\n##### Steps to reproduce the problem:\r\n\r\n1. Run mitmproxy on a publicly routable host, with default configuration including `listen_host=\"\"`, `listen_port=8080`, and `allow_remote=false`.\r\n2. From a host on a different network, send a request through that instance of mitmproxy, e.g. with `curl --proxy http://your-host.example:8080`\r\n\r\nThe default `allow_remote=false` should prevent this request from succeeding. However, it is served by mitmproxy just fine.\r\n\r\n##### Any other comments? What have you tried so far?\r\n\r\nI have a laptop sitting in the \u201cDMZ\u201d of a home router, which is globally IPv4 routable. I also have a VPS which is globally IPv4 routable. Both the laptop and the VPS are running Ubuntu 16.04 \u201cXenial Xerus\u201d. I can reproduce the problem with mitmproxy running on the VPS and curl on the laptop, as well as vice-versa.\r\n\r\nBoth tcpdump and mitmproxy\u2019s own Details pane show the request as originating from a remote network.\r\n\r\nI only noticed this because I saw strange flows in a mitmproxy instance that I spun up on the laptop.\r\n\r\n##### System information\r\n\r\nMitmproxy: 3.0.3 binary\r\nPython:    3.5.2\r\nOpenSSL:   OpenSSL 1.1.0g  2 Nov 2017\r\nPlatform:  Linux-4.4.0-116-generic-x86_64-with-debian-stretch-sid\r\n\n", "before_files": [{"content": "import ipaddress\nfrom mitmproxy import ctx\n\n\nclass AllowRemote:\n    def load(self, loader):\n        loader.add_option(\n            \"allow_remote\", bool, False,\n            \"\"\"\n            Allow remote clients to connect to proxy. If set to false,\n            client will not be able to connect to proxy unless it is on the same network\n            or the proxyauth option is set\n            \"\"\"\n        )\n\n    def clientconnect(self, layer):\n        address = layer.client_conn.address\n\n        accept_connection = (\n            ctx.options.allow_remote or\n            ipaddress.ip_address(address[0]).is_private or\n            ctx.options.proxyauth is not None\n        )\n\n        if not accept_connection:\n            layer.reply.kill()\n            ctx.log.warn(\"Client connection was killed because allow_remote option is set to false, \"\n                         \"client IP was not a private IP and proxyauth was not set.\\n\"\n                         \"To allow remote connections set allow_remote option to true or set proxyauth option.\")\n", "path": "mitmproxy/addons/allowremote.py"}]}