-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathViewController.swift
227 lines (194 loc) · 7.75 KB
/
ViewController.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
//
// ViewController.swift
// cziti.sample-ios
//
import UIKit
import CZiti
class ViewController: UIViewController {
@IBOutlet weak var urlTextField: UITextField!
@IBOutlet weak var textView: UITextView!
var ziti:Ziti?
var zidFile:String {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0].appendingPathComponent("zid.json", isDirectory: false).path
}
override func viewDidLoad() {
super.viewDidLoad()
print("zidFile=\(zidFile)")
urlTextField.addTarget(self, action: #selector(onTextFieldDidEndOnExit), for: .editingDidEndOnExit)
if ziti == nil {
runZiti()
}
}
func runZiti() {
ziti = Ziti(fromFile: zidFile)
if let ziti = ziti {
ziti.runAsync { zErr in
guard zErr == nil else {
print("Unable to run Ziti: \(String(describing: zErr!))")
self.handleZitiInitError(zErr!)
return
}
ZitiUrlProtocol.register(ziti)
}
} else {
onNoIdentity()
}
}
func enroll() {
DispatchQueue.main.async {
let dp = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import)
dp.modalPresentationStyle = .formSheet
dp.allowsMultipleSelection = false
dp.delegate = self
self.present(dp, animated: true, completion: nil)
}
}
func handleZitiInitError(_ zErr:ZitiError) {
let alert = UIAlertController(
title:"Ziti Init Error",
message: "\(zErr.localizedDescription)\n\nWhat do you want to do now?",
preferredStyle: .alert)
alert.addAction(UIAlertAction(
title: NSLocalizedString("Retry", comment: "Retry"),
style: .default,
handler: { _ in
self.runZiti()
}))
alert.addAction(UIAlertAction(
title: NSLocalizedString("Forget This Identity", comment: "Forget"),
style: .destructive,
handler: { _ in
if let ziti = self.ziti {
ziti.forget()
}
try? FileManager.default.removeItem(atPath: self.zidFile)
self.onNoIdentity()
}))
alert.addAction(UIAlertAction(
title: NSLocalizedString("Exit", comment: "Exit"),
style: .default,
handler: { _ in
exit(1)
}))
DispatchQueue.main.async {
self.present(alert, animated: true, completion: nil)
}
}
func onNoIdentity() {
let alert = UIAlertController(
title:"Ziti Identity Not Found",
message: "What do you want to do now?",
preferredStyle: .alert)
alert.addAction(UIAlertAction(
title: NSLocalizedString("Enroll", comment: "Enroll"),
style: .default,
handler: { _ in
self.enroll()
}))
alert.addAction(UIAlertAction(
title: NSLocalizedString("Exit", comment: "Exit"),
style: .default,
handler: { _ in
exit(1)
}))
DispatchQueue.main.async {
self.present(alert, animated: true, completion: nil)
}
}
@objc func onTextFieldDidEndOnExit(){
if let text = urlTextField.text, let url = URL(string: text) {
let urlReq = URLRequest(url: url)
let zitiCanHandle = ZitiUrlProtocol.canInit(with: urlReq)
if !zitiCanHandle {
let alert = UIAlertController(
title:"Not a Ziti request",
message: "This request will not be routed over your Ziti nework.\nAre you sure you'd like to request this URL?",
preferredStyle: .alert)
alert.addAction(UIAlertAction(
title: NSLocalizedString("OK", comment: "Default action"),
style: .default,
handler: { _ in
self.loadUrl(urlReq)
}))
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "Cancel"), style: .cancel))
present(alert, animated: true, completion: nil)
} else {
self.loadUrl(urlReq)
}
}
urlTextField.resignFirstResponder()
}
func setScrollableText(_ txt: String) {
DispatchQueue.main.async {
self.textView.textStorage.mutableString.setString(txt)
}
}
func loadUrl(_ urlReq:URLRequest) {
URLSession.shared.dataTask(with: urlReq) { (data, response, error) in
guard error == nil else {
self.setScrollableText(error!.localizedDescription)
return
}
var docStr = ""
if let response = response as? HTTPURLResponse {
docStr += "Status: \(response.statusCode) " +
"(\(HTTPURLResponse.localizedString(forStatusCode: response.statusCode)))\n" +
response.allHeaderFields.compactMap {
"\($0): \($1)\n"
}.joined() + "\n"
}
if let data = data, let str = String(data: data, encoding: .utf8) {
docStr += str
} else {
docStr += "...Unable to decode body to string..."
}
self.setScrollableText(docStr)
}.resume()
}
}
extension ViewController : UIDocumentPickerDelegate {
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
onNoIdentity()
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
Ziti.enroll(urls[0].path) { zid, zErr in
guard let zid = zid, zErr == nil else {
let alert = UIAlertController(
title:"Enrollment Error",
message: zErr?.localizedDescription ?? "",
preferredStyle: .alert)
alert.addAction(UIAlertAction(
title: NSLocalizedString("OK", comment: "Default action"),
style: .default,
handler: { _ in
self.onNoIdentity()
}))
self.present(alert, animated: true, completion: nil)
return
}
// Store zid @ zidFile
guard zid.save(self.zidFile) else {
let alert = UIAlertController(
title:"Unable to store identity file",
message: self.zidFile,
preferredStyle: .alert)
alert.addAction(UIAlertAction(
title: NSLocalizedString("OK", comment: "Default action"),
style: .default,
handler: { _ in
self.onNoIdentity()
}))
self.present(alert, animated: true, completion: nil)
return
}
let alert = UIAlertController(
title:"Enrolled!",
message: "You have successfully enrolled id: \(zid.id)",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default))
self.present(alert, animated: true, completion: nil)
self.runZiti()
}
}
}