This commit is contained in:
Palakis 2017-02-15 11:35:35 +01:00
parent bb232f1b3e
commit f2e6e137a6
5 changed files with 43 additions and 49 deletions

View File

@ -22,7 +22,6 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include "Utils.h"
WSRequestHandler::WSRequestHandler(QWebSocket *client) :
_authenticated(false),
_messageId(0),
_requestType(""),
_requestData(nullptr)
@ -56,15 +55,9 @@ WSRequestHandler::WSRequestHandler(QWebSocket *client) :
authNotRequired.insert("GetVersion");
authNotRequired.insert("GetAuthRequired");
authNotRequired.insert("Authenticate");
QByteArray client_ip = _client->peerAddress().toString().toUtf8();
blog(LOG_INFO, "[obs-websockets] new client connection from %s:%d", client_ip.constData(), _client->peerPort());
connect(_client, &QWebSocket::textMessageReceived, this, &WSRequestHandler::processTextMessage);
connect(_client, &QWebSocket::disconnected, this, &WSRequestHandler::socketDisconnected);
}
void WSRequestHandler::processTextMessage(QString textMessage) {
void WSRequestHandler::processIncomingMessage(QString textMessage) {
QByteArray msgData = textMessage.toUtf8();
const char *msg = msgData;
@ -83,7 +76,7 @@ void WSRequestHandler::processTextMessage(QString textMessage) {
_messageId = obs_data_get_string(_requestData, "message-id");
if (Config::Current()->AuthRequired
&& !_authenticated
&& _client->property(PROP_AUTHENTICATED) == false
&& authNotRequired.find(_requestType) == authNotRequired.end())
{
SendErrorResponse("Not Authenticated");
@ -102,23 +95,6 @@ void WSRequestHandler::processTextMessage(QString textMessage) {
obs_data_release(_requestData);
}
void WSRequestHandler::socketDisconnected() {
QByteArray client_ip = _client->peerAddress().toString().toUtf8();
blog(LOG_INFO, "[obs-websockets] client %s:%d disconnected", client_ip.constData(), _client->peerPort());
_authenticated = false;
_client->deleteLater();
emit disconnected();
}
void WSRequestHandler::sendTextMessage(QString textMessage) {
_client->sendTextMessage(textMessage);
}
bool WSRequestHandler::isAuthenticated() {
return _authenticated;
}
WSRequestHandler::~WSRequestHandler() {
if (_requestData != NULL) {
obs_data_release(_requestData);
@ -184,8 +160,8 @@ void WSRequestHandler::HandleAuthenticate(WSRequestHandler *owner) {
return;
}
if (!(owner->_authenticated) && Config::Current()->CheckAuth(auth)) {
owner->_authenticated = true;
if (owner->_client->property(PROP_AUTHENTICATED) == false && Config::Current()->CheckAuth(auth)) {
owner->_client->setProperty(PROP_AUTHENTICATED, true);
owner->SendOKResponse();
}
else {

View File

@ -20,6 +20,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#define WSREQUESTHANDLER_H
#include <QtWebSockets/QWebSocket>
#include <QtWebSockets/QWebSocketServer>
#include <obs-frontend-api.h>
class WSRequestHandler : public QObject
@ -29,19 +30,10 @@ class WSRequestHandler : public QObject
public:
explicit WSRequestHandler(QWebSocket *client);
~WSRequestHandler();
void sendTextMessage(QString textMessage);
bool isAuthenticated();
private Q_SLOTS:
void processTextMessage(QString textMessage);
void socketDisconnected();
Q_SIGNALS:
void disconnected();
void processIncomingMessage(QString textMessage);
private:
QWebSocket *_client;
bool _authenticated;
const char *_messageId;
const char *_requestType;
obs_data_t *_requestData;

View File

@ -25,6 +25,8 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include <QtCore/QThread>
#include <obs-frontend-api.h>
#include "obs-websocket.h"
QT_USE_NAMESPACE
WSServer::WSServer(quint16 port, QObject *parent) :
@ -56,15 +58,17 @@ WSServer::~WSServer()
_clMutex.lock();
qDeleteAll(_clients.begin(), _clients.end());
_clMutex.unlock();
delete _serverThread;
}
void WSServer::broadcast(QString message)
{
_clMutex.lock();
Q_FOREACH(WSRequestHandler *pClient, _clients) {
Q_FOREACH(QWebSocket *pClient, _clients) {
if (Config::Current()->AuthRequired == true
&& pClient->isAuthenticated() == false) {
&& pClient->property(PROP_AUTHENTICATED) == false) {
// Skip this client if unauthenticated
continue;
}
@ -80,24 +84,43 @@ void WSServer::onNewConnection()
QWebSocket *pSocket = _wsServer->nextPendingConnection();
if (pSocket) {
WSRequestHandler *pHandler = new WSRequestHandler(pSocket);
connect(pHandler, &WSRequestHandler::disconnected, this, &WSServer::socketDisconnected);
connect(pSocket, &QWebSocket::textMessageReceived, this, &WSServer::textMessageReceived);
connect(pSocket, &QWebSocket::disconnected, this, &WSServer::socketDisconnected);
pSocket->setProperty(PROP_AUTHENTICATED, false);
_clMutex.lock();
_clients << pHandler;
_clients << pSocket;
_clMutex.unlock();
QByteArray client_ip = pSocket->peerAddress().toString().toUtf8();
blog(LOG_INFO, "[obs-websockets] new client connection from %s:%d", client_ip.constData(), pSocket->peerPort());
}
}
void WSServer::textMessageReceived(QString message)
{
QWebSocket *pSocket = qobject_cast<QWebSocket *>(sender());
if (pSocket) {
WSRequestHandler handler(pSocket);
handler.processIncomingMessage(message);
}
}
void WSServer::socketDisconnected()
{
WSRequestHandler *pHandler = qobject_cast<WSRequestHandler *>(sender());
QWebSocket *pSocket = qobject_cast<QWebSocket *>(sender());
if (pSocket) {
pSocket->setProperty(PROP_AUTHENTICATED, false);
if (pHandler) {
_clMutex.lock();
_clients.removeAll(pHandler);
_clients.removeAll(pSocket);
_clMutex.unlock();
pHandler->deleteLater();
pSocket->deleteLater();
QByteArray client_ip = pSocket->peerAddress().toString().toUtf8();
blog(LOG_INFO, "[obs-websockets] client %s:%d disconnected", client_ip.constData(), pSocket->peerPort());
}
}

View File

@ -23,6 +23,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include <QtCore/QList>
#include <QtCore/QMutex>
#include <QtCore/QByteArray>
#include <QtCore/QThread>
#include "WSRequestHandler.h"
QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
@ -39,11 +40,12 @@ class WSServer : public QObject
private Q_SLOTS:
void onNewConnection();
void textMessageReceived(QString message);
void socketDisconnected();
private:
QWebSocketServer *_wsServer;
QList<WSRequestHandler *> _clients;
QList<QWebSocket *> _clients;
QMutex _clMutex;
QThread *_serverThread;
};

View File

@ -19,6 +19,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#ifndef OBSWEBSOCKET_H
#define OBSWEBSOCKET_H
#define PROP_AUTHENTICATED "authenticated"
#define OBS_WEBSOCKET_VERSION "0.3.2"
#endif // OBSWEBSOCKET_H