Skip to content

Commit c56260f

Browse files
authored
Merge pull request #297 from osljw/main
webui chromium proxy support
2 parents 146f40f + 5755fea commit c56260f

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

include/webui.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,16 @@ WEBUI_EXPORT void webui_set_position(size_t window, unsigned int x, unsigned int
421421
*/
422422
WEBUI_EXPORT void webui_set_profile(size_t window, const char* name, const char* path);
423423

424+
/**
425+
* @brief Set the web browser proxy_server to use. Need to be called before `webui_show()`.
426+
*
427+
* @param window The window number
428+
* @param proxy_server The web browser proxy_server
429+
*
430+
* @example webui_set_proxy(myWindow, "http://127.0.0.1:8888");
431+
*/
432+
WEBUI_EXPORT void webui_set_proxy(size_t window, const char* proxy_server);
433+
424434
/**
425435
* @brief Get the full current URL.
426436
*

include/webui.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ class window {
197197
webui_set_profile(webui_window, name.data(), path.data());
198198
}
199199

200+
// Set the web browser proxy to use. Need to be called before `webui_show()`.
201+
void set_proxy(const std::string_view proxy_server = {""}) const {
202+
webui_set_profile(webui_window, proxy_server.data());
203+
}
204+
200205
// Get the full current URL
201206
std::string_view get_url() const { return std::string_view{webui_get_url(webui_window)}; }
202207

src/webui.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ typedef struct _webui_window_t {
173173
webui_event_inf_t* events[WEBUI_MAX_IDS];
174174
size_t events_count;
175175
bool is_public;
176+
bool proxy_set;
177+
char *proxy_server;
176178
}
177179
_webui_window_t;
178180

@@ -1818,6 +1820,46 @@ void webui_set_profile(size_t window, const char* name, const char* path) {
18181820
win->default_profile = false;
18191821
}
18201822

1823+
1824+
void webui_set_proxy(size_t window, const char* proxy_server) {
1825+
1826+
#ifdef WEBUI_LOG
1827+
printf("[User] webui_set_proxy([%s])...\n", proxy_server);
1828+
#endif
1829+
1830+
// Initialization
1831+
_webui_init();
1832+
1833+
// Dereference
1834+
if (_webui_mtx_is_exit_now(WEBUI_MUTEX_NONE) || _webui_core.wins[window] == NULL)
1835+
return;
1836+
_webui_window_t * win = _webui_core.wins[window];
1837+
1838+
// Some wrappers do not guarantee pointers stay valid,
1839+
// so, let's make our copy.
1840+
1841+
char* proxy_server_cpy = NULL;
1842+
size_t len = _webui_strlen(proxy_server);
1843+
if (len > 0) {
1844+
proxy_server_cpy = (char*)_webui_malloc(len);
1845+
memcpy((char*)proxy_server_cpy, proxy_server, len);
1846+
}
1847+
1848+
1849+
// Free
1850+
if (win->proxy_server != NULL)
1851+
_webui_free_mem((void * ) win->proxy_server);
1852+
1853+
1854+
// Save
1855+
win->proxy_server = proxy_server_cpy;
1856+
1857+
if (proxy_server_cpy == NULL)
1858+
win->proxy_set = false;
1859+
else
1860+
win->proxy_set = true;
1861+
}
1862+
18211863
const char* webui_get_url(size_t window) {
18221864

18231865
#ifdef WEBUI_LOG
@@ -4829,7 +4871,7 @@ static int _webui_get_browser_args(_webui_window_t * win, size_t browser, char*
48294871

48304872
const char* chromium_options[] = {
48314873
"--no-first-run",
4832-
"--no-proxy-server",
4874+
// "--no-proxy-server",
48334875
"--safe-mode",
48344876
"--disable-extensions",
48354877
"--disable-background-mode",
@@ -4872,6 +4914,10 @@ static int _webui_get_browser_args(_webui_window_t * win, size_t browser, char*
48724914
// Window Position
48734915
if (win->position_set)
48744916
c += sprintf(buffer + c, " --window-position=%u,%u", win->x, win->y);
4917+
if (win->proxy_set)
4918+
c += sprintf(buffer + c, " --proxy-server=%s", win->proxy_server);
4919+
else
4920+
c += sprintf(buffer + c, " %s", "--no-proxy-server");
48754921

48764922
// URL (END)
48774923
c += sprintf(buffer + c, " %s", "--app=");

0 commit comments

Comments
 (0)