Skip to content

Commit

Permalink
Fix on demand so that it's not enabled too soon
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoreymendez committed Feb 21, 2024
1 parent ba18d0e commit c2b64f6
Showing 1 changed file with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ final class NetworkProtectionTunnelController: NetworkProtection.TunnelControlle
private let networkExtensionBundleID: String
private let networkExtensionController: NetworkExtensionController

// MARK: - Notification Center

private let notificationCenter: NotificationCenter

// MARK: - User Defaults

/* Temporarily disabled - https://app.asana.com/0/0/1205766100762904/f
Expand Down Expand Up @@ -129,13 +133,41 @@ final class NetworkProtectionTunnelController: NetworkProtection.TunnelControlle
self.logger = logger
self.networkExtensionBundleID = networkExtensionBundleID
self.networkExtensionController = networkExtensionController
self.notificationCenter = notificationCenter
self.settings = settings
self.tokenStore = tokenStore

subscribeToSettingsChanges()
subscribeToStatusChanges()
}

// MARK: - Observing Status Changes

private func subscribeToStatusChanges() {
notificationCenter.publisher(for: .NEVPNStatusDidChange)
.sink(receiveValue: handleStatusChange(_:))
.store(in: &cancellables)
}

private func handleStatusChange(_ notification: Notification) {
guard let session = (notification.object as? NETunnelProviderSession),
let manager = session.manager as? NETunnelProviderManager else {

return
}

Task { @MainActor in
switch session.status {
case .connected:
try await enableOnDemand(tunnelManager: manager)
default:
break
}

}
}

// MARK: - Tunnel Settings
// MARK: - Subscriptions

private func subscribeToSettingsChanges() {
settings.changePublisher
Expand All @@ -154,6 +186,8 @@ final class NetworkProtectionTunnelController: NetworkProtection.TunnelControlle
.store(in: &cancellables)
}

// MARK: - Handling Settings Changes

/// This is where the tunnel owner has a chance to handle the settings change locally.
///
/// The extension can also handle these changes so not everything needs to be handled here.
Expand Down Expand Up @@ -443,8 +477,6 @@ final class NetworkProtectionTunnelController: NetworkProtection.TunnelControlle
guard let self, error == nil, fired else { return }
self.settings.vpnFirstEnabled = PixelKit.pixelLastFireDate(event: NetworkProtectionPixelEvent.networkProtectionNewUser)
}

try await enableOnDemand(tunnelManager: tunnelManager)
}

/// Stops the VPN connection used for Network Protection
Expand Down

0 comments on commit c2b64f6

Please sign in to comment.