-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathViewController.m
254 lines (211 loc) · 9.28 KB
/
ViewController.m
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
//
// ViewController.m
// sample-ios-objc
//
#import "ViewController.h"
#import "CZiti-Swift.h"
@interface ViewController () <UIDocumentPickerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *urlTextField;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@end
@implementation ViewController
Ziti *ziti;
- (NSString*)zidFile {
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
return [documentPath stringByAppendingString:@"/zid.json"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[_urlTextField addTarget:self action:@selector(onTextFieldDidEndOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
if (ziti == NULL) {
[self runZiti];
}
}
- (void)runZiti {
NSLog(@"zidFile = %@", [self zidFile]);
ziti = [[Ziti alloc] initFromFile:[self zidFile]];
if (ziti != NULL) {
[ziti runAsync: ^(ZitiError *zErr) {
if (zErr != NULL) {
[self handleZitiInitError:zErr];
return;
}
[ZitiUrlProtocol register:ziti :10000];
}];
} else {
[self onNoIdentity];
}
}
- (void)handleZitiInitError:(ZitiError *)zErr {
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:@"Ziti Init Error"
message:[zErr localizedDescription]
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:@"Retry"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self runZiti];
}]];
[alert addAction:[UIAlertAction
actionWithTitle:@"Forget This Identity"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (ziti != NULL) {
[ziti forget];
}
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:[self zidFile] error:&error];
[self onNoIdentity];
}]];
[alert addAction:[UIAlertAction
actionWithTitle:@"Exit"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
exit(1);
}]];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:alert animated:YES completion:nil];
});
}
- (void) onNoIdentity {
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:@"Ziti Identity Not Found"
message:@"What do you want to do now?"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:@"Enroll"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self enroll];
}]];
[alert addAction:[UIAlertAction
actionWithTitle:@"Exit"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
exit(1);
}]];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:alert animated:YES completion:nil];
});
}
- (void) enroll {
dispatch_async(dispatch_get_main_queue(), ^{
UIDocumentPickerViewController* dp =
[[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.item"]
inMode:UIDocumentPickerModeImport];
dp.modalPresentationStyle = UIModalPresentationFormSheet;
dp.allowsMultipleSelection = NO;
dp.delegate = self;
[self presentViewController:dp animated:YES completion:^{}];
});
}
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
[self onNoIdentity];
}
- (void) documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
[Ziti enroll:[urls firstObject].path : ^(ZitiIdentity *zid, ZitiError *zErr) {
// Alert on Error
if (zErr != NULL) {
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:@"Enrollment Error"
message:[zErr localizedDescription]
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self onNoIdentity];
}]];
[self presentViewController:alert animated:YES completion:nil];
return;
}
// Attempt to save the identity file
if (![zid save:[self zidFile]]) {
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:@"Unable to store identity file"
message:[self zidFile]
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self onNoIdentity];
}]];
[self presentViewController:alert animated:YES completion:nil];
return;
}
// All good. Tell user "good job" and attempt to runZiti
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:@"Enrolled!"
message:@"You have successfully enrolled"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}]];
[self presentViewController:alert animated:YES completion:nil];
[self runZiti];
}];
}
- (void) onTextFieldDidEndOnExit:(id) obj {
NSURL *url = [[NSURL alloc] initWithString:_urlTextField.text];
NSURLRequest *urlReq = [[NSURLRequest alloc] initWithURL:url];
BOOL zitiCanHandle = [ZitiUrlProtocol canInitWithRequest:urlReq];
if (!zitiCanHandle) {
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:@"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:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self loadUrl:urlReq];
}]];
[self presentViewController:alert animated:YES completion:nil];
} else {
[self loadUrl:urlReq];
}
[_urlTextField resignFirstResponder];
}
-(void)setScrollableText:(NSString *)txt {
dispatch_async(dispatch_get_main_queue(), ^{
[self->_textView.textStorage.mutableString setString:txt];
});
}
- (void)loadUrl:(NSURLRequest *)urlReq {
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlReq
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error != NULL) {
[self setScrollableText:error.localizedDescription];
return;
}
NSString *docStr = @"";
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse *)response;
if (httpResp != NULL) {
NSString *statusStr = [NSHTTPURLResponse localizedStringForStatusCode:httpResp.statusCode];
docStr = [NSString stringWithFormat:@"Status: %ld (%@)\n", (long)httpResp.statusCode, statusStr];
NSDictionary *dict = [httpResp allHeaderFields];
for (NSString *key in dict) {
docStr = [NSString stringWithFormat:@"%@%@: %@\n", docStr, key, dict[key]];
}
docStr = [NSString stringWithFormat:@"%@\n", docStr];
}
BOOL canPrintData = NO;
if (data != NULL) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (str != NULL) {
canPrintData = YES;
docStr = [NSString stringWithFormat:@"%@%@", docStr, str];
}
}
if (!canPrintData) {
docStr = [NSString stringWithFormat:@"%@...Unable to decode body to string...", docStr];
}
[self setScrollableText:docStr];
}];
[dataTask resume];
}
@end