frostdeday

SSL Fix

Apr 13th, 2025
65
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | Source Code | 0 0
  1. """Sengled Bulb Integration."""
  2.  
  3. import json
  4. import logging
  5. import ssl
  6.  
  7. import aiohttp
  8. import certifi
  9. import requests
  10.  
  11. from .exceptions import SengledApiAccessToken
  12.  
  13. _LOGGER = logging.getLogger(__name__)
  14.  
  15. _LOGGER.info("SengledApi: Initializing Request")
  16.  
  17. import asyncio
  18. import functools
  19. from concurrent.futures import ThreadPoolExecutor
  20.  
  21. # You can keep this in case you later want to switch back to secure SSL
  22. async def async_create_ssl_context():
  23. loop = asyncio.get_running_loop()
  24. with ThreadPoolExecutor() as executor:
  25. return await loop.run_in_executor(
  26. executor, functools.partial(ssl.create_default_context, cafile=certifi.where())
  27. )
  28.  
  29.  
  30. class Request:
  31. def __init__(self, url, payload, no_return=False):
  32. _LOGGER.info("SengledApi: Sengled Request initializing.")
  33. self._url = url
  34. self._payload = json.dumps(payload)
  35. self._no_return = no_return
  36. self._response = None
  37. self._jsession_id = None
  38.  
  39. self._header = {
  40. "Content-Type": "application/json",
  41. "Host": "element.cloud.sengled.com:443",
  42. "Connection": "keep-alive",
  43. }
  44.  
  45. async def async_get_response(self, jsession_id):
  46. self._header = {
  47. "Content-Type": "application/json",
  48. "Cookie": f"JSESSIONID={jsession_id}",
  49. "Connection": "keep-alive",
  50. }
  51.  
  52. # WARNING: Disabling SSL certificate verification
  53. # Use ssl=False to bypass certificate checks (NOT SECURE)
  54. async with aiohttp.ClientSession() as session:
  55. async with session.post(self._url, headers=self._header, data=self._payload, ssl=False) as response:
  56. if response.status == 200:
  57. data = await response.json()
  58. return data
  59. else:
  60. _LOGGER.error("Failed to get response, status: %s", response.status)
  61. return None
  62.  
  63. def get_login_response(self):
  64. _LOGGER.info("SengledApi: Get Login Reponse.")
  65. r = requests.post(self._url, headers=self._header, data=self._payload)
  66. data = r.json()
  67. _LOGGER.debug("SengledApi: Get Login Reponse %s", str(data))
  68. return data
  69.  
  70. async def async_get_login_response(self):
  71. _LOGGER.info("SengledApi: Get Login Response async.")
  72. async with aiohttp.ClientSession() as session:
  73. async with session.post(
  74. self._url, headers=self._header, data=self._payload, ssl=False
  75. ) as resp:
  76. if resp.status == 200:
  77. data = await resp.json()
  78. _LOGGER.debug("SengledApi: Get Login Response %s ", str(data))
  79. return data
  80. else:
  81. _LOGGER.error("Failed to get login response, status: %s", resp.status)
  82. return None
  83.  
  84. def is_session_timeout_response(self, jsession_id):
  85. _LOGGER.info("SengledApi: Get Session Timeout Response")
  86. self._header = {
  87. "Content-Type": "application/json",
  88. "Cookie": "JSESSIONID={}".format(jsession_id),
  89. "sid": jsession_id,
  90. "X-Requested-With": "com.sengled.life2",
  91. }
  92.  
  93. r = requests.post(self._url, headers=self._header, data=self._payload)
  94. data = r.json()
  95. _LOGGER.debug("SengledApi: Get Session Timeout Response %s", str(data))
  96. return data
  97.  
  98. async def async_is_session_timeout_response(self, jsession_id):
  99. _LOGGER.info("SengledApi: Get Session Timeout Response Async")
  100. self._header = {
  101. "Content-Type": "application/json",
  102. "Cookie": "JSESSIONID={}".format(jsession_id),
  103. "sid": jsession_id,
  104. "X-Requested-With": "com.sengled.life2",
  105. }
  106. async with aiohttp.ClientSession() as session:
  107. async with session.post(
  108. self._url, headers=self._header, data=self._payload, ssl=False
  109. ) as resp:
  110. if resp.status == 200:
  111. data = await resp.json()
  112. _LOGGER.info(
  113. "SengledApi: Get Session Timeout Response Async %s", str(data)
  114. )
  115. return data
  116. else:
  117. _LOGGER.error("Failed to get session timeout response, status: %s", resp.status)
  118. return None
  119.  
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment