Skip to content

Commit 663e066

Browse files
committed
New API - webui_set_public
* New API: `webui_set_public()` to let window URL accessible from any public network
1 parent c063f07 commit 663e066

File tree

2 files changed

+83
-14
lines changed

2 files changed

+83
-14
lines changed

include/webui.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,16 @@ WEBUI_EXPORT void webui_set_profile(size_t window, const char* name, const char*
432432
*/
433433
WEBUI_EXPORT const char* webui_get_url(size_t window);
434434

435+
/**
436+
* @brief Allow a specific window address to be accessible from a public network
437+
*
438+
* @param window The window number
439+
* @param status True or False
440+
*
441+
* @example webui_set_public(myWindow, true);
442+
*/
443+
WEBUI_EXPORT void webui_set_public(size_t window, bool status);
444+
435445
/**
436446
* @brief Navigate to a specific URL
437447
*

src/webui.c

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ typedef struct _webui_window_t {
172172
struct mg_connection * mg_connection;
173173
webui_event_inf_t* events[WEBUI_MAX_IDS];
174174
size_t events_count;
175+
bool is_public;
175176
}
176177
_webui_window_t;
177178

@@ -1831,7 +1832,30 @@ const char* webui_get_url(size_t window) {
18311832
return NULL;
18321833
_webui_window_t * win = _webui_core.wins[window];
18331834

1834-
return (const char* ) win->url;
1835+
// Check if server is started
1836+
if (_webui_is_empty(win->url)) {
1837+
// Start server
1838+
webui_show_browser(window, "<html><head><script src=\"webui.js\"></script></head></html>", NoBrowser);
1839+
}
1840+
1841+
return (const char*) win->url;
1842+
}
1843+
1844+
void webui_set_public(size_t window, bool status) {
1845+
1846+
#ifdef WEBUI_LOG
1847+
printf("[User] webui_set_public([%zu])...\n", window);
1848+
#endif
1849+
1850+
// Initialization
1851+
_webui_init();
1852+
1853+
// Dereference
1854+
if (_webui_mtx_is_exit_now(WEBUI_MUTEX_NONE) || _webui_core.wins[window] == NULL)
1855+
return NULL;
1856+
_webui_window_t * win = _webui_core.wins[window];
1857+
1858+
win->is_public = status;
18351859
}
18361860

18371861
void webui_send_raw(size_t window, const char* function, const void * raw, size_t size) {
@@ -2906,8 +2930,8 @@ static bool _webui_socket_test_listen_mg(size_t port_num) {
29062930
#endif
29072931

29082932
// HTTP Port Test
2909-
char* test_port = (char*)_webui_malloc(16);
2910-
sprintf(test_port, "%zu", port_num);
2933+
char* test_port = (char*)_webui_malloc(64);
2934+
sprintf(test_port, "127.0.0.1:%zu", port_num);
29112935

29122936
// Start HTTP Server
29132937
const char* http_options[] = {
@@ -5913,7 +5937,36 @@ static bool _webui_show_window(_webui_window_t * win, const char* content, int t
59135937
_webui_free_mem((void * ) window_url);
59145938
}
59155939

5916-
return true;
5940+
// Wait for connection
5941+
if (browser != NoBrowser) {
5942+
size_t timeout = (_webui_core.startup_timeout > 0 ? _webui_core.startup_timeout : WEBUI_DEF_TIMEOUT);
5943+
_webui_timer_t timer;
5944+
_webui_timer_start( & timer);
5945+
for (;;) {
5946+
// Stop if window is connected
5947+
_webui_sleep(100);
5948+
if (_webui_mtx_is_connected(win, WEBUI_MUTEX_NONE))
5949+
break;
5950+
// Stop if timer is finished
5951+
if (_webui_timer_is_end(&timer, (timeout * 1000)))
5952+
break;
5953+
}
5954+
} else {
5955+
// Wait for server thread to start
5956+
_webui_timer_t timer;
5957+
_webui_timer_start(&timer);
5958+
for (;;) {
5959+
// Stop if server thread started
5960+
_webui_sleep(100);
5961+
if (win->server_running)
5962+
break;
5963+
// Stop if timer is finished
5964+
if (_webui_timer_is_end(&timer, (1000)))
5965+
break;
5966+
}
5967+
}
5968+
5969+
return _webui_mtx_is_connected(win, WEBUI_MUTEX_NONE);
59175970
}
59185971

59195972
static void _webui_window_event(
@@ -6630,25 +6683,31 @@ static WEBUI_THREAD_SERVER_START {
66306683
win->bridge_handled = false;
66316684
if (_webui_core.startup_timeout < 1)
66326685
_webui_core.startup_timeout = 0;
6633-
if (_webui_core.startup_timeout > 30)
6634-
_webui_core.startup_timeout = 30;
6686+
if (_webui_core.startup_timeout > WEBUI_DEF_TIMEOUT)
6687+
_webui_core.startup_timeout = WEBUI_DEF_TIMEOUT;
6688+
6689+
// Public host access
6690+
char host[16] = {0};
6691+
if (!win->is_public)
6692+
// Private localhost access
6693+
strcpy(host, "127.0.0.1:");
66356694

66366695
#ifdef WEBUI_TLS
66376696
// HTTP Secure Port
6638-
char* server_port = (char*)_webui_malloc(32);
6639-
sprintf(server_port, "127.0.0.1:%zus", win->server_port);
6697+
char* server_port = (char*)_webui_malloc(64);
6698+
sprintf(server_port, "%s%zus", host, win->server_port);
66406699

66416700
// WS Secure Port
6642-
char* ws_port = (char*)_webui_malloc(32);
6643-
sprintf(ws_port, "127.0.0.1:%zus", win->ws_port);
6701+
char* ws_port = (char*)_webui_malloc(64);
6702+
sprintf(ws_port, "%s%zus", host, win->ws_port);
66446703
#else
66456704
// HTTP Port
6646-
char* server_port = (char*)_webui_malloc(32);
6647-
sprintf(server_port, "127.0.0.1:%zu", win->server_port);
6705+
char* server_port = (char*)_webui_malloc(64);
6706+
sprintf(server_port, "%s%zu", host, win->server_port);
66486707

66496708
// WS Port
6650-
char* ws_port = (char*)_webui_malloc(32);
6651-
sprintf(ws_port, "127.0.0.1:%zu", win->ws_port);
6709+
char* ws_port = (char*)_webui_malloc(64);
6710+
sprintf(ws_port, "%s%zu", host, win->ws_port);
66526711
#endif
66536712

66546713
// Start HTTP Server

0 commit comments

Comments
 (0)