Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix on demand so that it's not enabled too soon #2235

Merged
merged 1 commit into from
Feb 21, 2024
Merged
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 @@ -70,6 +70,10 @@ final class NetworkProtectionTunnelController: TunnelController, TunnelSessionPr
private let networkExtensionBundleID: String
private let networkExtensionController: NetworkExtensionController

// MARK: - Notification Center

private let notificationCenter: NotificationCenter

/// The proxy manager
///
/// We're keeping a reference to this because we don't want to be calling `loadAllFromPreferences` more than
Expand Down Expand Up @@ -146,13 +150,41 @@ final class NetworkProtectionTunnelController: TunnelController, TunnelSessionPr
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 @@ -171,6 +203,8 @@ final class NetworkProtectionTunnelController: TunnelController, TunnelSessionPr
.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 @@ -483,8 +517,6 @@ final class NetworkProtectionTunnelController: TunnelController, TunnelSessionPr
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
Loading