2021-11-20 01:32:22 +00:00
|
|
|
/*
|
|
|
|
obs-websocket
|
|
|
|
Copyright (C) 2016-2021 Stephane Lepin <stephane.lepin@gmail.com>
|
|
|
|
Copyright (C) 2020-2021 Kyle Manning <tt2468@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program. If not, see <https://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
2021-04-27 02:59:50 +00:00
|
|
|
#include <obs-frontend-api.h>
|
|
|
|
|
|
|
|
#include "Config.h"
|
2021-08-27 11:34:06 +00:00
|
|
|
#include "utils/Crypto.h"
|
|
|
|
#include "utils/Platform.h"
|
2021-04-27 23:41:10 +00:00
|
|
|
|
2021-04-27 03:55:50 +00:00
|
|
|
#define CONFIG_SECTION_NAME "OBSWebSocket"
|
2021-04-27 02:59:50 +00:00
|
|
|
|
2021-05-08 08:42:06 +00:00
|
|
|
#define PARAM_FIRSTLOAD "FirstLoad"
|
2021-04-27 02:59:50 +00:00
|
|
|
#define PARAM_ENABLED "ServerEnabled"
|
|
|
|
#define PARAM_PORT "ServerPort"
|
|
|
|
#define PARAM_ALERTS "AlertsEnabled"
|
2021-09-15 10:46:52 +00:00
|
|
|
#define PARAM_AUTHREQUIRED "AuthRequired"
|
2021-04-27 02:59:50 +00:00
|
|
|
#define PARAM_PASSWORD "ServerPassword"
|
|
|
|
|
2021-04-28 22:59:29 +00:00
|
|
|
#define CMDLINE_WEBSOCKET_PORT "websocket_port"
|
|
|
|
#define CMDLINE_WEBSOCKET_PASSWORD "websocket_password"
|
2021-06-06 05:18:39 +00:00
|
|
|
#define CMDLINE_WEBSOCKET_DEBUG "websocket_debug"
|
2021-04-28 22:59:29 +00:00
|
|
|
|
2021-04-27 02:59:50 +00:00
|
|
|
Config::Config() :
|
2021-04-28 22:59:29 +00:00
|
|
|
PortOverridden(false),
|
|
|
|
PasswordOverridden(false),
|
2021-11-20 10:34:48 +00:00
|
|
|
FirstLoad(true),
|
2021-04-27 02:59:50 +00:00
|
|
|
ServerEnabled(true),
|
2022-01-28 23:38:08 +00:00
|
|
|
ServerPort(4455),
|
2021-04-27 02:59:50 +00:00
|
|
|
DebugEnabled(false),
|
|
|
|
AlertsEnabled(false),
|
|
|
|
AuthRequired(true),
|
|
|
|
ServerPassword("")
|
|
|
|
{
|
|
|
|
SetDefaultsToGlobalStore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::Load()
|
|
|
|
{
|
|
|
|
config_t* obsConfig = GetConfigStore();
|
|
|
|
if (!obsConfig) {
|
2021-04-28 22:59:29 +00:00
|
|
|
blog(LOG_ERROR, "[Config::Load] Unable to fetch OBS config!");
|
2021-04-27 02:59:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:42:06 +00:00
|
|
|
FirstLoad = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_FIRSTLOAD);
|
2021-04-27 02:59:50 +00:00
|
|
|
ServerEnabled = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ENABLED);
|
|
|
|
AlertsEnabled = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ALERTS);
|
2021-09-17 09:25:30 +00:00
|
|
|
ServerPort = config_get_uint(obsConfig, CONFIG_SECTION_NAME, PARAM_PORT);
|
|
|
|
AuthRequired = config_get_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_AUTHREQUIRED);
|
|
|
|
ServerPassword = config_get_string(obsConfig, CONFIG_SECTION_NAME, PARAM_PASSWORD);
|
2021-04-28 22:59:29 +00:00
|
|
|
|
2021-09-17 09:25:30 +00:00
|
|
|
// Set server password and save it to the config before processing overrides,
|
|
|
|
// so that there is always a true configured password regardless of if
|
|
|
|
// future loads use the override flag.
|
|
|
|
if (FirstLoad) {
|
|
|
|
FirstLoad = false;
|
2021-12-25 08:57:09 +00:00
|
|
|
if (ServerPassword.isEmpty()) {
|
2021-09-17 09:25:30 +00:00
|
|
|
blog(LOG_INFO, "[Config::Load] (FirstLoad) Generating new server password.");
|
|
|
|
ServerPassword = QString::fromStdString(Utils::Crypto::GeneratePassword());
|
|
|
|
} else {
|
|
|
|
blog(LOG_INFO, "[Config::Load] (FirstLoad) Not generating new password since one is already configured.");
|
|
|
|
}
|
|
|
|
Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process `--websocket_port` override
|
2021-04-28 22:59:29 +00:00
|
|
|
QString portArgument = Utils::Platform::GetCommandLineArgument(CMDLINE_WEBSOCKET_PORT);
|
|
|
|
if (portArgument != "") {
|
|
|
|
bool ok;
|
|
|
|
uint16_t serverPort = portArgument.toUShort(&ok);
|
|
|
|
if (ok) {
|
2021-09-25 01:09:46 +00:00
|
|
|
blog(LOG_INFO, "[Config::Load] --websocket_port passed. Overriding WebSocket port with: %d", serverPort);
|
2021-04-28 22:59:29 +00:00
|
|
|
PortOverridden = true;
|
|
|
|
ServerPort = serverPort;
|
|
|
|
} else {
|
2021-09-17 09:25:30 +00:00
|
|
|
blog(LOG_WARNING, "[Config::Load] Not overriding WebSocket port since integer conversion failed.");
|
2021-04-28 22:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 09:25:30 +00:00
|
|
|
// Process `--websocket_password` override
|
2021-04-28 22:59:29 +00:00
|
|
|
QString passwordArgument = Utils::Platform::GetCommandLineArgument(CMDLINE_WEBSOCKET_PASSWORD);
|
|
|
|
if (passwordArgument != "") {
|
2021-09-25 01:09:46 +00:00
|
|
|
blog(LOG_INFO, "[Config::Load] --websocket_password passed. Overriding WebSocket password.");
|
2021-04-28 22:59:29 +00:00
|
|
|
PasswordOverridden = true;
|
|
|
|
AuthRequired = true;
|
|
|
|
ServerPassword = passwordArgument;
|
|
|
|
}
|
2021-06-06 05:18:39 +00:00
|
|
|
|
2021-09-17 09:25:30 +00:00
|
|
|
// Process `--websocket_debug` override
|
2021-09-26 10:10:43 +00:00
|
|
|
if (Utils::Platform::GetCommandLineFlagSet(CMDLINE_WEBSOCKET_DEBUG)) {
|
2021-09-17 09:25:30 +00:00
|
|
|
// Debug does not persist on reload, so we let people override it with a flag.
|
2021-09-25 01:09:46 +00:00
|
|
|
blog(LOG_INFO, "[Config::Load] --websocket_debug passed. Enabling debug logging.");
|
2021-06-06 05:18:39 +00:00
|
|
|
DebugEnabled = true;
|
2021-09-26 10:10:43 +00:00
|
|
|
}
|
2021-04-27 02:59:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Config::Save()
|
|
|
|
{
|
|
|
|
config_t* obsConfig = GetConfigStore();
|
|
|
|
if (!obsConfig) {
|
2021-04-28 22:59:29 +00:00
|
|
|
blog(LOG_ERROR, "[Config::Save] Unable to fetch OBS config!");
|
2021-04-27 02:59:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:42:06 +00:00
|
|
|
config_set_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_FIRSTLOAD, FirstLoad);
|
2021-04-27 02:59:50 +00:00
|
|
|
config_set_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ENABLED, ServerEnabled);
|
2021-04-28 22:59:29 +00:00
|
|
|
if (!PortOverridden) {
|
|
|
|
config_set_uint(obsConfig, CONFIG_SECTION_NAME, PARAM_PORT, ServerPort);
|
|
|
|
}
|
2021-04-27 02:59:50 +00:00
|
|
|
config_set_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ALERTS, AlertsEnabled);
|
2021-04-28 22:59:29 +00:00
|
|
|
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));
|
|
|
|
}
|
2021-04-27 02:59:50 +00:00
|
|
|
|
|
|
|
config_save(obsConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::SetDefaultsToGlobalStore()
|
|
|
|
{
|
|
|
|
config_t* obsConfig = GetConfigStore();
|
|
|
|
if (!obsConfig) {
|
2021-04-28 22:59:29 +00:00
|
|
|
blog(LOG_ERROR, "[Config::SetDefaultsToGlobalStore] Unable to fetch OBS config!");
|
2021-04-27 02:59:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:42:06 +00:00
|
|
|
config_set_default_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_FIRSTLOAD, FirstLoad);
|
2021-04-27 02:59:50 +00:00
|
|
|
config_set_default_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ENABLED, ServerEnabled);
|
|
|
|
config_set_default_uint(obsConfig, CONFIG_SECTION_NAME, PARAM_PORT, ServerPort);
|
|
|
|
config_set_default_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_ALERTS, AlertsEnabled);
|
|
|
|
config_set_default_bool(obsConfig, CONFIG_SECTION_NAME, PARAM_AUTHREQUIRED, AuthRequired);
|
|
|
|
config_set_default_string(obsConfig, CONFIG_SECTION_NAME, PARAM_PASSWORD, QT_TO_UTF8(ServerPassword));
|
|
|
|
}
|
|
|
|
|
|
|
|
config_t* Config::GetConfigStore()
|
|
|
|
{
|
|
|
|
return obs_frontend_get_global_config();
|
2021-04-27 21:52:48 +00:00
|
|
|
}
|