diff options
Diffstat (limited to 'netlib')
-rw-r--r-- | netlib/tcp.py | 2 | ||||
-rw-r--r-- | netlib/utils.py | 16 | ||||
-rw-r--r-- | netlib/version_check.py | 43 | ||||
-rw-r--r-- | netlib/websockets/frame.py | 22 |
4 files changed, 12 insertions, 71 deletions
diff --git a/netlib/tcp.py b/netlib/tcp.py index 4fde657f..4379c9b5 100644 --- a/netlib/tcp.py +++ b/netlib/tcp.py @@ -18,7 +18,7 @@ import OpenSSL from OpenSSL import SSL from netlib import certutils -from netlib import version_check +from mitmproxy.utils import version_check from mitmproxy.types import serializable from netlib import exceptions from mitmproxy.types import basethread diff --git a/netlib/utils.py b/netlib/utils.py index 779eaa27..7b007cb5 100644 --- a/netlib/utils.py +++ b/netlib/utils.py @@ -1,21 +1,5 @@ import re - -def setbit(byte, offset, value): - """ - Set a bit in a byte to 1 if value is truthy, 0 if not. - """ - if value: - return byte | (1 << offset) - else: - return byte & ~(1 << offset) - - -def getbit(byte, offset): - mask = 1 << offset - return bool(byte & mask) - - _label_valid = re.compile(b"(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE) diff --git a/netlib/version_check.py b/netlib/version_check.py deleted file mode 100644 index 547c031c..00000000 --- a/netlib/version_check.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -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. -""" -import sys -import inspect -import os.path - -import OpenSSL - -PYOPENSSL_MIN_VERSION = (0, 15) - - -def check_pyopenssl_version(min_version=PYOPENSSL_MIN_VERSION, fp=sys.stderr): - min_version_str = u".".join(str(x) for x in min_version) - try: - v = tuple(int(x) for x in OpenSSL.__version__.split(".")[:2]) - except ValueError: - print( - u"Cannot parse pyOpenSSL version: {}" - u"mitmproxy requires pyOpenSSL {} or greater.".format( - OpenSSL.__version__, min_version_str - ), - file=fp - ) - return - if v < min_version: - print( - u"You are using an outdated version of pyOpenSSL: " - u"mitmproxy requires pyOpenSSL {} or greater.".format(min_version_str), - 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( - u"Your pyOpenSSL {} installation is located at {}".format( - OpenSSL.__version__, pyopenssl_path - ), - file=fp - ) - sys.exit(1) diff --git a/netlib/websockets/frame.py b/netlib/websockets/frame.py index 26303094..bc4ae43a 100644 --- a/netlib/websockets/frame.py +++ b/netlib/websockets/frame.py @@ -4,7 +4,7 @@ import io from netlib import tcp from mitmproxy.utils import strutils -from netlib import utils +from mitmproxy.utils import bits from mitmproxy.utils import human from mitmproxy.types import bidi from .masker import Masker @@ -119,13 +119,13 @@ class FrameHeader: return "".join(vals) def __bytes__(self): - first_byte = utils.setbit(0, 7, self.fin) - first_byte = utils.setbit(first_byte, 6, self.rsv1) - first_byte = utils.setbit(first_byte, 5, self.rsv2) - first_byte = utils.setbit(first_byte, 4, self.rsv3) + first_byte = bits.setbit(0, 7, self.fin) + first_byte = bits.setbit(first_byte, 6, self.rsv1) + first_byte = bits.setbit(first_byte, 5, self.rsv2) + first_byte = bits.setbit(first_byte, 4, self.rsv3) first_byte = first_byte | self.opcode - second_byte = utils.setbit(self.length_code, 7, self.mask) + second_byte = bits.setbit(self.length_code, 7, self.mask) b = bytes([first_byte, second_byte]) @@ -152,12 +152,12 @@ class FrameHeader: read a websockets frame header """ first_byte, second_byte = fp.safe_read(2) - fin = utils.getbit(first_byte, 7) - rsv1 = utils.getbit(first_byte, 6) - rsv2 = utils.getbit(first_byte, 5) - rsv3 = utils.getbit(first_byte, 4) + fin = bits.getbit(first_byte, 7) + rsv1 = bits.getbit(first_byte, 6) + rsv2 = bits.getbit(first_byte, 5) + rsv3 = bits.getbit(first_byte, 4) opcode = first_byte & 0xF - mask_bit = utils.getbit(second_byte, 7) + mask_bit = bits.getbit(second_byte, 7) length_code = second_byte & 0x7F # payload_length > 125 indicates you need to read more bytes |