-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial NetP controller and provider classes (#1835)
Task/Issue URL: https://app.asana.com/0/0/1205043071738197/f BSK PR: duckduckgo/BrowserServicesKit#418 Description: Adds the initial classes needed for the PacketTunnelProvider extension and for controlling it from the iOS app along with some configuration. This PR also brings in the NetP iOS compatibility changes needed after some incompatibility was merged. Note that until we’re ready to release, the extension will remain not embedded in the app and any Network Extension code will be compiler flagged
- Loading branch information
Showing
5 changed files
with
258 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// | ||
// NetworkProtectionTunnelController.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import Core | ||
import UIKit | ||
|
||
#if NETWORK_PROTECTION | ||
|
||
import NetworkExtension | ||
import NetworkProtection | ||
|
||
public protocol NetworkProtectionTunnelControlling { | ||
var status: ConnectionStatus { get } | ||
var statusPublisher: AnyPublisher<ConnectionStatus, Never> { get } | ||
func setState(to enabled: Bool) async throws | ||
} | ||
|
||
final class NetworkProtectionTunnelController: NetworkProtectionTunnelControlling { | ||
|
||
private let tokenStore = NetworkProtectionKeychainTokenStore(useSystemKeychain: false, errorEvents: nil) | ||
private let connectionObserver = ConnectionStatusObserverThroughSession(platformNotificationCenter: .default, | ||
platformDidWakeNotification: UIApplication.didBecomeActiveNotification) | ||
|
||
// MARK: - NetworkProtectionTunnelControlling | ||
|
||
@Published var status: ConnectionStatus = .disconnected | ||
|
||
var statusPublisher: AnyPublisher<ConnectionStatus, Never> { | ||
connectionObserver.publisher.eraseToAnyPublisher() | ||
} | ||
|
||
func setState(to enabled: Bool) async throws { | ||
if enabled { | ||
try await start() | ||
} else { | ||
try await ConnectionSessionUtilities.activeSession()?.stopVPNTunnel() | ||
} | ||
} | ||
|
||
/// Starts the VPN connection used for Network Protection | ||
/// | ||
private func start() async throws { | ||
let tunnelManager: NETunnelProviderManager | ||
|
||
do { | ||
tunnelManager = try await loadOrMakeTunnelManager() | ||
} catch { | ||
throw error | ||
} | ||
|
||
switch tunnelManager.connection.status { | ||
case .invalid: | ||
reloadTunnelManager() | ||
try await start() | ||
case .connected: | ||
// Intentional no-op | ||
break | ||
default: | ||
try start(tunnelManager) | ||
} | ||
} | ||
|
||
/// Reloads the tunnel manager from preferences. | ||
/// | ||
private func reloadTunnelManager() { | ||
internalTunnelManager = nil | ||
} | ||
|
||
private func start(_ tunnelManager: NETunnelProviderManager) throws { | ||
var options = [String: NSObject]() | ||
|
||
options["activationAttemptId"] = UUID().uuidString as NSString | ||
options["authToken"] = try tokenStore.fetchToken() as NSString? | ||
|
||
do { | ||
try tunnelManager.connection.startVPNTunnel(options: options) | ||
} catch { | ||
throw error | ||
} | ||
} | ||
|
||
/// The actual storage for our tunnel manager. | ||
/// | ||
private var internalTunnelManager: NETunnelProviderManager? | ||
|
||
/// The tunnel manager: will try to load if it its not loaded yet, but if one can't be loaded from preferences, | ||
/// a new one will not be created. This is useful for querying the connection state and information without triggering | ||
/// a VPN-access popup to the user. | ||
/// | ||
private var tunnelManager: NETunnelProviderManager? { | ||
get async { | ||
guard let tunnelManager = internalTunnelManager else { | ||
let tunnelManager = await loadTunnelManager() | ||
internalTunnelManager = tunnelManager | ||
return tunnelManager | ||
} | ||
|
||
return tunnelManager | ||
} | ||
} | ||
|
||
private func loadTunnelManager() async -> NETunnelProviderManager? { | ||
try? await NETunnelProviderManager.loadAllFromPreferences().first | ||
} | ||
|
||
private func loadOrMakeTunnelManager() async throws -> NETunnelProviderManager { | ||
guard let tunnelManager = await tunnelManager else { | ||
let tunnelManager = NETunnelProviderManager() | ||
try await setupAndSave(tunnelManager) | ||
internalTunnelManager = tunnelManager | ||
return tunnelManager | ||
} | ||
|
||
try await setupAndSave(tunnelManager) | ||
return tunnelManager | ||
} | ||
|
||
private func setupAndSave(_ tunnelManager: NETunnelProviderManager) async throws { | ||
try await setup(tunnelManager) | ||
try await tunnelManager.saveToPreferences() | ||
try await tunnelManager.loadFromPreferences() | ||
try await tunnelManager.saveToPreferences() | ||
} | ||
|
||
/// Setups the tunnel manager if it's not set up already. | ||
/// | ||
private func setup(_ tunnelManager: NETunnelProviderManager) async throws { | ||
tunnelManager.localizedDescription = "DuckDuckGo Network Protection" | ||
tunnelManager.isEnabled = true | ||
|
||
tunnelManager.protocolConfiguration = { | ||
let protocolConfiguration = NETunnelProviderProtocol() | ||
protocolConfiguration.serverAddress = "127.0.0.1" // Dummy address... the NetP service will take care of grabbing a real server | ||
|
||
// always-on | ||
protocolConfiguration.disconnectOnSleep = false | ||
|
||
return protocolConfiguration | ||
}() | ||
|
||
// reconnect on reboot | ||
tunnelManager.onDemandRules = [NEOnDemandRuleConnect()] | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
PacketTunnelProvider/NetworkProtectionPacketTunnelProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// NetworkProtectionPacketTunnelProvider.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import NetworkProtection | ||
import Common | ||
|
||
// Initial implementation for initial Network Protection tests. Will be fleshed out with https://app.asana.com/0/1203137811378537/1204630829332227/f | ||
final class NetworkProtectionPacketTunnelProvider: PacketTunnelProvider { | ||
|
||
private static var packetTunnelProviderEvents: EventMapping<PacketTunnelProvider.Event> = .init { _, _, _, _ in | ||
} | ||
|
||
@objc init() { | ||
super.init(notificationCenter: NotificationCenter.default, | ||
notificationsPresenter: DefaultNotificationPresenter(), | ||
useSystemKeychain: false, | ||
debugEvents: nil, | ||
providerEvents: Self.packetTunnelProviderEvents) | ||
} | ||
} | ||
|
||
final class DefaultNotificationPresenter: NetworkProtectionNotificationsPresenter { | ||
|
||
func showTestNotification() { | ||
} | ||
|
||
func showReconnectedNotification() { | ||
} | ||
|
||
func showReconnectingNotification() { | ||
} | ||
|
||
func showConnectionFailureNotification() { | ||
} | ||
|
||
func showSupersededNotification() { | ||
} | ||
} | ||
|
||
// MARK: - NetworkProtectionNotificationPosting | ||
|
||
extension NotificationCenter: NetworkProtectionNotificationPosting { | ||
public func post(_ networkProtectionNotification: NetworkProtection.NetworkProtectionNotification, object: String?, log: Common.OSLog) { | ||
} | ||
} |