mirror of
https://github.com/webui-dev/webui.git
synced 2024-09-11 22:17:13 +03:00
New API - webui_set_public
* New API: `webui_set_public()` to let window URL accessible from any public network
This commit is contained in:
parent
c063f07dcd
commit
663e066f5e
@ -432,6 +432,16 @@ WEBUI_EXPORT void webui_set_profile(size_t window, const char* name, const char*
|
||||
*/
|
||||
WEBUI_EXPORT const char* webui_get_url(size_t window);
|
||||
|
||||
/**
|
||||
* @brief Allow a specific window address to be accessible from a public network
|
||||
*
|
||||
* @param window The window number
|
||||
* @param status True or False
|
||||
*
|
||||
* @example webui_set_public(myWindow, true);
|
||||
*/
|
||||
WEBUI_EXPORT void webui_set_public(size_t window, bool status);
|
||||
|
||||
/**
|
||||
* @brief Navigate to a specific URL
|
||||
*
|
||||
|
87
src/webui.c
87
src/webui.c
@ -172,6 +172,7 @@ typedef struct _webui_window_t {
|
||||
struct mg_connection * mg_connection;
|
||||
webui_event_inf_t* events[WEBUI_MAX_IDS];
|
||||
size_t events_count;
|
||||
bool is_public;
|
||||
}
|
||||
_webui_window_t;
|
||||
|
||||
@ -1831,7 +1832,30 @@ const char* webui_get_url(size_t window) {
|
||||
return NULL;
|
||||
_webui_window_t * win = _webui_core.wins[window];
|
||||
|
||||
return (const char* ) win->url;
|
||||
// Check if server is started
|
||||
if (_webui_is_empty(win->url)) {
|
||||
// Start server
|
||||
webui_show_browser(window, "<html><head><script src=\"webui.js\"></script></head></html>", NoBrowser);
|
||||
}
|
||||
|
||||
return (const char*) win->url;
|
||||
}
|
||||
|
||||
void webui_set_public(size_t window, bool status) {
|
||||
|
||||
#ifdef WEBUI_LOG
|
||||
printf("[User] webui_set_public([%zu])...\n", window);
|
||||
#endif
|
||||
|
||||
// Initialization
|
||||
_webui_init();
|
||||
|
||||
// Dereference
|
||||
if (_webui_mtx_is_exit_now(WEBUI_MUTEX_NONE) || _webui_core.wins[window] == NULL)
|
||||
return NULL;
|
||||
_webui_window_t * win = _webui_core.wins[window];
|
||||
|
||||
win->is_public = status;
|
||||
}
|
||||
|
||||
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) {
|
||||
#endif
|
||||
|
||||
// HTTP Port Test
|
||||
char* test_port = (char*)_webui_malloc(16);
|
||||
sprintf(test_port, "%zu", port_num);
|
||||
char* test_port = (char*)_webui_malloc(64);
|
||||
sprintf(test_port, "127.0.0.1:%zu", port_num);
|
||||
|
||||
// Start HTTP Server
|
||||
const char* http_options[] = {
|
||||
@ -5913,7 +5937,36 @@ static bool _webui_show_window(_webui_window_t * win, const char* content, int t
|
||||
_webui_free_mem((void * ) window_url);
|
||||
}
|
||||
|
||||
return true;
|
||||
// Wait for connection
|
||||
if (browser != NoBrowser) {
|
||||
size_t timeout = (_webui_core.startup_timeout > 0 ? _webui_core.startup_timeout : WEBUI_DEF_TIMEOUT);
|
||||
_webui_timer_t timer;
|
||||
_webui_timer_start( & timer);
|
||||
for (;;) {
|
||||
// Stop if window is connected
|
||||
_webui_sleep(100);
|
||||
if (_webui_mtx_is_connected(win, WEBUI_MUTEX_NONE))
|
||||
break;
|
||||
// Stop if timer is finished
|
||||
if (_webui_timer_is_end(&timer, (timeout * 1000)))
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// Wait for server thread to start
|
||||
_webui_timer_t timer;
|
||||
_webui_timer_start(&timer);
|
||||
for (;;) {
|
||||
// Stop if server thread started
|
||||
_webui_sleep(100);
|
||||
if (win->server_running)
|
||||
break;
|
||||
// Stop if timer is finished
|
||||
if (_webui_timer_is_end(&timer, (1000)))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return _webui_mtx_is_connected(win, WEBUI_MUTEX_NONE);
|
||||
}
|
||||
|
||||
static void _webui_window_event(
|
||||
@ -6630,25 +6683,31 @@ static WEBUI_THREAD_SERVER_START {
|
||||
win->bridge_handled = false;
|
||||
if (_webui_core.startup_timeout < 1)
|
||||
_webui_core.startup_timeout = 0;
|
||||
if (_webui_core.startup_timeout > 30)
|
||||
_webui_core.startup_timeout = 30;
|
||||
if (_webui_core.startup_timeout > WEBUI_DEF_TIMEOUT)
|
||||
_webui_core.startup_timeout = WEBUI_DEF_TIMEOUT;
|
||||
|
||||
// Public host access
|
||||
char host[16] = {0};
|
||||
if (!win->is_public)
|
||||
// Private localhost access
|
||||
strcpy(host, "127.0.0.1:");
|
||||
|
||||
#ifdef WEBUI_TLS
|
||||
// HTTP Secure Port
|
||||
char* server_port = (char*)_webui_malloc(32);
|
||||
sprintf(server_port, "127.0.0.1:%zus", win->server_port);
|
||||
char* server_port = (char*)_webui_malloc(64);
|
||||
sprintf(server_port, "%s%zus", host, win->server_port);
|
||||
|
||||
// WS Secure Port
|
||||
char* ws_port = (char*)_webui_malloc(32);
|
||||
sprintf(ws_port, "127.0.0.1:%zus", win->ws_port);
|
||||
char* ws_port = (char*)_webui_malloc(64);
|
||||
sprintf(ws_port, "%s%zus", host, win->ws_port);
|
||||
#else
|
||||
// HTTP Port
|
||||
char* server_port = (char*)_webui_malloc(32);
|
||||
sprintf(server_port, "127.0.0.1:%zu", win->server_port);
|
||||
char* server_port = (char*)_webui_malloc(64);
|
||||
sprintf(server_port, "%s%zu", host, win->server_port);
|
||||
|
||||
// WS Port
|
||||
char* ws_port = (char*)_webui_malloc(32);
|
||||
sprintf(ws_port, "127.0.0.1:%zu", win->ws_port);
|
||||
char* ws_port = (char*)_webui_malloc(64);
|
||||
sprintf(ws_port, "%s%zu", host, win->ws_port);
|
||||
#endif
|
||||
|
||||
// Start HTTP Server
|
||||
|
Loading…
Reference in New Issue
Block a user