diff --git a/installer/installer-windows.generated.iss b/installer/installer-windows.generated.iss index e02fafaf..c297bf0b 100644 --- a/installer/installer-windows.generated.iss +++ b/installer/installer-windows.generated.iss @@ -7,7 +7,7 @@ ; NOTE: The value of AppId uniquely identifies this application. ; Do not use the same AppId value in installers for other applications. ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.) -AppId={117EE44F-48E1-49E5-A381-CC8D9195CF35} +AppId={{117EE44F-48E1-49E5-A381-CC8D9195CF35} AppName={#MyAppName} AppVersion={#MyAppVersion} AppPublisher={#MyAppPublisher} diff --git a/src/WebSocketServer.cpp b/src/WebSocketServer.cpp index 0b155100..db50ee2c 100644 --- a/src/WebSocketServer.cpp +++ b/src/WebSocketServer.cpp @@ -78,6 +78,7 @@ void WebSocketServer::Start() } _serverPort = conf->ServerPort; + _authenticationRequired = conf->AuthRequired; _authenticationSalt = Utils::Crypto::GenerateSalt(); _authenticationSecret = Utils::Crypto::GenerateSecret(conf->ServerPassword.toStdString(), _authenticationSalt); @@ -204,9 +205,43 @@ void WebSocketServer::onOpen(websocketpp::connection_hdl hdl) auto conn = _server.get_con_from_hdl(hdl); std::unique_lock lock(_sessionMutex); - _sessions[hdl].SetRemoteAddress(conn->get_remote_endpoint()); - _sessions[hdl].SetConnectedAt(QDateTime::currentSecsSinceEpoch()); + auto &session = _sessions[hdl]; lock.unlock(); + + session.SetRemoteAddress(conn->get_remote_endpoint()); + session.SetConnectedAt(QDateTime::currentSecsSinceEpoch()); + std::string contentType = conn->get_request_header("Content-Type"); + if (contentType == "") { + ; + } else if (contentType == "application/json") { + session.SetEncoding(WebSocketEncoding::Json); + } else if (contentType == "application/msgpack") { + session.SetEncoding(WebSocketEncoding::MsgPack); + } else { + conn->close(WebSocketCloseCode::InvalidContentType, "Your HTTP `Content-Type` header specifies an invalid encoding type."); + return; + } + + json helloMessage; + helloMessage["messageType"] = "Hello"; + helloMessage["obsWebSocketVersion"] = OBS_WEBSOCKET_VERSION; + helloMessage["rpcVersion"] = OBS_WEBSOCKET_RPC_VERSION; + // todo: Add request and event lists + if (_authenticationRequired) { + std::string sessionChallenge = Utils::Crypto::GenerateSalt(); + session.SetChallenge(sessionChallenge); + helloMessage["authentication"] = {}; + helloMessage["authentication"]["challenge"] = sessionChallenge; + helloMessage["authentication"]["salt"] = _authenticationSalt; + } + + auto sessionEncoding = session.Encoding(); + if (sessionEncoding == WebSocketEncoding::Json) { + conn->send(helloMessage.dump()); + } else if (sessionEncoding == WebSocketEncoding::MsgPack) { + auto message = json::to_msgpack(helloMessage); + conn->send(message.data(), sizeof(message[0]) * message.size()); + } } void WebSocketServer::onClose(websocketpp::connection_hdl hdl) diff --git a/src/WebSocketServer.h b/src/WebSocketServer.h index 90110518..bcc38c5a 100644 --- a/src/WebSocketServer.h +++ b/src/WebSocketServer.h @@ -87,6 +87,7 @@ class WebSocketServer std::mutex _sessionMutex; std::map> _sessions; uint16_t _serverPort; + bool _authenticationRequired; std::string _authenticationSecret; std::string _authenticationSalt; }; diff --git a/src/WebSocketSession.cpp b/src/WebSocketSession.cpp index 57aff2a5..6f2dc334 100644 --- a/src/WebSocketSession.cpp +++ b/src/WebSocketSession.cpp @@ -60,7 +60,7 @@ void WebSocketSession::IncrementOutgoingMessages() _outgoingMessages++; } -uint8_t WebSocketSession::GetEncoding() +uint8_t WebSocketSession::Encoding() { return _encoding.load(); } diff --git a/src/WebSocketSession.h b/src/WebSocketSession.h index f3f083d1..53933121 100644 --- a/src/WebSocketSession.h +++ b/src/WebSocketSession.h @@ -21,7 +21,7 @@ class WebSocketSession uint64_t OutgoingMessages(); void IncrementOutgoingMessages(); - uint8_t GetEncoding(); + uint8_t Encoding(); void SetEncoding(uint8_t encoding); std::string Challenge();