Server: Add --websocket_ipv4_only switch

Socket listening default changed to IPv4 and IPv6,
overridable to IPv4 only by using the command line switch.
This commit is contained in:
Chris Tallon 2022-03-12 13:56:15 +00:00 committed by tt2468
parent 9f68e0166b
commit 3a5f0d89b9
4 changed files with 17 additions and 2 deletions

View File

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

View File

@ -33,6 +33,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#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 != "") {

View File

@ -38,6 +38,7 @@ struct Config {
std::atomic<bool> FirstLoad;
std::atomic<bool> ServerEnabled;
std::atomic<uint16_t> ServerPort;
std::atomic<bool> Ipv4Only;
std::atomic<bool> DebugEnabled;
std::atomic<bool> AlertsEnabled;
std::atomic<bool> AuthRequired;

View File

@ -129,7 +129,13 @@ void WebSocketServer::Start()
_server.reset();
websocketpp::lib::error_code 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();