Update API to access current Wi-Fi SSID

This commit is contained in:
Davide De Rosa 2020-11-21 18:40:47 +01:00
parent 1c4c21fa22
commit 44844cfd9c
7 changed files with 60 additions and 27 deletions

View File

@ -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/), 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). 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) ## 3.0.0 (2020-11-15)
### Added ### Added

View File

@ -1,6 +1,6 @@
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = "TunnelKit" s.name = "TunnelKit"
s.version = "3.0.1" s.version = "3.1.0"
s.summary = "Non-official OpenVPN client for Apple platforms." s.summary = "Non-official OpenVPN client for Apple platforms."
s.homepage = "https://github.com/passepartoutvpn/tunnelkit" s.homepage = "https://github.com/passepartoutvpn/tunnelkit"

View File

@ -35,7 +35,12 @@
// //
import Foundation import Foundation
#if os(iOS)
import NetworkExtension
import SystemConfiguration.CaptiveNetwork import SystemConfiguration.CaptiveNetwork
#else
import CoreWLAN
#endif
import SwiftyBeaver import SwiftyBeaver
private let log = SwiftyBeaver.self private let log = SwiftyBeaver.self
@ -80,9 +85,14 @@ public class InterfaceObserver: NSObject {
} }
private func fireWifiChangeObserver() { private func fireWifiChangeObserver() {
let currentWifiName = currentWifiNetworkName() InterfaceObserver.fetchCurrentSSID {
if (currentWifiName != lastWifiName) { self.fireWifiChange(withSSID: $0)
if let current = currentWifiName { }
}
private func fireWifiChange(withSSID ssid: String?) {
if ssid != lastWifiName {
if let current = ssid {
log.debug("SSID is now '\(current.maskedDescription)'") log.debug("SSID is now '\(current.maskedDescription)'")
if let last = lastWifiName, (current != last) { if let last = lastWifiName, (current != last) {
queue?.async { queue?.async {
@ -93,7 +103,7 @@ public class InterfaceObserver: NSObject {
log.debug("SSID is null") 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. - Returns: The current Wi-Fi SSID if any.
**/ **/
public func currentWifiNetworkName() -> String? { public static func fetchCurrentSSID(completionHandler: @escaping (String?) -> Void) {
#if os(iOS) #if os(iOS)
// if #available(iOS 14.0, *) {
// NEHotspotNetwork.fetchCurrent {
// completionHandler($0?.ssid)
// }
// } else {
guard let interfaceNames = CNCopySupportedInterfaces() as? [CFString] else { guard let interfaceNames = CNCopySupportedInterfaces() as? [CFString] else {
return nil completionHandler(nil)
return
} }
for name in interfaceNames { for name in interfaceNames {
guard let iface = CNCopyCurrentNetworkInfo(name) as? [String: Any] else { guard let iface = CNCopyCurrentNetworkInfo(name) as? [String: Any] else {
continue continue
} }
if let ssid = iface["SSID"] as? String { if let ssid = iface["SSID"] as? String {
return ssid completionHandler(ssid)
return
} }
} }
completionHandler(nil)
// }
#else
let client = CWWiFiClient.shared()
let ssid = client.interfaces()?.compactMap { $0.ssid() }.first
completionHandler(ssid)
#endif #endif
return nil
} }
} }

View File

@ -36,6 +36,11 @@
import NetworkExtension import NetworkExtension
import SwiftyBeaver import SwiftyBeaver
#if os(iOS)
import SystemConfiguration.CaptiveNetwork
#else
import CoreWLAN
#endif
import __TunnelKitCore import __TunnelKitCore
private let log = SwiftyBeaver.self private let log = SwiftyBeaver.self
@ -90,8 +95,6 @@ open class OpenVPNTunnelProvider: NEPacketTunnelProvider {
private let memoryLog = MemoryDestination() private let memoryLog = MemoryDestination()
private let observer = InterfaceObserver()
private let tunnelQueue = DispatchQueue(label: OpenVPNTunnelProvider.description(), qos: .utility) private let tunnelQueue = DispatchQueue(label: OpenVPNTunnelProvider.description(), qos: .utility)
private let prngSeedLength = 64 private let prngSeedLength = 64
@ -829,12 +832,14 @@ extension OpenVPNTunnelProvider {
} }
private func logCurrentSSID() { private func logCurrentSSID() {
if let ssid = observer.currentWifiNetworkName() { InterfaceObserver.fetchCurrentSSID {
if let ssid = $0 {
log.debug("Current SSID: '\(ssid.maskedDescription)'") log.debug("Current SSID: '\(ssid.maskedDescription)'")
} else { } else {
log.debug("Current SSID: none (disconnected from WiFi)") log.debug("Current SSID: none (disconnected from WiFi)")
} }
} }
}
// private func anyPointer(_ object: Any?) -> UnsafeMutableRawPointer { // private func anyPointer(_ object: Any?) -> UnsafeMutableRawPointer {
// let anyObject = object as AnyObject // let anyObject = object as AnyObject

View File

@ -15,7 +15,7 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>APPL</string> <string>APPL</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>2.3.0</string> <string>3.1.0</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>1</string> <string>1</string>
<key>LSRequiresIPhoneOS</key> <key>LSRequiresIPhoneOS</key>

View File

@ -15,7 +15,7 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>BNDL</string> <string>BNDL</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>2.3.0</string> <string>3.1.0</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>1</string> <string>1</string>
</dict> </dict>

View File

@ -15,7 +15,7 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>BNDL</string> <string>BNDL</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>2.3.0</string> <string>3.1.0</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>1</string> <string>1</string>
</dict> </dict>