Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings: Add option to lock binding to IPv4 #559

Merged
merged 1 commit into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ OBSWebsocket.Settings.ServerEnable="Enable WebSockets server"
OBSWebsocket.Settings.ServerPort="Server Port"
OBSWebsocket.Settings.AuthRequired="Enable authentication"
OBSWebsocket.Settings.Password="Password"
OBSWebsocket.Settings.LockToIPv4="Lock server to only using IPv4"
OBSWebsocket.Settings.DebugEnable="Enable debug logging"
OBSWebsocket.Settings.AlertsEnable="Enable System Tray Alerts"
OBSWebsocket.NotifyConnect.Title="New WebSocket connection"
Expand Down
18 changes: 16 additions & 2 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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_LOCKTOIPV4 "LockToIPv4"
#define PARAM_DEBUG "DebugEnabled"
#define PARAM_ALERT "AlertsEnabled"
#define PARAM_AUTHREQUIRED "AuthRequired"
Expand All @@ -41,6 +42,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
Config::Config() :
ServerEnabled(true),
ServerPort(4444),
LockToIPv4(false),
DebugEnabled(false),
AlertsEnabled(true),
AuthRequired(false),
Expand All @@ -67,6 +69,7 @@ void Config::Load()

ServerEnabled = config_get_bool(obsConfig, SECTION_NAME, PARAM_ENABLE);
ServerPort = config_get_uint(obsConfig, SECTION_NAME, PARAM_PORT);
LockToIPv4 = config_get_bool(obsConfig, SECTION_NAME, PARAM_LOCKTOIPV4);

DebugEnabled = config_get_bool(obsConfig, SECTION_NAME, PARAM_DEBUG);
AlertsEnabled = config_get_bool(obsConfig, SECTION_NAME, PARAM_ALERT);
Expand All @@ -82,6 +85,7 @@ void Config::Save()

config_set_bool(obsConfig, SECTION_NAME, PARAM_ENABLE, ServerEnabled);
config_set_uint(obsConfig, SECTION_NAME, PARAM_PORT, ServerPort);
config_set_bool(obsConfig, SECTION_NAME, PARAM_LOCKTOIPV4, LockToIPv4);

config_set_bool(obsConfig, SECTION_NAME, PARAM_DEBUG, DebugEnabled);
config_set_bool(obsConfig, SECTION_NAME, PARAM_ALERT, AlertsEnabled);
Expand All @@ -104,6 +108,8 @@ void Config::SetDefaults()
SECTION_NAME, PARAM_ENABLE, ServerEnabled);
config_set_default_uint(obsConfig,
SECTION_NAME, PARAM_PORT, ServerPort);
config_set_default_bool(obsConfig,
SECTION_NAME, PARAM_LOCKTOIPV4, LockToIPv4);

config_set_default_bool(obsConfig,
SECTION_NAME, PARAM_DEBUG, DebugEnabled);
Expand Down Expand Up @@ -205,16 +211,17 @@ void Config::OnFrontendEvent(enum obs_frontend_event event, void* param)

bool previousEnabled = config->ServerEnabled;
uint64_t previousPort = config->ServerPort;
bool previousLock = config->LockToIPv4;

config->SetDefaults();
config->Load();

if (config->ServerEnabled != previousEnabled || config->ServerPort != previousPort) {
if (config->ServerEnabled != previousEnabled || config->ServerPort != previousPort || config->LockToIPv4 != previousLock) {
auto server = GetServer();
server->stop();

if (config->ServerEnabled) {
server->start(config->ServerPort);
server->start(config->ServerPort, config->LockToIPv4);

if (previousEnabled != config->ServerEnabled) {
Utils::SysTrayNotify(startMessage, QSystemTrayIcon::MessageIcon::Information);
Expand Down Expand Up @@ -246,6 +253,13 @@ void Config::MigrateFromGlobalSettings()

config_remove_value(source, SECTION_NAME, PARAM_PORT);
}

if(config_has_user_value(source, SECTION_NAME, PARAM_LOCKTOIPV4)) {
bool value = config_get_bool(source, SECTION_NAME, PARAM_LOCKTOIPV4);
config_set_bool(destination, SECTION_NAME, PARAM_LOCKTOIPV4, value);

config_remove_value(source, SECTION_NAME, PARAM_LOCKTOIPV4);
}

if(config_has_user_value(source, SECTION_NAME, PARAM_DEBUG)) {
bool value = config_get_bool(source, SECTION_NAME, PARAM_DEBUG);
Expand Down
1 change: 1 addition & 0 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Config {

bool ServerEnabled;
uint64_t ServerPort;
bool LockToIPv4;

bool DebugEnabled;
bool AlertsEnabled;
Expand Down
15 changes: 11 additions & 4 deletions src/WSServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ WSServer::~WSServer()
stop();
}

void WSServer::start(quint16 port)
void WSServer::start(quint16 port, bool lockToIPv4)
{
if (_server.is_listening() && port == _serverPort) {
blog(LOG_INFO, "WSServer::start: server already on this port. no restart needed");
if (_server.is_listening() && (port == _serverPort && _lockToIPv4 == lockToIPv4)) {
blog(LOG_INFO, "WSServer::start: server already on this port and protocol mode. no restart needed");
return;
}

Expand All @@ -74,9 +74,16 @@ void WSServer::start(quint16 port)
_server.reset();

_serverPort = port;
_lockToIPv4 = lockToIPv4;

websocketpp::lib::error_code errorCode;
_server.listen(_serverPort, errorCode);
if (lockToIPv4) {
blog(LOG_INFO, "WSServer::start: Locked to IPv4 bindings");
_server.listen(websocketpp::lib::asio::ip::tcp::v4(), _serverPort, errorCode);
} else {
blog(LOG_INFO, "WSServer::start: Not locked to IPv4 bindings");
_server.listen(_serverPort, errorCode);
}

if (errorCode) {
std::string errorCodeMessage = errorCode.message();
Expand Down
3 changes: 2 additions & 1 deletion src/WSServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Q_OBJECT
public:
explicit WSServer();
virtual ~WSServer();
void start(quint16 port);
void start(quint16 port, bool lockToIPv4);
void stop();
void broadcast(const RpcEvent& event);
QThreadPool* threadPool() {
Expand All @@ -62,6 +62,7 @@ Q_OBJECT

server _server;
quint16 _serverPort;
bool _lockToIPv4;
std::set<connection_hdl, std::owner_less<connection_hdl>> _connections;
std::map<connection_hdl, ConnectionProperties, std::owner_less<connection_hdl>> _connectionProperties;
QMutex _clMutex;
Expand Down
4 changes: 3 additions & 1 deletion src/forms/settings-dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void SettingsDialog::showEvent(QShowEvent* event) {

ui->serverEnabled->setChecked(conf->ServerEnabled);
ui->serverPort->setValue(conf->ServerPort);
ui->lockToIPv4->setChecked(conf->LockToIPv4);

ui->debugEnabled->setChecked(conf->DebugEnabled);
ui->alertsEnabled->setChecked(conf->AlertsEnabled);
Expand Down Expand Up @@ -72,6 +73,7 @@ void SettingsDialog::FormAccepted() {

conf->ServerEnabled = ui->serverEnabled->isChecked();
conf->ServerPort = ui->serverPort->value();
conf->LockToIPv4 = ui->lockToIPv4->isChecked();

conf->DebugEnabled = ui->debugEnabled->isChecked();
conf->AlertsEnabled = ui->alertsEnabled->isChecked();
Expand All @@ -95,7 +97,7 @@ void SettingsDialog::FormAccepted() {

auto server = GetServer();
if (conf->ServerEnabled) {
server->start(conf->ServerPort);
server->start(conf->ServerPort, conf->LockToIPv4);
} else {
server->stop();
}
Expand Down
Loading