From 4e5f01a971f2e35ad8d96fd5d191f577785c9425 Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Mon, 13 Jan 2025 12:41:52 +0800 Subject: [PATCH 1/3] style: format code style --- src/client/http.rs | 64 ++++++++++++++++---------------------- src/proxy.rs | 2 +- src/util/client/network.rs | 4 +-- 3 files changed, 29 insertions(+), 41 deletions(-) diff --git a/src/client/http.rs b/src/client/http.rs index beff66e9..2b4b6ff6 100644 --- a/src/client/http.rs +++ b/src/client/http.rs @@ -117,6 +117,7 @@ struct Config { http2_max_retry_count: usize, tls_info: bool, connector_layers: Vec, + http2: Option, tls: TlsSettings, } @@ -170,11 +171,12 @@ impl ClientBuilder { dns_overrides: HashMap::new(), dns_resolver: None, base_url: None, - builder: crate::util::client::Client::builder(TokioExecutor::new()), + builder: util::client::Client::builder(TokioExecutor::new()), https_only: false, http2_max_retry_count: 2, tls_info: false, connector_layers: Vec::new(), + http2: None, tls: Default::default(), }, } @@ -237,7 +239,12 @@ impl ClientBuilder { .pool_timer(TokioTimer::new()) .pool_idle_timeout(config.pool_idle_timeout) .pool_max_idle_per_host(config.pool_max_idle_per_host) - .pool_max_size(config.pool_max_size); + .pool_max_size(config.pool_max_size) + .with_http2_builder(|builder| { + if let Some(http2) = config.http2 { + apply_http2_settings(builder, http2) + } + }); let hyper = config .builder @@ -935,24 +942,12 @@ impl ClientBuilder { /// Apply the given TLS settings and header function. fn apply_impersonate_settings(mut self, mut settings: ImpersonateSettings) -> ClientBuilder { - // Set the headers if needed if let Some(mut headers) = settings.headers { std::mem::swap(&mut self.config.headers, &mut headers); } - - // Set the headers order if needed std::mem::swap(&mut self.config.headers_order, &mut settings.headers_order); - - // Set the TLS settings std::mem::swap(&mut self.config.tls, &mut settings.tls); - - // Set the http2 preference - if let Some(http2) = settings.http2 { - self.config - .builder - .with_http2_builder(|builder| apply_http2_settings(builder, http2)); - } - + std::mem::swap(&mut self.config.http2, &mut settings.http2); self } @@ -1504,7 +1499,7 @@ impl Client { /// Set the cookie provider for this client. #[cfg(feature = "cookies")] - pub fn set_cookie_provider(&mut self, cookie_store: Arc) -> &mut Self + pub fn set_cookie_provider(&mut self, cookie_store: Arc) -> &mut Client where C: cookie::CookieStore + 'static, { @@ -1512,13 +1507,12 @@ impl Client { &mut self.inner_mut().cookie_store, &mut Some(cookie_store as _), ); - self } /// Set the proxies for this client. #[inline] - pub fn set_proxies

(&mut self, proxies: P) -> &mut Self + pub fn set_proxies

(&mut self, proxies: P) -> &mut Client where P: Into>>, { @@ -1533,7 +1527,6 @@ impl Client { inner.proxies.clear(); } } - self } @@ -1543,7 +1536,7 @@ impl Client { /// /// Default is `None`. #[inline] - pub fn set_local_address(&mut self, addr: T) -> &mut Self + pub fn set_local_address(&mut self, addr: T) -> &mut Client where T: Into>, { @@ -1554,7 +1547,7 @@ impl Client { /// Set that all sockets are bound to the configured IPv4 or IPv6 address /// (depending on host's preferences) before connection. #[inline] - pub fn set_local_addresses(&mut self, ipv4: V4, ipv6: V6) -> &mut Self + pub fn set_local_addresses(&mut self, ipv4: V4, ipv6: V6) -> &mut Client where V4: Into>, V6: Into>, @@ -1566,7 +1559,7 @@ impl Client { cfg_bindable_device! { /// Bind to an interface by `SO_BINDTODEVICE`. #[inline] - pub fn set_interface(&mut self, interface: T) -> &mut Self + pub fn set_interface(&mut self, interface: T) -> &mut Client where T: Into>, { @@ -1576,7 +1569,7 @@ impl Client { } /// Set the headers order for this client. - pub fn set_headers_order(&mut self, order: T) -> &mut Self + pub fn set_headers_order(&mut self, order: T) -> &mut Client where T: Into>, { @@ -1585,29 +1578,28 @@ impl Client { } /// Set the redirect policy for this client. - pub fn set_redirect(&mut self, mut policy: redirect::Policy) -> &mut Self { + pub fn set_redirect(&mut self, mut policy: redirect::Policy) -> &mut Client { std::mem::swap(&mut self.inner_mut().redirect, &mut policy); self } /// Set the cross-origin proxy authorization for this client. - pub fn set_redirect_with_proxy_auth(&mut self, enabled: bool) -> &mut Self { + pub fn set_redirect_with_proxy_auth(&mut self, enabled: bool) -> &mut Client { self.inner_mut().redirect_with_proxy_auth = enabled; self } /// Set the bash url for this client. - pub fn set_base_url(&mut self, url: U) -> &mut Self { + pub fn set_base_url(&mut self, url: U) -> &mut Client { if let Ok(url) = url.into_url() { std::mem::swap(&mut self.inner_mut().base_url, &mut Some(url)); } - self } /// Set the impersonate for this client. #[inline] - pub fn set_impersonate(&mut self, var: Impersonate) -> crate::Result<&mut Self> { + pub fn set_impersonate(&mut self, var: Impersonate) -> crate::Result<&mut Client> { let settings = mimic::impersonate(var, true, ImpersonateOs::default()); self.apply_impersonate_settings(settings) } @@ -1618,14 +1610,14 @@ impl Client { &mut self, var: Impersonate, os: ImpersonateOs, - ) -> crate::Result<&mut Self> { + ) -> crate::Result<&mut Client> { let settings = mimic::impersonate(var, true, os); self.apply_impersonate_settings(settings) } /// Set the impersonate for this client skip setting the headers. #[inline] - pub fn set_impersonate_skip_headers(&mut self, var: Impersonate) -> crate::Result<&mut Self> { + pub fn set_impersonate_skip_headers(&mut self, var: Impersonate) -> crate::Result<&mut Client> { let settings = mimic::impersonate(var, false, ImpersonateOs::default()); self.apply_impersonate_settings(settings) } @@ -1636,7 +1628,7 @@ impl Client { &mut self, var: Impersonate, os: ImpersonateOs, - ) -> crate::Result<&mut Self> { + ) -> crate::Result<&mut Client> { let settings = mimic::impersonate(var, false, os); self.apply_impersonate_settings(settings) } @@ -1647,7 +1639,7 @@ impl Client { pub fn set_impersonate_settings( &mut self, settings: ImpersonateSettings, - ) -> crate::Result<&mut Self> { + ) -> crate::Result<&mut Client> { self.apply_impersonate_settings(settings) } @@ -1656,22 +1648,18 @@ impl Client { fn apply_impersonate_settings( &mut self, mut settings: ImpersonateSettings, - ) -> crate::Result<&mut Self> { + ) -> crate::Result<&mut Client> { let inner = self.inner_mut(); - - // Set the headers + if let Some(mut headers) = settings.headers { std::mem::swap(&mut inner.headers, &mut headers); } - // Set the headers order if needed std::mem::swap(&mut inner.headers_order, &mut settings.headers_order); - // Set the connector let connector = BoringTlsConnector::new(settings.tls)?; inner.hyper.with_connector(|c| c.set_connector(connector)); - // Set the http2 preference if let Some(http2) = settings.http2 { inner .hyper diff --git a/src/proxy.rs b/src/proxy.rs index 5e8d63a8..78493e21 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -376,7 +376,7 @@ impl Proxy { let in_no_proxy = self .no_proxy .as_ref() - .map_or(false, |np| np.contains(uri.host())); + .is_some_and(|np| np.contains(uri.host())); match self.intercept { Intercept::All(ref u) => { if !in_no_proxy { diff --git a/src/util/client/network.rs b/src/util/client/network.rs index c07b5ac9..c5f9e66e 100644 --- a/src/util/client/network.rs +++ b/src/util/client/network.rs @@ -102,10 +102,10 @@ impl NetworkScheme { #[inline] pub fn take_interface(&mut self) -> Option> { { - return match self { + match self { NetworkScheme::Scheme { interface, .. } => interface.take(), _ => None, - }; + } } } } From a161769a48d8a6ab0e8237fd9e64c563cae5cb4c Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Mon, 13 Jan 2025 12:46:46 +0800 Subject: [PATCH 2/3] update docs --- src/client/http.rs | 94 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 9 deletions(-) diff --git a/src/client/http.rs b/src/client/http.rs index 2b4b6ff6..1c9a0744 100644 --- a/src/client/http.rs +++ b/src/client/http.rs @@ -898,14 +898,33 @@ impl ClientBuilder { // TLS/HTTP2 mimic options - /// Sets the necessary values to mimic the specified impersonate version, including headers and TLS settings. + /// Configures the client to mimic the specified impersonate version. + /// This includes setting the necessary headers and TLS settings. + /// + /// # Arguments + /// + /// * `impersonate` - The impersonate version to mimic. + /// + /// # Returns + /// + /// A `ClientBuilder` instance with the applied settings. #[inline] pub fn impersonate(self, impersonate: Impersonate) -> ClientBuilder { let settings = mimic::impersonate(impersonate, true, ImpersonateOs::default()); self.apply_impersonate_settings(settings) } - /// Sets the necessary values to mimic the specified impersonate version, including headers, TLS settings and OS. + /// Configures the client to mimic the specified impersonate version with a specific OS. + /// This includes setting the necessary headers, TLS settings, and OS. + /// + /// # Arguments + /// + /// * `impersonate` - The impersonate version to mimic. + /// * `impersonate_os` - The OS to mimic. + /// + /// # Returns + /// + /// A `ClientBuilder` instance with the applied settings. #[inline] pub fn impersonate_with_os( self, @@ -916,14 +935,33 @@ impl ClientBuilder { self.apply_impersonate_settings(settings) } - /// Sets the necessary values to mimic the specified impersonate version, skipping header configuration. + /// Configures the client to mimic the specified impersonate version, skipping header configuration. + /// This includes setting the necessary TLS settings. + /// + /// # Arguments + /// + /// * `impersonate` - The impersonate version to mimic. + /// + /// # Returns + /// + /// A `ClientBuilder` instance with the applied settings. #[inline] pub fn impersonate_skip_headers(self, impersonate: Impersonate) -> ClientBuilder { let settings = mimic::impersonate(impersonate, false, ImpersonateOs::default()); self.apply_impersonate_settings(settings) } - /// Sets the necessary values to mimic the specified impersonate version, with os and skipping header configuration. + /// Configures the client to mimic the specified impersonate version with a specific OS, skipping header configuration. + /// This includes setting the necessary TLS settings and OS. + /// + /// # Arguments + /// + /// * `impersonate` - The impersonate version to mimic. + /// * `impersonate_os` - The OS to mimic. + /// + /// # Returns + /// + /// A `ClientBuilder` instance with the applied settings. #[inline] pub fn impersonate_with_os_skip_headers( self, @@ -1597,14 +1635,33 @@ impl Client { self } - /// Set the impersonate for this client. + /// Set the impersonate version for this client. + /// This includes setting the necessary headers and TLS settings. + /// + /// # Arguments + /// + /// * `var` - The impersonate version to set. + /// + /// # Returns + /// + /// A mutable reference to the `Client` instance with the applied settings. #[inline] pub fn set_impersonate(&mut self, var: Impersonate) -> crate::Result<&mut Client> { let settings = mimic::impersonate(var, true, ImpersonateOs::default()); self.apply_impersonate_settings(settings) } - /// Set the impersonate with os for this client. + /// Set the impersonate version with a specific OS for this client. + /// This includes setting the necessary headers, TLS settings, and OS. + /// + /// # Arguments + /// + /// * `var` - The impersonate version to set. + /// * `os` - The OS to set. + /// + /// # Returns + /// + /// A mutable reference to the `Client` instance with the applied settings. #[inline] pub fn set_impersonate_with_os( &mut self, @@ -1615,14 +1672,33 @@ impl Client { self.apply_impersonate_settings(settings) } - /// Set the impersonate for this client skip setting the headers. + /// Set the impersonate version for this client, skipping header configuration. + /// This includes setting the necessary TLS settings. + /// + /// # Arguments + /// + /// * `var` - The impersonate version to set. + /// + /// # Returns + /// + /// A mutable reference to the `Client` instance with the applied settings. #[inline] pub fn set_impersonate_skip_headers(&mut self, var: Impersonate) -> crate::Result<&mut Client> { let settings = mimic::impersonate(var, false, ImpersonateOs::default()); self.apply_impersonate_settings(settings) } - /// Set the impersonate for this client skip setting the headers. + /// Set the impersonate version with a specific OS for this client, skipping header configuration. + /// This includes setting the necessary TLS settings and OS. + /// + /// # Arguments + /// + /// * `var` - The impersonate version to set. + /// * `os` - The OS to set. + /// + /// # Returns + /// + /// A mutable reference to the `Client` instance with the applied settings. #[inline] pub fn set_impersonate_with_os_skip_headers( &mut self, @@ -1650,7 +1726,7 @@ impl Client { mut settings: ImpersonateSettings, ) -> crate::Result<&mut Client> { let inner = self.inner_mut(); - + if let Some(mut headers) = settings.headers { std::mem::swap(&mut inner.headers, &mut headers); } From 63d35408dff2a794c4d053e642d7b477c8383d4b Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Mon, 13 Jan 2025 13:12:29 +0800 Subject: [PATCH 3/3] update --- src/client/http.rs | 16 ++++++++-------- src/mimic/chrome.rs | 2 +- src/mimic/firefox.rs | 2 +- src/mimic/mod.rs | 4 ++-- src/mimic/okhttp.rs | 2 +- src/mimic/safari.rs | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/client/http.rs b/src/client/http.rs index 1c9a0744..11cdb5a3 100644 --- a/src/client/http.rs +++ b/src/client/http.rs @@ -910,7 +910,7 @@ impl ClientBuilder { /// A `ClientBuilder` instance with the applied settings. #[inline] pub fn impersonate(self, impersonate: Impersonate) -> ClientBuilder { - let settings = mimic::impersonate(impersonate, true, ImpersonateOs::default()); + let settings = mimic::impersonate(impersonate, ImpersonateOs::default(), true); self.apply_impersonate_settings(settings) } @@ -931,7 +931,7 @@ impl ClientBuilder { impersonate: Impersonate, impersonate_os: ImpersonateOs, ) -> ClientBuilder { - let settings = mimic::impersonate(impersonate, true, impersonate_os); + let settings = mimic::impersonate(impersonate, impersonate_os, true); self.apply_impersonate_settings(settings) } @@ -947,7 +947,7 @@ impl ClientBuilder { /// A `ClientBuilder` instance with the applied settings. #[inline] pub fn impersonate_skip_headers(self, impersonate: Impersonate) -> ClientBuilder { - let settings = mimic::impersonate(impersonate, false, ImpersonateOs::default()); + let settings = mimic::impersonate(impersonate, ImpersonateOs::default(), false); self.apply_impersonate_settings(settings) } @@ -968,7 +968,7 @@ impl ClientBuilder { impersonate: Impersonate, impersonate_os: ImpersonateOs, ) -> ClientBuilder { - let settings = mimic::impersonate(impersonate, false, impersonate_os); + let settings = mimic::impersonate(impersonate, impersonate_os, false); self.apply_impersonate_settings(settings) } @@ -1647,7 +1647,7 @@ impl Client { /// A mutable reference to the `Client` instance with the applied settings. #[inline] pub fn set_impersonate(&mut self, var: Impersonate) -> crate::Result<&mut Client> { - let settings = mimic::impersonate(var, true, ImpersonateOs::default()); + let settings = mimic::impersonate(var, ImpersonateOs::default(), true); self.apply_impersonate_settings(settings) } @@ -1668,7 +1668,7 @@ impl Client { var: Impersonate, os: ImpersonateOs, ) -> crate::Result<&mut Client> { - let settings = mimic::impersonate(var, true, os); + let settings = mimic::impersonate(var, os, true); self.apply_impersonate_settings(settings) } @@ -1684,7 +1684,7 @@ impl Client { /// A mutable reference to the `Client` instance with the applied settings. #[inline] pub fn set_impersonate_skip_headers(&mut self, var: Impersonate) -> crate::Result<&mut Client> { - let settings = mimic::impersonate(var, false, ImpersonateOs::default()); + let settings = mimic::impersonate(var, ImpersonateOs::default(), false); self.apply_impersonate_settings(settings) } @@ -1705,7 +1705,7 @@ impl Client { var: Impersonate, os: ImpersonateOs, ) -> crate::Result<&mut Client> { - let settings = mimic::impersonate(var, false, os); + let settings = mimic::impersonate(var, os, false); self.apply_impersonate_settings(settings) } diff --git a/src/mimic/chrome.rs b/src/mimic/chrome.rs index 133549da..08f6080a 100644 --- a/src/mimic/chrome.rs +++ b/src/mimic/chrome.rs @@ -14,7 +14,7 @@ macro_rules! mod_generator { use super::*; #[inline(always)] - pub fn settings(with_headers: bool, os_choice: ImpersonateOs) -> ImpersonateSettings { + pub fn settings(os_choice: ImpersonateOs, with_headers: bool) -> ImpersonateSettings { #[allow(unreachable_patterns)] match os_choice { $( diff --git a/src/mimic/firefox.rs b/src/mimic/firefox.rs index 79a2d277..0263c8c3 100644 --- a/src/mimic/firefox.rs +++ b/src/mimic/firefox.rs @@ -14,7 +14,7 @@ macro_rules! mod_generator { use super::*; #[inline(always)] - pub fn settings(with_headers: bool, os_choice: ImpersonateOs) -> ImpersonateSettings { + pub fn settings(os_choice: ImpersonateOs, with_headers: bool) -> ImpersonateSettings { #[allow(unreachable_patterns)] match os_choice { $( diff --git a/src/mimic/mod.rs b/src/mimic/mod.rs index b64c0831..1173f8d2 100644 --- a/src/mimic/mod.rs +++ b/src/mimic/mod.rs @@ -65,13 +65,13 @@ pub struct ImpersonateSettings { #[inline] pub fn impersonate( ver: Impersonate, - with_headers: bool, impersonate_os: ImpersonateOs, + with_headers: bool, ) -> ImpersonateSettings { impersonate_match!( ver, - with_headers, impersonate_os, + with_headers, Chrome100 => v100::settings, Chrome101 => v101::settings, Chrome104 => v104::settings, diff --git a/src/mimic/okhttp.rs b/src/mimic/okhttp.rs index cd2c30ff..569465d0 100644 --- a/src/mimic/okhttp.rs +++ b/src/mimic/okhttp.rs @@ -8,7 +8,7 @@ macro_rules! mod_generator { use super::*; #[inline(always)] - pub fn settings(with_headers: bool, _os: ImpersonateOs) -> ImpersonateSettings { + pub fn settings(_: ImpersonateOs, with_headers: bool) -> ImpersonateSettings { ImpersonateSettings::builder() .tls(tls_settings!($cipher_list)) .http2(http2_settings!()) diff --git a/src/mimic/safari.rs b/src/mimic/safari.rs index 0d8f71db..15161c9d 100644 --- a/src/mimic/safari.rs +++ b/src/mimic/safari.rs @@ -8,7 +8,7 @@ macro_rules! mod_generator { use super::*; #[inline(always)] - pub fn settings(with_headers: bool, _os: ImpersonateOs) -> ImpersonateSettings { + pub fn settings(_: ImpersonateOs, with_headers: bool) -> ImpersonateSettings { ImpersonateSettings::builder() .tls($tls_settings) .http2($http2_settings)