-
Notifications
You must be signed in to change notification settings - Fork 417
/
VPNWidget.swift
301 lines (258 loc) · 10.2 KB
/
VPNWidget.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
//
// VPNWidget.swift
// DuckDuckGo
//
// 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 Foundation
import AppIntents
import Core
import DesignResourcesKit
import SwiftUI
import WidgetKit
import NetworkExtension
import NetworkProtection
#if ALPHA
enum VPNStatus {
case status(NEVPNStatus)
case error
case notConfigured
}
struct VPNStatusTimelineEntry: TimelineEntry {
let date: Date
let status: VPNStatus
let location: String
internal init(date: Date, status: VPNStatus = .notConfigured, location: String) {
self.date = date
self.status = status
self.location = location
}
}
class VPNStatusTimelineProvider: TimelineProvider {
typealias Entry = VPNStatusTimelineEntry
func placeholder(in context: Context) -> VPNStatusTimelineEntry {
return VPNStatusTimelineEntry(date: Date(), status: .status(.connected), location: "Los Angeles, CA")
}
func getSnapshot(in context: Context, completion: @escaping (VPNStatusTimelineEntry) -> Void) {
let entry = VPNStatusTimelineEntry(date: Date(), status: .status(.connected), location: "Los Angeles, CA")
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<VPNStatusTimelineEntry>) -> Void) {
NETunnelProviderManager.loadAllFromPreferences { managers, error in
let defaults = UserDefaults.networkProtectionGroupDefaults
let location = defaults.string(forKey: NetworkProtectionUserDefaultKeys.lastSelectedServer) ?? "Unknown Location"
let expiration = Date().addingTimeInterval(TimeInterval.minutes(5))
if error != nil {
let entry = VPNStatusTimelineEntry(date: expiration, status: .error, location: location)
let timeline = Timeline(entries: [entry], policy: .atEnd)
completion(timeline)
return
}
guard let manager = managers?.first else {
let entry = VPNStatusTimelineEntry(date: expiration, status: .notConfigured, location: location)
let timeline = Timeline(entries: [entry], policy: .atEnd)
completion(timeline)
return
}
let status = manager.connection.status
let entry = VPNStatusTimelineEntry(date: expiration, status: .status(status), location: location)
let timeline = Timeline(entries: [entry], policy: .atEnd)
completion(timeline)
}
}
}
extension NEVPNStatus {
var description: String {
switch self {
case .connected: return "Connected"
case .connecting: return "Connecting"
case .disconnected: return "Disconnected"
case .disconnecting: return "Disconnecting"
case .invalid: return "Invalid"
case .reasserting: return "Reasserting"
default: return "Unknown Status"
}
}
var isConnected: Bool {
switch self {
case .connected, .connecting, .reasserting: return true
case .disconnecting, .disconnected: return false
default: return false
}
}
}
@available(iOSApplicationExtension 17.0, *)
struct VPNStatusView: View {
@Environment(\.widgetFamily) var family: WidgetFamily
var entry: VPNStatusTimelineProvider.Entry
@ViewBuilder
var body: some View {
Group {
switch entry.status {
case .status(let status):
HStack {
connectionView(with: status)
.padding([.leading, .trailing], 16)
Spacer()
}
case .error:
Text("Error")
.foregroundStyle(Color.black)
case .notConfigured:
Text("VPN Not Configured")
.foregroundStyle(Color.black)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.containerBackground(for: .widget) {
switch entry.status {
case .status(let status):
switch status {
case .connecting, .connected, .reasserting:
Color.vpnWidgetBackgroundColor
case .disconnecting, .disconnected, .invalid:
Color.white
@unknown default:
Color.white
}
case .error, .notConfigured:
Color.white
}
}
}
private func connectionView(with status: NEVPNStatus) -> some View {
HStack {
VStack(alignment: .leading, spacing: 0) {
Image(headerImageName(with: status))
.frame(width: 50, height: 54)
.padding(.top, 15)
Spacer()
Text(title(with: status))
.font(.system(size: 16, weight: .semibold))
.fontWeight(.semibold)
.foregroundStyle(status.isConnected ? Color.white : Color.black)
Text(status.isConnected ? entry.location : "VPN is Off")
.font(.system(size: 12, weight: .medium))
.foregroundStyle(status.isConnected ? Color.white : Color.black)
.opacity(status.isConnected ? 0.8 : 0.6)
switch status {
case .connected, .connecting, .reasserting:
Button(intent: DisableVPNIntent()) {
Text("Disconnect")
.font(.system(size: 15, weight: .medium))
.fontWeight(.semibold)
}
.foregroundStyle(Color.vpnWidgetBackgroundColor)
.buttonStyle(.borderedProminent)
.tint(.white)
.disabled(status != .connected)
.padding(.top, 6)
.padding(.bottom, 16)
case .disconnected, .disconnecting:
Button(intent: EnableVPNIntent()) {
Text("Connect")
.font(.system(size: 15, weight: .medium))
.fontWeight(.semibold)
}
.foregroundStyle(.white)
.buttonStyle(.borderedProminent)
.tint(Color.vpnWidgetBackgroundColor)
.disabled(status != .disconnected)
.padding(.top, 6)
.padding(.bottom, 16)
default:
Spacer()
}
}
}
}
private func headerImageName(with status: NEVPNStatus) -> String {
switch status {
case .connecting, .connected, .reasserting: return "vpn-on"
case .disconnecting, .disconnected: return "vpn-off"
case .invalid: return "vpn-off"
@unknown default: return "vpn-off"
}
}
private func title(with status: NEVPNStatus) -> String {
switch status {
case .connecting, .connected, .reasserting: return "Protected"
case .disconnecting, .disconnected: return "Unprotected"
case .invalid: return "Invalid"
@unknown default: return "Unknown"
}
}
}
@available(iOSApplicationExtension 17.0, *)
struct VPNStatusWidget: Widget {
let kind: String = "VPNStatusWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: VPNStatusTimelineProvider()) { entry in
VPNStatusView(entry: entry).widgetURL(DeepLinks.openVPN)
}
.configurationDisplayName("VPN Status")
.description("View and manage the VPN connection")
.supportedFamilies([.systemSmall])
.contentMarginsDisabled()
}
}
struct VPNStatusView_Previews: PreviewProvider {
static let connectedState = VPNStatusTimelineProvider.Entry(
date: Date(),
status: .status(.connected),
location: "Paoli, PA"
)
static let disconnectedState = VPNStatusTimelineProvider.Entry(
date: Date(),
status: .status(.disconnected),
location: "Paoli, PA"
)
static let notConfiguredState = VPNStatusTimelineProvider.Entry(
date: Date(),
status: .notConfigured,
location: "Paoli, PA"
)
static var previews: some View {
if #available(iOSApplicationExtension 17.0, *) {
VPNStatusView(entry: connectedState)
.previewContext(WidgetPreviewContext(family: .systemSmall))
.environment(\.colorScheme, .light)
VPNStatusView(entry: connectedState)
.previewContext(WidgetPreviewContext(family: .systemSmall))
.environment(\.colorScheme, .dark)
VPNStatusView(entry: disconnectedState)
.previewContext(WidgetPreviewContext(family: .systemSmall))
.environment(\.colorScheme, .light)
VPNStatusView(entry: disconnectedState)
.previewContext(WidgetPreviewContext(family: .systemSmall))
.environment(\.colorScheme, .dark)
VPNStatusView(entry: notConfiguredState)
.previewContext(WidgetPreviewContext(family: .systemSmall))
.environment(\.colorScheme, .light)
VPNStatusView(entry: notConfiguredState)
.previewContext(WidgetPreviewContext(family: .systemSmall))
.environment(\.colorScheme, .dark)
} else {
Text("iOS 17 required")
}
}
}
extension Color {
static var vpnWidgetBackgroundColor: Color {
let color = UIColor(designSystemColor: .accent).resolvedColor(with: UITraitCollection(userInterfaceStyle: .light))
return Color(color)
}
}
#endif