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

Adds BSK support for the transparent proxy network extension #652

Merged
merged 10 commits into from
Feb 21, 2024
28 changes: 20 additions & 8 deletions Sources/NetworkProtection/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import NetworkExtension
import UserNotifications

// swiftlint:disable file_length

Check failure on line 28 in Sources/NetworkProtection/PacketTunnelProvider.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

SwiftLint rule 'file_length' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)

// swiftlint:disable:next type_body_length
open class PacketTunnelProvider: NEPacketTunnelProvider {

Expand Down Expand Up @@ -341,10 +343,6 @@
try loadAuthToken(from: options)
}

open func prepareToConnect(using provider: NETunnelProviderProtocol?) {
// no-op
}

open func loadVendorOptions(from provider: NETunnelProviderProtocol?) throws {
let vendorOptions = provider?.providerConfiguration

Expand Down Expand Up @@ -459,6 +457,17 @@
.store(in: &cancellables)
}

// MARK: - Overrideable Connection Events

open func prepareToConnect(using provider: NETunnelProviderProtocol?) {
// no-op: abstract method to be overridden in subclass
}

@MainActor
open func handleConnected(with tunnelConfiguration: TunnelConfiguration) {
// no-op: abstract method to be overridden in subclass
}

// MARK: - Tunnel Start

open override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) {
Expand Down Expand Up @@ -576,17 +585,21 @@
return
}

Task { [weak self] in
Task { @MainActor [weak self] in
// It's important to call this completion handler before running the tester
// as if we don't, the tester will just fail. It seems like the connection
// won't fully work until the completion handler is called.
completionHandler(nil)

guard let self else { return }

self.handleConnected(with: tunnelConfiguration)

do {
let startReason: AdapterStartReason = onDemand ? .onDemand : .manual
try await self?.handleAdapterStarted(startReason: startReason)
try await self.handleAdapterStarted(startReason: startReason)
} catch {
self?.cancelTunnelWithError(error)
self.cancelTunnelWithError(error)
return
}
}
Expand Down Expand Up @@ -1154,5 +1167,4 @@
public var debugDescription: String {
errorDescription!
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public class ConnectionSessionUtilities {
/// Retrieves a session from a `NEVPNStatusDidChange` notification.
///
public static func session(from notification: Notification) -> NETunnelProviderSession? {
guard let session = (notification.object as? NETunnelProviderSession) else {
guard let session = (notification.object as? NETunnelProviderSession),
session.manager is NETunnelProviderManager else {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ public class ConnectionStatusObserverThroughSession: ConnectionStatusObserver {
}

private func startObservers() {
notificationCenter.publisher(for: .NEVPNConfigurationChange).sink { _ in
notificationCenter.publisher(for: .NEVPNConfigurationChange).sink { notification in
guard let session = ConnectionSessionUtilities.session(from: notification),
session.manager is NETunnelProviderManager else {
return
}

Task {
// As crazy as it seems, this calls fixes an issue with tunnel session
// having a nil manager, when in theory it should never be `nil`. I don't know
Expand Down
Loading