WireGuardKit: Pass logHandler via constructor

Signed-off-by: Andrej Mihajlov <and@mullvad.net>
This commit is contained in:
Andrej Mihajlov 2020-12-02 13:48:24 +01:00
parent d2c38702c8
commit 2329f712cf
2 changed files with 15 additions and 31 deletions

View File

@ -8,7 +8,11 @@ import os
class PacketTunnelProvider: NEPacketTunnelProvider {
private lazy var adapter = WireGuardAdapter(with: self)
private lazy var adapter: WireGuardAdapter = {
return WireGuardAdapter(with: self) { logLevel, message in
wg_log(logLevel.osLogLevel, message: message)
}
}()
override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) {
let activationAttemptId = options?["activationAttemptId"] as? String
@ -25,11 +29,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
return
}
// Setup WireGuard logger
adapter.setLogHandler { logLevel, message in
wg_log(logLevel.osLogLevel, message: message)
}
// Start the tunnel
adapter.start(tunnelConfiguration: tunnelConfiguration) { adapterError in
guard let adapterError = adapterError else {

View File

@ -35,7 +35,7 @@ public class WireGuardAdapter {
private weak var packetTunnelProvider: NEPacketTunnelProvider?
/// Log handler closure.
private var logHandler: LogHandler?
private let logHandler: LogHandler
/// WireGuard internal handle returned by `wgTurnOn` that's used to associate the calls
/// with the specific WireGuard tunnel.
@ -91,14 +91,18 @@ public class WireGuardAdapter {
/// Designated initializer.
/// - Parameter packetTunnelProvider: an instance of `NEPacketTunnelProvider`. Internally stored
/// as a weak reference.
public init(with packetTunnelProvider: NEPacketTunnelProvider) {
/// - Parameter logHandler: a log handler closure.
public init(with packetTunnelProvider: NEPacketTunnelProvider, logHandler: @escaping LogHandler) {
self.packetTunnelProvider = packetTunnelProvider
self.logHandler = logHandler
setupLogHandler()
}
deinit {
// Force deactivate logger to make sure that no further calls to the instance of this class
// Force remove logger to make sure that no further calls to the instance of this class
// can happen after deallocation.
deactivateLogHandler()
wgSetLogger(nil, nil)
// Cancel network monitor
networkMonitor?.cancel()
@ -129,20 +133,6 @@ public class WireGuardAdapter {
}
}
/// Set log handler.
/// - Parameter logHandler: log handler closure
public func setLogHandler(_ logHandler: LogHandler?) {
workQueue.async {
self.logHandler = logHandler
}
if logHandler == nil {
deactivateLogHandler()
} else {
activateLogHandler()
}
}
/// Start the tunnel tunnel.
/// - Parameters:
/// - tunnelConfiguration: tunnel configuration.
@ -247,8 +237,8 @@ public class WireGuardAdapter {
// MARK: - Private methods
/// Install WireGuard log handler.
private func activateLogHandler() {
/// Setup WireGuard log handler.
private func setupLogHandler() {
let context = Unmanaged.passUnretained(self).toOpaque()
wgSetLogger(context) { (context, logLevel, message) in
guard let context = context, let message = message else { return }
@ -263,11 +253,6 @@ public class WireGuardAdapter {
}
}
/// Uninstall WireGuard log handler.
private func deactivateLogHandler() {
wgSetLogger(nil, nil)
}
/// Resolve endpoints and update network configuration.
/// - Parameters:
/// - tunnelConfiguration: tunnel configuration