From 23647df38745cd5a899f7e0e6fd93480aa28e217 Mon Sep 17 00:00:00 2001 From: univrsal Date: Fri, 6 Oct 2023 13:38:47 +0200 Subject: [PATCH] WebSocket server: Add option to bind to other ips --- data/locale/en-US.ini | 2 +- src/gui/io_settings_dialog.cpp | 3 +++ src/gui/io_settings_dialog.ui | 46 +++++++++++++++++++++----------- src/network/mg.cpp | 2 ++ src/network/websocket_server.cpp | 2 +- src/util/config.cpp | 4 +++ src/util/config.hpp | 1 + src/util/settings.h | 1 + 8 files changed, 43 insertions(+), 18 deletions(-) diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index 0282720f..6456532e 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -30,7 +30,7 @@ Dialog.Title="input-overlay configuration" Dialog.LocalFeatures="Local features" Dialog.LocalFeatures.Info="Most of these settings will require a restart!" Dialog.Uiohook.Enable="Enable mouse and keyboard hook" -Dialog.Wss="Forward and receive events through websocket server with port " +Dialog.Wss="Forward and receive events through websocket server with address" Dialog.GamepadHook.Enable="Enable gamepad hook" Dialog.InputOverlay.Enable="Enable Input Overlay Source" Dialog.InputHistory.Enable="Enable Input History Source" diff --git a/src/gui/io_settings_dialog.cpp b/src/gui/io_settings_dialog.cpp index 99e5b051..7028efe1 100644 --- a/src/gui/io_settings_dialog.cpp +++ b/src/gui/io_settings_dialog.cpp @@ -64,6 +64,7 @@ io_settings_dialog::io_settings_dialog(QWidget *parent) : QDialog(parent, Qt::Di ui->cb_enable_control->setChecked(io_config::enable_input_control); ui->cb_enable_wss->setChecked(io_config::enable_websocket_server); ui->cb_log->setChecked(io_config::log_flag); + ui->txt_bind_address->setText(utf8_to_qt(io_config::wss_bind_address.c_str())); ui->tab_remote->hide(); // TODO: Redo protocol and update client to sdl2 @@ -160,6 +161,7 @@ void io_settings_dialog::FormAccepted() io_config::io_window_filters.write_to_config(); io_config::enable_websocket_server = ui->cb_enable_wss->isChecked(); + io_config::wss_bind_address = qt_to_utf8(ui->txt_bind_address->text()); io_config::save(); } @@ -184,4 +186,5 @@ void io_settings_dialog::CbWssStateChanged(int state) { ui->sb_wss_port->setEnabled(state); ui->cb_log->setEnabled(state); + ui->txt_bind_address->setEnabled(state); } diff --git a/src/gui/io_settings_dialog.ui b/src/gui/io_settings_dialog.ui index 69a4eccb..d8a98433 100644 --- a/src/gui/io_settings_dialog.ui +++ b/src/gui/io_settings_dialog.ui @@ -6,7 +6,7 @@ 0 0 - 461 + 505 611 @@ -224,7 +224,7 @@ - + @@ -233,20 +233,34 @@ - - - false - - - 1025 - - - 65534 - - - 16899 - - + + + + + + + + 0.0.0.0 + + + + + + + false + + + 1025 + + + 65534 + + + 16899 + + + + diff --git a/src/network/mg.cpp b/src/network/mg.cpp index 98122fff..2dc59fec 100644 --- a/src/network/mg.cpp +++ b/src/network/mg.cpp @@ -89,6 +89,8 @@ bool start(const std::string &addr) if (!nc) { berr("Failed to start mongoose listener"); return false; + } else { + binfo("Websocket server listening on %s", addr.c_str()); } thread_handle = std::thread(thread_method); diff --git a/src/network/websocket_server.cpp b/src/network/websocket_server.cpp index 8bd4969e..f3e3def3 100644 --- a/src/network/websocket_server.cpp +++ b/src/network/websocket_server.cpp @@ -17,7 +17,7 @@ bool start() if (state) return true; const auto port = CGET_INT(S_WSS_PORT); - std::string url = "ws://0.0.0.0:"; + std::string url = "ws://" + io_config::wss_bind_address + ":"; url = url.append(std::to_string(port)); auto result = mg::start(url); if (result) { diff --git a/src/util/config.cpp b/src/util/config.cpp index 7e376e90..e93f8de1 100644 --- a/src/util/config.cpp +++ b/src/util/config.cpp @@ -37,6 +37,7 @@ int filter_mode = 0; uint16_t server_refresh_rate = 250; uint16_t server_port = 1608; uint16_t wss_port = 16899; +std::string wss_bind_address; void set_defaults() { @@ -45,6 +46,7 @@ void set_defaults() CDEF_BOOL(S_GAMEPAD, enable_gamepad_hook); CDEF_BOOL(S_OVERLAY, enable_overlay_source); + CDEF_STR(S_WSS_ADDRESS, "0.0.0.0"); CDEF_BOOL(S_LOGGING, log_flag); CDEF_INT(S_PORT, server_port); CDEF_INT(S_WSS_PORT, wss_port); @@ -69,6 +71,7 @@ void load() wss_port = CGET_INT(S_WSS_PORT); log_flag = CGET_BOOL(S_LOGGING); server_refresh_rate = CGET_INT(S_REFRESH); + wss_bind_address = CGET_STR(S_WSS_ADDRESS); } void save() @@ -84,6 +87,7 @@ void save() CSET_BOOL(S_REGEX, regex); CSET_INT(S_WSS_PORT, wss_port); CSET_BOOL(S_ENABLE_WSS, enable_websocket_server); + CSET_STR(S_WSS_ADDRESS, wss_bind_address.c_str()); } } diff --git a/src/util/config.hpp b/src/util/config.hpp index dbcdba5c..774f3f6a 100644 --- a/src/util/config.hpp +++ b/src/util/config.hpp @@ -56,6 +56,7 @@ extern bool log_flag; extern uint16_t server_refresh_rate; extern uint16_t server_port; extern uint16_t wss_port; +extern std::string wss_bind_address; extern void set_defaults(); diff --git a/src/util/settings.h b/src/util/settings.h index fd65fffa..df3efc63 100644 --- a/src/util/settings.h +++ b/src/util/settings.h @@ -28,6 +28,7 @@ #define S_LOGGING "logging" #define S_PORT "server_port" #define S_WSS_PORT "wss_port" +#define S_WSS_ADDRESS "wss_address" #define S_ENABLE_WSS "enable_wss" #define S_REFRESH "server_refresh_rate" #define S_CONTROL "control"