From 3a5f0d89b99ffb304b3b01a50466d461f94f54be Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sat, 12 Mar 2022 13:56:15 +0000 Subject: [PATCH] Server: Add --websocket_ipv4_only switch Socket listening default changed to IPv4 and IPv6, overridable to IPv4 only by using the command line switch. --- README.md | 2 +- src/Config.cpp | 8 ++++++++ src/Config.h | 1 + src/websocketserver/WebSocketServer.cpp | 8 +++++++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 568b9943..2d0159ff 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Binaries for Windows, MacOS, and Linux are available in the [Releases](https://g 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`(value), `--websocket_password`(value), and `--websocket_debug`(flag) on the command line to override the configured values.) +(Psst. You can use `--websocket_port`(value), `--websocket_password`(value), `--websocket_debug`(flag) and `--websocket_ipv4_only`(flag) on the command line to override the configured values.) ### Possible use cases diff --git a/src/Config.cpp b/src/Config.cpp index 5c45b2dd..2243183b 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -33,6 +33,7 @@ with this program. If not, see #define PARAM_PASSWORD "ServerPassword" #define CMDLINE_WEBSOCKET_PORT "websocket_port" +#define CMDLINE_WEBSOCKET_IPV4_ONLY "websocket_ipv4_only" #define CMDLINE_WEBSOCKET_PASSWORD "websocket_password" #define CMDLINE_WEBSOCKET_DEBUG "websocket_debug" @@ -42,6 +43,7 @@ Config::Config() : FirstLoad(true), ServerEnabled(true), ServerPort(4455), + Ipv4Only(false), DebugEnabled(false), AlertsEnabled(false), AuthRequired(true), @@ -93,6 +95,12 @@ void Config::Load() } } + // Process `--websocket_ipv4_only` override + if (Utils::Platform::GetCommandLineFlagSet(CMDLINE_WEBSOCKET_IPV4_ONLY)) { + blog(LOG_INFO, "[Config::Load] --websocket_ipv4_only passed. Binding only to IPv4 interfaces."); + Ipv4Only = true; + } + // Process `--websocket_password` override QString passwordArgument = Utils::Platform::GetCommandLineArgument(CMDLINE_WEBSOCKET_PASSWORD); if (passwordArgument != "") { diff --git a/src/Config.h b/src/Config.h index beec3673..17533223 100644 --- a/src/Config.h +++ b/src/Config.h @@ -38,6 +38,7 @@ struct Config { std::atomic FirstLoad; std::atomic ServerEnabled; std::atomic ServerPort; + std::atomic Ipv4Only; std::atomic DebugEnabled; std::atomic AlertsEnabled; std::atomic AuthRequired; diff --git a/src/websocketserver/WebSocketServer.cpp b/src/websocketserver/WebSocketServer.cpp index 0219922d..2fa1c40f 100644 --- a/src/websocketserver/WebSocketServer.cpp +++ b/src/websocketserver/WebSocketServer.cpp @@ -129,7 +129,13 @@ void WebSocketServer::Start() _server.reset(); websocketpp::lib::error_code errorCode; - _server.listen(websocketpp::lib::asio::ip::tcp::v4(), conf->ServerPort, errorCode); + if (conf->Ipv4Only) { + blog(LOG_INFO, "[WebSocketServer::Start] Locked to IPv4 bindings"); + _server.listen(websocketpp::lib::asio::ip::tcp::v4(), conf->ServerPort, errorCode); + } else { + blog(LOG_INFO, "[WebSocketServer::Start] Not locked to IPv4 bindings"); + _server.listen(conf->ServerPort, errorCode); + } if (errorCode) { std::string errorCodeMessage = errorCode.message();