From 5b5b27010928d398aa20977c56ebc57e30416c99 Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Sun, 9 Feb 2025 04:51:23 +0800 Subject: [PATCH 1/3] build(deps): macOS platform dependency is minimized --- Cargo.toml | 8 ++++++-- src/proxy.rs | 16 +++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 335d7a10..cda2b9a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"] targets = ["x86_64-unknown-linux-gnu"] [features] -default = ["webpki-roots", "charset"] +default = ["webpki-roots", "charset", "macos-system-configuration"] full = [ "json", @@ -60,8 +60,12 @@ stream = ["tokio/fs", "dep:tokio-util"] socks = ["dep:tokio-socks"] native-roots = ["dep:rustls-native-certs"] + webpki-roots = ["dep:webpki-root-certs"] +# Use the system's proxy configuration. +macos-system-configuration = ["dep:system-configuration"] + # Optional enable http2 tracing http2-tracing = ["hyper2/http2-tracing"] @@ -146,7 +150,7 @@ hickory-resolver = { version = "0.24", optional = true } windows-registry = "0.4.0" [target.'cfg(target_os = "macos")'.dependencies] -system-configuration = "0.6.0" +system-configuration = { version = "0.6.0", optional = true } [target.'cfg(any(target_os = "ios", target_os = "visionos", target_os = "macos", target_os = "tvos", target_os = "watchos"))'.dependencies] libc = "0.2" diff --git a/src/proxy.rs b/src/proxy.rs index 799672e3..fd57801a 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -13,7 +13,7 @@ use std::collections::HashMap; use std::env; use std::error::Error; use std::net::IpAddr; -#[cfg(target_os = "macos")] +#[cfg(all(target_os = "macos", feature = "macos-system-configuration"))] use system_configuration::{ core_foundation::{ base::CFType, @@ -963,7 +963,7 @@ fn get_from_platform_impl() -> Result, Box> { Ok((proxy_enable == 1).then_some(proxy_server)) } -#[cfg(target_os = "macos")] +#[cfg(all(target_os = "macos", feature = "macos-system-configuration"))] fn parse_setting_from_dynamic_store( proxies_map: &CFDictionary, enabled_key: CFStringRef, @@ -1001,7 +1001,7 @@ fn parse_setting_from_dynamic_store( None } -#[cfg(target_os = "macos")] +#[cfg(all(target_os = "macos", feature = "macos-system-configuration"))] fn get_from_platform_impl() -> Result, Box> { let store = SCDynamicStoreBuilder::new("rquest").build(); @@ -1032,12 +1032,18 @@ fn get_from_platform_impl() -> Result, Box> { } } -#[cfg(any(target_os = "windows", target_os = "macos"))] +#[cfg(any( + target_os = "windows", + all(target_os = "macos", feature = "macos-system-configuration") +))] fn get_from_platform() -> Option { get_from_platform_impl().ok().flatten() } -#[cfg(not(any(target_os = "windows", target_os = "macos")))] +#[cfg(not(any( + target_os = "windows", + all(target_os = "macos", feature = "macos-system-configuration") +)))] fn get_from_platform() -> Option { None } From 2ed48ac3ed4d7d2874bf63219f11e4e5ca501c40 Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Sun, 9 Feb 2025 05:08:17 +0800 Subject: [PATCH 2/3] build(deps): Apple platform dependencies are minimized as much as possible --- Cargo.toml | 6 +++++- examples/client_chain.rs | 2 +- src/lib.rs | 30 ++++++++++++++++++++---------- src/util/client/connect/http.rs | 15 +++++++++------ 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cda2b9a6..744d04fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,6 +66,9 @@ webpki-roots = ["dep:webpki-root-certs"] # Use the system's proxy configuration. macos-system-configuration = ["dep:system-configuration"] +# Use the Apple platform's network device binding. +apple-bindable-device = ["dep:libc"] + # Optional enable http2 tracing http2-tracing = ["hyper2/http2-tracing"] @@ -153,7 +156,7 @@ windows-registry = "0.4.0" system-configuration = { version = "0.6.0", optional = true } [target.'cfg(any(target_os = "ios", target_os = "visionos", target_os = "macos", target_os = "tvos", target_os = "watchos"))'.dependencies] -libc = "0.2" +libc = { version = "0.2", optional = true } [dev-dependencies] hyper = { version = "1.1.0", default-features = false, features = [ @@ -308,6 +311,7 @@ required-features = ["websocket", "http2-tracing", "futures-util/std"] [[example]] name = "client_chain" path = "examples/client_chain.rs" +required-features = ["apple-bindable-device"] [[example]] name = "request_with_redirect" diff --git a/examples/client_chain.rs b/examples/client_chain.rs index b66e6a45..947b94a5 100644 --- a/examples/client_chain.rs +++ b/examples/client_chain.rs @@ -30,7 +30,7 @@ async fn main() -> Result<(), rquest::Error> { .as_mut() .impersonate(Impersonate::Safari18) .headers_order(HEADER_ORDER) - .interface("utun4") + .interface("utun6") .apply()?; let text = client diff --git a/src/lib.rs b/src/lib.rs index d83067d1..1f7339c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -286,11 +286,16 @@ macro_rules! cfg_bindable_device { target_os = "android", target_os = "fuchsia", target_os = "linux", - target_os = "ios", - target_os = "visionos", - target_os = "macos", - target_os = "tvos", - target_os = "watchos" + all( + feature = "apple-bindable-device", + any( + target_os = "ios", + target_os = "visionos", + target_os = "macos", + target_os = "tvos", + target_os = "watchos", + ) + ) ))] $( $tt @@ -306,11 +311,16 @@ macro_rules! cfg_non_bindable_device { target_os = "android", target_os = "fuchsia", target_os = "linux", - target_os = "ios", - target_os = "visionos", - target_os = "macos", - target_os = "tvos", - target_os = "watchos" + all( + feature = "apple-bindable-device", + any( + target_os = "ios", + target_os = "visionos", + target_os = "macos", + target_os = "tvos", + target_os = "watchos", + ) + ) )))] $( $tt diff --git a/src/util/client/connect/http.rs b/src/util/client/connect/http.rs index e425ea7f..78a9150d 100644 --- a/src/util/client/connect/http.rs +++ b/src/util/client/connect/http.rs @@ -812,12 +812,15 @@ fn connect( .map_err(ConnectError::m("tcp bind interface error"))?; } - #[cfg(any( - target_os = "ios", - target_os = "visionos", - target_os = "macos", - target_os = "tvos", - target_os = "watchos", + #[cfg(all( + feature = "apple-bindable-device", + any( + target_os = "ios", + target_os = "visionos", + target_os = "macos", + target_os = "tvos", + target_os = "watchos", + ) ))] /// That this only supports ios, visionos, macos, tvos, watchos if let Some(interface) = &config.interface { From a2ccc7ac54f6ee00be97ae8f8908a3629758af09 Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Sun, 9 Feb 2025 05:13:36 +0800 Subject: [PATCH 3/3] update --- src/util/client/connect/http.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/util/client/connect/http.rs b/src/util/client/connect/http.rs index 78a9150d..a7c04cf6 100644 --- a/src/util/client/connect/http.rs +++ b/src/util/client/connect/http.rs @@ -81,11 +81,16 @@ struct Config { target_os = "android", target_os = "fuchsia", target_os = "linux", - target_os = "ios", - target_os = "visionos", - target_os = "macos", - target_os = "tvos", - target_os = "watchos", + all( + feature = "apple-bindable-device", + any( + target_os = "ios", + target_os = "visionos", + target_os = "macos", + target_os = "tvos", + target_os = "watchos", + ) + ) ))] interface: Option>, #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))] @@ -246,11 +251,16 @@ impl HttpConnector { target_os = "android", target_os = "fuchsia", target_os = "linux", - target_os = "ios", - target_os = "visionos", - target_os = "macos", - target_os = "tvos", - target_os = "watchos", + all( + feature = "apple-bindable-device", + any( + target_os = "ios", + target_os = "visionos", + target_os = "macos", + target_os = "tvos", + target_os = "watchos", + ) + ) ))] interface: None, #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]