Skip to content

Commit

Permalink
Fix tls-support
Browse files Browse the repository at this point in the history
  • Loading branch information
SukramJ committed Nov 26, 2021
1 parent f46256f commit 0df8a45
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion hahomematic/central_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, central_config):
self.username = None
else:
self.username = self.central_config.username
self.tls = self.central_config.json_tls
self.tls = self.central_config.tls
self.verify_tls = self.central_config.verify_tls
self.client_session = self.central_config.client_session

Expand Down
35 changes: 25 additions & 10 deletions hahomematic/json_rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,20 @@ def __init__(
self._port = self._central_config.json_port
self._username = self._central_config.username
self._password = self._central_config.password
self._tls = self._central_config.json_tls
self._json_tls = self._central_config.json_tls
self._verify_tls = self._central_config.verify_tls
self._ssl_context = self._get_tls_context()

def _get_tls_context(self):
ssl_context = None
if self._json_tls:
if self._verify_tls:
ssl_context = ssl.create_default_context()
else:
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
return ssl_context

@property
def is_activated(self):
Expand Down Expand Up @@ -158,17 +170,13 @@ async def _post(
}

_LOGGER.debug("json_rpc_client._post: API-Endpoint: %s", self._url)
if self._tls:
ssl_context = UNVERIFIED_CTX
if self._verify_tls:
ssl_context = VERIFIED_CTX

if self._json_tls:
resp = await self._client_session.post(
self._url,
data=payload,
headers=headers,
timeout=config.TIMEOUT,
context=ssl_context,
ssl=self._ssl_context,
)
else:
resp = await self._client_session.post(
Expand All @@ -194,6 +202,9 @@ async def _post(
except ClientError as cce:
_LOGGER.exception("json_rpc_client._post: ClientError")
return {"error": str(cce), "result": {}}
except TypeError as ter:
_LOGGER.exception("json_rpc_client._post: TypeError")
return {"error": str(ter), "result": {}}

async def logout(self):
"""Logout of CCU."""
Expand Down Expand Up @@ -224,9 +235,13 @@ async def _logout(self, session_id):
@property
def _url(self):
"""Return the required url."""
if self._tls:
return f"https://{self._host}:{self._port}{PATH_JSON_RPC}"
return f"http://{self._host}:{self._port}{PATH_JSON_RPC}"
url = "http://"
if self._json_tls:
url = "https://"
url = f"{url}{self._host}"
if self._port:
url = f"{url}:{self._port}"
return f"{url}{PATH_JSON_RPC}"


def _get_params(session_id, extra_params, use_default_params) -> dict[str, str]:
Expand Down
24 changes: 19 additions & 5 deletions hahomematic/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import logging
import ssl
import xmlrpc.client
from hahomematic.const import ATTR_TLS, ATTR_VERIFY_TLS

_LOGGER = logging.getLogger(__name__)

ATTR_CONTEXT = "context"
ATTR_ENCODING_ISO_8859_1 = "ISO-8859-1"

class ProxyException(Exception):
"""hahomematic Proxy exception."""
Expand All @@ -28,11 +31,22 @@ def __init__(self, executor_func, *args, **kwargs):
Initialize new proxy for server and get local ip
"""
self._executor_func = executor_func
self._tls = kwargs.pop("tls", False)
self._verify_tls = kwargs.pop("verify_tls", True)
if self._tls and not self._verify_tls and self._verify_tls is not None:
kwargs["context"] = ssl._create_unverified_context()
xmlrpc.client.ServerProxy.__init__(self, encoding="ISO-8859-1", *args, **kwargs)
self._tls = kwargs.pop(ATTR_TLS, False)
self._verify_tls = kwargs.pop(ATTR_VERIFY_TLS, True)
if self._tls:
kwargs[ATTR_CONTEXT] = self._get_tls_context()
xmlrpc.client.ServerProxy.__init__(self, encoding=ATTR_ENCODING_ISO_8859_1, *args, **kwargs)

def _get_tls_context(self):
ssl_context = None
if self._tls:
if self._verify_tls:
ssl_context = ssl.create_default_context()
else:
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
return ssl_context

async def __async_request(self, *args, **kwargs):
"""
Expand Down

0 comments on commit 0df8a45

Please sign in to comment.