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

Fix VPN configuration removal to stop the tunnel #900

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Changes from all 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
17 changes: 13 additions & 4 deletions Sources/NetworkProtection/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
// Subscription Errors - 100+
case vpnAccessRevoked

// State Reset - 200+
case appRequestedCancellation

public var errorDescription: String? {
switch self {
case .startingTunnelWithoutAuthToken:
Expand All @@ -93,6 +96,8 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
return "Failed to generate a tunnel configuration: \(internalError.localizedDescription)"
case .simulateTunnelFailureError:
return "Simulated a tunnel error as requested"
case .appRequestedCancellation:
return nil
}
}

Expand All @@ -104,14 +109,17 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
case .simulateTunnelFailureError: return 2
// Subscription Errors - 100+
case .vpnAccessRevoked: return 100
// State Reset - 200+
case .appRequestedCancellation: return 200
}
}

public var errorUserInfo: [String: Any] {
switch self {
case .startingTunnelWithoutAuthToken,
.simulateTunnelFailureError,
.vpnAccessRevoked:
.vpnAccessRevoked,
.appRequestedCancellation:
return [:]
case .couldNotGenerateTunnelConfiguration(let underlyingError):
return [NSUnderlyingErrorKey: underlyingError]
Expand Down Expand Up @@ -1147,9 +1155,10 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
try? tokenStore.deleteToken()
#endif

// This is not really an error, we received a command to reset the connection
cancelTunnelWithError(nil)
completionHandler?(nil)
Task {
completionHandler?(nil)
await cancelTunnel(with: TunnelError.appRequestedCancellation)
Comment on lines +1159 to +1160
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason the completion handler comes before the cancel operation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - I want to make sure the completion handler is always called to prevent things from getting into a weird state. It's very likely that cancelling the tunnel will try to deallocate the PacketTunnelProvider and I'm ware of what might happen if the completion wasn't called before.

}
}

private func handleGetLastErrorMessage(completionHandler: ((Data?) -> Void)? = nil) {
Expand Down
Loading