server: per-connection properties

This commit is contained in:
Stéphane L 2019-01-01 01:07:50 +01:00
parent 62e4c42aa6
commit 5b0410a207
5 changed files with 35 additions and 19 deletions

View File

@ -128,10 +128,11 @@ QSet<QString> WSRequestHandler::authNotRequired {
"Authenticate"
};
WSRequestHandler::WSRequestHandler() :
WSRequestHandler::WSRequestHandler(QVariantHash* connProperties) :
_messageId(0),
_requestType(""),
data(nullptr)
data(nullptr),
_connProperties(connProperties)
{
}
@ -160,13 +161,13 @@ std::string WSRequestHandler::processIncomingMessage(std::string& textMessage) {
_requestType = obs_data_get_string(data, "request-type");
_messageId = obs_data_get_string(data, "message-id");
// if (Config::Current()->AuthRequired
// && (_client->property(PROP_AUTHENTICATED).toBool() == false)
// && (authNotRequired.find(_requestType) == authNotRequired.end()))
// {
// SendErrorResponse("Not Authenticated");
// return;
// }
if (Config::Current()->AuthRequired
&& (!authNotRequired.contains(_requestType))
&& (_connProperties->value(PROP_AUTHENTICATED).toBool() == false))
{
SendErrorResponse("Not Authenticated");
return _response;
}
void (*handlerFunc)(WSRequestHandler*) = (messageMap[_requestType]);

View File

@ -22,6 +22,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include <QHash>
#include <QSet>
#include <QVariantHash>
#include <obs.hpp>
#include <obs-frontend-api.h>
@ -32,7 +33,7 @@ class WSRequestHandler : public QObject {
Q_OBJECT
public:
explicit WSRequestHandler();
explicit WSRequestHandler(QVariantHash* connProperties);
~WSRequestHandler();
std::string processIncomingMessage(std::string& textMessage);
bool hasField(QString name);
@ -41,6 +42,7 @@ class WSRequestHandler : public QObject {
const char* _messageId;
const char* _requestType;
std::string _response;
QVariantHash* _connProperties;
OBSDataAutoRelease data;
void SendOKResponse(obs_data_t* additionalFields = NULL);

View File

@ -85,20 +85,24 @@ void WSRequestHandler::HandleAuthenticate(WSRequestHandler* req) {
return;
}
if (req->_connProperties->value(PROP_AUTHENTICATED).toBool() == true) {
req->SendErrorResponse("already authenticated");
return;
}
QString auth = obs_data_get_string(req->data, "auth");
if (auth.isEmpty()) {
req->SendErrorResponse("auth not specified!");
return;
}
// if ((req->_client->property(PROP_AUTHENTICATED).toBool() == false)
// && Config::Current()->CheckAuth(auth))
// {
// req->_client->setProperty(PROP_AUTHENTICATED, true);
// req->SendOKResponse();
// } else {
// req->SendErrorResponse("Authentication Failed.");
// }
if (Config::Current()->CheckAuth(auth) == false) {
req->SendErrorResponse("Authentication Failed.");
return;
}
req->_connProperties->insert(PROP_AUTHENTICATED, true);
req->SendOKResponse();
}
/**

View File

@ -118,11 +118,15 @@ void WSServer::onMessage(connection_hdl hdl, server::message_ptr message)
return;
}
QVariantHash connProperties = _connectionProperties[hdl];
std::string payload = message->get_payload();
WSRequestHandler handler;
WSRequestHandler handler(&connProperties);
std::string response = handler.processIncomingMessage(payload);
_connectionProperties[hdl] = connProperties;
_server.send(hdl, response, websocketpp::frame::opcode::text);
}
@ -130,6 +134,7 @@ void WSServer::onClose(connection_hdl hdl)
{
QMutexLocker locker(&_clMutex);
_connections.erase(hdl);
_connectionProperties.erase(hdl);
locker.unlock();
QString clientIp = getRemoteEndpoint(hdl);

View File

@ -21,8 +21,11 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include <QObject>
#include <QMutex>
#include <QVariantHash>
#include <map>
#include <set>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
@ -59,6 +62,7 @@ private:
server _server;
quint16 _serverPort;
std::set<connection_hdl, std::owner_less<connection_hdl>> _connections;
std::map<connection_hdl, QVariantHash, std::owner_less<connection_hdl>> _connectionProperties;
QMutex _clMutex;
};