Skip to content

Commit

Permalink
chore: update deps
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Nov 16, 2023
1 parent db771f8 commit be89da3
Show file tree
Hide file tree
Showing 15 changed files with 841 additions and 445 deletions.
13 changes: 8 additions & 5 deletions plugin/guesslang/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import threading
from typing import Protocol
from typing import Any, Protocol

import sublime

Expand All @@ -25,10 +25,13 @@ def on_close(self, ws: websocket.WebSocketApp, close_status_code: int, close_msg


class NullTransportCallbacks:
on_open = None
on_message = None
on_error = None
on_close = None
def _(*args: Any) -> None:
pass

on_open = _
on_message = _
on_error = _
on_close = _


class GuesslangClient:
Expand Down
6 changes: 3 additions & 3 deletions plugin/libs/websocket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__init__.py
websocket - WebSocket client library for Python
Copyright 2021 engn33r
Copyright 2023 engn33r
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,10 +17,10 @@
limitations under the License.
"""
from ._abnf import *
from ._app import WebSocketApp
from ._app import WebSocketApp, setReconnect
from ._core import *
from ._exceptions import *
from ._logging import *
from ._socket import *

__version__ = "1.2.1"
__version__ = "1.6.4"
109 changes: 56 additions & 53 deletions plugin/libs/websocket/_abnf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
"""
import array
import os
import struct
import sys

"""
from threading import Lock
from typing import Callable, Union

from ._exceptions import *
from ._utils import validate_utf8

"""
_abnf.py
websocket - WebSocket client library for Python
Copyright 2021 engn33r
Copyright 2023 engn33r
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -20,14 +27,6 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
import array
import os
import struct
import sys

from ._exceptions import *
from ._utils import validate_utf8
from threading import Lock

try:
# If wsaccel is available, use compiled routines to mask data.
Expand All @@ -36,18 +35,18 @@
# Note that wsaccel is unmaintained.
from wsaccel.xormask import XorMaskerSimple

def _mask(_m, _d):
def _mask(_m, _d) -> bytes:
return XorMaskerSimple(_m).process(_d)

except ImportError:
# wsaccel is not available, use websocket-client _mask()
native_byteorder = sys.byteorder

def _mask(mask_value, data_value):
def _mask(mask_value: array.array, data_value: array.array) -> bytes:
datalen = len(data_value)
data_value = int.from_bytes(data_value, native_byteorder)
mask_value = int.from_bytes(mask_value * (datalen // 4) + mask_value[: datalen % 4], native_byteorder)
return (data_value ^ mask_value).to_bytes(datalen, native_byteorder)
int_data_value = int.from_bytes(data_value, native_byteorder)
int_mask_value = int.from_bytes(mask_value * (datalen // 4) + mask_value[: datalen % 4], native_byteorder)
return (int_data_value ^ int_mask_value).to_bytes(datalen, native_byteorder)


__all__ = [
Expand Down Expand Up @@ -79,6 +78,8 @@ def _mask(mask_value, data_value):
STATUS_MESSAGE_TOO_BIG = 1009
STATUS_INVALID_EXTENSION = 1010
STATUS_UNEXPECTED_CONDITION = 1011
STATUS_SERVICE_RESTART = 1012
STATUS_TRY_AGAIN_LATER = 1013
STATUS_BAD_GATEWAY = 1014
STATUS_TLS_HANDSHAKE_ERROR = 1015

Expand All @@ -92,11 +93,13 @@ def _mask(mask_value, data_value):
STATUS_MESSAGE_TOO_BIG,
STATUS_INVALID_EXTENSION,
STATUS_UNEXPECTED_CONDITION,
STATUS_SERVICE_RESTART,
STATUS_TRY_AGAIN_LATER,
STATUS_BAD_GATEWAY,
)


class ABNF(object):
class ABNF:
"""
ABNF frame class.
See http://tools.ietf.org/html/rfc5234
Expand Down Expand Up @@ -130,8 +133,8 @@ class ABNF(object):
LENGTH_16 = 1 << 16
LENGTH_63 = 1 << 63

def __init__(self, fin=0, rsv1=0, rsv2=0, rsv3=0,
opcode=OPCODE_TEXT, mask=1, data=""):
def __init__(self, fin: int = 0, rsv1: int = 0, rsv2: int = 0, rsv3: int = 0,
opcode: int = OPCODE_TEXT, mask: int = 1, data: Union[str, bytes] = "") -> None:
"""
Constructor for ABNF. Please check RFC for arguments.
"""
Expand All @@ -146,7 +149,7 @@ def __init__(self, fin=0, rsv1=0, rsv2=0, rsv3=0,
self.data = data
self.get_mask_key = os.urandom

def validate(self, skip_utf8_validation=False):
def validate(self, skip_utf8_validation: bool = False) -> None:
"""
Validate the ABNF frame.
Expand Down Expand Up @@ -174,39 +177,39 @@ def validate(self, skip_utf8_validation=False):

code = 256 * self.data[0] + self.data[1]
if not self._is_valid_close_status(code):
raise WebSocketProtocolException("Invalid close opcode.")
raise WebSocketProtocolException("Invalid close opcode %r", code)

@staticmethod
def _is_valid_close_status(code):
def _is_valid_close_status(code: int) -> bool:
return code in VALID_CLOSE_STATUS or (3000 <= code < 5000)

def __str__(self):
def __str__(self) -> str:
return "fin=" + str(self.fin) \
+ " opcode=" + str(self.opcode) \
+ " data=" + str(self.data)

@staticmethod
def create_frame(data, opcode, fin=1):
def create_frame(data: Union[bytes, str], opcode: int, fin: int = 1) -> 'ABNF':
"""
Create frame to send text, binary and other data.
Parameters
----------
data: <type>
data: str
data to send. This is string value(byte array).
If opcode is OPCODE_TEXT and this value is unicode,
data value is converted into unicode string, automatically.
opcode: <type>
operation code. please see OPCODE_XXX.
fin: <type>
opcode: int
operation code. please see OPCODE_MAP.
fin: int
fin flag. if set to 0, create continue fragmentation.
"""
if opcode == ABNF.OPCODE_TEXT and isinstance(data, str):
data = data.encode("utf-8")
# mask must be set if send data from client
return ABNF(fin, 0, 0, 0, opcode, 1, data)

def format(self):
def format(self) -> bytes:
"""
Format this object to string(byte array) to send data to server.
"""
Expand Down Expand Up @@ -236,7 +239,7 @@ def format(self):
mask_key = self.get_mask_key(4)
return frame_header + self._get_masked(mask_key)

def _get_masked(self, mask_key):
def _get_masked(self, mask_key: Union[str, bytes]) -> bytes:
s = ABNF.mask(mask_key, self.data)

if isinstance(mask_key, str):
Expand All @@ -245,15 +248,15 @@ def _get_masked(self, mask_key):
return mask_key + s

@staticmethod
def mask(mask_key, data):
def mask(mask_key: Union[str, bytes], data: Union[str, bytes]) -> bytes:
"""
Mask or unmask data. Just do xor for each byte
Parameters
----------
mask_key: <type>
4 byte string.
data: <type>
mask_key: bytes or str
4 byte mask.
data: bytes or str
data to mask/unmask.
"""
if data is None:
Expand All @@ -268,11 +271,11 @@ def mask(mask_key, data):
return _mask(array.array("B", mask_key), array.array("B", data))


class frame_buffer(object):
class frame_buffer:
_HEADER_MASK_INDEX = 5
_HEADER_LENGTH_INDEX = 6

def __init__(self, recv_fn, skip_utf8_validation):
def __init__(self, recv_fn: Callable[[int], int], skip_utf8_validation: bool) -> None:
self.recv = recv_fn
self.skip_utf8_validation = skip_utf8_validation
# Buffers over the packets from the layer beneath until desired amount
Expand All @@ -281,15 +284,15 @@ def __init__(self, recv_fn, skip_utf8_validation):
self.clear()
self.lock = Lock()

def clear(self):
def clear(self) -> None:
self.header = None
self.length = None
self.mask = None

def has_received_header(self):
def has_received_header(self) -> bool:
return self.header is None

def recv_header(self):
def recv_header(self) -> None:
header = self.recv_strict(2)
b1 = header[0]
fin = b1 >> 7 & 1
Expand All @@ -303,15 +306,15 @@ def recv_header(self):

self.header = (fin, rsv1, rsv2, rsv3, opcode, has_mask, length_bits)

def has_mask(self):
def has_mask(self) -> Union[bool, int]:
if not self.header:
return False
return self.header[frame_buffer._HEADER_MASK_INDEX]

def has_received_length(self):
def has_received_length(self) -> bool:
return self.length is None

def recv_length(self):
def recv_length(self) -> None:
bits = self.header[frame_buffer._HEADER_LENGTH_INDEX]
length_bits = bits & 0x7f
if length_bits == 0x7e:
Expand All @@ -323,13 +326,13 @@ def recv_length(self):
else:
self.length = length_bits

def has_received_mask(self):
def has_received_mask(self) -> bool:
return self.mask is None

def recv_mask(self):
def recv_mask(self) -> None:
self.mask = self.recv_strict(4) if self.has_mask() else ""

def recv_frame(self):
def recv_frame(self) -> ABNF:

with self.lock:
# Header
Expand Down Expand Up @@ -360,7 +363,7 @@ def recv_frame(self):

return frame

def recv_strict(self, bufsize):
def recv_strict(self, bufsize: int) -> bytes:
shortage = bufsize - sum(map(len, self.recv_buffer))
while shortage > 0:
# Limit buffer size that we pass to socket.recv() to avoid
Expand All @@ -373,7 +376,7 @@ def recv_strict(self, bufsize):
self.recv_buffer.append(bytes_)
shortage -= len(bytes_)

unified = bytes("", 'utf-8').join(self.recv_buffer)
unified = b"".join(self.recv_buffer)

if shortage == 0:
self.recv_buffer = []
Expand All @@ -383,22 +386,22 @@ def recv_strict(self, bufsize):
return unified[:bufsize]


class continuous_frame(object):
class continuous_frame:

def __init__(self, fire_cont_frame, skip_utf8_validation):
def __init__(self, fire_cont_frame: bool, skip_utf8_validation: bool) -> None:
self.fire_cont_frame = fire_cont_frame
self.skip_utf8_validation = skip_utf8_validation
self.cont_data = None
self.recving_frames = None

def validate(self, frame):
def validate(self, frame: ABNF) -> None:
if not self.recving_frames and frame.opcode == ABNF.OPCODE_CONT:
raise WebSocketProtocolException("Illegal frame")
if self.recving_frames and \
frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY):
raise WebSocketProtocolException("Illegal frame")

def add(self, frame):
def add(self, frame: ABNF) -> None:
if self.cont_data:
self.cont_data[1] += frame.data
else:
Expand All @@ -409,10 +412,10 @@ def add(self, frame):
if frame.fin:
self.recving_frames = None

def is_fire(self, frame):
def is_fire(self, frame: ABNF) -> Union[bool, int]:
return frame.fin or self.fire_cont_frame

def extract(self, frame):
def extract(self, frame: ABNF) -> list:
data = self.cont_data
self.cont_data = None
frame.data = data[1]
Expand Down
Loading

0 comments on commit be89da3

Please sign in to comment.