From ef5fbba56b074e9aa8c7f8098bfbbf4d0c2c8816 Mon Sep 17 00:00:00 2001 From: Shiroki Satsuki Date: Fri, 1 Jul 2022 20:05:32 +0800 Subject: [PATCH] feature: Added network proxy support for hex::Net (#562) * feat(i18n): update Chinese(Simplified) translation * feat: proxy setting * refactor: add hex::Net::setProxy * fix: undefined symbol: hex::Net::m_proxyUrl * style: m_proxyUrl -> s_proxyUrl --- lib/libimhex/include/hex/helpers/net.hpp | 4 ++ lib/libimhex/source/helpers/net.cpp | 10 +++++ .../source/content/settings_entries.cpp | 40 +++++++++++++++++++ plugins/builtin/source/lang/de_DE.cpp | 5 +++ plugins/builtin/source/lang/en_US.cpp | 5 +++ plugins/builtin/source/lang/it_IT.cpp | 5 +++ plugins/builtin/source/lang/ja_JP.cpp | 5 +++ plugins/builtin/source/lang/pt_BR.cpp | 5 +++ plugins/builtin/source/lang/zh_CN.cpp | 11 ++++- 9 files changed, 88 insertions(+), 2 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/net.hpp b/lib/libimhex/include/hex/helpers/net.hpp index b0daedbfd..f85087f87 100644 --- a/lib/libimhex/include/hex/helpers/net.hpp +++ b/lib/libimhex/include/hex/helpers/net.hpp @@ -49,6 +49,8 @@ namespace hex { void cancel() { this->m_shouldCancel = true; } + static void setProxy(const std::string &url); + private: void setCommonSettings(std::string &response, const std::string &url, u32 timeout = 2000, const std::map &extraHeaders = {}, const std::string &body = {}); std::optional execute(); @@ -62,6 +64,8 @@ namespace hex { std::mutex m_transmissionActive; float m_progress = 0.0F; bool m_shouldCancel = false; + + static std::string s_proxyUrl; }; } \ No newline at end of file diff --git a/lib/libimhex/source/helpers/net.cpp b/lib/libimhex/source/helpers/net.cpp index a0693534a..3c930fb02 100644 --- a/lib/libimhex/source/helpers/net.cpp +++ b/lib/libimhex/source/helpers/net.cpp @@ -4,6 +4,8 @@ #include #include +#include + #include #include @@ -113,6 +115,8 @@ namespace hex { curl_easy_setopt(this->m_ctx, CURLOPT_SSLCERTTYPE, "PEM"); curl_easy_setopt(this->m_ctx, CURLOPT_SSL_CTX_FUNCTION, sslCtxFunction); #endif + + curl_easy_setopt(this->m_ctx, CURLOPT_PROXY, Net::s_proxyUrl.c_str()); } std::optional Net::execute() { @@ -241,4 +245,10 @@ namespace hex { return {}; } + std::string Net::s_proxyUrl; + + void Net::setProxy(const std::string &url) { + Net::s_proxyUrl = url; + } + } \ No newline at end of file diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index 4a95384cc..49ed0d788 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -350,6 +351,45 @@ namespace hex::plugin::builtin { return result; }); + + /* Proxy */ + + static const std::string proxySetting { "hex.builtin.setting.proxy" }; + + // init hex::Net proxy url + hex::Net::setProxy(ContentRegistry::Settings::read(proxySetting, "hex.builtin.setting.proxy.url", "")); + + ContentRegistry::Settings::addCategoryDescription(proxySetting, "hex.builtin.setting.proxy.description"); + + ContentRegistry::Settings::add( + proxySetting, "hex.builtin.setting.proxy.url", "", [](auto name, nlohmann::json &setting) { + static std::string proxyUrl = static_cast(setting); + static bool enableProxy = !proxyUrl.empty(); + + bool result = false; + + if (ImGui::Checkbox("hex.builtin.setting.proxy.enable"_lang, &enableProxy)) { + setting = enableProxy ? proxyUrl : ""; + hex::Net::setProxy(enableProxy ? proxyUrl : ""); + result = true; + } + + ImGui::BeginDisabled(!enableProxy); + if (ImGui::InputText("##proxy_url", proxyUrl)) { + setting = proxyUrl; + hex::Net::setProxy(proxyUrl); + result = true; + } + ImGui::EndDisabled(); + + ImGui::InfoTooltip("hex.builtin.setting.proxy.url.tooltip"_lang); + + ImGui::SameLine(); + + ImGui::TextFormatted("{}", name); + return result; + }, + false); } } diff --git a/plugins/builtin/source/lang/de_DE.cpp b/plugins/builtin/source/lang/de_DE.cpp index f7052b7bd..2ecc3af4b 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -696,6 +696,11 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.font", "Schriftart" }, { "hex.builtin.setting.font.font_path", "Eigene Schriftart" }, { "hex.builtin.setting.font.font_size", "Schriftgrösse" }, + //{ "hex.builtin.setting.proxy", "Proxy" }, + //{ "hex.builtin.setting.proxy.description", "Proxy will take effect on store, wikipedia or any other plugin immediately." }, + //{ "hex.builtin.setting.proxy.enable", "Enable Proxy" }, + //{ "hex.builtin.setting.proxy.url", "Proxy URL" }, + //{ "hex.builtin.setting.proxy.url.tooltip", "http(s):// or socks5:// (e.g., http://127.0.0.1:1080)" }, { "hex.builtin.provider.file.path", "Dateipfad" }, { "hex.builtin.provider.file.size", "Größe" }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index b4aca7144..3e8862806 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -698,6 +698,11 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.font", "Font" }, { "hex.builtin.setting.font.font_path", "Custom Font Path" }, { "hex.builtin.setting.font.font_size", "Font Size" }, + { "hex.builtin.setting.proxy", "Proxy" }, + { "hex.builtin.setting.proxy.description", "Proxy will take effect on store, wikipedia or any other plugin immediately." }, + { "hex.builtin.setting.proxy.enable", "Enable Proxy" }, + { "hex.builtin.setting.proxy.url", "Proxy URL" }, + { "hex.builtin.setting.proxy.url.tooltip", "http(s):// or socks5:// (e.g., http://127.0.0.1:1080)" }, { "hex.builtin.provider.file.path", "File path" }, { "hex.builtin.provider.file.size", "Size" }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index bcee5a0ff..47efd62b0 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -702,6 +702,11 @@ namespace hex::plugin::builtin { //{ "hex.builtin.setting.font", "Font" }, //{ "hex.builtin.setting.font.font_path", "Custom Font Path" }, //{ "hex.builtin.setting.font.font_size", "Font Size" }, + //{ "hex.builtin.setting.proxy", "Proxy" }, + //{ "hex.builtin.setting.proxy.description", "Proxy will take effect on store, wikipedia or any other plugin immediately." }, + //{ "hex.builtin.setting.proxy.enable", "Enable Proxy" }, + //{ "hex.builtin.setting.proxy.url", "Proxy URL" }, + //{ "hex.builtin.setting.proxy.url.tooltip", "http(s):// or socks5:// (e.g., http://127.0.0.1:1080)" }, { "hex.builtin.provider.file.path", "Percorso del File" }, { "hex.builtin.provider.file.size", "Dimensione" }, diff --git a/plugins/builtin/source/lang/ja_JP.cpp b/plugins/builtin/source/lang/ja_JP.cpp index cd5f4d058..4175ece64 100644 --- a/plugins/builtin/source/lang/ja_JP.cpp +++ b/plugins/builtin/source/lang/ja_JP.cpp @@ -699,6 +699,11 @@ namespace hex::plugin::builtin { //{ "hex.builtin.setting.font", "Font" }, //{ "hex.builtin.setting.font.font_path", "Custom Font Path" }, //{ "hex.builtin.setting.font.font_size", "Font Size" }, + //{ "hex.builtin.setting.proxy", "Proxy" }, + //{ "hex.builtin.setting.proxy.description", "Proxy will take effect on store, wikipedia or any other plugin immediately." }, + //{ "hex.builtin.setting.proxy.enable", "Enable Proxy" }, + //{ "hex.builtin.setting.proxy.url", "Proxy URL" }, + //{ "hex.builtin.setting.proxy.url.tooltip", "http(s):// or socks5:// (e.g., http://127.0.0.1:1080)" }, { "hex.builtin.provider.file.path", "ファイルパス" }, { "hex.builtin.provider.file.size", "サイズ" }, diff --git a/plugins/builtin/source/lang/pt_BR.cpp b/plugins/builtin/source/lang/pt_BR.cpp index 6af9ad144..e1b90d3fa 100644 --- a/plugins/builtin/source/lang/pt_BR.cpp +++ b/plugins/builtin/source/lang/pt_BR.cpp @@ -696,6 +696,11 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.font", "Fonte" }, { "hex.builtin.setting.font.font_path", "Caminho da Fonte Customizada" }, { "hex.builtin.setting.font.font_size", "Tamanho da Fonte" }, + //{ "hex.builtin.setting.proxy", "Proxy" }, + //{ "hex.builtin.setting.proxy.description", "Proxy will take effect on store, wikipedia or any other plugin immediately." }, + //{ "hex.builtin.setting.proxy.enable", "Enable Proxy" }, + //{ "hex.builtin.setting.proxy.url", "Proxy URL" }, + //{ "hex.builtin.setting.proxy.url.tooltip", "http(s):// or socks5:// (e.g., http://127.0.0.1:1080)" }, { "hex.builtin.provider.file.path", "Caminho do Arquivo" }, { "hex.builtin.provider.file.size", "Tamanho" }, diff --git a/plugins/builtin/source/lang/zh_CN.cpp b/plugins/builtin/source/lang/zh_CN.cpp index 93d3090de..f0c89f6bd 100644 --- a/plugins/builtin/source/lang/zh_CN.cpp +++ b/plugins/builtin/source/lang/zh_CN.cpp @@ -343,6 +343,8 @@ namespace hex::plugin::builtin { { "hex.builtin.view.strings.extract", "搜索" }, { "hex.builtin.view.strings.regex_error", "非法正则" }, { "hex.builtin.view.strings.results", "找到 {0} 个结果" }, + { "hex.builtin.view.strings.filter", "过滤" }, + { "hex.builtin.view.strings.extract", "提取" }, { "hex.builtin.view.strings.searching", "搜索中..." }, { "hex.builtin.view.strings.offset", "偏移" }, { "hex.builtin.view.strings.size", "大小" }, @@ -567,7 +569,7 @@ namespace hex::plugin::builtin { { "hex.builtin.tools.demangler", "Itanium/MSVC 名还原" }, { "hex.builtin.tools.demangler.mangled", "修饰名" }, { "hex.builtin.tools.demangler.demangled", "还原名" }, - { "hex.builtin.tools.ascii_table", "ASCII表" }, + { "hex.builtin.tools.ascii_table", "ASCII 表" }, { "hex.builtin.tools.ascii_table.octal", "显示八进制" }, { "hex.builtin.tools.regex_replacer", "正则替换" }, { "hex.builtin.tools.regex_replacer.pattern", "正则表达式" }, @@ -689,7 +691,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.hex_editor", "Hex 编辑器" }, { "hex.builtin.setting.hex_editor.highlight_color", "选区高亮色" }, { "hex.builtin.setting.hex_editor.bytes_per_row", "每行显示的字节数" }, - { "hex.builtin.setting.hex_editor.ascii", "显示ASCII栏" }, + { "hex.builtin.setting.hex_editor.ascii", "显示 ASCII 栏" }, { "hex.builtin.setting.hex_editor.advanced_decoding", "显示高级解码栏" }, { "hex.builtin.setting.hex_editor.grey_zeros", "显示零字节为灰色" }, { "hex.builtin.setting.hex_editor.uppercase_hex", "大写十六进制" }, @@ -701,6 +703,11 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.font", "字体" }, { "hex.builtin.setting.font.font_path", "自定义字体路径" }, { "hex.builtin.setting.font.font_size", "字体大小" }, + { "hex.builtin.setting.proxy", "网络代理" }, + { "hex.builtin.setting.proxy.description", "代理设置会立即在可下载内容、维基百科查询上生效。" }, + { "hex.builtin.setting.proxy.enable", "启用代理" }, + { "hex.builtin.setting.proxy.url", "代理 URL" }, + { "hex.builtin.setting.proxy.url.tooltip", "http(s):// 或 socks5://(如 http://127.0.0.1:1080)" }, { "hex.builtin.provider.file.path", "路径" }, { "hex.builtin.provider.file.size", "大小" },