diff --git a/src/Config.cpp b/src/Config.cpp index aaeb32c3..65a5b95c 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -38,11 +38,13 @@ with this program. If not, see #define PARAM_FIRSTLOAD "first_load" #define PARAM_ENABLED "server_enabled" #define PARAM_PORT "server_port" +#define PARAM_HOST "server_host" #define PARAM_ALERTS "alerts_enabled" #define PARAM_AUTHREQUIRED "auth_required" #define PARAM_PASSWORD "server_password" #define CMDLINE_WEBSOCKET_PORT "websocket_port" +#define CMDLINE_WEBSOCKET_HOST "websocket_host" #define CMDLINE_WEBSOCKET_IPV4_ONLY "websocket_ipv4_only" #define CMDLINE_WEBSOCKET_PASSWORD "websocket_password" #define CMDLINE_WEBSOCKET_DEBUG "websocket_debug" @@ -72,6 +74,8 @@ void Config::Load(json config) AuthRequired = config[PARAM_AUTHREQUIRED]; if (config.contains(PARAM_PASSWORD) && config[PARAM_PASSWORD].is_string()) ServerPassword = config[PARAM_PASSWORD]; + if (config.contains(PARAM_HOST) && config[PARAM_HOST].is_string()) + ServerHost = config[PARAM_HOST]; // Set server password and save it to the config before processing overrides, // so that there is always a true configured password regardless of if @@ -105,6 +109,17 @@ void Config::Load(json config) } } + // Process `--websocket_host` override + QString host_argument = Utils::Platform::GetCommandLineArgument(CMDLINE_WEBSOCKET_HOST); + if (host_argument != "") { + blog(LOG_INFO, "[Config::Load] --websocket_host passed. Overriding WebSocket host with: %s", + host_argument.toStdString().c_str()); + ServerHost = host_argument.toStdString(); + HostOverridden = true; + } + + + // 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."); @@ -139,6 +154,8 @@ void Config::Save() config[PARAM_ENABLED] = ServerEnabled.load(); if (!PortOverridden) config[PARAM_PORT] = ServerPort.load(); + if (!HostOverridden) + config[PARAM_HOST] = ServerHost; config[PARAM_ALERTS] = AlertsEnabled.load(); if (!PasswordOverridden) { config[PARAM_AUTHREQUIRED] = AuthRequired.load(); diff --git a/src/Config.h b/src/Config.h index f0b52bd7..c7c90752 100644 --- a/src/Config.h +++ b/src/Config.h @@ -32,9 +32,11 @@ struct Config { std::atomic PortOverridden = false; std::atomic PasswordOverridden = false; + std::atomic HostOverridden = false; std::atomic FirstLoad = true; std::atomic ServerEnabled = false; + std::string ServerHost; std::atomic ServerPort = 4455; std::atomic Ipv4Only = false; std::atomic DebugEnabled = false; diff --git a/src/forms/ConnectInfo.cpp b/src/forms/ConnectInfo.cpp index 06507a6f..1e1f07c6 100644 --- a/src/forms/ConnectInfo.cpp +++ b/src/forms/ConnectInfo.cpp @@ -56,6 +56,8 @@ void ConnectInfo::RefreshData() } QString serverIp = QString::fromStdString(Utils::Platform::GetLocalAddress()); + if (conf->ServerHost != "") + serverIp = QString::fromStdString(conf->ServerHost); ui->serverIpLineEdit->setText(serverIp); QString serverPort = QString::number(conf->ServerPort); diff --git a/src/websocketserver/WebSocketServer.cpp b/src/websocketserver/WebSocketServer.cpp index bd1bc3c6..4f2cadff 100644 --- a/src/websocketserver/WebSocketServer.cpp +++ b/src/websocketserver/WebSocketServer.cpp @@ -100,7 +100,10 @@ void WebSocketServer::Start() _server.reset(); websocketpp::lib::error_code errorCode; - if (conf->Ipv4Only) { + if (conf->ServerHost != "") { + blog(LOG_INFO, "[WebSocketServer::Start] Locked to %s", conf->ServerHost); + _server.listen(conf->ServerHost, std::to_string(conf->ServerPort), errorCode); + } else if (conf->Ipv4Only) { blog(LOG_INFO, "[WebSocketServer::Start] Locked to IPv4 bindings"); _server.listen(websocketpp::lib::asio::ip::tcp::v4(), conf->ServerPort, errorCode); } else {