From 2e079ad6812b07f31e089f735e98407aa2c29bd0 Mon Sep 17 00:00:00 2001 From: tt2468 <tt2468@gmail.com> Date: Sun, 21 Nov 2021 01:04:16 -0800 Subject: [PATCH] WebSocketServer: Various code cleanup --- src/websocketserver/WebSocketServer.cpp | 15 ++++++------ src/websocketserver/WebSocketServer.h | 24 ++++++++++--------- .../WebSocketServer_Protocol.cpp | 6 ++--- ...WebSocketServer_RequestBatchProcessing.cpp | 2 +- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/websocketserver/WebSocketServer.cpp b/src/websocketserver/WebSocketServer.cpp index be288b75..aa823082 100644 --- a/src/websocketserver/WebSocketServer.cpp +++ b/src/websocketserver/WebSocketServer.cpp @@ -114,9 +114,10 @@ void WebSocketServer::Start() _serverPort = conf->ServerPort; _serverPassword = conf->ServerPassword; - AuthenticationRequired = conf->AuthRequired; - AuthenticationSalt = Utils::Crypto::GenerateSalt(); - AuthenticationSecret = Utils::Crypto::GenerateSecret(conf->ServerPassword.toStdString(), AuthenticationSalt); + _authenticationRequired = conf->AuthRequired; + + _authenticationSalt = Utils::Crypto::GenerateSalt(); + _authenticationSecret = Utils::Crypto::GenerateSecret(conf->ServerPassword.toStdString(), _authenticationSalt); // Set log levels if debug is enabled if (IsDebugEnabled()) { @@ -264,7 +265,7 @@ void WebSocketServer::onOpen(websocketpp::connection_hdl hdl) // Configure session details session->SetRemoteAddress(conn->get_remote_endpoint()); session->SetConnectedAt(QDateTime::currentSecsSinceEpoch()); - session->SetAuthenticationRequired(AuthenticationRequired); + session->SetAuthenticationRequired(_authenticationRequired); std::string selectedSubprotocol = conn->get_subprotocol(); if (!selectedSubprotocol.empty()) { if (selectedSubprotocol == "obswebsocket.json") @@ -277,13 +278,13 @@ void WebSocketServer::onOpen(websocketpp::connection_hdl hdl) json helloMessageData; helloMessageData["obsWebSocketVersion"] = OBS_WEBSOCKET_VERSION; helloMessageData["rpcVersion"] = OBS_WEBSOCKET_RPC_VERSION; - if (AuthenticationRequired) { - session->SetSecret(AuthenticationSecret); + if (_authenticationRequired) { + session->SetSecret(_authenticationSecret); std::string sessionChallenge = Utils::Crypto::GenerateSalt(); session->SetChallenge(sessionChallenge); helloMessageData["authentication"] = json::object(); helloMessageData["authentication"]["challenge"] = sessionChallenge; - helloMessageData["authentication"]["salt"] = AuthenticationSalt; + helloMessageData["authentication"]["salt"] = _authenticationSalt; } json helloMessage; helloMessage["op"] = 0; diff --git a/src/websocketserver/WebSocketServer.h b/src/websocketserver/WebSocketServer.h index 315338c8..782ae668 100644 --- a/src/websocketserver/WebSocketServer.h +++ b/src/websocketserver/WebSocketServer.h @@ -58,7 +58,7 @@ class WebSocketServer : QObject void Start(); void Stop(); void InvalidateSession(websocketpp::connection_hdl hdl); - void BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData = nullptr, uint8_t rpcVersion = 0); + void BroadcastEvent(uint64_t requiredIntent, const std::string &eventType, const json &eventData = nullptr, uint8_t rpcVersion = 0); bool IsListening() { return _server.is_listening(); @@ -70,10 +70,6 @@ class WebSocketServer : QObject return &_threadPool; } - bool AuthenticationRequired; - std::string AuthenticationSecret; - std::string AuthenticationSalt; - signals: void ClientConnected(WebSocketSessionState state); void ClientDisconnected(WebSocketSessionState state, uint16_t closeCode); @@ -93,16 +89,22 @@ class WebSocketServer : QObject void onClose(websocketpp::connection_hdl hdl); void onMessage(websocketpp::connection_hdl hdl, websocketpp::server<websocketpp::config::asio>::message_ptr message); - void SetSessionParameters(SessionPtr session, WebSocketServer::ProcessResult &ret, json &payloadData); - void ProcessMessage(SessionPtr session, ProcessResult &ret, WebSocketOpCode::WebSocketOpCode opCode, json &payloadData); + void SetSessionParameters(SessionPtr session, WebSocketServer::ProcessResult &ret, const json &payloadData); + void ProcessMessage(SessionPtr session, ProcessResult &ret, WebSocketOpCode::WebSocketOpCode opCode, const json &payloadData); - void ProcessRequestBatch(SessionPtr session, ObsWebSocketRequestBatchExecutionType executionType, std::vector<json> &requests, std::vector<json> &results, json &variables); + void ProcessRequestBatch(SessionPtr session, ObsWebSocketRequestBatchExecutionType executionType, const std::vector<json> &requests, std::vector<json> &results, json &variables); + + QThreadPool _threadPool; std::thread _serverThread; websocketpp::server<websocketpp::config::asio> _server; - QThreadPool _threadPool; - std::mutex _sessionMutex; - std::map<websocketpp::connection_hdl, SessionPtr, std::owner_less<websocketpp::connection_hdl>> _sessions; uint16_t _serverPort; QString _serverPassword; + bool _authenticationRequired; + + std::string _authenticationSecret; + std::string _authenticationSalt; + + std::mutex _sessionMutex; + std::map<websocketpp::connection_hdl, SessionPtr, std::owner_less<websocketpp::connection_hdl>> _sessions; }; diff --git a/src/websocketserver/WebSocketServer_Protocol.cpp b/src/websocketserver/WebSocketServer_Protocol.cpp index 6d36587e..8eb9af32 100644 --- a/src/websocketserver/WebSocketServer_Protocol.cpp +++ b/src/websocketserver/WebSocketServer_Protocol.cpp @@ -33,7 +33,7 @@ bool IsSupportedRpcVersion(uint8_t requestedVersion) return (requestedVersion == 1); } -void WebSocketServer::SetSessionParameters(SessionPtr session, ProcessResult &ret, json &payloadData) +void WebSocketServer::SetSessionParameters(SessionPtr session, ProcessResult &ret, const json &payloadData) { if (payloadData.contains("ignoreInvalidMessages")) { if (!payloadData["ignoreInvalidMessages"].is_boolean()) { @@ -54,7 +54,7 @@ void WebSocketServer::SetSessionParameters(SessionPtr session, ProcessResult &re } } -void WebSocketServer::ProcessMessage(SessionPtr session, WebSocketServer::ProcessResult &ret, WebSocketOpCode::WebSocketOpCode opCode, json &payloadData) +void WebSocketServer::ProcessMessage(SessionPtr session, WebSocketServer::ProcessResult &ret, WebSocketOpCode::WebSocketOpCode opCode, const json &payloadData) { if (!payloadData.is_object()) { if (payloadData.is_null()) { @@ -289,7 +289,7 @@ void WebSocketServer::ProcessMessage(SessionPtr session, WebSocketServer::Proces } // It isn't consistent to directly call the WebSocketServer from the events system, but it would also be dumb to make it unnecessarily complicated. -void WebSocketServer::BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData, uint8_t rpcVersion) +void WebSocketServer::BroadcastEvent(uint64_t requiredIntent, const std::string &eventType, const json &eventData, uint8_t rpcVersion) { if (!_server.is_listening()) return; diff --git a/src/websocketserver/WebSocketServer_RequestBatchProcessing.cpp b/src/websocketserver/WebSocketServer_RequestBatchProcessing.cpp index dee8e156..3ff4c775 100644 --- a/src/websocketserver/WebSocketServer_RequestBatchProcessing.cpp +++ b/src/websocketserver/WebSocketServer_RequestBatchProcessing.cpp @@ -193,7 +193,7 @@ void ObsTickCallback(void *param, float) profile_end("obs-websocket-request-batch-frame-tick"); } -void WebSocketServer::ProcessRequestBatch(SessionPtr session, ObsWebSocketRequestBatchExecutionType executionType, std::vector<json> &requests, std::vector<json> &results, json &variables) +void WebSocketServer::ProcessRequestBatch(SessionPtr session, ObsWebSocketRequestBatchExecutionType executionType, const std::vector<json> &requests, std::vector<json> &results, json &variables) { RequestHandler requestHandler(session); if (executionType == OBS_WEBSOCKET_REQUEST_BATCH_EXECUTION_TYPE_SERIAL_REALTIME) {