From 5755fead944b9093d24eb1caf12ee20771036952 Mon Sep 17 00:00:00 2001 From: 123 <123@test.test> Date: Fri, 5 Jan 2024 09:16:11 +0800 Subject: [PATCH] remove chromium_options --no-proxy-server, export webui_set_proxy api --- include/webui.h | 10 ++++++++++ include/webui.hpp | 5 +++++ src/webui.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/include/webui.h b/include/webui.h index 728ae9f6..ceb365ee 100644 --- a/include/webui.h +++ b/include/webui.h @@ -421,6 +421,16 @@ WEBUI_EXPORT void webui_set_position(size_t window, unsigned int x, unsigned int */ WEBUI_EXPORT void webui_set_profile(size_t window, const char* name, const char* path); +/** + * @brief Set the web browser proxy_server to use. Need to be called before `webui_show()`. + * + * @param window The window number + * @param proxy_server The web browser proxy_server + * + * @example webui_set_proxy(myWindow, "http://127.0.0.1:8888"); + */ +WEBUI_EXPORT void webui_set_proxy(size_t window, const char* proxy_server); + /** * @brief Get the full current URL. * diff --git a/include/webui.hpp b/include/webui.hpp index daca4cd6..a9bc14ff 100644 --- a/include/webui.hpp +++ b/include/webui.hpp @@ -197,6 +197,11 @@ class window { webui_set_profile(webui_window, name.data(), path.data()); } + // Set the web browser proxy to use. Need to be called before `webui_show()`. + void set_proxy(const std::string_view proxy_server = {""}) const { + webui_set_profile(webui_window, proxy_server.data()); + } + // Get the full current URL std::string_view get_url() const { return std::string_view{webui_get_url(webui_window)}; } diff --git a/src/webui.c b/src/webui.c index 6a0aa2ac..8d085a5f 100644 --- a/src/webui.c +++ b/src/webui.c @@ -173,6 +173,8 @@ typedef struct _webui_window_t { webui_event_inf_t* events[WEBUI_MAX_IDS]; size_t events_count; bool is_public; + bool proxy_set; + char *proxy_server; } _webui_window_t; @@ -1818,6 +1820,46 @@ void webui_set_profile(size_t window, const char* name, const char* path) { win->default_profile = false; } + +void webui_set_proxy(size_t window, const char* proxy_server) { + + #ifdef WEBUI_LOG + printf("[User] webui_set_proxy([%s])...\n", proxy_server); + #endif + + // Initialization + _webui_init(); + + // Dereference + if (_webui_mtx_is_exit_now(WEBUI_MUTEX_NONE) || _webui_core.wins[window] == NULL) + return; + _webui_window_t * win = _webui_core.wins[window]; + + // Some wrappers do not guarantee pointers stay valid, + // so, let's make our copy. + + char* proxy_server_cpy = NULL; + size_t len = _webui_strlen(proxy_server); + if (len > 0) { + proxy_server_cpy = (char*)_webui_malloc(len); + memcpy((char*)proxy_server_cpy, proxy_server, len); + } + + + // Free + if (win->proxy_server != NULL) + _webui_free_mem((void * ) win->proxy_server); + + + // Save + win->proxy_server = proxy_server_cpy; + + if (proxy_server_cpy == NULL) + win->proxy_set = false; + else + win->proxy_set = true; +} + const char* webui_get_url(size_t window) { #ifdef WEBUI_LOG @@ -4829,7 +4871,7 @@ static int _webui_get_browser_args(_webui_window_t * win, size_t browser, char* const char* chromium_options[] = { "--no-first-run", - "--no-proxy-server", + // "--no-proxy-server", "--safe-mode", "--disable-extensions", "--disable-background-mode", @@ -4872,6 +4914,10 @@ static int _webui_get_browser_args(_webui_window_t * win, size_t browser, char* // Window Position if (win->position_set) c += sprintf(buffer + c, " --window-position=%u,%u", win->x, win->y); + if (win->proxy_set) + c += sprintf(buffer + c, " --proxy-server=%s", win->proxy_server); + else + c += sprintf(buffer + c, " %s", "--no-proxy-server"); // URL (END) c += sprintf(buffer + c, " %s", "--app=");