mirror of
https://github.com/Palakis/obs-websocket.git
synced 2024-08-30 18:12:16 +00:00
Fixed #21
This commit is contained in:
parent
bb232f1b3e
commit
f2e6e137a6
@ -22,7 +22,6 @@ with this program. If not, see <https://www.gnu.org/licenses/>
|
|||||||
#include "Utils.h"
|
#include "Utils.h"
|
||||||
|
|
||||||
WSRequestHandler::WSRequestHandler(QWebSocket *client) :
|
WSRequestHandler::WSRequestHandler(QWebSocket *client) :
|
||||||
_authenticated(false),
|
|
||||||
_messageId(0),
|
_messageId(0),
|
||||||
_requestType(""),
|
_requestType(""),
|
||||||
_requestData(nullptr)
|
_requestData(nullptr)
|
||||||
@ -56,15 +55,9 @@ WSRequestHandler::WSRequestHandler(QWebSocket *client) :
|
|||||||
authNotRequired.insert("GetVersion");
|
authNotRequired.insert("GetVersion");
|
||||||
authNotRequired.insert("GetAuthRequired");
|
authNotRequired.insert("GetAuthRequired");
|
||||||
authNotRequired.insert("Authenticate");
|
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();
|
QByteArray msgData = textMessage.toUtf8();
|
||||||
const char *msg = msgData;
|
const char *msg = msgData;
|
||||||
|
|
||||||
@ -83,7 +76,7 @@ void WSRequestHandler::processTextMessage(QString textMessage) {
|
|||||||
_messageId = obs_data_get_string(_requestData, "message-id");
|
_messageId = obs_data_get_string(_requestData, "message-id");
|
||||||
|
|
||||||
if (Config::Current()->AuthRequired
|
if (Config::Current()->AuthRequired
|
||||||
&& !_authenticated
|
&& _client->property(PROP_AUTHENTICATED) == false
|
||||||
&& authNotRequired.find(_requestType) == authNotRequired.end())
|
&& authNotRequired.find(_requestType) == authNotRequired.end())
|
||||||
{
|
{
|
||||||
SendErrorResponse("Not Authenticated");
|
SendErrorResponse("Not Authenticated");
|
||||||
@ -102,23 +95,6 @@ void WSRequestHandler::processTextMessage(QString textMessage) {
|
|||||||
obs_data_release(_requestData);
|
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() {
|
WSRequestHandler::~WSRequestHandler() {
|
||||||
if (_requestData != NULL) {
|
if (_requestData != NULL) {
|
||||||
obs_data_release(_requestData);
|
obs_data_release(_requestData);
|
||||||
@ -184,8 +160,8 @@ void WSRequestHandler::HandleAuthenticate(WSRequestHandler *owner) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(owner->_authenticated) && Config::Current()->CheckAuth(auth)) {
|
if (owner->_client->property(PROP_AUTHENTICATED) == false && Config::Current()->CheckAuth(auth)) {
|
||||||
owner->_authenticated = true;
|
owner->_client->setProperty(PROP_AUTHENTICATED, true);
|
||||||
owner->SendOKResponse();
|
owner->SendOKResponse();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -20,6 +20,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
|
|||||||
#define WSREQUESTHANDLER_H
|
#define WSREQUESTHANDLER_H
|
||||||
|
|
||||||
#include <QtWebSockets/QWebSocket>
|
#include <QtWebSockets/QWebSocket>
|
||||||
|
#include <QtWebSockets/QWebSocketServer>
|
||||||
#include <obs-frontend-api.h>
|
#include <obs-frontend-api.h>
|
||||||
|
|
||||||
class WSRequestHandler : public QObject
|
class WSRequestHandler : public QObject
|
||||||
@ -29,19 +30,10 @@ class WSRequestHandler : public QObject
|
|||||||
public:
|
public:
|
||||||
explicit WSRequestHandler(QWebSocket *client);
|
explicit WSRequestHandler(QWebSocket *client);
|
||||||
~WSRequestHandler();
|
~WSRequestHandler();
|
||||||
void sendTextMessage(QString textMessage);
|
void processIncomingMessage(QString textMessage);
|
||||||
bool isAuthenticated();
|
|
||||||
|
|
||||||
private Q_SLOTS:
|
|
||||||
void processTextMessage(QString textMessage);
|
|
||||||
void socketDisconnected();
|
|
||||||
|
|
||||||
Q_SIGNALS:
|
|
||||||
void disconnected();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QWebSocket *_client;
|
QWebSocket *_client;
|
||||||
bool _authenticated;
|
|
||||||
const char *_messageId;
|
const char *_messageId;
|
||||||
const char *_requestType;
|
const char *_requestType;
|
||||||
obs_data_t *_requestData;
|
obs_data_t *_requestData;
|
||||||
|
43
WSServer.cpp
43
WSServer.cpp
@ -25,6 +25,8 @@ with this program. If not, see <https://www.gnu.org/licenses/>
|
|||||||
#include <QtCore/QThread>
|
#include <QtCore/QThread>
|
||||||
#include <obs-frontend-api.h>
|
#include <obs-frontend-api.h>
|
||||||
|
|
||||||
|
#include "obs-websocket.h"
|
||||||
|
|
||||||
QT_USE_NAMESPACE
|
QT_USE_NAMESPACE
|
||||||
|
|
||||||
WSServer::WSServer(quint16 port, QObject *parent) :
|
WSServer::WSServer(quint16 port, QObject *parent) :
|
||||||
@ -56,15 +58,17 @@ WSServer::~WSServer()
|
|||||||
_clMutex.lock();
|
_clMutex.lock();
|
||||||
qDeleteAll(_clients.begin(), _clients.end());
|
qDeleteAll(_clients.begin(), _clients.end());
|
||||||
_clMutex.unlock();
|
_clMutex.unlock();
|
||||||
|
|
||||||
|
delete _serverThread;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WSServer::broadcast(QString message)
|
void WSServer::broadcast(QString message)
|
||||||
{
|
{
|
||||||
_clMutex.lock();
|
_clMutex.lock();
|
||||||
|
|
||||||
Q_FOREACH(WSRequestHandler *pClient, _clients) {
|
Q_FOREACH(QWebSocket *pClient, _clients) {
|
||||||
if (Config::Current()->AuthRequired == true
|
if (Config::Current()->AuthRequired == true
|
||||||
&& pClient->isAuthenticated() == false) {
|
&& pClient->property(PROP_AUTHENTICATED) == false) {
|
||||||
// Skip this client if unauthenticated
|
// Skip this client if unauthenticated
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -80,24 +84,43 @@ void WSServer::onNewConnection()
|
|||||||
QWebSocket *pSocket = _wsServer->nextPendingConnection();
|
QWebSocket *pSocket = _wsServer->nextPendingConnection();
|
||||||
|
|
||||||
if (pSocket) {
|
if (pSocket) {
|
||||||
WSRequestHandler *pHandler = new WSRequestHandler(pSocket);
|
connect(pSocket, &QWebSocket::textMessageReceived, this, &WSServer::textMessageReceived);
|
||||||
connect(pHandler, &WSRequestHandler::disconnected, this, &WSServer::socketDisconnected);
|
connect(pSocket, &QWebSocket::disconnected, this, &WSServer::socketDisconnected);
|
||||||
|
pSocket->setProperty(PROP_AUTHENTICATED, false);
|
||||||
|
|
||||||
_clMutex.lock();
|
_clMutex.lock();
|
||||||
_clients << pHandler;
|
_clients << pSocket;
|
||||||
_clMutex.unlock();
|
_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()
|
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();
|
_clMutex.lock();
|
||||||
_clients.removeAll(pHandler);
|
_clients.removeAll(pSocket);
|
||||||
_clMutex.unlock();
|
_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());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -23,6 +23,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
|
|||||||
#include <QtCore/QList>
|
#include <QtCore/QList>
|
||||||
#include <QtCore/QMutex>
|
#include <QtCore/QMutex>
|
||||||
#include <QtCore/QByteArray>
|
#include <QtCore/QByteArray>
|
||||||
|
#include <QtCore/QThread>
|
||||||
#include "WSRequestHandler.h"
|
#include "WSRequestHandler.h"
|
||||||
|
|
||||||
QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
|
QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
|
||||||
@ -39,11 +40,12 @@ class WSServer : public QObject
|
|||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void onNewConnection();
|
void onNewConnection();
|
||||||
|
void textMessageReceived(QString message);
|
||||||
void socketDisconnected();
|
void socketDisconnected();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QWebSocketServer *_wsServer;
|
QWebSocketServer *_wsServer;
|
||||||
QList<WSRequestHandler *> _clients;
|
QList<QWebSocket *> _clients;
|
||||||
QMutex _clMutex;
|
QMutex _clMutex;
|
||||||
QThread *_serverThread;
|
QThread *_serverThread;
|
||||||
};
|
};
|
||||||
|
@ -19,6 +19,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
|
|||||||
#ifndef OBSWEBSOCKET_H
|
#ifndef OBSWEBSOCKET_H
|
||||||
#define OBSWEBSOCKET_H
|
#define OBSWEBSOCKET_H
|
||||||
|
|
||||||
|
#define PROP_AUTHENTICATED "authenticated"
|
||||||
#define OBS_WEBSOCKET_VERSION "0.3.2"
|
#define OBS_WEBSOCKET_VERSION "0.3.2"
|
||||||
|
|
||||||
#endif // OBSWEBSOCKET_H
|
#endif // OBSWEBSOCKET_H
|
Loading…
x
Reference in New Issue
Block a user