From 8a5f791ece92312a1dfd538d7ded0fe7a5a03a22 Mon Sep 17 00:00:00 2001 From: Prateek Sazawal Date: Sat, 28 Sep 2024 15:56:24 +0200 Subject: [PATCH] Added patch to fix ssl.wrap_socket error --- LGTV/auth.py | 3 ++- LGTV/cursor.py | 3 ++- LGTV/remote.py | 3 ++- LGTV/ws4py_patch.py | 55 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 LGTV/ws4py_patch.py diff --git a/LGTV/auth.py b/LGTV/auth.py index 91aacf2..0c73d3f 100644 --- a/LGTV/auth.py +++ b/LGTV/auth.py @@ -1,4 +1,5 @@ -from ws4py.client.threadedclient import WebSocketClient +# from ws4py.client.threadedclient import WebSocketClient +from .ws4py_patch import WebSocketClientPatch as WebSocketClient from getmac import get_mac_address import subprocess import socket diff --git a/LGTV/cursor.py b/LGTV/cursor.py index 42c460a..2194afc 100644 --- a/LGTV/cursor.py +++ b/LGTV/cursor.py @@ -4,7 +4,8 @@ from time import sleep from .remote import LGTVRemote -from ws4py.client.threadedclient import WebSocketClient +# from ws4py.client.threadedclient import WebSocketClient +from .ws4py_patch import WebSocketClientPatch as WebSocketClient class LGTVCursor(WebSocketClient): diff --git a/LGTV/remote.py b/LGTV/remote.py index 6cc141c..8faa293 100644 --- a/LGTV/remote.py +++ b/LGTV/remote.py @@ -1,4 +1,5 @@ -from ws4py.client.threadedclient import WebSocketClient +# from ws4py.client.threadedclient import WebSocketClient +from .ws4py_patch import WebSocketClientPatch as WebSocketClient from types import FunctionType from urllib.parse import parse_qs, urlparse from wakeonlan import send_magic_packet diff --git a/LGTV/ws4py_patch.py b/LGTV/ws4py_patch.py new file mode 100644 index 0000000..66feda1 --- /dev/null +++ b/LGTV/ws4py_patch.py @@ -0,0 +1,55 @@ +from ws4py.client.threadedclient import WebSocketClient +import ssl + +class WebSocketClientPatch(WebSocketClient): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def connect(self): + """ + Connects this websocket and starts the upgrade handshake + with the remote endpoint. + """ + if self.scheme == "wss": + # default port is now 443; upgrade self.sender to send ssl + context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) + if self.ssl_options.get('certfile', None): + context.load_cert_chain(self.ssl_options.get('certfile'), self.ssl_options.get('keyfile')) + # Prevent check_hostname requires server_hostname (ref #187) + if "cert_reqs" not in self.ssl_options: + context.check_hostname = False + context.verify_mode = ssl.CERT_NONE + self.sock = context.wrap_socket(self.sock) + self._is_secure = True + + self.sock.connect(self.bind_addr) + + self._write(self.handshake_request) + + response = b'' + doubleCLRF = b'\r\n\r\n' + while True: + bytes = self.sock.recv(128) + if not bytes: + break + response += bytes + if doubleCLRF in response: + break + + if not response: + self.close_connection() + raise HandshakeError("Invalid response") + + headers, _, body = response.partition(doubleCLRF) + response_line, _, headers = headers.partition(b'\r\n') + + try: + self.process_response_line(response_line) + self.protocols, self.extensions = self.process_handshake_header(headers) + except HandshakeError: + self.close_connection() + raise + + self.handshake_ok() + if body: + self.process(body)