server: dedicated ConnectionProperties object

This commit is contained in:
Stéphane Lepin
2019-05-02 13:06:10 +02:00
parent 56fc6ae47c
commit bb9cf83744
9 changed files with 73 additions and 11 deletions

View File

@ -30,6 +30,7 @@ find_package(Boost REQUIRED)
set(obs-websocket_SOURCES set(obs-websocket_SOURCES
src/obs-websocket.cpp src/obs-websocket.cpp
src/WSServer.cpp src/WSServer.cpp
src/ConnectionProperties.cpp
src/WSRequestHandler.cpp src/WSRequestHandler.cpp
src/WSRequestHandler_General.cpp src/WSRequestHandler_General.cpp
src/WSRequestHandler_Profiles.cpp src/WSRequestHandler_Profiles.cpp
@ -50,6 +51,7 @@ set(obs-websocket_SOURCES
set(obs-websocket_HEADERS set(obs-websocket_HEADERS
src/obs-websocket.h src/obs-websocket.h
src/WSServer.h src/WSServer.h
src/ConnectionProperties.h
src/WSRequestHandler.h src/WSRequestHandler.h
src/WSEvents.h src/WSEvents.h
src/Config.h src/Config.h

View File

@ -0,0 +1,29 @@
/*
obs-websocket
Copyright (C) 2016-2019 Stéphane Lepin <stephane.lepin@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>
*/
#include "ConnectionProperties.h"
bool ConnectionProperties::isAuthenticated()
{
return _authenticated;
}
void ConnectionProperties::setAuthenticated(bool authenticated)
{
_authenticated = authenticated;
}

View File

@ -0,0 +1,28 @@
/*
obs-websocket
Copyright (C) 2016-2019 Stéphane Lepin <stephane.lepin@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>
*/
#pragma once
class ConnectionProperties
{
public:
bool isAuthenticated();
void setAuthenticated(bool authenticated);
private:
bool _authenticated;
};

View File

@ -132,7 +132,7 @@ QSet<QString> WSRequestHandler::authNotRequired {
"Authenticate" "Authenticate"
}; };
WSRequestHandler::WSRequestHandler(QVariantHash& connProperties) : WSRequestHandler::WSRequestHandler(ConnectionProperties& connProperties) :
_messageId(0), _messageId(0),
_requestType(""), _requestType(""),
data(nullptr), data(nullptr),
@ -174,7 +174,7 @@ HandlerResponse WSRequestHandler::processRequest(std::string& textMessage){
if (Config::Current()->AuthRequired if (Config::Current()->AuthRequired
&& (!authNotRequired.contains(_requestType)) && (!authNotRequired.contains(_requestType))
&& (_connProperties.value(PROP_AUTHENTICATED).toBool() == false)) && (!_connProperties.isAuthenticated()))
{ {
return SendErrorResponse("Not Authenticated"); return SendErrorResponse("Not Authenticated");
} }

View File

@ -28,6 +28,8 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include <obs.hpp> #include <obs.hpp>
#include <obs-frontend-api.h> #include <obs-frontend-api.h>
#include "ConnectionProperties.h"
#include "obs-websocket.h" #include "obs-websocket.h"
typedef obs_data_t* HandlerResponse; typedef obs_data_t* HandlerResponse;
@ -36,7 +38,7 @@ class WSRequestHandler : public QObject {
Q_OBJECT Q_OBJECT
public: public:
explicit WSRequestHandler(QVariantHash& connProperties); explicit WSRequestHandler(ConnectionProperties& connProperties);
~WSRequestHandler(); ~WSRequestHandler();
std::string processIncomingMessage(std::string& textMessage); std::string processIncomingMessage(std::string& textMessage);
bool hasField(QString name); bool hasField(QString name);
@ -44,7 +46,7 @@ class WSRequestHandler : public QObject {
private: private:
const char* _messageId; const char* _messageId;
const char* _requestType; const char* _requestType;
QVariantHash& _connProperties; ConnectionProperties& _connProperties;
OBSDataAutoRelease data; OBSDataAutoRelease data;
HandlerResponse processRequest(std::string& textMessage); HandlerResponse processRequest(std::string& textMessage);

View File

@ -82,7 +82,7 @@ HandlerResponse WSRequestHandler::HandleAuthenticate(WSRequestHandler* req) {
return req->SendErrorResponse("missing request parameters"); return req->SendErrorResponse("missing request parameters");
} }
if (req->_connProperties.value(PROP_AUTHENTICATED).toBool() == true) { if (req->_connProperties.isAuthenticated()) {
return req->SendErrorResponse("already authenticated"); return req->SendErrorResponse("already authenticated");
} }
@ -95,7 +95,7 @@ HandlerResponse WSRequestHandler::HandleAuthenticate(WSRequestHandler* req) {
return req->SendErrorResponse("Authentication Failed."); return req->SendErrorResponse("Authentication Failed.");
} }
req->_connProperties.insert(PROP_AUTHENTICATED, true); req->_connProperties.setAuthenticated(true);
return req->SendOKResponse(); return req->SendOKResponse();
} }

View File

@ -142,7 +142,7 @@ void WSServer::broadcast(std::string message)
QMutexLocker locker(&_clMutex); QMutexLocker locker(&_clMutex);
for (connection_hdl hdl : _connections) { for (connection_hdl hdl : _connections) {
if (Config::Current()->AuthRequired) { if (Config::Current()->AuthRequired) {
bool authenticated = _connectionProperties[hdl].value(PROP_AUTHENTICATED).toBool(); bool authenticated = _connectionProperties[hdl].isAuthenticated();
if (!authenticated) { if (!authenticated) {
continue; continue;
} }
@ -155,7 +155,7 @@ void WSServer::onOpen(connection_hdl hdl)
{ {
QMutexLocker locker(&_clMutex); QMutexLocker locker(&_clMutex);
_connections.insert(hdl); _connections.insert(hdl);
_connectionProperties[hdl] = QVariantHash(); _connectionProperties[hdl] = ConnectionProperties();
locker.unlock(); locker.unlock();
QString clientIp = getRemoteEndpoint(hdl); QString clientIp = getRemoteEndpoint(hdl);
@ -174,7 +174,7 @@ void WSServer::onMessage(connection_hdl hdl, server::message_ptr message)
std::string payload = message->get_payload(); std::string payload = message->get_payload();
QMutexLocker locker(&_clMutex); QMutexLocker locker(&_clMutex);
QVariantHash& connProperties = _connectionProperties[hdl]; ConnectionProperties& connProperties = _connectionProperties[hdl];
locker.unlock(); locker.unlock();
WSRequestHandler handler(connProperties); WSRequestHandler handler(connProperties);

View File

@ -30,6 +30,8 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include <websocketpp/config/asio_no_tls.hpp> #include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp> #include <websocketpp/server.hpp>
#include "ConnectionProperties.h"
#include "WSRequestHandler.h" #include "WSRequestHandler.h"
QT_FORWARD_DECLARE_CLASS(QWebSocketServer) QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
@ -73,7 +75,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; std::map<connection_hdl, ConnectionProperties, std::owner_less<connection_hdl>> _connectionProperties;
QMutex _clMutex; QMutex _clMutex;
QThreadPool _threadPool; QThreadPool _threadPool;
}; };

View File

@ -37,7 +37,6 @@ using OBSDataArrayAutoRelease =
using OBSOutputAutoRelease = using OBSOutputAutoRelease =
OBSRef<obs_output_t*, ___output_dummy_addref, obs_output_release>; OBSRef<obs_output_t*, ___output_dummy_addref, obs_output_release>;
#define PROP_AUTHENTICATED "wsclient_authenticated"
#define OBS_WEBSOCKET_VERSION "4.6.0" #define OBS_WEBSOCKET_VERSION "4.6.0"
#define blog(level, msg, ...) blog(level, "[obs-websocket] " msg, ##__VA_ARGS__) #define blog(level, msg, ...) blog(level, "[obs-websocket] " msg, ##__VA_ARGS__)