From c73f6608b9c317e97d579971bc809a3862532a4f Mon Sep 17 00:00:00 2001 From: tt2468 Date: Wed, 28 Apr 2021 15:59:29 -0700 Subject: [PATCH] Config: Add command line parameters to override port and password --- README.md | 2 ++ src/Config.cpp | 61 +++++++++++++++++++++++++++++------- src/Config.h | 3 ++ src/WebSocketServer.cpp | 4 +++ src/forms/SettingsDialog.cpp | 9 ++++++ src/utils/Platform.cpp | 17 ++++++++++ src/utils/Utils.h | 1 + 7 files changed, 85 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 9f12e6a9..221ccecc 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ Here is a list of available web clients: (compatible with tablets and other touc It is **highly recommended** to protect obs-websocket with a password against unauthorized control. To do this, open the "Websocket server settings" dialog under OBS' "Tools" menu. In the settings dialogs, you can enable or disable authentication and set a password for it. +(Psst. You can use `--websocket_port` and `--websocket_password` on the command line to override the configured values.) + ### Possible use cases - Remote control OBS from a phone or tablet on the same local network diff --git a/src/Config.cpp b/src/Config.cpp index 30c6e4ff..986421ee 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1,7 +1,7 @@ -#include #include #include "Config.h" +#include "utils/Utils.h" #include "plugin-macros.generated.h" @@ -14,7 +14,19 @@ #define PARAM_AUTHREQUIRED "AuthRequred" #define PARAM_PASSWORD "ServerPassword" +#define CMDLINE_WEBSOCKET_PORT "websocket_port" +#define CMDLINE_WEBSOCKET_PASSWORD "websocket_password" + +std::vector GetCmdlineArgs() +{ + struct obs_cmdline_args args = obs_get_cmdline_args(); + std::vector ret(args.argv + 1, args.argv + args.argc); + return ret; +} + Config::Config() : + PortOverridden(false), + PasswordOverridden(false), ServerEnabled(true), ServerPort(4444), DebugEnabled(false), @@ -22,8 +34,6 @@ Config::Config() : AuthRequired(true), ServerPassword("") { - qsrand(QTime::currentTime().msec()); - SetDefaultsToGlobalStore(); } @@ -31,32 +41,59 @@ void Config::Load() { config_t* obsConfig = GetConfigStore(); if (!obsConfig) { - blog(LOG_ERROR, "Unable to fetch OBS config!"); + blog(LOG_ERROR, "[Config::Load] Unable to fetch OBS config!"); return; } ServerEnabled = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ENABLED); - ServerPort = config_get_uint(obsConfig, CONFIG_SECTION_NAME, PARAM_PORT); DebugEnabled = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_DEBUG); AlertsEnabled = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ALERTS); - AuthRequired = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_AUTHREQUIRED); - ServerPassword = config_get_string(obsConfig, CONFIG_SECTION_NAME, PARAM_PASSWORD); + + QString portArgument = Utils::Platform::GetCommandLineArgument(CMDLINE_WEBSOCKET_PORT); + if (portArgument != "") { + bool ok; + uint16_t serverPort = portArgument.toUShort(&ok); + if (ok) { + blog(LOG_INFO, "[Config::Load] Overriding websocket port with: %d", serverPort); + PortOverridden = true; + ServerPort = serverPort; + } else { + ServerPort = config_get_uint(obsConfig, CONFIG_SECTION_NAME, PARAM_PORT); + } + } else { + ServerPort = config_get_uint(obsConfig, CONFIG_SECTION_NAME, PARAM_PORT); + } + + QString passwordArgument = Utils::Platform::GetCommandLineArgument(CMDLINE_WEBSOCKET_PASSWORD); + if (passwordArgument != "") { + blog(LOG_INFO, "[Config::Load] Overriding websocket password"); + PasswordOverridden = true; + AuthRequired = true; + ServerPassword = passwordArgument; + } else { + AuthRequired = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_AUTHREQUIRED); + ServerPassword = config_get_string(obsConfig, CONFIG_SECTION_NAME, PARAM_PASSWORD); + } } void Config::Save() { config_t* obsConfig = GetConfigStore(); if (!obsConfig) { - blog(LOG_ERROR, "Unable to fetch OBS config!"); + blog(LOG_ERROR, "[Config::Save] Unable to fetch OBS config!"); return; } config_set_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ENABLED, ServerEnabled); - config_set_uint(obsConfig, CONFIG_SECTION_NAME, PARAM_PORT, ServerPort); + if (!PortOverridden) { + config_set_uint(obsConfig, CONFIG_SECTION_NAME, PARAM_PORT, ServerPort); + } config_set_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_DEBUG, DebugEnabled); config_set_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ALERTS, AlertsEnabled); - config_set_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_AUTHREQUIRED, AuthRequired); - config_set_string(obsConfig, CONFIG_SECTION_NAME, PARAM_PASSWORD, QT_TO_UTF8(ServerPassword)); + if (!PasswordOverridden) { + config_set_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_AUTHREQUIRED, AuthRequired); + config_set_string(obsConfig, CONFIG_SECTION_NAME, PARAM_PASSWORD, QT_TO_UTF8(ServerPassword)); + } config_save(obsConfig); } @@ -65,7 +102,7 @@ void Config::SetDefaultsToGlobalStore() { config_t* obsConfig = GetConfigStore(); if (!obsConfig) { - blog(LOG_ERROR, "Unable to fetch OBS config!"); + blog(LOG_ERROR, "[Config::SetDefaultsToGlobalStore] Unable to fetch OBS config!"); return; } diff --git a/src/Config.h b/src/Config.h index 89e9acd4..2f197b64 100644 --- a/src/Config.h +++ b/src/Config.h @@ -11,6 +11,9 @@ class Config { void SetDefaultsToGlobalStore(); config_t* GetConfigStore(); + bool PortOverridden; + bool PasswordOverridden; + bool ServerEnabled; uint16_t ServerPort; bool DebugEnabled; diff --git a/src/WebSocketServer.cpp b/src/WebSocketServer.cpp index 393ba84b..0b155100 100644 --- a/src/WebSocketServer.cpp +++ b/src/WebSocketServer.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "WebSocketServer.h" #include "obs-websocket.h" @@ -14,6 +15,9 @@ WebSocketServer::WebSocketServer() : _sessions() { + // Randomize the random number generator + qsrand(QTime::currentTime().msec()); + _server.get_alog().clear_channels(websocketpp::log::alevel::all); _server.get_elog().clear_channels(websocketpp::log::elevel::all); _server.init_asio(); diff --git a/src/forms/SettingsDialog.cpp b/src/forms/SettingsDialog.cpp index eff4fea3..7e60be34 100644 --- a/src/forms/SettingsDialog.cpp +++ b/src/forms/SettingsDialog.cpp @@ -52,6 +52,15 @@ void SettingsDialog::showEvent(QShowEvent *event) ui->serverPasswordLineEdit->setEnabled(conf->AuthRequired); ui->serverPortSpinBox->setValue(conf->ServerPort); + if (conf->PortOverridden) { + ui->serverPortSpinBox->setEnabled(false); + } + + if (conf->PasswordOverridden) { + ui->enableAuthenticationCheckBox->setEnabled(false); + ui->serverPasswordLineEdit->setEnabled(false); + } + FillSessionTable(); sessionTableTimer->start(1000); diff --git a/src/utils/Platform.cpp b/src/utils/Platform.cpp index 9e9dcabe..13860ba2 100644 --- a/src/utils/Platform.cpp +++ b/src/utils/Platform.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include @@ -5,6 +7,8 @@ #include "../plugin-macros.generated.h" +#include + std::string Utils::Platform::GetLocalAddress() { std::vector validAddresses; @@ -37,4 +41,17 @@ std::string Utils::Platform::GetLocalAddress() return validAddresses[0].toStdString(); return "0.0.0.0"; +} + +QString Utils::Platform::GetCommandLineArgument(QString arg) +{ + QCommandLineParser parser; + QCommandLineOption cmdlineOption(arg, arg, arg, ""); + parser.addOption(cmdlineOption); + parser.parse(QCoreApplication::arguments()); + + if (!parser.isSet(cmdlineOption)) + return ""; + + return parser.value(cmdlineOption); } \ No newline at end of file diff --git a/src/utils/Utils.h b/src/utils/Utils.h index d7afc14d..0dadc44e 100644 --- a/src/utils/Utils.h +++ b/src/utils/Utils.h @@ -22,5 +22,6 @@ namespace Utils { namespace Platform { std::string GetLocalAddress(); + QString GetCommandLineArgument(QString arg); } }