add: Http proxy request client

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

View File

@@ -132,10 +132,10 @@ wallpaper = { git = "https://github.com/21pages/wallpaper.rs" }
[target.'cfg(any(target_os = "macos", target_os = "windows"))'.dependencies] [target.'cfg(any(target_os = "macos", target_os = "windows"))'.dependencies]
# https://github.com/rustdesk/rustdesk-server-pro/issues/189, using native-tls for better tls support # https://github.com/rustdesk/rustdesk-server-pro/issues/189, using native-tls for better tls support
reqwest = { git = "https://github.com/rustdesk-org/reqwest", features = ["blocking", "json", "native-tls", "gzip"], default-features=false } reqwest = { git = "https://github.com/rustdesk-org/reqwest", features = ["blocking", "socks", "json", "native-tls", "gzip"], default-features=false }
[target.'cfg(not(any(target_os = "macos", target_os = "windows")))'.dependencies] [target.'cfg(not(any(target_os = "macos", target_os = "windows")))'.dependencies]
reqwest = { git = "https://github.com/rustdesk-org/reqwest", features = ["blocking", "json", "rustls-tls", "rustls-tls-native-roots", "gzip"], default-features=false } reqwest = { git = "https://github.com/rustdesk-org/reqwest", features = ["blocking", "socks", "json", "rustls-tls", "rustls-tls-native-roots", "gzip"], default-features=false }
[target.'cfg(target_os = "linux")'.dependencies] [target.'cfg(target_os = "linux")'.dependencies]
psimple = { package = "libpulse-simple-binding", version = "2.27" } psimple = { package = "libpulse-simple-binding", version = "2.27" }

View File

@@ -111,9 +111,12 @@ pub struct Auth {
impl Auth { impl Auth {
fn get_proxy_authorization(&self) -> String { fn get_proxy_authorization(&self) -> String {
format!("Proxy-Authorization: Basic {}\r\n", self.get_basic_authorization())
}
pub fn get_basic_authorization(&self) -> String {
let authorization = format!("{}:{}", &self.user_name, &self.password); let authorization = format!("{}:{}", &self.user_name, &self.password);
let authorization = general_purpose::STANDARD.encode(authorization.as_bytes()); general_purpose::STANDARD.encode(authorization.as_bytes())
format!("Proxy-Authorization: Basic {}\r\n", authorization)
} }
} }
@@ -135,7 +138,7 @@ pub enum ProxyScheme {
} }
impl ProxyScheme { impl ProxyScheme {
fn maybe_auth(&self) -> Option<&Auth> { pub fn maybe_auth(&self) -> Option<&Auth> {
match self { match self {
ProxyScheme::Http { auth, .. } | ProxyScheme::Http { auth, .. } |
ProxyScheme::Https { auth, .. } | ProxyScheme::Https { auth, .. } |
@@ -308,7 +311,7 @@ impl IntoProxyScheme for ProxyScheme {
#[derive(Clone)] #[derive(Clone)]
pub struct Proxy { pub struct Proxy {
intercept: ProxyScheme, pub intercept: ProxyScheme,
ms_timeout: u64, ms_timeout: u64,
} }

View File

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

View File

@@ -9,6 +9,7 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use url::Url; use url::Url;
use crate::hbbs_http::create_client;
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref OIDC_SESSION: Arc<RwLock<OidcSession>> = Arc::new(RwLock::new(OidcSession::new())); static ref OIDC_SESSION: Arc<RwLock<OidcSession>> = Arc::new(RwLock::new(OidcSession::new()));
@@ -130,7 +131,7 @@ impl Default for UserStatus {
impl OidcSession { impl OidcSession {
fn new() -> Self { fn new() -> Self {
Self { Self {
client: Client::new(), client: create_client().unwrap_or(Client::new()),
state_msg: REQUESTING_ACCOUNT_AUTH, state_msg: REQUESTING_ACCOUNT_AUTH,
failed_msg: "".to_owned(), failed_msg: "".to_owned(),
code_url: None, 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}, sync::{mpsc::Receiver, Arc, Mutex},
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use crate::hbbs_http::create_client;
const MAX_HEADER_LEN: usize = 1024; const MAX_HEADER_LEN: usize = 1024;
const SHOULD_SEND_TIME: Duration = Duration::from_secs(1); const SHOULD_SEND_TIME: Duration = Duration::from_secs(1);
@@ -25,7 +26,7 @@ pub fn is_enable() -> bool {
pub fn run(rx: Receiver<RecordState>) { pub fn run(rx: Receiver<RecordState>) {
let mut uploader = RecordUploader { let mut uploader = RecordUploader {
client: Client::new(), client: create_client().unwrap_or(Client::new()),
api_server: crate::get_api_server( api_server: crate::get_api_server(
Config::get_option("api-server"), Config::get_option("api-server"),
Config::get_option("custom-rendezvous-server"), Config::get_option("custom-rendezvous-server"),

View File

@@ -13,6 +13,8 @@ use std::{
thread, thread,
time::Duration, time::Duration,
}; };
use reqwest::blocking::Client;
use crate::hbbs_http::create_client;
const MSG_TO_RUSTDESK_TARGET: &str = "rustdesk"; const MSG_TO_RUSTDESK_TARGET: &str = "rustdesk";
const MSG_TO_PEER_TARGET: &str = "peer"; 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 || { thread::spawn(move || {
let sign_url = format!("{}/lic/web/api/plugin-sign", get_api_server()); 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 { let req = PluginSignReq {
plugin_id: id.clone(), plugin_id: id.clone(),
version: signature_data.version, version: signature_data.version,