-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathFirewallRepair.swift
144 lines (127 loc) · 5.84 KB
/
FirewallRepair.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
//
// FirewallRepair.swift
// Lockdown
//
// Created by Oleg Dreyman on 21.10.2020.
// Copyright © 2020 Confirmed Inc. All rights reserved.
//
import Foundation
import CocoaLumberjackSwift
import BackgroundTasks
enum FirewallRepair {
static let identifier = "com.confirmed.lockdown.firewallscheduler"
enum Result {
case repairAttempted
case failed(Swift.Error?)
case noAction
}
enum Context: CustomDebugStringConvertible {
case backgroundRefresh
case homeScreenDidLoad
var debugDescription: String {
switch self {
case .backgroundRefresh:
return "Background Check"
case .homeScreenDidLoad:
return "Home"
}
}
}
@available(iOS 13.0, *)
static func handleAppRefresh(_ task: BGTask) {
DDLogInfo("BGTask: Handle App Refresh called")
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
let operation = BlockOperation {
DDLogInfo("BGTask: Operation started")
FirewallRepair.run(context: .backgroundRefresh) { (result) in
DDLogInfo("BGTask: Result: \(result)")
}
}
operation.completionBlock = {
DDLogInfo("BGTask: Operation completion block")
let success = !operation.isCancelled
DDLogInfo("BGTask: Operation completion success: \(success)")
task.setTaskCompleted(success: success)
}
queue.addOperation(operation)
task.expirationHandler = {
DDLogError("BGTask: Expiration Handler called")
queue.cancelAllOperations()
}
FirewallRepair.reschedule()
}
static func reschedule() {
if #available(iOS 13.0, *) {
DDLogInfo("BGTask: Cancelling all task requests")
BGTaskScheduler.shared.cancelAllTaskRequests()
let timeIntervalSeconds: Double = 3600
let request = BGAppRefreshTaskRequest(identifier: FirewallRepair.identifier)
request.earliestBeginDate = Date(timeIntervalSinceNow: timeIntervalSeconds)
DDLogInfo("BGTask: Scheduling app refresh id: \(FirewallRepair.identifier), earliest date \(Date(timeIntervalSinceNow: TimeInterval(timeIntervalSeconds)))")
do {
try BGTaskScheduler.shared.submit(request)
} catch {
DDLogError("Could not schedule app refresh: \(error)")
}
}
else {
DDLogInfo("BGTask: Not iOS 13, not calling reschedule")
}
}
static func run(context: Context, completion: @escaping (FirewallRepair.Result) -> Void = { _ in }) {
DDLogInfo("Repair \(context): Starting")
// Check 2 conditions for firewall restart, but reload manager first to get non-stale one
FirewallController.shared.refreshManager(completion: { error in
if let e = error {
DDLogError("Repair \(context): Error refreshing Manager in \(context): \(e)")
completion(.failed(e))
return
}
DDLogInfo("Repair \(context): refreshed manager")
DDLogInfo("Repair \(context): userWantsFirewallEnabled \(getUserWantsFirewallEnabled())")
DDLogInfo("Repair \(context): firewallStatus \(FirewallController.shared.status())")
if getUserWantsFirewallEnabled() && (FirewallController.shared.status() == .connected || FirewallController.shared.status() == .invalid || FirewallController.shared.status() == .disconnected) {
DDLogInfo("Repair \(context): user wants enabled")
if (appHasJustBeenUpgradedOrIsNewInstall()) {
DDLogInfo("Repair \(context): APP UPGRADED, REFRESHING DEFAULT BLOCK LISTS, WHITELISTS, RESTARTING FIREWALL")
setupFirewallDefaultBlockLists()
setupLockdownWhitelistedDomains()
FirewallController.shared.restart(completion: {
error in
if error != nil {
DDLogError("Error restarting firewall on \(context): \(error!)")
}
completion(.repairAttempted)
})
}
else {
Client.getBlockedDomainTest().done {
DDLogError("Repair \(context): Repair Fetch Test Failed: Connected to \(testFirewallDomain) even though it's supposed to be blocked")
DDLogError("Repair \(context): Doing repair")
FirewallController.shared.restart(completion: {
error in
if error != nil {
DDLogError("Repair \(context): Error restarting firewall on \(context): \(error!)")
}
DDLogError("Repair \(context): Returned from restart, no error")
completion(.repairAttempted)
})
}.catch { error in
let nsError = error as NSError
if nsError.domain == NSURLErrorDomain {
DDLogInfo("Repair \(context): Successful blocking of \(testFirewallDomain) with NSURLErrorDomain error: \(nsError)")
}
else {
DDLogInfo("Repair \(context): Successful blocking of \(testFirewallDomain), but seeing non-NSURLErrorDomain error: \(error)")
}
completion(.repairAttempted)
}
}
}
else {
completion(.noAction)
}
})
}
}