This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDuckDuckGoVPNAppDelegate.swift
538 lines (449 loc) · 21.8 KB
/
DuckDuckGoVPNAppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//
// DuckDuckGoVPNAppDelegate.swift
//
// Copyright © 2023 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 AppLauncher
import BrowserServicesKit
import Cocoa
import Combine
import Common
import Configuration
import FeatureFlags
import LoginItems
import Networking
import NetworkExtension
import NetworkProtection
import NetworkProtectionProxy
import NetworkProtectionUI
import os.log
import PixelKit
import ServiceManagement
import Subscription
import SwiftUICore
import VPNAppLauncher
@objc(Application)
final class DuckDuckGoVPNApplication: NSApplication {
public var accountManager: AccountManager
private let _delegate: DuckDuckGoVPNAppDelegate
override init() {
Logger.networkProtection.log("🟢 Status Bar Agent starting\nPath: (\(Bundle.main.bundlePath, privacy: .public))\nVersion: \("\(Bundle.main.versionNumber!).\(Bundle.main.buildNumber)", privacy: .public)\nPID: \(NSRunningApplication.current.processIdentifier, privacy: .public)")
// prevent agent from running twice
if let anotherInstance = NSRunningApplication.runningApplications(withBundleIdentifier: Bundle.main.bundleIdentifier!).first(where: { $0 != .current }) {
Logger.networkProtection.error("Stopping: another instance is running: \(anotherInstance.processIdentifier, privacy: .public).")
exit(0)
}
// MARK: - Configure Subscription
let subscriptionAppGroup = Bundle.main.appGroup(bundle: .subs)
let subscriptionUserDefaults = UserDefaults(suiteName: subscriptionAppGroup)!
let subscriptionEnvironment = DefaultSubscriptionManager.getSavedOrDefaultEnvironment(userDefaults: subscriptionUserDefaults)
let subscriptionEndpointService = DefaultSubscriptionEndpointService(currentServiceEnvironment: subscriptionEnvironment.serviceEnvironment)
let authEndpointService = DefaultAuthEndpointService(currentServiceEnvironment: subscriptionEnvironment.serviceEnvironment)
let entitlementsCache = UserDefaultsCache<[Entitlement]>(userDefaults: subscriptionUserDefaults,
key: UserDefaultsCacheKey.subscriptionEntitlements,
settings: UserDefaultsCacheSettings(defaultExpirationInterval: .minutes(20)))
let accessTokenStorage = SubscriptionTokenKeychainStorage(keychainType: .dataProtection(.named(subscriptionAppGroup)))
accountManager = DefaultAccountManager(accessTokenStorage: accessTokenStorage,
entitlementsCache: entitlementsCache,
subscriptionEndpointService: subscriptionEndpointService,
authEndpointService: authEndpointService)
_delegate = DuckDuckGoVPNAppDelegate(accountManager: accountManager,
accessTokenStorage: accessTokenStorage,
subscriptionEnvironment: subscriptionEnvironment)
super.init()
setupPixelKit()
self.delegate = _delegate
accountManager.delegate = _delegate
#if DEBUG
if accountManager.accessToken != nil {
Logger.networkProtection.error("🟢 VPN Agent found token")
} else {
Logger.networkProtection.error("VPN Agent found no token")
}
#endif
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@MainActor
private func setupPixelKit() {
let dryRun: Bool
#if DEBUG
dryRun = true
#else
dryRun = false
#endif
let pixelSource: String
#if NETP_SYSTEM_EXTENSION
pixelSource = "vpnAgent"
#else
pixelSource = "vpnAgentAppStore"
#endif
PixelKit.setUp(dryRun: dryRun,
appVersion: AppVersion.shared.versionNumber,
source: pixelSource,
defaultHeaders: [:],
defaults: .netP) { (pixelName: String, headers: [String: String], parameters: [String: String], _, _, onComplete: @escaping PixelKit.CompletionBlock) in
let url = URL.pixelUrl(forPixelNamed: pixelName)
let apiHeaders = APIRequest.Headers(additionalHeaders: headers) // workaround - Pixel class should really handle APIRequest.Headers by itself
let configuration = APIRequest.Configuration(url: url, method: .get, queryParameters: parameters, headers: apiHeaders)
let request = APIRequest(configuration: configuration)
request.fetch { _, error in
onComplete(error == nil, error)
}
}
}
}
@main
final class DuckDuckGoVPNAppDelegate: NSObject, NSApplicationDelegate {
private static let recentThreshold: TimeInterval = 5.0
private let appLauncher = AppLauncher()
private let accountManager: AccountManager
private let accessTokenStorage: SubscriptionTokenKeychainStorage
private let configurationStore = ConfigurationStore()
private let configurationManager: ConfigurationManager
private var configurationSubscription: AnyCancellable?
private let privacyConfigurationManager = VPNPrivacyConfigurationManager(internalUserDecider: DefaultInternalUserDecider(store: UserDefaults.appConfiguration))
private lazy var featureFlagger = DefaultFeatureFlagger(
internalUserDecider: privacyConfigurationManager.internalUserDecider,
privacyConfigManager: privacyConfigurationManager,
localOverrides: FeatureFlagLocalOverrides(
keyValueStore: UserDefaults.appConfiguration,
actionHandler: FeatureFlagOverridesPublishingHandler<FeatureFlag>()
),
experimentManager: nil,
for: FeatureFlag.self)
public init(accountManager: AccountManager,
accessTokenStorage: SubscriptionTokenKeychainStorage,
subscriptionEnvironment: SubscriptionEnvironment) {
self.accountManager = accountManager
self.accessTokenStorage = accessTokenStorage
self.tunnelSettings = VPNSettings(defaults: .netP)
self.tunnelSettings.alignTo(subscriptionEnvironment: subscriptionEnvironment)
self.configurationManager = ConfigurationManager(privacyConfigManager: privacyConfigurationManager, store: configurationStore)
}
private var cancellables = Set<AnyCancellable>()
var proxyExtensionBundleID: String {
Bundle.proxyExtensionBundleID
}
var tunnelExtensionBundleID: String {
Bundle.tunnelExtensionBundleID
}
private lazy var networkExtensionController = NetworkExtensionController(extensionBundleID: tunnelExtensionBundleID)
private var storeProxySettingsInProviderConfiguration: Bool {
#if NETP_SYSTEM_EXTENSION
true
#else
false
#endif
}
private let tunnelSettings: VPNSettings
private lazy var userDefaults = UserDefaults.netP
private lazy var proxySettings: TransparentProxySettings = {
let settings = TransparentProxySettings(defaults: .netP)
#if APPSTORE
settings.proxyAvailable = false
#else
settings.proxyAvailable = true
#endif
return settings
}()
@MainActor
private lazy var vpnProxyLauncher = VPNProxyLauncher(
tunnelController: tunnelController,
proxyController: proxyController)
@MainActor
private lazy var proxyController: TransparentProxyController = {
let eventHandler = TransparentProxyControllerEventHandler(logger: .transparentProxyLogger)
let controller = TransparentProxyController(
extensionID: proxyExtensionBundleID,
storeSettingsInProviderConfiguration: storeProxySettingsInProviderConfiguration,
settings: proxySettings,
eventHandler: eventHandler) { [weak self] manager in
guard let self else { return }
manager.localizedDescription = "DuckDuckGo VPN Proxy"
if !manager.isEnabled {
manager.isEnabled = true
}
manager.protocolConfiguration = {
let protocolConfiguration = manager.protocolConfiguration as? NETunnelProviderProtocol ?? NETunnelProviderProtocol()
protocolConfiguration.serverAddress = "127.0.0.1" // Dummy address... the NetP service will take care of grabbing a real server
protocolConfiguration.providerBundleIdentifier = self.proxyExtensionBundleID
// always-on
protocolConfiguration.disconnectOnSleep = false
// kill switch
// protocolConfiguration.enforceRoutes = false
// this setting breaks Connection Tester
// protocolConfiguration.includeAllNetworks = settings.includeAllNetworks
// This is intentionally not used but left here for documentation purposes.
// The reason for this is that we want to have full control of the routes that
// are excluded, so instead of using this setting we're just configuring the
// excluded routes through our VPNSettings class, which our extension reads directly.
// protocolConfiguration.excludeLocalNetworks = settings.excludeLocalNetworks
return protocolConfiguration
}()
}
return controller
}()
@MainActor
private lazy var tunnelController = NetworkProtectionTunnelController(
networkExtensionBundleID: tunnelExtensionBundleID,
networkExtensionController: networkExtensionController,
featureFlagger: featureFlagger,
settings: tunnelSettings,
defaults: userDefaults,
accessTokenStorage: accessTokenStorage)
/// An IPC server that provides access to the tunnel controller.
///
/// This is used by our main app to control the tunnel through the VPN login item.
///
@MainActor
private lazy var tunnelControllerIPCService: TunnelControllerIPCService = {
let ipcServer = TunnelControllerIPCService(
tunnelController: tunnelController,
uninstaller: vpnUninstaller,
networkExtensionController: networkExtensionController,
statusReporter: statusReporter)
ipcServer.activate()
return ipcServer
}()
@MainActor
private lazy var statusObserver = ConnectionStatusObserverThroughSession(
tunnelSessionProvider: tunnelController,
platformSnoozeTimingStore: NetworkProtectionSnoozeTimingStore(userDefaults: .netP),
platformNotificationCenter: NSWorkspace.shared.notificationCenter,
platformDidWakeNotification: NSWorkspace.didWakeNotification)
@MainActor
private lazy var statusReporter: NetworkProtectionStatusReporter = {
let errorObserver = ConnectionErrorObserverThroughSession(
tunnelSessionProvider: tunnelController,
platformNotificationCenter: NSWorkspace.shared.notificationCenter,
platformDidWakeNotification: NSWorkspace.didWakeNotification)
let serverInfoObserver = ConnectionServerInfoObserverThroughSession(
tunnelSessionProvider: tunnelController,
platformNotificationCenter: NSWorkspace.shared.notificationCenter,
platformDidWakeNotification: NSWorkspace.didWakeNotification)
let dataVolumeObserver = DataVolumeObserverThroughSession(
tunnelSessionProvider: tunnelController,
platformNotificationCenter: NSWorkspace.shared.notificationCenter,
platformDidWakeNotification: NSWorkspace.didWakeNotification)
return DefaultNetworkProtectionStatusReporter(
statusObserver: statusObserver,
serverInfoObserver: serverInfoObserver,
connectionErrorObserver: errorObserver,
connectivityIssuesObserver: DisabledConnectivityIssueObserver(),
controllerErrorMessageObserver: ControllerErrorMesssageObserverThroughDistributedNotifications(),
dataVolumeObserver: dataVolumeObserver,
knownFailureObserver: KnownFailureObserverThroughDistributedNotifications()
)
}()
@MainActor
private lazy var vpnAppEventsHandler = {
VPNAppEventsHandler(tunnelController: tunnelController)
}()
@MainActor
private lazy var vpnUninstaller: VPNUninstaller = {
VPNUninstaller(
tunnelController: tunnelController,
networkExtensionController: networkExtensionController)
}()
/// The status bar NetworkProtection menu
///
/// For some reason the App will crash if this is initialized right away, which is why it was changed to be lazy.
///
@MainActor
private lazy var networkProtectionMenu: StatusBarMenu = {
makeStatusBarMenu()
}()
private func statusViewSubmenu() -> [StatusBarMenu.MenuItem] {
let appLauncher = AppLauncher(appBundleURL: Bundle.main.bundleURL)
let proxySettings = TransparentProxySettings(defaults: .netP)
var menuItems = [StatusBarMenu.MenuItem]()
if UserDefaults.netP.networkProtectionOnboardingStatus == .completed {
menuItems.append(
.text(icon: Image(.settings16), title: UserText.vpnStatusViewVPNSettingsMenuItemTitle, action: {
try? await appLauncher.launchApp(withCommand: VPNAppLaunchCommand.showSettings)
}))
}
menuItems.append(contentsOf: [
.textWithDetail(
icon: Image(.window16),
title: UserText.vpnStatusViewExcludedAppsMenuItemTitle,
detail: "(\(proxySettings.excludedApps.count))",
action: { [weak self] in
try? await self?.appLauncher.launchApp(withCommand: VPNAppLaunchCommand.manageExcludedApps)
}),
.textWithDetail(
icon: Image(.globe16),
title: UserText.vpnStatusViewExcludedDomainsMenuItemTitle,
detail: "(\(proxySettings.excludedDomains.count))",
action: { [weak self] in
try? await self?.appLauncher.launchApp(withCommand: VPNAppLaunchCommand.manageExcludedDomains)
}),
.divider(),
.text(icon: Image(.help16), title: UserText.vpnStatusViewFAQMenuItemTitle, action: { [weak self] in
try? await self?.appLauncher.launchApp(withCommand: VPNAppLaunchCommand.showFAQ)
}),
.text(icon: Image(.support16), title: UserText.vpnStatusViewSendFeedbackMenuItemTitle, action: { [weak self] in
try? await self?.appLauncher.launchApp(withCommand: VPNAppLaunchCommand.shareFeedback)
})
])
return menuItems
}
private func legacyStatusViewSubmenu() -> [StatusBarMenu.MenuItem] {
[
.text(title: UserText.networkProtectionStatusMenuVPNSettings, action: { [weak self] in
try? await self?.appLauncher.launchApp(withCommand: VPNAppLaunchCommand.showSettings)
}),
.text(title: UserText.networkProtectionStatusMenuFAQ, action: { [weak self] in
try? await self?.appLauncher.launchApp(withCommand: VPNAppLaunchCommand.showFAQ)
}),
.text(title: UserText.networkProtectionStatusMenuSendFeedback, action: { [weak self] in
try? await self?.appLauncher.launchApp(withCommand: VPNAppLaunchCommand.shareFeedback)
}),
.text(title: UserText.networkProtectionStatusMenuOpenDuckDuckGo, action: { [weak self] in
try? await self?.appLauncher.launchApp(withCommand: VPNAppLaunchCommand.justOpen)
}),
]
}
@MainActor
private func makeStatusBarMenu() -> StatusBarMenu {
#if DEBUG
let iconProvider = DebugMenuIconProvider()
#elseif REVIEW
let iconProvider = ReviewMenuIconProvider()
#else
let iconProvider = MenuIconProvider()
#endif
let onboardingStatusPublisher = UserDefaults.netP.publisher(for: \.networkProtectionOnboardingStatusRawValue).map { rawValue in
OnboardingStatus(rawValue: rawValue) ?? .default
}.eraseToAnyPublisher()
let model = StatusBarMenuModel(vpnSettings: .init(defaults: .netP))
let uiActionHandler = VPNUIActionHandler(
appLauncher: appLauncher,
proxySettings: proxySettings)
return StatusBarMenu(
model: model,
onboardingStatusPublisher: onboardingStatusPublisher,
statusReporter: statusReporter,
controller: tunnelController,
iconProvider: iconProvider,
uiActionHandler: uiActionHandler,
menuItems: { [weak self] in
guard let self else { return [] }
guard featureFlagger.isFeatureOn(.networkProtectionAppExclusions) else {
return legacyStatusViewSubmenu()
}
return statusViewSubmenu()
},
agentLoginItem: nil,
isMenuBarStatusView: true,
userDefaults: .netP,
locationFormatter: DefaultVPNLocationFormatter(),
uninstallHandler: { [weak self] in
guard let self else { return }
do {
try await self.vpnUninstaller.uninstall(includingSystemExtension: true)
exit(EXIT_SUCCESS)
} catch {
// Intentional no-op: we already anonymously track VPN uninstallation failures using
// pixels within the vpn uninstaller.
}
}
)
}
@MainActor
func applicationDidFinishLaunching(_ aNotification: Notification) {
APIRequest.Headers.setUserAgent(UserAgent.duckDuckGoUserAgent())
Logger.networkProtection.log("DuckDuckGoVPN started")
// Setup Remote Configuration
Configuration.setURLProvider(VPNAgentConfigurationURLProvider())
configurationManager.start()
// Load cached config (if any)
privacyConfigurationManager.reload(etag: configurationStore.loadEtag(for: .privacyConfiguration), data: configurationStore.loadData(for: .privacyConfiguration))
// It's important for this to be set-up after the privacy configuration is loaded
// as it relies on it for the remote feature flag.
TipKitAppEventHandler(featureFlagger: featureFlagger).appDidFinishLaunching()
setupMenuVisibility()
Task { @MainActor in
// Initialize lazy properties
_ = tunnelControllerIPCService
_ = vpnProxyLauncher
vpnAppEventsHandler.appDidFinishLaunching()
let launchInformation = LoginItemLaunchInformation(agentBundleID: Bundle.main.bundleIdentifier!, defaults: .netP)
let launchedOnStartup = launchInformation.wasLaunchedByStartup
launchInformation.update()
setUpSubscriptionMonitoring()
if launchedOnStartup {
Task {
let isConnected = await tunnelController.isConnected
if !isConnected && tunnelSettings.connectOnLogin {
await tunnelController.start()
}
}
}
}
}
@MainActor
private func setupMenuVisibility() {
if tunnelSettings.showInMenuBar {
networkProtectionMenu.show()
} else {
networkProtectionMenu.hide()
}
tunnelSettings.showInMenuBarPublisher.sink { [weak self] showInMenuBar in
Task { @MainActor in
if showInMenuBar {
self?.networkProtectionMenu.show()
} else {
self?.networkProtectionMenu.hide()
}
}
}.store(in: &cancellables)
}
private lazy var entitlementMonitor = NetworkProtectionEntitlementMonitor()
private func setUpSubscriptionMonitoring() {
guard accountManager.isUserAuthenticated else { return }
let entitlementsCheck = {
await self.accountManager.hasEntitlement(forProductName: .networkProtection, cachePolicy: .reloadIgnoringLocalCacheData)
}
Task {
await entitlementMonitor.start(entitlementCheck: entitlementsCheck) { [weak self] result in
switch result {
case .validEntitlement:
UserDefaults.netP.networkProtectionEntitlementsExpired = false
case .invalidEntitlement:
UserDefaults.netP.networkProtectionEntitlementsExpired = true
guard let self else { return }
Task {
let isConnected = await self.tunnelController.isConnected
if isConnected {
await self.tunnelController.stop()
DistributedNotificationCenter.default().post(.showExpiredEntitlementNotification)
}
}
case .error:
break
}
}
}
}
}
extension DuckDuckGoVPNAppDelegate: AccountManagerKeychainAccessDelegate {
public func accountManagerKeychainAccessFailed(accessType: AccountKeychainAccessType, error: AccountKeychainAccessError) {
PixelKit.fire(PrivacyProErrorPixel.privacyProKeychainAccessError(accessType: accessType, accessError: error),
frequency: .legacyDailyAndCount)
}
}