WebSocketServer: Include password in changed GetConnectString()

New format is `obswebsocket|ip:port|password`
This commit is contained in:
tt2468
2021-04-29 08:59:54 -07:00
parent 4028d42931
commit ba710efe09
2 changed files with 12 additions and 4 deletions

View File

@ -79,6 +79,7 @@ void WebSocketServer::Start()
} }
_serverPort = conf->ServerPort; _serverPort = conf->ServerPort;
_serverPassword = conf->ServerPassword;
_debugEnabled = conf->DebugEnabled; _debugEnabled = conf->DebugEnabled;
_authenticationRequired = conf->AuthRequired; _authenticationRequired = conf->AuthRequired;
_authenticationSalt = Utils::Crypto::GenerateSalt(); _authenticationSalt = Utils::Crypto::GenerateSalt();
@ -187,10 +188,14 @@ std::vector<WebSocketServer::WebSocketSessionState> WebSocketServer::GetWebSocke
return webSocketSessions; return webSocketSessions;
} }
std::string WebSocketServer::GetConnectUrl() QString WebSocketServer::GetConnectString()
{ {
QString ret = QString("ws://%1:%2").arg(QString::fromStdString(Utils::Platform::GetLocalAddress())).arg(_serverPort); QString ret;
return ret.toStdString(); if (_authenticationRequired)
ret = QString("obswebsocket|%1:%2|%3").arg(QString::fromStdString(Utils::Platform::GetLocalAddress())).arg(_serverPort).arg(_serverPassword);
else
ret = QString("obswebsocket|%1:%2").arg(QString::fromStdString(Utils::Platform::GetLocalAddress())).arg(_serverPort);
return ret;
} }
void WebSocketServer::BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData) void WebSocketServer::BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData)
@ -207,6 +212,7 @@ void WebSocketServer::BroadcastEvent(uint64_t requiredIntent, std::string eventT
std::string messageJson; std::string messageJson;
std::string messageMsgPack; std::string messageMsgPack;
// Recurse connected sessions and send the event to suitable sessions.
std::unique_lock<std::mutex> lock(_sessionMutex); std::unique_lock<std::mutex> lock(_sessionMutex);
for (auto & it : _sessions) { for (auto & it : _sessions) {
if (!it.second.IsIdentified()) if (!it.second.IsIdentified())

View File

@ -2,6 +2,7 @@
#include <QObject> #include <QObject>
#include <QThreadPool> #include <QThreadPool>
#include <QString>
#include <mutex> #include <mutex>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
@ -71,7 +72,7 @@ class WebSocketServer : QObject
return &_threadPool; return &_threadPool;
} }
std::string GetConnectUrl(); QString GetConnectString();
public Q_SLOTS: public Q_SLOTS:
void BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData = nullptr); void BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData = nullptr);
@ -94,6 +95,7 @@ class WebSocketServer : QObject
std::mutex _sessionMutex; std::mutex _sessionMutex;
std::map<websocketpp::connection_hdl, WebSocketSession, std::owner_less<websocketpp::connection_hdl>> _sessions; std::map<websocketpp::connection_hdl, WebSocketSession, std::owner_less<websocketpp::connection_hdl>> _sessions;
uint16_t _serverPort; uint16_t _serverPort;
QString _serverPassword;
bool _debugEnabled; bool _debugEnabled;
bool _authenticationRequired; bool _authenticationRequired;
std::string _authenticationSecret; std::string _authenticationSecret;