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" "Authenticate"
}; };
WSRequestHandler::WSRequestHandler() : WSRequestHandler::WSRequestHandler(QVariantHash* connProperties) :
_messageId(0), _messageId(0),
_requestType(""), _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"); _requestType = obs_data_get_string(data, "request-type");
_messageId = obs_data_get_string(data, "message-id"); _messageId = obs_data_get_string(data, "message-id");
// if (Config::Current()->AuthRequired if (Config::Current()->AuthRequired
// && (_client->property(PROP_AUTHENTICATED).toBool() == false) && (!authNotRequired.contains(_requestType))
// && (authNotRequired.find(_requestType) == authNotRequired.end())) && (_connProperties->value(PROP_AUTHENTICATED).toBool() == false))
// { {
// SendErrorResponse("Not Authenticated"); SendErrorResponse("Not Authenticated");
// return; return _response;
// } }
void (*handlerFunc)(WSRequestHandler*) = (messageMap[_requestType]); 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 <QHash>
#include <QSet> #include <QSet>
#include <QVariantHash>
#include <obs.hpp> #include <obs.hpp>
#include <obs-frontend-api.h> #include <obs-frontend-api.h>
@ -32,7 +33,7 @@ class WSRequestHandler : public QObject {
Q_OBJECT Q_OBJECT
public: public:
explicit WSRequestHandler(); explicit WSRequestHandler(QVariantHash* connProperties);
~WSRequestHandler(); ~WSRequestHandler();
std::string processIncomingMessage(std::string& textMessage); std::string processIncomingMessage(std::string& textMessage);
bool hasField(QString name); bool hasField(QString name);
@ -41,6 +42,7 @@ class WSRequestHandler : public QObject {
const char* _messageId; const char* _messageId;
const char* _requestType; const char* _requestType;
std::string _response; std::string _response;
QVariantHash* _connProperties;
OBSDataAutoRelease data; OBSDataAutoRelease data;
void SendOKResponse(obs_data_t* additionalFields = NULL); void SendOKResponse(obs_data_t* additionalFields = NULL);

View File

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

View File

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

View File

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