From ec4942174b008a9a4545246d8e82dae96252a4db Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 10 Nov 2023 21:59:20 +0100 Subject: [PATCH] fix: Proxy not being disabled correctly when disabling it in the settings --- .../include/hex/helpers/http_requests.hpp | 3 ++- .../source/helpers/http_requests_emscripten.cpp | 6 +++++- .../source/helpers/http_requests_native.cpp | 15 ++++++++++++--- .../builtin/source/content/settings_entries.cpp | 10 +++------- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/http_requests.hpp b/lib/libimhex/include/hex/helpers/http_requests.hpp index 31b0c8782..6f220e23e 100644 --- a/lib/libimhex/include/hex/helpers/http_requests.hpp +++ b/lib/libimhex/include/hex/helpers/http_requests.hpp @@ -73,7 +73,8 @@ namespace hex { HttpRequest& operator=(HttpRequest &&other) noexcept; - static void setProxy(std::string proxy); + static void setProxyState(bool enabled); + static void setProxyUrl(std::string proxy); void setMethod(std::string method) { this->m_method = std::move(method); diff --git a/lib/libimhex/source/helpers/http_requests_emscripten.cpp b/lib/libimhex/source/helpers/http_requests_emscripten.cpp index d13a0698c..fb10070c2 100644 --- a/lib/libimhex/source/helpers/http_requests_emscripten.cpp +++ b/lib/libimhex/source/helpers/http_requests_emscripten.cpp @@ -40,10 +40,14 @@ namespace hex { }); } - void HttpRequest::setProxy(std::string proxy) { + void HttpRequest::setProxyUrl(std::string proxy) { hex::unused(proxy); } + void HttpRequest::setProxyState(bool state) { + hex::unused(state); + } + void HttpRequest::checkProxyErrors() { } int HttpRequest::progressCallback(void *contents, curl_off_t dlTotal, curl_off_t dlNow, curl_off_t ulTotal, curl_off_t ulNow) { diff --git a/lib/libimhex/source/helpers/http_requests_native.cpp b/lib/libimhex/source/helpers/http_requests_native.cpp index cae9953df..14b3a4af0 100644 --- a/lib/libimhex/source/helpers/http_requests_native.cpp +++ b/lib/libimhex/source/helpers/http_requests_native.cpp @@ -5,7 +5,10 @@ namespace hex { namespace { + std::string s_proxyUrl; + bool s_proxyState; + } HttpRequest::HttpRequest(std::string method, std::string url) : m_method(std::move(method)), m_url(std::move(url)) { @@ -60,7 +63,9 @@ namespace hex { curl_easy_setopt(this->m_curl, CURLOPT_NOPROGRESS, 0L); curl_easy_setopt(this->m_curl, CURLOPT_XFERINFODATA, this); curl_easy_setopt(this->m_curl, CURLOPT_XFERINFOFUNCTION, progressCallback); - curl_easy_setopt(this->m_curl, CURLOPT_PROXY, s_proxyUrl.c_str()); + + if (s_proxyState) + curl_easy_setopt(this->m_curl, CURLOPT_PROXY, s_proxyUrl.c_str()); } std::future>> HttpRequest::downloadFile() { @@ -76,12 +81,16 @@ namespace hex { - void HttpRequest::setProxy(std::string proxy) { + void HttpRequest::setProxyUrl(std::string proxy) { s_proxyUrl = std::move(proxy); } + void HttpRequest::setProxyState(bool state) { + s_proxyState = state; + } + void HttpRequest::checkProxyErrors() { - if (!s_proxyUrl.empty()){ + if (s_proxyState && !s_proxyUrl.empty()){ log::info("A custom proxy '{0}' is in use. Is it working correctly?", s_proxyUrl); } } diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index 25f7385ec..d40215fa0 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -276,18 +276,14 @@ namespace hex::plugin::builtin { /* Proxy */ - HttpRequest::setProxy(ContentRegistry::Settings::read("hex.builtin.setting.proxy", "hex.builtin.setting.proxy.url", "").get()); + HttpRequest::setProxyUrl(ContentRegistry::Settings::read("hex.builtin.setting.proxy", "hex.builtin.setting.proxy.url", "").get()); ContentRegistry::Settings::setCategoryDescription("hex.builtin.setting.proxy", "hex.builtin.setting.proxy.description"); auto proxyEnabledSetting = ContentRegistry::Settings::add("hex.builtin.setting.proxy", "", "hex.builtin.setting.proxy.enable", false).setChangedCallback([](Widgets::Widget &widget) { auto checkBox = static_cast(&widget); - if (checkBox->isChecked()) { - HttpRequest::setProxy(ContentRegistry::Settings::read("hex.builtin.setting.proxy", "hex.builtin.setting.proxy.url", "").get()); - } else { - HttpRequest::setProxy(""); - } + HttpRequest::setProxyState(checkBox->isChecked()); }); ContentRegistry::Settings::add("hex.builtin.setting.proxy", "", "hex.builtin.setting.proxy.url", "") @@ -299,7 +295,7 @@ namespace hex::plugin::builtin { .setChangedCallback([](Widgets::Widget &widget) { auto textBox = static_cast(&widget); - HttpRequest::setProxy(textBox->getValue()); + HttpRequest::setProxyUrl(textBox->getValue()); });