WebSocketServer: Various code cleanup

This commit is contained in:
tt2468 2021-11-21 01:04:16 -08:00
parent 29a72f9af8
commit 2e079ad681
4 changed files with 25 additions and 22 deletions

View File

@ -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;

View File

@ -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;
};

View File

@ -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;

View File

@ -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) {