Config: Add command line parameters to override port and password

This commit is contained in:
tt2468 2021-04-28 15:59:29 -07:00
parent 1dcf6460f1
commit c73f6608b9
7 changed files with 85 additions and 12 deletions

View File

@ -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

View File

@ -1,7 +1,7 @@
#include <QTime>
#include <obs-frontend-api.h>
#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<std::string> GetCmdlineArgs()
{
struct obs_cmdline_args args = obs_get_cmdline_args();
std::vector<std::string> 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;
}

View File

@ -11,6 +11,9 @@ class Config {
void SetDefaultsToGlobalStore();
config_t* GetConfigStore();
bool PortOverridden;
bool PasswordOverridden;
bool ServerEnabled;
uint16_t ServerPort;
bool DebugEnabled;

View File

@ -2,6 +2,7 @@
#include <thread>
#include <QtConcurrent>
#include <QDateTime>
#include <QTime>
#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();

View File

@ -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);

View File

@ -1,3 +1,5 @@
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QNetworkInterface>
#include <QHostAddress>
@ -5,6 +7,8 @@
#include "../plugin-macros.generated.h"
#include <QDebug>
std::string Utils::Platform::GetLocalAddress()
{
std::vector<QString> 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);
}

View File

@ -22,5 +22,6 @@ namespace Utils {
namespace Platform {
std::string GetLocalAddress();
QString GetCommandLineArgument(QString arg);
}
}