From 44844cfd9c5d672ea3d134c9262bc4a8f002417a Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sat, 21 Nov 2020 18:40:47 +0100 Subject: [PATCH] Update API to access current Wi-Fi SSID --- CHANGELOG.md | 6 +++ TunnelKit.podspec | 2 +- .../AppExtension/InterfaceObserver.swift | 52 +++++++++++++------ .../AppExtension/OpenVPNTunnelProvider.swift | 21 +++++--- TunnelKitHost/Info.plist | 2 +- TunnelKitTests-iOS/Info.plist | 2 +- TunnelKitTests-macOS/Info.plist | 2 +- 7 files changed, 60 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1d99be..14e59f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Changed + +- Update API to access current Wi-Fi SSID. + ## 3.0.0 (2020-11-15) ### Added diff --git a/TunnelKit.podspec b/TunnelKit.podspec index 6666b63..7f9b3e2 100644 --- a/TunnelKit.podspec +++ b/TunnelKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "TunnelKit" - s.version = "3.0.1" + s.version = "3.1.0" s.summary = "Non-official OpenVPN client for Apple platforms." s.homepage = "https://github.com/passepartoutvpn/tunnelkit" diff --git a/TunnelKit/Sources/AppExtension/InterfaceObserver.swift b/TunnelKit/Sources/AppExtension/InterfaceObserver.swift index f364f84..b88610b 100644 --- a/TunnelKit/Sources/AppExtension/InterfaceObserver.swift +++ b/TunnelKit/Sources/AppExtension/InterfaceObserver.swift @@ -35,7 +35,12 @@ // import Foundation +#if os(iOS) +import NetworkExtension import SystemConfiguration.CaptiveNetwork +#else +import CoreWLAN +#endif import SwiftyBeaver private let log = SwiftyBeaver.self @@ -80,9 +85,14 @@ public class InterfaceObserver: NSObject { } private func fireWifiChangeObserver() { - let currentWifiName = currentWifiNetworkName() - if (currentWifiName != lastWifiName) { - if let current = currentWifiName { + InterfaceObserver.fetchCurrentSSID { + self.fireWifiChange(withSSID: $0) + } + } + + private func fireWifiChange(withSSID ssid: String?) { + if ssid != lastWifiName { + if let current = ssid { log.debug("SSID is now '\(current.maskedDescription)'") if let last = lastWifiName, (current != last) { queue?.async { @@ -93,7 +103,7 @@ public class InterfaceObserver: NSObject { log.debug("SSID is null") } } - lastWifiName = currentWifiName + lastWifiName = ssid } /** @@ -101,20 +111,32 @@ public class InterfaceObserver: NSObject { - Returns: The current Wi-Fi SSID if any. **/ - public func currentWifiNetworkName() -> String? { + public static func fetchCurrentSSID(completionHandler: @escaping (String?) -> Void) { #if os(iOS) - guard let interfaceNames = CNCopySupportedInterfaces() as? [CFString] else { - return nil - } - for name in interfaceNames { - guard let iface = CNCopyCurrentNetworkInfo(name) as? [String: Any] else { - continue +// if #available(iOS 14.0, *) { +// NEHotspotNetwork.fetchCurrent { +// completionHandler($0?.ssid) +// } +// } else { + guard let interfaceNames = CNCopySupportedInterfaces() as? [CFString] else { + completionHandler(nil) + return } - if let ssid = iface["SSID"] as? String { - return ssid + for name in interfaceNames { + guard let iface = CNCopyCurrentNetworkInfo(name) as? [String: Any] else { + continue + } + if let ssid = iface["SSID"] as? String { + completionHandler(ssid) + return + } } - } + completionHandler(nil) +// } + #else + let client = CWWiFiClient.shared() + let ssid = client.interfaces()?.compactMap { $0.ssid() }.first + completionHandler(ssid) #endif - return nil } } diff --git a/TunnelKit/Sources/Protocols/OpenVPN/AppExtension/OpenVPNTunnelProvider.swift b/TunnelKit/Sources/Protocols/OpenVPN/AppExtension/OpenVPNTunnelProvider.swift index c39f4e8..1e8197d 100644 --- a/TunnelKit/Sources/Protocols/OpenVPN/AppExtension/OpenVPNTunnelProvider.swift +++ b/TunnelKit/Sources/Protocols/OpenVPN/AppExtension/OpenVPNTunnelProvider.swift @@ -36,6 +36,11 @@ import NetworkExtension import SwiftyBeaver +#if os(iOS) +import SystemConfiguration.CaptiveNetwork +#else +import CoreWLAN +#endif import __TunnelKitCore private let log = SwiftyBeaver.self @@ -90,8 +95,6 @@ open class OpenVPNTunnelProvider: NEPacketTunnelProvider { private let memoryLog = MemoryDestination() - private let observer = InterfaceObserver() - private let tunnelQueue = DispatchQueue(label: OpenVPNTunnelProvider.description(), qos: .utility) private let prngSeedLength = 64 @@ -827,15 +830,17 @@ extension OpenVPNTunnelProvider { memoryLog.flush(to: url) } } - + private func logCurrentSSID() { - if let ssid = observer.currentWifiNetworkName() { - log.debug("Current SSID: '\(ssid.maskedDescription)'") - } else { - log.debug("Current SSID: none (disconnected from WiFi)") + InterfaceObserver.fetchCurrentSSID { + if let ssid = $0 { + log.debug("Current SSID: '\(ssid.maskedDescription)'") + } else { + log.debug("Current SSID: none (disconnected from WiFi)") + } } } - + // private func anyPointer(_ object: Any?) -> UnsafeMutableRawPointer { // let anyObject = object as AnyObject // return Unmanaged.passUnretained(anyObject).toOpaque() diff --git a/TunnelKitHost/Info.plist b/TunnelKitHost/Info.plist index e4e050c..f062a9a 100644 --- a/TunnelKitHost/Info.plist +++ b/TunnelKitHost/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 2.3.0 + 3.1.0 CFBundleVersion 1 LSRequiresIPhoneOS diff --git a/TunnelKitTests-iOS/Info.plist b/TunnelKitTests-iOS/Info.plist index fe05d8c..43dc03c 100644 --- a/TunnelKitTests-iOS/Info.plist +++ b/TunnelKitTests-iOS/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 2.3.0 + 3.1.0 CFBundleVersion 1 diff --git a/TunnelKitTests-macOS/Info.plist b/TunnelKitTests-macOS/Info.plist index fe05d8c..43dc03c 100644 --- a/TunnelKitTests-macOS/Info.plist +++ b/TunnelKitTests-macOS/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 2.3.0 + 3.1.0 CFBundleVersion 1