From 310ec6821ddfdb2263d2eac5876e032bc7674c13 Mon Sep 17 00:00:00 2001 From: shellrow Date: Sun, 26 Jul 2026 00:22:38 +0900 Subject: [PATCH] fix: make macOS Wi-Fi transmit speed opt-in --- Cargo.toml | 7 ++++--- README.md | 14 ++++++++++++++ src/interface/interface.rs | 3 +++ src/os/macos/interface.rs | 10 +++++----- src/os/macos/mod.rs | 1 + 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b077a92..1e4c176 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,9 +44,9 @@ objc2-system-configuration = { version = "0.3", default-features = false, featur plist = "1.8" [target.'cfg(target_os = "macos")'.dependencies] -objc2 = { version = "0.6" } -objc2-core-wlan = { version = "0.3" } -objc2-foundation = { version = "0.3" } +objc2 = { version = "0.6", optional = true } +objc2-core-wlan = { version = "0.3", optional = true } +objc2-foundation = { version = "0.3", optional = true } [target.'cfg(target_os = "ios")'.dependencies] dispatch2 = "0.3" @@ -60,6 +60,7 @@ default = ["gateway", "apple-system-configuration-extra", "android-extra"] serde = ["dep:serde", "mac-addr/serde", "ipnet/serde"] gateway = [] android-extra = ["dep:jni", "dep:ndk-context"] +apple-wifi-extra = ["dep:objc2", "dep:objc2-core-wlan", "dep:objc2-foundation"] apple-system-configuration-extra = [ "objc2-system-configuration/SCPreferences", "objc2-system-configuration/SCDynamicStore", diff --git a/README.md b/README.md index 0989834..f1bd20c 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,9 @@ For more details, see [examples][examples-url] or [doc][doc-url]. - `android-extra` (default) - Enables deeper Android metadata enrichment using Android platform APIs through JNI bindings. - On Android, this can add metadata such as traffic stats, DNS servers, DHCP hints, and Wi-Fi link speed when the app provides the required Android context and permissions. +- `apple-wifi-extra` + - Enables macOS Wi-Fi transmit-speed collection through CoreWLAN. + - This feature is disabled by default because CoreWLAN metadata collection may perform synchronous IPC during interface enumeration. To opt out of the additional Apple metadata enrichment while keeping gateway helpers: @@ -65,6 +68,17 @@ netdev = { version = "0.45", default-features = false, features = ["gateway"] } `netdev` links `SystemConfiguration.framework` automatically on `macOS` and `iOS` through its build script. If your app is ultimately linked by Xcode, you may still need to add `SystemConfiguration.framework` to the app target manually. +macOS Wi-Fi transmit-speed collection is disabled by default. To enable it, opt in to the +`apple-wifi-extra` feature: + +```toml +[dependencies] +netdev = { version = "0.45", features = ["apple-wifi-extra"] } +``` + +This uses CoreWLAN, whose metadata lookup may perform synchronous IPC while interfaces are +being enumerated. + ## Android behavior If you want Android-specific values such as DNS servers, DHCP hints, or Wi-Fi link speed, your app may still need to initialize the Android context for Rust and declare Android permissions such as `ACCESS_NETWORK_STATE` and `ACCESS_WIFI_STATE`. diff --git a/src/interface/interface.rs b/src/interface/interface.rs index 1671417..6e3b43c 100644 --- a/src/interface/interface.rs +++ b/src/interface/interface.rs @@ -74,6 +74,9 @@ pub struct Interface { /// This field is usually available on Linux, Android, BSD, Apple targets, and Windows. /// It may be `None` for virtual adapters, unsupported drivers, or platforms that do not /// expose link speed. + /// + /// On macOS, collecting Wi-Fi transmit speed through CoreWLAN requires the + /// `apple-wifi-extra` feature, which is disabled by default. pub transmit_speed: Option, /// Reported receive link speed in bits per second. /// diff --git a/src/os/macos/interface.rs b/src/os/macos/interface.rs index bb89d30..0a8cffd 100644 --- a/src/os/macos/interface.rs +++ b/src/os/macos/interface.rs @@ -1,11 +1,10 @@ use crate::os::darwin::types::{get_functional_type, interface_type_by_name}; use crate::os::macos::sc::{get_sc_interface_map, read_sc_plist_interface_map}; +#[cfg(feature = "apple-wifi-extra")] +use crate::os::macos::wifi::get_wifi_transmit_rate; use crate::{ interface::interface::Interface, - os::{ - macos::sc::SCInterface, macos::wifi::get_wifi_transmit_rate, - unix::interface::unix_interfaces, - }, + os::{macos::sc::SCInterface, unix::interface::unix_interfaces}, prelude::InterfaceType, }; @@ -44,7 +43,8 @@ pub fn interfaces() -> Vec { iface.dhcp_v6_enabled = sc_inface.dhcp_v6_enabled; } - if iface.if_type == InterfaceType::Wireless80211 { + #[cfg(feature = "apple-wifi-extra")] + if iface.if_type == InterfaceType::Wireless80211 && iface.is_up() { iface.transmit_speed = get_wifi_transmit_rate(&iface.name); } diff --git a/src/os/macos/mod.rs b/src/os/macos/mod.rs index 8e2954f..8d20cd8 100644 --- a/src/os/macos/mod.rs +++ b/src/os/macos/mod.rs @@ -1,4 +1,5 @@ pub mod interface; pub mod sc; +#[cfg(feature = "apple-wifi-extra")] mod wifi;