Skip to content

Commit

Permalink
Egress Server Failure Detection & Recovery (#2657)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/72649045549333/1206669340827392/f
BSK PR: duckduckgo/BrowserServicesKit#786

**Description**:
- Use the WireGuard handshakes to determine when a connection fails by
checking that the last handshake time is less than 300 seconds (just
over two handshakes).
- When a failure is detected the client will call /register with server
set to the name of the current (failed) server and mode set to
failureRecovery. If the server is not in the "active" status on the
controller a new registration will be returned otherwise the previous
data will be returned (this data should be checked against the current
tunnel data including allowedIPs).

**Steps to test this PR**:
1. Launch the VPN and connect 
2. Block UDP traffic by following these instructions:
https://app.asana.com/0/0/1207029295107667/f
3. Open the Console.app and filter by Category: `Network Protection`.
The failure recovery logs are indicated with a 🟢 so you can also look
for them.
4. After around 5 minutes you will see logs indicating that a register
request has been made.
5. It will error with no recovery required as there is not a real
problem.

<!--
Tagging instructions
If this PR isn't ready to be merged for whatever reason it should be
marked with the `DO NOT MERGE` label (particularly if it's a draft)
If it's pending Product Review/PFR, please add the `Pending Product
Review` label.

If at any point it isn't actively being worked on/ready for
review/otherwise moving forward (besides the above PR/PFR exception)
strongly consider closing it (or not opening it in the first place). If
you decide not to close it, make sure it's labelled to make it clear the
PRs state and comment with more information.
-->

---
###### Internal references:
[Pull Request Review
Checklist](https://app.asana.com/0/1202500774821704/1203764234894239/f)
[Software Engineering
Expectations](https://app.asana.com/0/59792373528535/199064865822552)
[Technical Design
Template](https://app.asana.com/0/59792373528535/184709971311943)
[Pull Request
Documentation](https://app.asana.com/0/1202500774821704/1204012835277482/f)
  • Loading branch information
graeme authored Apr 30, 2024
1 parent c290fc6 commit e0f5c47
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 8 deletions.
16 changes: 15 additions & 1 deletion DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2510,6 +2510,8 @@
EEA3EEB12B24EBD000E8333A /* NetworkProtectionVPNCountryLabelsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEA3EEB02B24EBD000E8333A /* NetworkProtectionVPNCountryLabelsModel.swift */; };
EEA3EEB32B24EC0600E8333A /* VPNLocationViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEA3EEB22B24EC0600E8333A /* VPNLocationViewModel.swift */; };
EEAD7A7C2A1D3E20002A24E7 /* AppLauncher.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEAD7A6E2A1D3E1F002A24E7 /* AppLauncher.swift */; };
EEBCA0C62BD7CE2C004DF19C /* VPNFailureRecoveryPixel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBCA0C52BD7CE2C004DF19C /* VPNFailureRecoveryPixel.swift */; };
EEBCA0C72BD7CE2C004DF19C /* VPNFailureRecoveryPixel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBCA0C52BD7CE2C004DF19C /* VPNFailureRecoveryPixel.swift */; };
EEBCE6822BA444FA00B9DF00 /* XCUIElementExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBCE6812BA444FA00B9DF00 /* XCUIElementExtension.swift */; };
EEBCE6832BA463DD00B9DF00 /* NSImageExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B139AFC26B60BD800894F82 /* NSImageExtensions.swift */; };
EEBCE6842BA4643200B9DF00 /* NSSizeExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAC5E4E325D6BA9C007F5990 /* NSSizeExtension.swift */; };
Expand Down Expand Up @@ -4006,6 +4008,7 @@
EEA3EEB02B24EBD000E8333A /* NetworkProtectionVPNCountryLabelsModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkProtectionVPNCountryLabelsModel.swift; sourceTree = "<group>"; };
EEA3EEB22B24EC0600E8333A /* VPNLocationViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VPNLocationViewModel.swift; sourceTree = "<group>"; };
EEAD7A6E2A1D3E1F002A24E7 /* AppLauncher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppLauncher.swift; sourceTree = "<group>"; };
EEBCA0C52BD7CE2C004DF19C /* VPNFailureRecoveryPixel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VPNFailureRecoveryPixel.swift; sourceTree = "<group>"; };
EEBCE6812BA444FA00B9DF00 /* XCUIElementExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCUIElementExtension.swift; sourceTree = "<group>"; };
EEC111E3294D06020086524F /* JSAlert.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = JSAlert.storyboard; sourceTree = "<group>"; };
EEC111E5294D06290086524F /* JSAlertViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSAlertViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5123,6 +5126,7 @@
4B4D607D2A0B29FA00BCD287 /* NetworkExtensionTargets */ = {
isa = PBXGroup;
children = (
EEBCA0C12BD7CDDA004DF19C /* Pixels */,
4B41ED9F2B15437A001EEDF4 /* NetworkProtectionNotificationsPresenterFactory.swift */,
EEF12E6D2A2111880023E6BF /* MacPacketTunnelProvider.swift */,
7B0099802B65C6B300FE7C31 /* MacTransparentProxyProvider.swift */,
Expand Down Expand Up @@ -8073,6 +8077,14 @@
path = JSAlert;
sourceTree = "<group>";
};
EEBCA0C12BD7CDDA004DF19C /* Pixels */ = {
isa = PBXGroup;
children = (
EEBCA0C52BD7CE2C004DF19C /* VPNFailureRecoveryPixel.swift */,
);
path = Pixels;
sourceTree = "<group>";
};
EEBCE6802BA444FA00B9DF00 /* Common */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -10490,6 +10502,7 @@
4B25377A2A11C01700610219 /* UserText+NetworkProtectionExtensions.swift in Sources */,
B65DA5F42A77D3FA00CBEE8D /* BundleExtension.swift in Sources */,
EE66418D2B9B1981005BCD17 /* NetworkProtectionTokenStore+SubscriptionTokenKeychainStorage.swift in Sources */,
EEBCA0C72BD7CE2C004DF19C /* VPNFailureRecoveryPixel.swift in Sources */,
7B2E52252A5FEC09000C6D39 /* NetworkProtectionAgentNotificationsPresenter.swift in Sources */,
B602E8232A1E260E006D261F /* Bundle+NetworkProtectionExtensions.swift in Sources */,
4B2D062A2A11C0C900DE1F49 /* NetworkProtectionOptionKeyExtension.swift in Sources */,
Expand Down Expand Up @@ -10609,6 +10622,7 @@
4B4D609F2A0B2C7300BCD287 /* Logging.swift in Sources */,
EE66418C2B9B1981005BCD17 /* NetworkProtectionTokenStore+SubscriptionTokenKeychainStorage.swift in Sources */,
7B7DFB202B7E736B009EA1A3 /* MacPacketTunnelProvider.swift in Sources */,
EEBCA0C62BD7CE2C004DF19C /* VPNFailureRecoveryPixel.swift in Sources */,
4B4D60A12A0B2D6100BCD287 /* NetworkProtectionOptionKeyExtension.swift in Sources */,
B602E8182A1E2570006D261F /* URL+NetworkProtection.swift in Sources */,
B65DA5F52A77D3FA00CBEE8D /* BundleExtension.swift in Sources */,
Expand Down Expand Up @@ -12724,7 +12738,7 @@
repositoryURL = "https://github.com/duckduckgo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 141.1.2;
version = 142.0.0;
};
};
9FF521422BAA8FF300B9819B /* XCRemoteSwiftPackageReference "lottie-spm" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/BrowserServicesKit",
"state" : {
"revision" : "f8c73292d4d6724ec81f98bd29dbb2061f1a8cf6",
"version" : "141.1.2"
"revision" : "2681b5271a4e0582f175771737617adb8a4d6e78",
"version" : "142.0.0"
}
},
{
Expand Down Expand Up @@ -120,7 +120,7 @@
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "46989693916f56d1186bd59ac15124caef896560",
"version" : "1.3.1"
Expand All @@ -138,7 +138,7 @@
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"location" : "https://github.com/apple/swift-syntax",
"state" : {
"revision" : "64889f0c732f210a935a0ad7cda38f77f876262d",
"version" : "509.1.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,33 @@ final class MacPacketTunnelProvider: PacketTunnelProvider {
frequency: .dailyAndCount,
includeAppVersionParameter: true)
}
case .failureRecoveryAttempt(let step):
switch step {
case .started:
PixelKit.fire(
VPNFailureRecoveryPixel.vpnFailureRecoveryStarted,
frequency: .dailyAndCount,
includeAppVersionParameter: true
)
case .completed(.healthy):
PixelKit.fire(
VPNFailureRecoveryPixel.vpnFailureRecoveryCompletedHealthy,
frequency: .dailyAndCount,
includeAppVersionParameter: true
)
case .completed(.unhealthy):
PixelKit.fire(
VPNFailureRecoveryPixel.vpnFailureRecoveryCompletedUnhealthy,
frequency: .dailyAndCount,
includeAppVersionParameter: true
)
case .failed(let error):
PixelKit.fire(
VPNFailureRecoveryPixel.vpnFailureRecoveryFailed(error),
frequency: .dailyAndCount,
includeAppVersionParameter: true
)
}
}
}

Expand All @@ -300,6 +327,7 @@ final class MacPacketTunnelProvider: PacketTunnelProvider {

// MARK: - Initialization

@MainActor
@objc public init() {
let isSubscriptionEnabled = false

Expand Down Expand Up @@ -360,6 +388,7 @@ final class MacPacketTunnelProvider: PacketTunnelProvider {

/// Observe server changes to broadcast those changes through distributed notifications.
///
@MainActor
private func observeServerChanges() {
lastSelectedServerInfoPublisher.sink { [weak self] server in
self?.lastStatusChangeDate = Date()
Expand Down Expand Up @@ -404,6 +433,7 @@ final class MacPacketTunnelProvider: PacketTunnelProvider {

/// Broadcasts the current server information.
///
@MainActor
private func broadcastLastSelectedServerInfo() {
broadcast(lastSelectedServerInfo)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// VPNFailureRecoveryPixel.swift
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
import PixelKit

/// PrivacyPro pixels.
///
/// Ref: https://app.asana.com/0/0/1206939413299475/f
///
public enum VPNFailureRecoveryPixel: PixelKitEventV2 {

/// This pixel is emitted when the last handshake diff is greater than n minutes and an attempt to recover is made (/register is called with failureRecovery)
///
case vpnFailureRecoveryStarted

/// This pixel is emitted when the recovery attempt failed due to any reason.
///
case vpnFailureRecoveryFailed(Error)

/// This pixel is emitted when the recovery attempt completed and the server was healthy and no further action needs to be taken.
///
case vpnFailureRecoveryCompletedHealthy

/// This pixel is emitted when the recovery attempt completed and the server was unhealthy resulting to reconnecting to a different server.
///
case vpnFailureRecoveryCompletedUnhealthy

public var name: String {
switch self {
case .vpnFailureRecoveryStarted:
return "m_mac_netp_ev_failure_recovery_started"
case .vpnFailureRecoveryFailed:
return "m_mac_netp_ev_failure_recovery_failed"
case .vpnFailureRecoveryCompletedHealthy:
return "m_mac_netp_ev_failure_recovery_completed_server_healthy"
case .vpnFailureRecoveryCompletedUnhealthy:
return "m_mac_netp_ev_failure_recovery_completed_server_unhealthy"
}
}

public var error: Error? {
switch self {
case .vpnFailureRecoveryStarted, .vpnFailureRecoveryCompletedHealthy, .vpnFailureRecoveryCompletedUnhealthy: return nil
case .vpnFailureRecoveryFailed(let error): return error
}
}

public var parameters: [String: String]? {
nil
}
}
2 changes: 1 addition & 1 deletion LocalPackages/DataBrokerProtection/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let package = Package(
targets: ["DataBrokerProtection"])
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "141.1.2"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "142.0.0"),
.package(path: "../SwiftUIExtensions"),
.package(path: "../XPCHelper"),
],
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/NetworkProtectionMac/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let package = Package(
.library(name: "NetworkProtectionUI", targets: ["NetworkProtectionUI"]),
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "141.1.2"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "142.0.0"),
.package(url: "https://github.com/airbnb/lottie-spm", exact: "4.4.1"),
.package(path: "../XPCHelper"),
.package(path: "../SwiftUIExtensions"),
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/SubscriptionUI/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
targets: ["SubscriptionUI"]),
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "141.1.2"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "142.0.0"),
.package(path: "../SwiftUIExtensions")
],
targets: [
Expand Down

0 comments on commit e0f5c47

Please sign in to comment.