rustdesk/src/hbbs_http/http_client.rs
yuluo 43a0a4f8e0
fix: http/https proxy (#7821)
* add http(s) proxy

* Add front-end translation

* fix ui description

* For linux platform, add rustls support

* fix: Fix the proxy address test function.

* add: Added default prompts for agency agreement and some multi-language translations

* add: Http proxy request client

* fix: add async http proxy func and format the code

* add: Preliminary support for flutter front-end calling rust back-end http request

* Optimize HTTP calls

* Optimize HTTP calls

* fix: Optimize HTTP requests, refine translations, and fix dependencies

* fix: Win and macOS compilation errors

* fix: web platforms

* fix: Optimize import

* fix: Fix web platform issues

* fix: Fix web platform issues

* fix: update ci

* fix: test ci

* test: test CI

* Revert "fix: update ci"

This reverts commit 2e5f247b2ed0cc63a6f6f7bbaaffd0a1223712e5.

* test: test CI

* test: test CI

* fix: fix lock file

* fix: Optimize imports
2024-04-25 11:46:21 +08:00

72 lines
2.8 KiB
Rust

use hbb_common::config::Config;
use hbb_common::log::info;
use hbb_common::proxy::{Proxy, ProxyScheme};
use reqwest::blocking::Client as SyncClient;
use reqwest::Client as AsyncClient;
macro_rules! configure_http_client {
($builder:expr, $Client: ty) => {{
let mut builder = $builder;
let client = if let Some(conf) = Config::get_socks() {
let proxy_result = Proxy::from_conf(&conf, None);
match proxy_result {
Ok(proxy) => {
let proxy_setup = match &proxy.intercept {
ProxyScheme::Http { host, .. } =>{ reqwest::Proxy::http(format!("http://{}", host))},
ProxyScheme::Https { host, .. } => {reqwest::Proxy::https(format!("https://{}", host))},
ProxyScheme::Socks5 { addr, .. } => { reqwest::Proxy::all(&format!("socks5://{}", addr)) }
};
match proxy_setup {
Ok(p) => {
builder = builder.proxy(p);
if let Some(auth) = proxy.intercept.maybe_auth() {
let basic_auth =
format!("Basic {}", auth.get_basic_authorization());
builder = builder.default_headers(
vec![(
reqwest::header::PROXY_AUTHORIZATION,
basic_auth.parse().unwrap(),
)]
.into_iter()
.collect(),
);
}
builder.build().unwrap_or_else(|e| {
info!("Failed to create a proxied client: {}", e);
<$Client>::new()
})
}
Err(e) => {
info!("Failed to set up proxy: {}", e);
<$Client>::new()
}
}
}
Err(e) => {
info!("Failed to configure proxy: {}", e);
<$Client>::new()
}
}
} else {
builder.build().unwrap_or_else(|e| {
info!("Failed to create a client: {}", e);
<$Client>::new()
})
};
client
}};
}
pub fn create_http_client() -> SyncClient {
let builder = SyncClient::builder();
configure_http_client!(builder, SyncClient)
}
pub fn create_http_client_async() -> AsyncClient {
let builder = AsyncClient::builder();
configure_http_client!(builder, AsyncClient)
}