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
|
//
// AppDelegate.swift
// MullvadVPN
//
// Created by pronebird on 19/03/2019.
// Copyright © 2019 Mullvad VPN AB. All rights reserved.
//
import UIKit
import StoreKit
import Logging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var rootContainer: RootContainerViewController?
#if targetEnvironment(simulator)
let simulatorTunnelProvider = SimulatorTunnelProviderHost()
#endif
#if DEBUG
private let packetTunnelLogForwarder = LogStreamer<UTF8>(fileURLs: [ApplicationConfiguration.packetTunnelLogFileURL!])
#endif
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Setup logging
initLoggingSystem(bundleIdentifier: Bundle.main.bundleIdentifier!)
#if DEBUG
let stdoutStream = TextFileOutputStream.standardOutputStream()
packetTunnelLogForwarder.start { (str) in
stdoutStream.write("\(str)\n")
}
#endif
#if targetEnvironment(simulator)
// Configure mock tunnel provider on simulator
SimulatorTunnelProvider.shared.delegate = simulatorTunnelProvider
#endif
// Create an app window
self.window = UIWindow(frame: UIScreen.main.bounds)
// Set an empty view controller while loading tunnels
let launchController = UIViewController()
launchController.view.backgroundColor = .primaryColor
self.window?.rootViewController = launchController
// Update relays
RelayCache.shared.updateRelays()
// Load tunnels
let accountToken = Account.shared.token
TunnelManager.shared.loadTunnel(accountToken: accountToken) { (result) in
DispatchQueue.main.async {
if case .failure(let error) = result {
fatalError(error.displayChain(message: "Failed to load the tunnel for account"))
}
let rootViewController = RootContainerViewController()
rootViewController.delegate = self
let showMainController = { (_ animated: Bool) in
self.showConnectController(in: rootViewController, animated: animated) {
self.didPresentTheMainController()
}
}
if Account.shared.isAgreedToTermsOfService {
showMainController(false)
} else {
self.showTermsOfService(in: rootViewController) {
Account.shared.agreeToTermsOfService()
showMainController(true)
}
}
self.window?.rootViewController = rootViewController
self.rootContainer = rootViewController
}
}
// Show the window
self.window?.makeKeyAndVisible()
return true
}
func applicationDidBecomeActive(_ application: UIApplication) {
TunnelManager.shared.refreshTunnelState(completionHandler: nil)
}
private func didPresentTheMainController() {
let paymentManager = AppStorePaymentManager.shared
paymentManager.delegate = self
paymentManager.startPaymentQueueMonitoring()
Account.shared.startPaymentMonitoring(with: paymentManager)
}
private func showTermsOfService(in rootViewController: RootContainerViewController, completionHandler: @escaping () -> Void) {
let consentViewController = ConsentViewController()
consentViewController.completionHandler = completionHandler
rootViewController.setViewControllers([consentViewController], animated: false)
}
private func showConnectController(
in rootViewController: RootContainerViewController,
animated: Bool,
completionHandler: @escaping () -> Void)
{
let loginViewController = LoginViewController()
loginViewController.delegate = self
var viewControllers: [UIViewController] = [loginViewController]
if Account.shared.isLoggedIn {
viewControllers.append(ConnectViewController())
}
rootViewController.setViewControllers(viewControllers, animated: animated, completion: completionHandler)
}
}
extension AppDelegate: RootContainerViewControllerDelegate {
func rootContainerViewControllerShouldShowSettings(_ controller: RootContainerViewController, navigateTo route: SettingsNavigationRoute?, animated: Bool) {
let settingsController = SettingsViewController(style: .grouped)
settingsController.settingsDelegate = self
let navController = SettingsNavigationController(navigationBarClass: CustomNavigationBar.self, toolbarClass: nil)
navController.pushViewController(settingsController, animated: false)
if let route = route {
settingsController.navigate(to: route)
}
controller.present(navController, animated: animated)
}
func rootContainerViewSupportedInterfaceOrientations(_ controller: RootContainerViewController) -> UIInterfaceOrientationMask {
switch self.window?.traitCollection.userInterfaceIdiom {
case .pad:
return [.landscape, .portrait]
case .phone:
return [.portrait]
default:
fatalError("Not supported")
}
}
}
extension AppDelegate: LoginViewControllerDelegate {
func loginViewControllerDidLogin(_ controller: LoginViewController) {
rootContainer?.pushViewController(ConnectViewController(), animated: true)
}
}
extension AppDelegate: SettingsViewControllerDelegate {
func settingsViewController(_ controller: SettingsViewController, didFinishWithReason reason: SettingsDismissReason) {
if case .userLoggedOut = reason {
rootContainer?.popToRootViewController(animated: false)
let loginController = rootContainer?.topViewController as? LoginViewController
loginController?.reset()
}
controller.dismiss(animated: true)
}
}
extension AppDelegate: AppStorePaymentManagerDelegate {
func appStorePaymentManager(_ manager: AppStorePaymentManager,
didRequestAccountTokenFor payment: SKPayment) -> String?
{
// Since we do not persist the relation between the payment and account token between the
// app launches, we assume that all successful purchases belong to the active account token.
return Account.shared.token
}
}
|