aboutsummaryrefslogtreecommitdiffstats
path: root/netlib
diff options
context:
space:
mode:
Diffstat (limited to 'netlib')
-rw-r--r--netlib/tcp.py10
-rw-r--r--netlib/version_check.py49
-rw-r--r--netlib/websockets.py8
3 files changed, 61 insertions, 6 deletions
diff --git a/netlib/tcp.py b/netlib/tcp.py
index a705c95b..f6179faa 100644
--- a/netlib/tcp.py
+++ b/netlib/tcp.py
@@ -147,6 +147,7 @@ class Writer(_FileLike):
May raise NetLibDisconnect
"""
if v:
+ self.first_byte_timestamp = self.first_byte_timestamp or time.time()
try:
if hasattr(self.o, "sendall"):
self.add_log(v)
@@ -224,9 +225,12 @@ class Reader(_FileLike):
"""
result = self.read(length)
if length != -1 and len(result) != length:
- raise NetLibIncomplete(
- "Expected %s bytes, got %s" % (length, len(result))
- )
+ if not result:
+ raise NetLibDisconnect()
+ else:
+ raise NetLibIncomplete(
+ "Expected %s bytes, got %s" % (length, len(result))
+ )
return result
diff --git a/netlib/version_check.py b/netlib/version_check.py
new file mode 100644
index 00000000..09dc23ae
--- /dev/null
+++ b/netlib/version_check.py
@@ -0,0 +1,49 @@
+from __future__ import print_function, absolute_import
+import sys
+import inspect
+import os.path
+
+import OpenSSL
+from . import version
+
+PYOPENSSL_MIN_VERSION = (0, 15)
+
+
+def version_check(
+ mitmproxy_version,
+ pyopenssl_min_version=PYOPENSSL_MIN_VERSION,
+ fp=sys.stderr):
+ """
+ Having installed a wrong version of pyOpenSSL or netlib is unfortunately a
+ very common source of error. Check before every start that both versions
+ are somewhat okay.
+ """
+ # We don't introduce backward-incompatible changes in patch versions. Only
+ # consider major and minor version.
+ if version.IVERSION[:2] != mitmproxy_version[:2]:
+ print(
+ "You are using mitmproxy %s with netlib %s. "
+ "Most likely, that won't work - please upgrade!" % (
+ mitmproxy_version, version.VERSION
+ ),
+ file=fp
+ )
+ sys.exit(1)
+ v = tuple([int(x) for x in OpenSSL.__version__.split(".")][:2])
+ if v < pyopenssl_min_version:
+ print(
+ "You are using an outdated version of pyOpenSSL:"
+ " mitmproxy requires pyOpenSSL %x or greater." %
+ pyopenssl_min_version,
+ file=fp
+ )
+ # Some users apparently have multiple versions of pyOpenSSL installed.
+ # Report which one we got.
+ pyopenssl_path = os.path.dirname(inspect.getfile(OpenSSL))
+ print(
+ "Your pyOpenSSL %s installation is located at %s" % (
+ OpenSSL.__version__, pyopenssl_path
+ ),
+ file=fp
+ )
+ sys.exit(1)
diff --git a/netlib/websockets.py b/netlib/websockets.py
index 63dc03f1..bf920897 100644
--- a/netlib/websockets.py
+++ b/netlib/websockets.py
@@ -175,7 +175,7 @@ class FrameHeader:
def human_readable(self):
vals = [
- "wf:",
+ "ws frame:",
OPCODE.get_name(self.opcode, hex(self.opcode)).lower()
]
flags = []
@@ -327,8 +327,10 @@ class Frame(object):
return cls.from_file(tcp.Reader(io.BytesIO(bytestring)))
def human_readable(self):
- hdr = self.header.human_readable()
- return hdr + "\n" + repr(self.payload)
+ ret = self.header.human_readable()
+ if self.payload:
+ ret = ret + "\nPayload:\n" + utils.cleanBin(self.payload)
+ return ret
def to_bytes(self):
"""