Repository: mitmproxy/mitmproxy

Issue:
mitmproxy has crashed: ETIMEDOUT
Hi, I've been analysing an android app's traffic for a few weeks now without any problem and today mitmdump started crashing with the following stacktrace:

```
mitmdump -T --host -s analysis.py 

192.168.0.103:49825: clientconnect
192.168.0.103:36334: Traceback (most recent call last):
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/tcp.py", line 211, in read
    data = self.o.read(rlen)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/OpenSSL/SSL.py", line 1304, in recv
    self._raise_ssl_error(self._ssl, result)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/OpenSSL/SSL.py", line 1166, in _raise_ssl_error
    raise SysCallError(errno, errorcode.get(errno))
OpenSSL.SSL.SysCallError: (60, 'ETIMEDOUT')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/server.py", line 119, in handle
    root_layer()
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/modes/transparent_proxy.py", line 19, in __call__
    layer()
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/tls.py", line 383, in __call__
    layer()
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http1.py", line 72, in __call__
    layer()
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http.py", line 176, in __call__
    if not self._process_flow(flow):
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http.py", line 227, in _process_flow
    request = self.read_request_headers(f)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http1.py", line 14, in read_request_headers
    http1.read_request_head(self.client_conn.rfile)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/http/http1/read.py", line 52, in read_request_head
    form, method, scheme, host, port, path, http_version = _read_request_line(rfile)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/http/http1/read.py", line 240, in _read_request_line
    line = _get_first_line(rfile)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/http/http1/read.py", line 227, in _get_first_line
    line = rfile.readline()
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/tcp.py", line 251, in readline
    ch = self.read(1)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/tcp.py", line 233, in read
    raise exceptions.TlsException(str(e))
mitmproxy.exceptions.TlsException: (60, 'ETIMEDOUT')

Traceback (most recent call last):
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/tcp.py", line 211, in read
    data = self.o.read(rlen)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/OpenSSL/SSL.py", line 1304, in recv
    self._raise_ssl_error(self._ssl, result)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/OpenSSL/SSL.py", line 1166, in _raise_ssl_error
    raise SysCallError(errno, errorcode.get(errno))
OpenSSL.SSL.SysCallError: (60, 'ETIMEDOUT')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/server.py", line 119, in handle
    root_layer()
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/modes/transparent_proxy.py", line 19, in __call__
    layer()
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/tls.py", line 383, in __call__
    layer()
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http1.py", line 72, in __call__
    layer()
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http.py", line 176, in __call__
    if not self._process_flow(flow):
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http.py", line 227, in _process_flow
    request = self.read_request_headers(f)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http1.py", line 14, in read_request_headers
    http1.read_request_head(self.client_conn.rfile)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/http/http1/read.py", line 52, in read_request_head
    form, method, scheme, host, port, path, http_version = _read_request_line(rfile)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/http/http1/read.py", line 240, in _read_request_line
    line = _get_first_line(rfile)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/http/http1/read.py", line 227, in _get_first_line
    line = rfile.readline()
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/tcp.py", line 251, in readline
    ch = self.read(1)
  File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/tcp.py", line 233, in read
    raise exceptions.TlsException(str(e))
mitmproxy.exceptions.TlsException: (60, 'ETIMEDOUT')

mitmproxy has crashed!
Please lodge a bug report at: https://github.com/mitmproxy/mitmproxy
```
Since it nicely asked ti lodge a report, here I am.

I was running v0.18.2 but after this I've tried the latest build (v1.0.2) and still experiencing the same issue.

##### Steps to reproduce the problem:

1. `mitmdump -T --host -s analysis.py`


##### Any other comments? What have you tried so far?

Tried to run `mitmproxy -T --host` instead, same result.


##### System information

$ mitmdump --sysinfo
Mitmproxy version: 1.0.2
Python version: 3.5.1
Platform: Darwin-15.6.0-x86_64-i386-64bit
SSL version: OpenSSL 1.0.2j  26 Sep 2016
Mac version: 10.11.6 ('', '', '') x86_64



Discussion:
Thanks! ETIMEDOUT is weird. How do you connect your Android app to mitmproxy?
Hey! I changed the wifi settings to use my computer as the gateway. I'm also using ip forwarding by following the instructions in [docs > OSX](http://docs.mitmproxy.org/en/stable/transparent/osx.html?highlight=pfctl).
Update: for the last 2 days I haven't seen the issue. At the time I was experiencing it coincided with an app update, where lots of downloads were happening from their servers. I'm now thinking this issue may have some correlation with latency from app's servers.
Thanks for reporting back, happy to hear it works again. This certainly was an issue with your network, as it seems between client and mitmproxy. Nonetheless, we should not crash, so this remains open. 😃 

Resolving patch:
diff --git a/mitmproxy/net/http/http1/read.py b/mitmproxy/net/http/http1/read.py
--- a/mitmproxy/net/http/http1/read.py
+++ b/mitmproxy/net/http/http1/read.py
@@ -227,7 +227,7 @@ def _get_first_line(rfile):
         if line == b"\r\n" or line == b"\n":
             # Possible leftover from previous message
             line = rfile.readline()
-    except exceptions.TcpDisconnect:
+    except (exceptions.TcpDisconnect, exceptions.TlsException):
         raise exceptions.HttpReadDisconnect("Remote disconnected")
     if not line:
         raise exceptions.HttpReadDisconnect("Remote disconnected")


Test patch: