Skip to content

Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.

feat(proxy): implement global proxy management with initialization and application functions

Tonho951912791955
diff --git a/src-tauri/omniget-core/src/core/http_client.rs b/src-tauri/omniget-core/src/core/http_client.rs
new file mode 100644
index 00000000..dd9ce636
--- /dev/null
+++ b/src-tauri/omniget-core/src/core/http_client.rs
@@ -0,0 +1,74 @@
+use std::sync::LazyLock;
+use std::sync::RwLock;
+
+use crate::models::settings::ProxySettings;
+
+static GLOBAL_PROXY: LazyLock<RwLock<ProxySettings>> =
+ LazyLock::new(|| RwLock::new(ProxySettings::default()));
+
+pub fn init_proxy(proxy: ProxySettings) {
+ if let Ok(mut guard) = GLOBAL_PROXY.write() {
+ *guard = proxy;
+ }
+}
+
+pub fn get_proxy_snapshot() -> ProxySettings {
+ GLOBAL_PROXY
+ .read()
+ .map(|g| g.clone())
+ .unwrap_or_default()
+}
+
+pub fn proxy_url() -> Option<String> {
+ let proxy = get_proxy_snapshot();
+ if !proxy.enabled || proxy.host.is_empty() {
+ return None;
+ }
+ let scheme = match proxy.proxy_type.as_str() {
+ "socks5" => "socks5",
+ "https" => "https",
+ _ => "http",
+ };
+ if !proxy.username.is_empty() {
+ Some(format!(
+ "{}://{}:{}@{}:{}",
+ scheme, proxy.username, proxy.password, proxy.host, proxy.port
+ ))
+ } else {
+ Some(format!("{}://{}:{}", scheme, proxy.host, proxy.port))
+ }
+}
+
+pub fn apply_proxy(
+ builder: reqwest::ClientBuilder,
+ proxy: &ProxySettings,
+) -> reqwest::ClientBuilder {
+ if !proxy.enabled || proxy.host.is_empty() {
+ return builder;
+ }
+ let scheme = match proxy.proxy_type.as_str() {
+ "socks5" => "socks5",
+ "https" => "https",
+ _ => "http",
+ };
+ let proxy_url = if !proxy.username.is_empty() {
+ format!(
+ "{}://{}:{}@{}:{}",
+ scheme, proxy.username, proxy.password, proxy.host, proxy.port
+ )
+ } else {
+ format!("{}://{}:{}", scheme, proxy.host, proxy.port)
+ };
+ match reqwest::Proxy::all(&proxy_url) {
+ Ok(p) => builder.proxy(p),
+ Err(e) => {
+ tracing::warn!("Invalid proxy URL: {}", e);
+ builder
+ }
+ }
+}
+
+pub fn apply_global_proxy(builder: reqwest::ClientBuilder) -> reqwest::ClientBuilder {
+ let proxy = get_proxy_snapshot();
+ apply_proxy(builder, &proxy)
+}