File size: 1,241 Bytes
bbb1195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use reqwest::{Client, Proxy};
use crate::modules::config::load_app_config;

/// Create a unified HTTP client with global configuration
pub fn create_client(timeout_secs: u64) -> Client {
    if let Ok(config) = load_app_config() {
        create_client_with_proxy(timeout_secs, Some(config.proxy.upstream_proxy))
    } else {
        create_client_with_proxy(timeout_secs, None)
    }
}

/// Create HTTP client with specific proxy configuration
pub fn create_client_with_proxy(
    timeout_secs: u64,
    proxy_config: Option<crate::proxy::config::UpstreamProxyConfig>
) -> Client {
    let mut builder = Client::builder()
        .timeout(std::time::Duration::from_secs(timeout_secs));

    if let Some(config) = proxy_config {
        if config.enabled && !config.url.is_empty() {
            match Proxy::all(&config.url) {
                Ok(proxy) => {
                    builder = builder.proxy(proxy);
                    tracing::info!("HTTP client using upstream proxy: {}", config.url);
                }
                Err(e) => {
                    tracing::error!("Invalid proxy address: {}, error: {}", config.url, e);
                }
            }
        }
    }

    builder.build().unwrap_or_else(|_| Client::new())
}