add: Http proxy request client

This commit is contained in:
yuluo
2024-04-13 14:49:19 +08:00
Unverified
parent 726f419172
commit c3b8621554
7 changed files with 61 additions and 9 deletions

View File

@@ -6,6 +6,8 @@ use serde_json::{Map, Value};
pub mod account;
pub mod record_upload;
pub mod sync;
mod http_client;
pub use http_client::create_client;
#[derive(Debug)]
pub enum HbbHttpResponse<T> {

View File

@@ -9,6 +9,7 @@ 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()));
@@ -130,7 +131,7 @@ impl Default for UserStatus {
impl OidcSession {
fn new() -> Self {
Self {
client: Client::new(),
client: create_client().unwrap_or(Client::new()),
state_msg: REQUESTING_ACCOUNT_AUTH,
failed_msg: "".to_owned(),
code_url: None,

View File

@@ -0,0 +1,43 @@
use reqwest::blocking::Client;
use hbb_common::config::Config;
use hbb_common::log::info;
use hbb_common::proxy::{Proxy, ProxyScheme};
use hbb_common::ResultType;
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);
}
}
// 如果有认证信息添加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());
}
}
Ok(builder.build()?)
}

View File

@@ -10,6 +10,7 @@ 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);
@@ -25,7 +26,7 @@ pub fn is_enable() -> bool {
pub fn run(rx: Receiver<RecordState>) {
let mut uploader = RecordUploader {
client: Client::new(),
client: create_client().unwrap_or(Client::new()),
api_server: crate::get_api_server(
Config::get_option("api-server"),
Config::get_option("custom-rendezvous-server"),

View File

@@ -13,6 +13,8 @@ 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";
@@ -280,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 = reqwest::blocking::Client::new();
let client = create_client().unwrap_or(Client::new());
let req = PluginSignReq {
plugin_id: id.clone(),
version: signature_data.version,