Decouple dependency on NEPacketTunnelProvider

This commit is contained in:
Davide De Rosa 2024-03-25 22:51:50 +01:00
parent e5364da8d3
commit 9b3e1541d9
No known key found for this signature in database
GPG Key ID: A48836171C759F5E
2 changed files with 21 additions and 8 deletions

View File

@ -43,8 +43,8 @@ public class WireGuardAdapter {
/// Network routes monitor. /// Network routes monitor.
private var networkMonitor: NWPathMonitor? private var networkMonitor: NWPathMonitor?
/// Packet tunnel provider. /// Adapter delegate.
private weak var packetTunnelProvider: NEPacketTunnelProvider? private weak var delegate: WireGuardAdapterDelegate?
/// Log handler closure. /// Log handler closure.
private let logHandler: LogHandler private let logHandler: LogHandler
@ -125,12 +125,12 @@ public class WireGuardAdapter {
// MARK: - Initialization // MARK: - Initialization
/// Designated initializer. /// Designated initializer.
/// - Parameter packetTunnelProvider: an instance of `NEPacketTunnelProvider`. Internally stored /// - Parameter delegate: an instance of `WireGuardAdapterDelegate`. Internally stored
/// as a weak reference. /// as a weak reference.
/// - Parameter backend: a backend implementation. /// - Parameter backend: a backend implementation.
/// - Parameter logHandler: a log handler closure. /// - Parameter logHandler: a log handler closure.
public init(with packetTunnelProvider: NEPacketTunnelProvider, backend: WireGuardBackend, logHandler: @escaping LogHandler) { public init(with delegate: WireGuardAdapterDelegate, backend: WireGuardBackend, logHandler: @escaping LogHandler) {
self.packetTunnelProvider = packetTunnelProvider self.delegate = delegate
self.backend = backend self.backend = backend
self.logHandler = logHandler self.logHandler = logHandler
@ -248,9 +248,9 @@ public class WireGuardAdapter {
// Tell the system that the tunnel is going to reconnect using new WireGuard // Tell the system that the tunnel is going to reconnect using new WireGuard
// configuration. // configuration.
// This will broadcast the `NEVPNStatusDidChange` notification to the GUI process. // This will broadcast the `NEVPNStatusDidChange` notification to the GUI process.
self.packetTunnelProvider?.reasserting = true self.delegate?.adapterShouldReassert(self, reasserting: true)
defer { defer {
self.packetTunnelProvider?.reasserting = false self.delegate?.adapterShouldReassert(self, reasserting: false)
} }
do { do {
@ -319,7 +319,7 @@ public class WireGuardAdapter {
condition.lock() condition.lock()
defer { condition.unlock() } defer { condition.unlock() }
self.packetTunnelProvider?.setTunnelNetworkSettings(networkSettings) { error in self.delegate?.adapterShouldSetNetworkSettings(self, settings: networkSettings) { error in
systemError = error systemError = error
condition.signal() condition.signal()
} }

View File

@ -106,6 +106,19 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
} }
} }
extension PacketTunnelProvider: WireGuardAdapterDelegate {
public func adapterShouldReassert(_ adapter: WireGuardAdapter, reasserting: Bool) {
self.reasserting = reasserting
}
public func adapterShouldSetNetworkSettings(_ adapter: WireGuardAdapter, settings: Any?, completionHandler: ((Error?) -> Void)?) {
guard let settings = settings as? NEPacketTunnelNetworkSettings else {
return
}
setTunnelNetworkSettings(settings, completionHandler: completionHandler)
}
}
extension WireGuardLogLevel { extension WireGuardLogLevel {
var osLogLevel: OSLogType { var osLogLevel: OSLogType {
switch self { switch self {