only check for host+port values in init.

only pass auth token if auth enabled

add context manager methods to reqclient.

added logging
This commit is contained in:
onyx-and-iris
2022-10-24 22:42:16 +01:00
parent 69b0d4137a
commit 27fd86efa5
4 changed files with 63 additions and 44 deletions

View File

@@ -1,6 +1,7 @@
import base64
import hashlib
import json
import logging
from pathlib import Path
from random import randint
@@ -11,9 +12,11 @@ except ModuleNotFoundError:
import websocket
from .error import OBSSDKError
class ObsClient:
DELAY = 0.001
logger = logging.getLogger("baseclient.obsclient")
def __init__(self, **kwargs):
defaultkwargs = {
@@ -23,7 +26,7 @@ class ObsClient:
kwargs = defaultkwargs | kwargs
for attr, val in kwargs.items():
setattr(self, attr, val)
if not (self.host and self.port and self.password):
if not (self.host and self.port):
conn = self._conn_from_toml()
self.host = conn["host"]
self.port = conn["port"]
@@ -40,50 +43,51 @@ class ObsClient:
return conn["connection"]
def authenticate(self):
secret = base64.b64encode(
hashlib.sha256(
(
self.password + self.server_hello["d"]["authentication"]["salt"]
).encode()
).digest()
)
auth = base64.b64encode(
hashlib.sha256(
(
secret.decode()
+ self.server_hello["d"]["authentication"]["challenge"]
).encode()
).digest()
).decode()
payload = {
"op": 1,
"d": {
"rpcVersion": 1,
"authentication": auth,
"eventSubscriptions": self.subs,
},
}
if "authentication" in self.server_hello["d"]:
secret = base64.b64encode(
hashlib.sha256(
(
self.password + self.server_hello["d"]["authentication"]["salt"]
).encode()
).digest()
)
auth = base64.b64encode(
hashlib.sha256(
(
secret.decode()
+ self.server_hello["d"]["authentication"]["challenge"]
).encode()
).digest()
).decode()
payload["d"]["authentication"] = auth
self.ws.send(json.dumps(payload))
return self.ws.recv()
try:
response = json.loads(self.ws.recv())
return response["op"] == 2
except json.decoder.JSONDecodeError:
raise OBSSDKError("failed to identify client with the server")
def req(self, req_type, req_data=None):
id = randint(1, 1000)
self.logger.debug(f"Sending request with response id {id}")
payload = {
"op": 6,
"d": {"requestType": req_type, "requestId": id},
}
if req_data:
payload = {
"op": 6,
"d": {
"requestType": req_type,
"requestId": randint(1, 1000),
"requestData": req_data,
},
}
else:
payload = {
"op": 6,
"d": {"requestType": req_type, "requestId": randint(1, 1000)},
}
payload["d"]["requestData"] = req_data
self.ws.send(json.dumps(payload))
response = json.loads(self.ws.recv())
self.logger.debug(f"Reponse received {response}")
return response["d"]