fix: add async http proxy func and format the code
This commit is contained in:
@@ -145,6 +145,7 @@ use hbb_common::{
|
||||
// #[cfg(any(target_os = "android", target_os = "ios", feature = "cli"))]
|
||||
use hbb_common::{config::RENDEZVOUS_PORT, futures::future::join_all};
|
||||
|
||||
use crate::hbbs_http::create_http_client_async;
|
||||
use crate::ui_interface::{get_option, set_option};
|
||||
|
||||
pub type NotifyMessageBox = fn(String, String, String, String) -> dyn Future<Output = ()>;
|
||||
@@ -972,7 +973,7 @@ pub fn check_software_update() {
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn check_software_update_() -> hbb_common::ResultType<()> {
|
||||
let url = "https://github.com/rustdesk/rustdesk/releases/latest";
|
||||
let latest_release_response = reqwest::get(url).await?;
|
||||
let latest_release_response = create_http_client_async().get(url).send().await?;
|
||||
let latest_release_version = latest_release_response
|
||||
.url()
|
||||
.path()
|
||||
@@ -1067,7 +1068,7 @@ pub fn get_audit_server(api: String, custom: String, typ: String) -> String {
|
||||
}
|
||||
|
||||
pub async fn post_request(url: String, body: String, header: &str) -> ResultType<String> {
|
||||
let mut req = reqwest::Client::new().post(url);
|
||||
let mut req = create_http_client_async().post(url);
|
||||
if !header.is_empty() {
|
||||
let tmp: Vec<&str> = header.split(": ").collect();
|
||||
if tmp.len() == 2 {
|
||||
|
||||
@@ -4,10 +4,11 @@ use serde_json::{Map, Value};
|
||||
|
||||
#[cfg(feature = "flutter")]
|
||||
pub mod account;
|
||||
mod http_client;
|
||||
pub mod record_upload;
|
||||
pub mod sync;
|
||||
mod http_client;
|
||||
pub use http_client::create_client;
|
||||
pub use http_client::create_http_client;
|
||||
pub use http_client::create_http_client_async;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum HbbHttpResponse<T> {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use super::HbbHttpResponse;
|
||||
use crate::hbbs_http::create_http_client;
|
||||
use hbb_common::{config::LocalConfig, log, ResultType};
|
||||
use reqwest::blocking::Client;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
@@ -9,7 +10,6 @@ use std::{
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use url::Url;
|
||||
use crate::hbbs_http::create_client;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref OIDC_SESSION: Arc<RwLock<OidcSession>> = Arc::new(RwLock::new(OidcSession::new()));
|
||||
@@ -131,7 +131,7 @@ impl Default for UserStatus {
|
||||
impl OidcSession {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
client: create_client().unwrap_or(Client::new()),
|
||||
client: create_http_client(),
|
||||
state_msg: REQUESTING_ACCOUNT_AUTH,
|
||||
failed_msg: "".to_owned(),
|
||||
code_url: None,
|
||||
@@ -169,7 +169,7 @@ impl OidcSession {
|
||||
id: &str,
|
||||
uuid: &str,
|
||||
) -> ResultType<HbbHttpResponse<AuthBody>> {
|
||||
let url = reqwest::Url::parse_with_params(
|
||||
let url = Url::parse_with_params(
|
||||
&format!("{}/api/oidc/auth-query", api_server),
|
||||
&[("code", code), ("id", id), ("uuid", uuid)],
|
||||
)?;
|
||||
|
||||
@@ -1,43 +1,74 @@
|
||||
use reqwest::blocking::Client;
|
||||
use hbb_common::config::Config;
|
||||
use hbb_common::log::info;
|
||||
use hbb_common::proxy::{Proxy, ProxyScheme};
|
||||
use hbb_common::ResultType;
|
||||
use reqwest::blocking::Client as SyncClient;
|
||||
use reqwest::Client as AsyncClient;
|
||||
|
||||
pub fn create_client() -> ResultType<Client> {
|
||||
let mut builder = Client::builder();
|
||||
if let Some(conf) = Config::get_socks() {
|
||||
info!("Create an http request client with proxy forwarding");
|
||||
let proxy = Proxy::form_conf(&conf, None)?;
|
||||
// 根据不同的代理类型设置代理
|
||||
match &proxy.intercept {
|
||||
ProxyScheme::Http { host, .. } => {
|
||||
let proxy = reqwest::Proxy::http(host)?;
|
||||
builder = builder.proxy(proxy);
|
||||
}
|
||||
ProxyScheme::Https { host, .. } => {
|
||||
let proxy = reqwest::Proxy::https(host)?;
|
||||
builder = builder.proxy(proxy);
|
||||
}
|
||||
ProxyScheme::Socks5 { addr, .. } => {
|
||||
// 使用socks5代理
|
||||
let proxy = reqwest::Proxy::all(&format!("socks5://{}", addr))?;
|
||||
builder = builder.proxy(proxy);
|
||||
}
|
||||
}
|
||||
macro_rules! configure_http_client {
|
||||
($builder:expr, $Client: ty) => {{
|
||||
let mut builder = $builder;
|
||||
let client = if let Some(conf) = Config::get_socks() {
|
||||
info!("Create an http request client with proxy forwarding");
|
||||
let proxy_result = Proxy::from_conf(&conf, None);
|
||||
|
||||
// 如果有认证信息,添加Basic认证头
|
||||
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());
|
||||
}
|
||||
}
|
||||
match proxy_result {
|
||||
Ok(proxy) => {
|
||||
let proxy_setup = match &proxy.intercept {
|
||||
ProxyScheme::Http { host, .. } => reqwest::Proxy::http(host),
|
||||
ProxyScheme::Https { host, .. } => reqwest::Proxy::https(host),
|
||||
ProxyScheme::Socks5 { addr, .. } => {
|
||||
reqwest::Proxy::all(&format!("socks5://{}", addr))
|
||||
}
|
||||
};
|
||||
|
||||
Ok(builder.build()?)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::hbbs_http::create_http_client;
|
||||
use bytes::Bytes;
|
||||
use hbb_common::{bail, config::Config, lazy_static, log, ResultType};
|
||||
use reqwest::blocking::{Body, Client};
|
||||
@@ -10,7 +11,6 @@ use std::{
|
||||
sync::{mpsc::Receiver, Arc, Mutex},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use crate::hbbs_http::create_client;
|
||||
|
||||
const MAX_HEADER_LEN: usize = 1024;
|
||||
const SHOULD_SEND_TIME: Duration = Duration::from_secs(1);
|
||||
@@ -26,7 +26,7 @@ pub fn is_enable() -> bool {
|
||||
|
||||
pub fn run(rx: Receiver<RecordState>) {
|
||||
let mut uploader = RecordUploader {
|
||||
client: create_client().unwrap_or(Client::new()),
|
||||
client: create_http_client(),
|
||||
api_server: crate::get_api_server(
|
||||
Config::get_option("api-server"),
|
||||
Config::get_option("custom-rendezvous-server"),
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use super::*;
|
||||
use crate::hbbs_http::create_http_client;
|
||||
use crate::{
|
||||
flutter::{self, APP_TYPE_CM, APP_TYPE_MAIN, SESSIONS},
|
||||
ui_interface::get_api_server,
|
||||
};
|
||||
use hbb_common::{lazy_static, log, message_proto::PluginRequest};
|
||||
use reqwest::blocking::Client;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
use std::{
|
||||
@@ -13,8 +15,6 @@ use std::{
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
use reqwest::blocking::Client;
|
||||
use crate::hbbs_http::create_client;
|
||||
|
||||
const MSG_TO_RUSTDESK_TARGET: &str = "rustdesk";
|
||||
const MSG_TO_PEER_TARGET: &str = "peer";
|
||||
@@ -282,7 +282,7 @@ fn request_plugin_sign(id: String, msg_to_rustdesk: MsgToRustDesk) -> PluginRetu
|
||||
);
|
||||
thread::spawn(move || {
|
||||
let sign_url = format!("{}/lic/web/api/plugin-sign", get_api_server());
|
||||
let client = create_client().unwrap_or(Client::new());
|
||||
let client = create_http_client();
|
||||
let req = PluginSignReq {
|
||||
plugin_id: id.clone(),
|
||||
version: signature_data.version,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
use super::{desc::Meta as PluginMeta, ipc::InstallStatus, *};
|
||||
use crate::flutter;
|
||||
use crate::hbbs_http::create_http_client;
|
||||
use hbb_common::{allow_err, bail, log, tokio, toml};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
@@ -67,7 +68,7 @@ fn get_source_plugins() -> HashMap<String, PluginInfo> {
|
||||
let mut plugins = HashMap::new();
|
||||
for source in get_plugin_source_list().into_iter() {
|
||||
let url = format!("{}/meta.toml", source.url);
|
||||
match reqwest::blocking::get(&url) {
|
||||
match create_http_client().get(&url).send() {
|
||||
Ok(resp) => {
|
||||
if !resp.status().is_success() {
|
||||
log::error!(
|
||||
@@ -441,6 +442,7 @@ fn update_uninstall_id_set(set: HashSet<String>) -> ResultType<()> {
|
||||
// install process
|
||||
pub(super) mod install {
|
||||
use super::IPC_PLUGIN_POSTFIX;
|
||||
use crate::hbbs_http::create_http_client;
|
||||
use crate::{
|
||||
ipc::{connect, Data},
|
||||
plugin::ipc::{InstallStatus, Plugin},
|
||||
@@ -469,7 +471,7 @@ pub(super) mod install {
|
||||
}
|
||||
|
||||
fn download_to_file(url: &str, file: File) -> ResultType<()> {
|
||||
let resp = match reqwest::blocking::get(url) {
|
||||
let resp = match create_http_client().get(url).send() {
|
||||
Ok(resp) => resp,
|
||||
Err(e) => {
|
||||
bail!("get plugin from '{}', {}", url, e);
|
||||
|
||||
@@ -9,6 +9,8 @@ use std::{
|
||||
|
||||
use uuid::Uuid;
|
||||
|
||||
use hbb_common::log::info;
|
||||
use hbb_common::proxy::Proxy;
|
||||
use hbb_common::{
|
||||
allow_err,
|
||||
anyhow::{self, bail},
|
||||
@@ -28,8 +30,6 @@ use hbb_common::{
|
||||
udp::FramedSocket,
|
||||
AddrMangle, IntoTargetAddr, ResultType, TargetAddr,
|
||||
};
|
||||
use hbb_common::log::info;
|
||||
use hbb_common::proxy::Proxy;
|
||||
|
||||
use crate::{
|
||||
check_port,
|
||||
@@ -392,12 +392,12 @@ impl RendezvousMediator {
|
||||
info!("start rendezvous mediator of {}", host);
|
||||
//If the investment agent type is http or https, then tcp forwarding is enabled.
|
||||
let is_http_proxy = if let Some(conf) = Config::get_socks() {
|
||||
let proxy = Proxy::form_conf(&conf, None)?;
|
||||
let proxy = Proxy::from_conf(&conf, None)?;
|
||||
proxy.is_http_or_https()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if (cfg!(debug_assertions) && option_env!("TEST_TCP").is_some()) || is_http_proxy {
|
||||
if (cfg!(debug_assertions) && option_env!("TEST_TCP").is_some()) || is_http_proxy {
|
||||
Self::start_tcp(server, host).await
|
||||
} else {
|
||||
Self::start_udp(server, host).await
|
||||
|
||||
Reference in New Issue
Block a user