From f9eecf8f8c490599bcf9ffc473c93ea3100fc08e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 20 Feb 2025 10:50:35 +0800 Subject: [PATCH] Fix read WIFI information on macOS app store version --- Extension/Extension.entitlements | 2 + .../Network/ExtensionPlatformInterface.swift | 2 +- Library/Network/ExtensionProvider.swift | 37 ++++++++++++++++++- SFM/Info.plist | 8 ++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Extension/Extension.entitlements b/Extension/Extension.entitlements index 54b9ef2..6c6aded 100644 --- a/Extension/Extension.entitlements +++ b/Extension/Extension.entitlements @@ -30,5 +30,7 @@ com.apple.security.network.server + com.apple.security.personal-information.location + diff --git a/Library/Network/ExtensionPlatformInterface.swift b/Library/Network/ExtensionPlatformInterface.swift index 3af6378..77927f5 100644 --- a/Library/Network/ExtensionPlatformInterface.swift +++ b/Library/Network/ExtensionPlatformInterface.swift @@ -2,7 +2,7 @@ import Foundation import Libbox import NetworkExtension import UserNotifications -#if canImport(CoreWLAN) +#if os(macOS) import CoreWLAN #endif diff --git a/Library/Network/ExtensionProvider.swift b/Library/Network/ExtensionProvider.swift index e4adacc..8bcccc7 100644 --- a/Library/Network/ExtensionProvider.swift +++ b/Library/Network/ExtensionProvider.swift @@ -4,6 +4,9 @@ import NetworkExtension #if os(iOS) import WidgetKit #endif +#if os(macOS) + import CoreLocation +#endif open class ExtensionProvider: NEPacketTunnelProvider { public var username: String? = nil @@ -73,7 +76,7 @@ open class ExtensionProvider: NEPacketTunnelProvider { writeMessage(message) var error: NSError? LibboxWriteServiceError(message, &error) - cancelTunnelWithError(NSError(domain: message, code: 0)) + cancelTunnelWithError(nil) } private func startService() async { @@ -114,9 +117,41 @@ open class ExtensionProvider: NEPacketTunnelProvider { boxService = service #if os(macOS) await SharedPreferences.startedByUser.set(true) + if service.needWIFIState() { + if !Variant.useSystemExtension { + locationManager = CLLocationManager() + locationDelegate = stubLocationDelegate(boxService) + locationManager?.delegate = locationDelegate + locationManager?.requestLocation() + } else { + commandServer.writeMessage("(packet-tunnel) WIFI SSID and BSSID information is not currently available in the standalone version of SFM. We are working on resolving this issue.") + } + } #endif } + #if os(macOS) + + private var locationManager: CLLocationManager? + private var locationDelegate: stubLocationDelegate? + + class stubLocationDelegate: NSObject, CLLocationManagerDelegate { + private unowned let boxService: LibboxBoxService + init(_ boxService: LibboxBoxService) { + self.boxService = boxService + } + + func locationManagerDidChangeAuthorization(_: CLLocationManager) { + boxService.updateWIFIState() + } + + func locationManager(_: CLLocationManager, didUpdateLocations _: [CLLocation]) {} + + func locationManager(_: CLLocationManager, didFailWithError _: Error) {} + } + + #endif + private func stopService() { if let service = boxService { do { diff --git a/SFM/Info.plist b/SFM/Info.plist index dcf7fa9..4dc9e17 100644 --- a/SFM/Info.plist +++ b/SFM/Info.plist @@ -105,5 +105,13 @@ CFBundleDevelopmentRegion en + NSLocationAlwaysUsageDescription + sing-box uses the Location permission to provide users with routing based on WIFI SSID and BSSID rules, without reading your location. + NSLocationUsageDescription + sing-box uses the Location permission to provide users with routing based on WIFI SSID and BSSID rules, without reading your location. + NSLocationWhenInUseUsageDescription + sing-box uses the Location permission to provide users with routing based on WIFI SSID and BSSID rules, without reading your location. + NSLocationAlwaysAndWhenInUseUsageDescription + sing-box uses the Location permission to provide users with routing based on WIFI SSID and BSSID rules, without reading your location.