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

websocketserver: Allow the user to bind to a specific address #1279

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
websocketserver: Allow the user to bind to a specific address
silasary committed Jan 22, 2025
commit 460003fff50f146a6ccb9fea19bbf7f24825c96a
17 changes: 17 additions & 0 deletions src/Config.cpp
Original file line number Diff line number Diff line change
@@ -38,11 +38,13 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#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();
2 changes: 2 additions & 0 deletions src/Config.h
Original file line number Diff line number Diff line change
@@ -32,9 +32,11 @@ struct Config {

std::atomic<bool> PortOverridden = false;
std::atomic<bool> PasswordOverridden = false;
std::atomic<bool> HostOverridden = false;

std::atomic<bool> FirstLoad = true;
std::atomic<bool> ServerEnabled = false;
std::string ServerHost;
std::atomic<uint16_t> ServerPort = 4455;
std::atomic<bool> Ipv4Only = false;
std::atomic<bool> DebugEnabled = false;
2 changes: 2 additions & 0 deletions src/forms/ConnectInfo.cpp
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 4 additions & 1 deletion src/websocketserver/WebSocketServer.cpp
Original file line number Diff line number Diff line change
@@ -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 {