WebSocketServer: Send hello in onOpen

This commit is contained in:
tt2468 2021-04-28 22:03:23 -07:00
parent b0a594e509
commit d1d5dd74ea
5 changed files with 41 additions and 5 deletions

View File

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

View File

@ -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<std::mutex> 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)

View File

@ -87,6 +87,7 @@ class WebSocketServer
std::mutex _sessionMutex;
std::map<websocketpp::connection_hdl, WebSocketSession, std::owner_less<websocketpp::connection_hdl>> _sessions;
uint16_t _serverPort;
bool _authenticationRequired;
std::string _authenticationSecret;
std::string _authenticationSalt;
};

View File

@ -60,7 +60,7 @@ void WebSocketSession::IncrementOutgoingMessages()
_outgoingMessages++;
}
uint8_t WebSocketSession::GetEncoding()
uint8_t WebSocketSession::Encoding()
{
return _encoding.load();
}

View File

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