Skip to content

Commit

Permalink
WebSocket server: Add option to bind to other ips
Browse files Browse the repository at this point in the history
  • Loading branch information
univrsal committed Oct 6, 2023
1 parent 3f7ac7e commit 23647df
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions src/gui/io_settings_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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();
}

Expand All @@ -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);
}
46 changes: 30 additions & 16 deletions src/gui/io_settings_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>461</width>
<width>505</width>
<height>611</height>
</rect>
</property>
Expand Down Expand Up @@ -224,7 +224,7 @@
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_26">
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QCheckBox" name="cb_enable_wss">
<property name="text">
Expand All @@ -233,20 +233,34 @@
</widget>
</item>
<item>
<widget class="QSpinBox" name="sb_wss_port">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimum">
<number>1025</number>
</property>
<property name="maximum">
<number>65534</number>
</property>
<property name="value">
<number>16899</number>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="txt_bind_address">
<property name="inputMask">
<string/>
</property>
<property name="text">
<string>0.0.0.0</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="sb_wss_port">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimum">
<number>1025</number>
</property>
<property name="maximum">
<number>65534</number>
</property>
<property name="value">
<number>16899</number>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
Expand Down
2 changes: 2 additions & 0 deletions src/network/mg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/network/websocket_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/util/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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);
Expand All @@ -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()
Expand All @@ -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());
}

}
1 change: 1 addition & 0 deletions src/util/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions src/util/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 23647df

Please sign in to comment.