diff options
-rw-r--r-- | mitmproxy/tools/main.py | 2 | ||||
-rw-r--r-- | mitmproxy/utils/bits.py | 15 | ||||
-rw-r--r-- | mitmproxy/utils/version_check.py (renamed from netlib/version_check.py) | 0 | ||||
-rw-r--r-- | netlib/tcp.py | 2 | ||||
-rw-r--r-- | netlib/utils.py | 16 | ||||
-rw-r--r-- | netlib/websockets/frame.py | 22 | ||||
-rw-r--r-- | test/mitmproxy/test_utils_version_check.py (renamed from test/netlib/test_version_check.py) | 2 |
7 files changed, 29 insertions, 30 deletions
diff --git a/mitmproxy/tools/main.py b/mitmproxy/tools/main.py index f3526de9..9fce9234 100644 --- a/mitmproxy/tools/main.py +++ b/mitmproxy/tools/main.py @@ -6,7 +6,7 @@ from mitmproxy.tools import cmdline from mitmproxy import exceptions from mitmproxy.proxy import config from mitmproxy.proxy import server -from netlib import version_check +from mitmproxy.utils import version_check from mitmproxy.utils import debug diff --git a/mitmproxy/utils/bits.py b/mitmproxy/utils/bits.py new file mode 100644 index 00000000..ec0d40ef --- /dev/null +++ b/mitmproxy/utils/bits.py @@ -0,0 +1,15 @@ + + +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) diff --git a/netlib/version_check.py b/mitmproxy/utils/version_check.py index 547c031c..547c031c 100644 --- a/netlib/version_check.py +++ b/mitmproxy/utils/version_check.py 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/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 diff --git a/test/netlib/test_version_check.py b/test/mitmproxy/test_utils_version_check.py index 01820dad..5c8d8c8c 100644 --- a/test/netlib/test_version_check.py +++ b/test/mitmproxy/test_utils_version_check.py @@ -1,6 +1,6 @@ import io import mock -from netlib import version_check +from mitmproxy.utils import version_check @mock.patch("sys.exit") |