WireGuardKit: Remove isStarted: bool from WireGuardAdapter

Signed-off-by: Andrej Mihajlov <and@mullvad.net>
This commit is contained in:
Andrej Mihajlov 2020-12-02 13:52:24 +01:00
parent 4f9f61f7a7
commit 697d449dc8
1 changed files with 6 additions and 17 deletions

View File

@ -44,9 +44,6 @@ public class WireGuardAdapter {
/// Private queue used to synchronize access to `WireGuardAdapter` members. /// Private queue used to synchronize access to `WireGuardAdapter` members.
private let workQueue = DispatchQueue(label: "WireGuardAdapterWorkQueue") private let workQueue = DispatchQueue(label: "WireGuardAdapterWorkQueue")
/// Flag that tells if the adapter has already started.
private var isStarted = false
/// Packet tunnel settings generator. /// Packet tunnel settings generator.
private var settingsGenerator: PacketTunnelSettingsGenerator? private var settingsGenerator: PacketTunnelSettingsGenerator?
@ -139,7 +136,7 @@ public class WireGuardAdapter {
/// - completionHandler: completion handler. /// - completionHandler: completion handler.
public func start(tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (WireGuardAdapterError?) -> Void) { public func start(tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (WireGuardAdapterError?) -> Void) {
workQueue.async { workQueue.async {
guard !self.isStarted else { guard self.wireguardHandle == nil else {
completionHandler(.invalidState) completionHandler(.invalidState)
return return
} }
@ -170,7 +167,6 @@ public class WireGuardAdapter {
if handle >= 0 { if handle >= 0 {
self.wireguardHandle = handle self.wireguardHandle = handle
self.isStarted = true
} else { } else {
returnError = .startWireGuardBackend(handle) returnError = .startWireGuardBackend(handle)
} }
@ -185,7 +181,7 @@ public class WireGuardAdapter {
/// - Parameter completionHandler: completion handler. /// - Parameter completionHandler: completion handler.
public func stop(completionHandler: @escaping (WireGuardAdapterError?) -> Void) { public func stop(completionHandler: @escaping (WireGuardAdapterError?) -> Void) {
workQueue.async { workQueue.async {
guard self.isStarted else { guard let handle = self.wireguardHandle else {
completionHandler(.invalidState) completionHandler(.invalidState)
return return
} }
@ -193,12 +189,8 @@ public class WireGuardAdapter {
self.networkMonitor?.cancel() self.networkMonitor?.cancel()
self.networkMonitor = nil self.networkMonitor = nil
if let handle = self.wireguardHandle { wgTurnOff(handle)
wgTurnOff(handle) self.wireguardHandle = nil
self.wireguardHandle = nil
}
self.isStarted = false
completionHandler(nil) completionHandler(nil)
} }
@ -210,7 +202,7 @@ public class WireGuardAdapter {
/// - completionHandler: completion handler. /// - completionHandler: completion handler.
public func update(tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (WireGuardAdapterError?) -> Void) { public func update(tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (WireGuardAdapterError?) -> Void) {
workQueue.async { workQueue.async {
guard self.isStarted else { guard let handle = self.wireguardHandle else {
completionHandler(.invalidState) completionHandler(.invalidState)
return return
} }
@ -224,9 +216,7 @@ public class WireGuardAdapter {
if let error = error { if let error = error {
completionHandler(error) completionHandler(error)
} else { } else {
if let handle = self.wireguardHandle { wgSetConfig(handle, settingsGenerator!.uapiConfiguration())
wgSetConfig(handle, settingsGenerator!.uapiConfiguration())
}
completionHandler(nil) completionHandler(nil)
} }
@ -339,7 +329,6 @@ public class WireGuardAdapter {
/// Helper method used by network path monitor. /// Helper method used by network path monitor.
/// - Parameter path: new network path /// - Parameter path: new network path
private func didReceivePathUpdate(path: Network.NWPath) { private func didReceivePathUpdate(path: Network.NWPath) {
guard self.isStarted else { return }
if let handle = self.wireguardHandle { if let handle = self.wireguardHandle {
self.handleLogLine(level: .debug, message: "Network change detected with \(path.status) route and interface order \(path.availableInterfaces)") self.handleLogLine(level: .debug, message: "Network change detected with \(path.status) route and interface order \(path.availableInterfaces)")