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


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `mitmproxy/addons/allowremote.py`
Content:
```
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 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/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
         )

{"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"}], "after_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 = 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).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"}]}