From d7d067774749bc77dcdf3a82170b62bd5b42b328 Mon Sep 17 00:00:00 2001 From: Andriy Kushnir Date: Wed, 14 Feb 2024 08:44:13 +0200 Subject: [PATCH] Replace deprecated ssl.wrap_context method (#228) --- roombapy/__init__.py | 1 + roombapy/getpassword.py | 13 +++++-------- roombapy/remote_client.py | 5 ++--- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/roombapy/__init__.py b/roombapy/__init__.py index 719db53..4d3e195 100644 --- a/roombapy/__init__.py +++ b/roombapy/__init__.py @@ -1,4 +1,5 @@ """Import to public all required modules.""" + from .discovery import RoombaDiscovery from .getpassword import RoombaPassword from .roomba import Roomba, RoombaConnectionError diff --git a/roombapy/getpassword.py b/roombapy/getpassword.py index 5b8101c..fdebf7e 100644 --- a/roombapy/getpassword.py +++ b/roombapy/getpassword.py @@ -1,9 +1,11 @@ """The class helps you to get password for your Roomba.""" + import logging import socket -import ssl import struct +from roombapy.remote_client import generate_tls_context + class RoombaPassword: """Main class to get a password.""" @@ -78,10 +80,5 @@ def _decode_password(data): def _get_socket(): server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.settimeout(10) - ssl_socket = ssl.wrap_socket( - server_socket, - ssl_version=ssl.PROTOCOL_TLS, - ciphers="DEFAULT@SECLEVEL=1", - ) - ssl_socket.context.options |= 0x4 # set OP_LEGACY_SERVER_CONNECT - return ssl_socket + context = generate_tls_context() + return context.wrap_socket(server_socket) diff --git a/roombapy/remote_client.py b/roombapy/remote_client.py index 5d56a96..da7379f 100644 --- a/roombapy/remote_client.py +++ b/roombapy/remote_client.py @@ -10,7 +10,7 @@ @cache -def _generate_tls_context() -> ssl.SSLContext: +def generate_tls_context() -> ssl.SSLContext: """Generate TLS context. We only want to do this once ever because it's expensive. @@ -108,10 +108,9 @@ def _get_mqtt_client(self): self.log.debug("Setting TLS certificate") mqtt_client._ssl_context = None - ssl_context = _generate_tls_context() + ssl_context = generate_tls_context() mqtt_client.tls_set_context(ssl_context) mqtt_client.tls_insecure_set(True) - mqtt_client._ssl_context.options |= 0x4 # set OP_LEGACY_SERVER_CONNECT return mqtt_client