Merge pull request #98 from haganbmj/request-logging

General: Toggle to log incoming requests
This commit is contained in:
Stéphane L 2017-07-04 20:20:33 +02:00 committed by GitHub
commit 35cb506d6e
8 changed files with 60 additions and 11 deletions

View File

@ -27,6 +27,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#define SECTION_NAME "WebsocketAPI"
#define PARAM_ENABLE "ServerEnabled"
#define PARAM_PORT "ServerPort"
#define PARAM_DEBUG "DebugEnabled"
#define PARAM_AUTHREQUIRED "AuthRequired"
#define PARAM_SECRET "AuthSecret"
#define PARAM_SALT "AuthSalt"
@ -38,6 +39,8 @@ Config::Config()
// Default settings
ServerEnabled = true;
ServerPort = 4444;
DebugEnabled = false;
AuthRequired = false;
Secret = "";
@ -52,6 +55,9 @@ Config::Config()
SECTION_NAME, PARAM_ENABLE, ServerEnabled);
config_set_default_uint(obs_config,
SECTION_NAME, PARAM_PORT, ServerPort);
config_set_default_bool(obs_config,
SECTION_NAME, PARAM_DEBUG, DebugEnabled);
config_set_default_bool(obs_config,
SECTION_NAME, PARAM_AUTHREQUIRED, AuthRequired);
@ -81,6 +87,8 @@ void Config::Load()
ServerEnabled = config_get_bool(obs_config, SECTION_NAME, PARAM_ENABLE);
ServerPort = config_get_uint(obs_config, SECTION_NAME, PARAM_PORT);
DebugEnabled = config_get_bool(obs_config, SECTION_NAME, PARAM_DEBUG);
AuthRequired = config_get_bool(obs_config, SECTION_NAME, PARAM_AUTHREQUIRED);
Secret = config_get_string(obs_config, SECTION_NAME, PARAM_SECRET);
@ -93,6 +101,8 @@ void Config::Save()
config_set_bool(obs_config, SECTION_NAME, PARAM_ENABLE, ServerEnabled);
config_set_uint(obs_config, SECTION_NAME, PARAM_PORT, ServerPort);
config_set_bool(obs_config, SECTION_NAME, PARAM_DEBUG, DebugEnabled);
config_set_bool(obs_config, SECTION_NAME, PARAM_AUTHREQUIRED, AuthRequired);
config_set_string(obs_config, SECTION_NAME, PARAM_SECRET, Secret);

View File

@ -38,6 +38,8 @@ class Config
bool ServerEnabled;
uint64_t ServerPort;
bool DebugEnabled;
bool AuthRequired;
const char *Secret;

View File

@ -20,6 +20,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include <util/platform.h>
#include <QTimer>
#include <QPushButton>
#include "Config.h"
#include "Utils.h"
#include "WSEvents.h"
#include "obs-websocket.h"
@ -213,9 +214,15 @@ void WSEvents::broadcastUpdate(const char* updateType, obs_data_t* additionalFie
if (additionalFields != NULL)
obs_data_apply(update, additionalFields);
_srv->broadcast(obs_data_get_json(update));
const char *json = obs_data_get_json(update);
obs_data_release(update);
if (Config::Current()->DebugEnabled)
{
blog(LOG_DEBUG, "Update << '%s'", json);
}
_srv->broadcast(json);
}
void WSEvents::connectTransitionSignals(obs_source_t* transition)

View File

@ -114,6 +114,11 @@ void WSRequestHandler::processIncomingMessage(QString textMessage)
SendErrorResponse("invalid JSON payload");
return;
}
if (Config::Current()->DebugEnabled)
{
blog(LOG_DEBUG, "Request >> '%s'", msg);
}
if (!hasField("request-type") ||
!hasField("message-id"))
@ -156,9 +161,7 @@ void WSRequestHandler::SendOKResponse(obs_data_t* additionalFields)
if (additionalFields)
obs_data_apply(response, additionalFields);
_client->sendTextMessage(obs_data_get_json(response));
obs_data_release(response);
SendResponse(response);
}
void WSRequestHandler::SendErrorResponse(const char* errorMessage)
@ -168,9 +171,20 @@ void WSRequestHandler::SendErrorResponse(const char* errorMessage)
obs_data_set_string(response, "error", errorMessage);
obs_data_set_string(response, "message-id", _messageId);
_client->sendTextMessage(obs_data_get_json(response));
SendResponse(response);
}
void WSRequestHandler::SendResponse(obs_data_t* response)
{
const char *json = obs_data_get_json(response);
obs_data_release(response);
if (Config::Current()->DebugEnabled)
{
blog(LOG_DEBUG, "Response << '%s'", json);
}
_client->sendTextMessage(json);
}
bool WSRequestHandler::hasField(const char* name)

View File

@ -45,6 +45,7 @@ class WSRequestHandler : public QObject
void SendOKResponse(obs_data_t* additionalFields = NULL);
void SendErrorResponse(const char* errorMessage);
void SendResponse(obs_data_t* response);
static void HandleGetVersion(WSRequestHandler* req);
static void HandleGetAuthRequired(WSRequestHandler* req);

View File

@ -4,6 +4,7 @@ OBSWebsocket.Settings.ServerEnable="Enable Websocket server"
OBSWebsocket.Settings.ServerPort="Server Port"
OBSWebsocket.Settings.AuthRequired="Enable authentication"
OBSWebsocket.Settings.Password="Password"
OBSWebsocket.Settings.DebugEnable="Enable debug logging"
OBSWebsocket.ConnectNotify.Connected="New WebSocket connection"
OBSWebsocket.ConnectNotify.Disconnected="WebSocket client disconnected"
OBSWebsocket.ConnectNotify.ClientIP="Client Address:"

View File

@ -47,6 +47,8 @@ void SettingsDialog::showEvent(QShowEvent* event)
ui->serverEnabled->setChecked(conf->ServerEnabled);
ui->serverPort->setValue(conf->ServerPort);
ui->debugEnabled->setChecked(conf->DebugEnabled);
ui->authRequired->setChecked(conf->AuthRequired);
ui->password->setText(CHANGE_ME);
@ -74,6 +76,8 @@ void SettingsDialog::FormAccepted()
conf->ServerEnabled = ui->serverEnabled->isChecked();
conf->ServerPort = ui->serverPort->value();
conf->DebugEnabled = ui->debugEnabled->isChecked();
if (ui->authRequired->isChecked())
{

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>407</width>
<height>155</height>
<height>175</height>
</rect>
</property>
<property name="sizePolicy">
@ -79,6 +79,16 @@
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QCheckBox" name="debugEnabled">
<property name="text">
<string>OBSWebsocket.Settings.DebugEnable</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
@ -103,11 +113,11 @@
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
<y>274</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
<y>294</y>
</hint>
</hints>
</connection>
@ -119,11 +129,11 @@
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
<y>280</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
<y>294</y>
</hint>
</hints>
</connection>