8000 Pass sslcontext for post requests, direct c&p from sslaestransport · python-kasa/python-kasa@78eda5f · GitHub
[go: up one dir, main page]

Skip to content

Commit 78eda5f

Browse files
committed
Pass sslcontext for post requests, direct c&p from sslaestransport
1 parent f1595b8 commit 78eda5f

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

kasa/transports/klaptransport.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import hashlib
4949
import logging
5050
import secrets
51+
import ssl
5152
import struct
5253
import time
5354
from asyncio import Future
@@ -94,6 +95,17 @@ class KlapTransport(BaseTransport):
9495
DEFAULT_PORT: int = 80
9596
SESSION_COOKIE_NAME = "TP_SESSIONID"
9697
TIMEOUT_COOKIE_NAME = "TIMEOUT"
98+
# Copy & paste from sslaestransport
99+
CIPHERS = ":".join(
100+
[
101+
"AES256-GCM-SHA384",
102+
"AES256-SHA256",
103+
"AES128-GCM-SHA256",
104+
"AES128-SHA256",
105+
"AES256-SHA",
106+
]
107+
)
108+
_ssl_context: ssl.SSLContext | None = None
97109

98110
def __init__(
99111
self,
@@ -153,7 +165,9 @@ async def perform_handshake1(self) -> tuple[bytes, bytes, bytes]:
153165

154166
url = self._app_url / "handshake1"
155167

156-
response_status, response_data = await self._http_client.post(url, data=payload)
168+
response_status, response_data = await self._http_client.post(
169+
url, data=payload, ssl=await self._get_ssl_context()
170+
)
157171

158172
if _LOGGER.isEnabledFor(logging.DEBUG):
159173
_LOGGER.debug(
@@ -264,6 +278,7 @@ async def perform_handshake2(
264278
url,
265279
data=payload,
266280
cookies_dict=self._session_cookie,
281+
ssl=await self._get_ssl_context(),
267282
)
268283

269284
if _LOGGER.isEnabledFor(logging.DEBUG):
@@ -338,6 +353,7 @@ async def send(self, request: str) -> Generator[Future, None, dict[str, str]]:
338353
params={"seq": seq},
339354
data=payload,
340355
cookies_dict=self._session_cookie,
356+
ssl=await self._get_ssl_context(),
341357
)
342358

343359
msg = (
@@ -414,6 +430,23 @@ def generate_owner_hash(creds: Credentials) -> bytes:
414430
un = creds.username
415431
return md5(un.encode())
416432

433+
# Copy & paste from sslaestransport.
434+
def _create_ssl_context(self) -> ssl.SSLContext:
435+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
436+
context.set_ciphers(self.CIPHERS)
437+
context.check_hostname = False
438+
context.verify_mode = ssl.CERT_NONE
439+
return context
440+
441+
# Copy & paste from sslaestransport.
442+
async def _get_ssl_context(self) -> ssl.SSLContext:
443+
if not self._ssl_context:
444+
loop = asyncio.get_running_loop()
445+
self._ssl_context = await loop.run_in_executor(
446+
None, self._create_ssl_context
447+
)
448+
return self._ssl_context
449+
417450

418451
class KlapTransportV2(KlapTransport):
419452
"""Implementation of the KLAP encryption protocol with v2 hanshake hashes."""

0 commit comments

Comments
 (0)
0