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

Prevent VPN server list persistence failures #603

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public enum NetworkProtectionError: LocalizedError {

// Wireguard errors
case wireGuardCannotLocateTunnelFileDescriptor
case wireGuardInvalidState
case wireGuardInvalidState(reason: String)
case wireGuardDnsResolution
case wireGuardSetNetworkSettings(Error)
case startWireGuardBackend(Int32)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ extension WireGuardAdapterError: NetworkProtectionErrorConvertible {
switch self {
case .cannotLocateTunnelFileDescriptor:
return .wireGuardCannotLocateTunnelFileDescriptor
case .invalidState:
return .wireGuardInvalidState
case .invalidState(let reason):
return .wireGuardInvalidState(reason: reason.rawValue)
case .dnsResolution:
return .wireGuardDnsResolution
case .setNetworkSettings(let error):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,19 @@ public class NetworkProtectionServerListFileSystemStore: NetworkProtectionServer
do {
return try JSONDecoder().decode([NetworkProtectionServer].self, from: data)
} catch {
try removeServerList()
samsymons marked this conversation as resolved.
Show resolved Hide resolved
removeServerList()
throw NetworkProtectionServerListStoreError.failedToDecodeServerList(error)
}
}

public func removeServerList() throws {
if FileManager.default.fileExists(atPath: fileURL.relativePath) {
try FileManager.default.removeItem(at: fileURL)
public func removeServerList() {
if FileManager.default.fileExists(atPath: fileURL.path) {
try? FileManager.default.removeItem(at: fileURL)
}
}

private func replaceServerList(with newList: [NetworkProtectionServer]) throws {
try removeServerList()
removeServerList()

let serializedJSONData: Data

Expand Down
18 changes: 12 additions & 6 deletions Sources/NetworkProtection/WireGuardKit/WireGuardAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import NetworkExtension
import WireGuard
import Common

public enum WireGuardAdapterErrorInvalidStateReason: String {
case alreadyStarted
case alreadyStopped
case updatedTunnelWhileStopped
}

public enum WireGuardAdapterError: Error {
/// Failure to locate tunnel file descriptor.
case cannotLocateTunnelFileDescriptor

/// Failure to perform an operation in such state.
case invalidState
/// Failure to perform an operation in such state. Includes a reason why the error was returned.
case invalidState(WireGuardAdapterErrorInvalidStateReason)

/// Failure to resolve endpoints.
case dnsResolution([DNSResolutionError])
Expand Down Expand Up @@ -263,7 +269,7 @@ public class WireGuardAdapter {
public func start(tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (WireGuardAdapterError?) -> Void) {
workQueue.async {
guard case .stopped = self.state else {
completionHandler(.invalidState)
completionHandler(.invalidState(.alreadyStarted))
return
}

Expand Down Expand Up @@ -307,7 +313,7 @@ public class WireGuardAdapter {
break

case .stopped:
completionHandler(.invalidState)
completionHandler(.invalidState(.alreadyStopped))
return
}

Expand All @@ -323,14 +329,14 @@ public class WireGuardAdapter {
/// Update runtime configuration.
/// - Parameters:
/// - tunnelConfiguration: tunnel configuration.
/// - reassert: wether the connection should reassert or not.
/// - reassert: whether the connection should reassert or not.
/// - completionHandler: completion handler.
public func update(tunnelConfiguration: TunnelConfiguration,
reassert: Bool = true,
completionHandler: @escaping (WireGuardAdapterError?) -> Void) {
workQueue.async {
if case .stopped = self.state {
completionHandler(.invalidState)
completionHandler(.invalidState(.updatedTunnelWhileStopped))
return
}

Expand Down
Loading