We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Transparent mode fail with looking up failure.
##### Steps to reproduce the problem:

1. Launch Wifi Access Point(OS X)
2. Setup pfctl configuration so that http packet will be forwarded.
3. Launch mitmproxy ( `sudo mitmproxy -p 8080 -m transparent --showhost` )
4. Access web page after connecting to AP which launched before.
5. See event log.


##### Any other comments? What have you tried so far?

When I tried to use transparent mode with OS X(10.11.6).
RuntimeError("Could not resolve original destination.") raised.

I investigated this bug.
And I found that this is caused by difference between AF_INET's and AF_INET6's peername.
https://github.com/mitmproxy/mitmproxy/blob/de006ea8adc08b9a8a6aa94eda2b30468727c307/mitmproxy/net/tcp.py#L567

If we use AF_INET, getpeername() return string like `"192.168.2.5:45670"`.
But if we use AF_INET6, getpeername() return string like `"::ffff:192.168.2.5:45670"`.

`pfctl -s state` 's result is like below.
`ALL tcp 192.168.2.5:45670 -> xx.xx.xx.xx:33291 -> xx.xx.xx.xx:443       ESTABLISHED:ESTABLISHED`

As you see, `::ffff:` doesn't exist.

So [lookup](https://github.com/mitmproxy/mitmproxy/blob/f17c0fdac636f7269f4885294e2a8d2c52c23590/mitmproxy/platform/pf.py#L4) function raises RuntimeError() because `spec in i` condition won't become true.

##### System information

Mitmproxy version: 3.0.0 (release version)
Python version: 3.6.2
Platform: Darwin-15.6.0-x86_64-i386-64bit
SSL version: OpenSSL 1.0.2l  25 May 2017
Mac version: 10.11.6 ('', '', '') x86_64

--- 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/platform/pf.py`
Content:
```
1 import sys
2 
3 
4 def lookup(address, port, s):
5     """
6         Parse the pfctl state output s, to look up the destination host
7         matching the client (address, port).
8 
9         Returns an (address, port) tuple, or None.
10     """
11     s = s.decode()
12     spec = "%s:%s" % (address, port)
13     for i in s.split("\n"):
14         if "ESTABLISHED:ESTABLISHED" in i and spec in i:
15             s = i.split()
16             if len(s) > 4:
17                 if sys.platform.startswith("freebsd"):
18                     # strip parentheses for FreeBSD pfctl
19                     s = s[3][1:-1].split(":")
20                 else:
21                     s = s[4].split(":")
22 
23                 if len(s) == 2:
24                     return s[0], int(s[1])
25     raise RuntimeError("Could not resolve original destination.")
26
```
--- 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/platform/pf.py b/mitmproxy/platform/pf.py
--- a/mitmproxy/platform/pf.py
+++ b/mitmproxy/platform/pf.py
@@ -1,3 +1,4 @@
+import re
 import sys
 
 
@@ -8,6 +9,9 @@
 
         Returns an (address, port) tuple, or None.
     """
+    # We may get an ipv4-mapped ipv6 address here, e.g. ::ffff:127.0.0.1.
+    # Those still appear as "127.0.0.1" in the table, so we need to strip the prefix.
+    address = re.sub("^::ffff:(?=\d+.\d+.\d+.\d+$)", "", address)
     s = s.decode()
     spec = "%s:%s" % (address, port)
     for i in s.split("\n"):

{"golden_diff": "diff --git a/mitmproxy/platform/pf.py b/mitmproxy/platform/pf.py\n--- a/mitmproxy/platform/pf.py\n+++ b/mitmproxy/platform/pf.py\n@@ -1,3 +1,4 @@\n+import re\n import sys\n \n \n@@ -8,6 +9,9 @@\n \n         Returns an (address, port) tuple, or None.\n     \"\"\"\n+    # We may get an ipv4-mapped ipv6 address here, e.g. ::ffff:127.0.0.1.\n+    # Those still appear as \"127.0.0.1\" in the table, so we need to strip the prefix.\n+    address = re.sub(\"^::ffff:(?=\\d+.\\d+.\\d+.\\d+$)\", \"\", address)\n     s = s.decode()\n     spec = \"%s:%s\" % (address, port)\n     for i in s.split(\"\\n\"):\n", "issue": "Transparent mode fail with looking up failure.\n##### Steps to reproduce the problem:\r\n\r\n1. Launch Wifi Access Point(OS X)\r\n2. Setup pfctl configuration so that http packet will be forwarded.\r\n3. Launch mitmproxy ( `sudo mitmproxy -p 8080 -m transparent --showhost` )\r\n4. Access web page after connecting to AP which launched before.\r\n5. See event log.\r\n\r\n\r\n##### Any other comments? What have you tried so far?\r\n\r\nWhen I tried to use transparent mode with OS X(10.11.6).\r\nRuntimeError(\"Could not resolve original destination.\") raised.\r\n\r\nI investigated this bug.\r\nAnd I found that this is caused by difference between AF_INET's and AF_INET6's peername.\r\nhttps://github.com/mitmproxy/mitmproxy/blob/de006ea8adc08b9a8a6aa94eda2b30468727c307/mitmproxy/net/tcp.py#L567\r\n\r\nIf we use AF_INET, getpeername() return string like `\"192.168.2.5:45670\"`.\r\nBut if we use AF_INET6, getpeername() return string like `\"::ffff:192.168.2.5:45670\"`.\r\n\r\n`pfctl -s state` 's result is like below.\r\n`ALL tcp 192.168.2.5:45670 -> xx.xx.xx.xx:33291 -> xx.xx.xx.xx:443       ESTABLISHED:ESTABLISHED`\r\n\r\nAs you see, `::ffff:` doesn't exist.\r\n\r\nSo [lookup](https://github.com/mitmproxy/mitmproxy/blob/f17c0fdac636f7269f4885294e2a8d2c52c23590/mitmproxy/platform/pf.py#L4) function raises RuntimeError() because `spec in i` condition won't become true.\r\n\r\n##### System information\r\n\r\nMitmproxy version: 3.0.0 (release version)\r\nPython version: 3.6.2\r\nPlatform: Darwin-15.6.0-x86_64-i386-64bit\r\nSSL version: OpenSSL 1.0.2l  25 May 2017\r\nMac version: 10.11.6 ('', '', '') x86_64\n", "before_files": [{"content": "import sys\n\n\ndef lookup(address, port, s):\n    \"\"\"\n        Parse the pfctl state output s, to look up the destination host\n        matching the client (address, port).\n\n        Returns an (address, port) tuple, or None.\n    \"\"\"\n    s = s.decode()\n    spec = \"%s:%s\" % (address, port)\n    for i in s.split(\"\\n\"):\n        if \"ESTABLISHED:ESTABLISHED\" in i and spec in i:\n            s = i.split()\n            if len(s) > 4:\n                if sys.platform.startswith(\"freebsd\"):\n                    # strip parentheses for FreeBSD pfctl\n                    s = s[3][1:-1].split(\":\")\n                else:\n                    s = s[4].split(\":\")\n\n                if len(s) == 2:\n                    return s[0], int(s[1])\n    raise RuntimeError(\"Could not resolve original destination.\")\n", "path": "mitmproxy/platform/pf.py"}], "after_files": [{"content": "import re\nimport sys\n\n\ndef lookup(address, port, s):\n    \"\"\"\n        Parse the pfctl state output s, to look up the destination host\n        matching the client (address, port).\n\n        Returns an (address, port) tuple, or None.\n    \"\"\"\n    # We may get an ipv4-mapped ipv6 address here, e.g. ::ffff:127.0.0.1.\n    # Those still appear as \"127.0.0.1\" in the table, so we need to strip the prefix.\n    address = re.sub(\"^::ffff:(?=\\d+.\\d+.\\d+.\\d+$)\", \"\", address)\n    s = s.decode()\n    spec = \"%s:%s\" % (address, port)\n    for i in s.split(\"\\n\"):\n        if \"ESTABLISHED:ESTABLISHED\" in i and spec in i:\n            s = i.split()\n            if len(s) > 4:\n                if sys.platform.startswith(\"freebsd\"):\n                    # strip parentheses for FreeBSD pfctl\n                    s = s[3][1:-1].split(\":\")\n                else:\n                    s = s[4].split(\":\")\n\n                if len(s) == 2:\n                    return s[0], int(s[1])\n    raise RuntimeError(\"Could not resolve original destination.\")\n", "path": "mitmproxy/platform/pf.py"}]}