Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ class SessionAttributesCollector(
}

val ipAddress = resolveIpAddress()
val ssid = if (interfaceType == "wifi") resolveSsid() else ""

return NetworkSnapshot(interfaceType, ipAddress, vpnActive, ssid)
return NetworkSnapshot(interfaceType, ipAddress, vpnActive, resolveSsid())
}

private fun resolveIpAddress(): String {
Expand Down
44 changes: 35 additions & 9 deletions ios/SessionAttributes/SessionAttributesCollector.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import CoreLocation
import Foundation
import Network
import NetworkExtension
Expand All @@ -13,7 +14,7 @@ struct NetworkSnapshot {
let ssid: String
}

public class SessionAttributesCollector {
public class SessionAttributesCollector: NSObject {
public static let shared = SessionAttributesCollector()

private let store = SessionAttributesStore.shared
Expand All @@ -23,11 +24,14 @@ public class SessionAttributesCollector {
private let pathMonitor = NWPathMonitor()
private var cachedInterfaceType = ""
private var cachedSsid = ""
private let ssidUnavailable: Bool
private var wifiAvailable = false
private var locationManager: CLLocationManager?

private init() {
ssidUnavailable = Bundle.main.bundlePath.hasSuffix(".appex") &&
Bundle.main.bundleIdentifier?.contains("NotificationService") == true
private static let ssidUnavailable = Bundle.main.bundleURL.pathExtension == "appex" &&
Bundle.main.bundleIdentifier?.contains("NotificationService") ?? false

private override init() {
super.init()

pathMonitor.pathUpdateHandler = { [weak self] path in
guard let self else {
Expand All @@ -45,13 +49,22 @@ public class SessionAttributesCollector {
self.cachedInterfaceType = "other"
}

if self.cachedInterfaceType == "wifi" {
self.wifiAvailable = path.availableInterfaces.contains { $0.type == .wifi }
if self.wifiAvailable {
self.refreshSsid()
} else {
self.cachedSsid = ""
}
}
pathMonitor.start(queue: monitorQueue)

if !Self.ssidUnavailable {
DispatchQueue.main.async {
let manager = CLLocationManager()
manager.delegate = self
self.locationManager = manager
}
}
}

func collect(_ name: String, serverUrl: String) -> String {
Expand Down Expand Up @@ -119,18 +132,17 @@ public class SessionAttributesCollector {
let ipAddress = resolveIpAddress()
return monitorQueue.sync {
let interfaceType = vpnActive ? "vpn" : cachedInterfaceType
let ssid = interfaceType == "wifi" ? cachedSsid : ""
return NetworkSnapshot(
interfaceType: interfaceType,
ipAddress: ipAddress,
vpnActive: vpnActive,
ssid: ssid
ssid: cachedSsid
)
}
}

private func refreshSsid() {
guard !ssidUnavailable else {
guard !Self.ssidUnavailable else {
cachedSsid = ""
return
}
Expand All @@ -149,6 +161,9 @@ public class SessionAttributesCollector {
ssid = ""
}
self.monitorQueue.async {
guard self.wifiAvailable else {
return
}
self.cachedSsid = ssid
}
}
Expand Down Expand Up @@ -225,3 +240,14 @@ public class SessionAttributesCollector {
return address
}
}

extension SessionAttributesCollector: CLLocationManagerDelegate {
public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
monitorQueue.async {
guard self.wifiAvailable else {
return
}
self.refreshSsid()
}
}
}
4 changes: 2 additions & 2 deletions react-native-network-client-session-attributes.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
# the collision cannot be fixed in place.
#
# The sources here depend only on system frameworks (Foundation, Network,
# NetworkExtension, SystemConfiguration, CryptoKit), which is what makes them
# safe to link into app extensions.
# NetworkExtension, SystemConfiguration, CryptoKit, CoreLocation), which is what
# makes them safe to link into app extensions.
#
# Swift module name is react_native_network_client_session_attributes (CocoaPods
# replaces the hyphens in the pod name with underscores).
Expand Down
Loading