-
Notifications
You must be signed in to change notification settings - Fork 14
/
VTInfoWindowController.m
454 lines (341 loc) · 12.6 KB
/
VTInfoWindowController.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//
// VTInfoWindow.m
// TaskExplorer
//
// Created by Patrick Wardle on 3/29/15.
// Copyright (c) 2015 Objective-See. All rights reserved.
//
#import "File.h"
#import "Consts.h"
#import "Utilities.h"
#import "VirusTotal.h"
#import "AppDelegate.h"
#import "VTInfoWindowController.h"
#import "3rdParty/HyperlinkTextField.h"
#import <QuartzCore/QuartzCore.h>
@interface VTInfoWindowController ()
@end
@implementation VTInfoWindowController
@synthesize item;
@synthesize windowController;
//init method
// ->save item and load nib
-(id)initWithItem:(Binary*)binary
{
self = [super init];
if(nil != self)
{
//load nib
self.windowController = [[VTInfoWindowController alloc] initWithWindowNibName:@"VTInfoWindow"];
//save item
self.windowController.item = binary;
//save row index
//self.windowController.rowIndex = itemRowIndex;
}
return self;
}
//automatically invoked
// ->make it white
-(void)windowDidLoad
{
//super
[super windowDidLoad];
//no dark mode?
// make window white
if(YES != isDarkMode())
{
//make white
self.window.backgroundColor = NSColor.whiteColor;
}
//make close button selected
[self.window makeFirstResponder:self.closeButton];
return;
}
//automatically called when nib is loaded
// ->save self into iVar, and center window
-(void)awakeFromNib
{
//configure UI
[self configure];
//center
[self.window center];
}
//configure window
// ->add item's attributes (name, path, etc.)
-(void)configure
{
//flag
BOOL isKnown = NO;
//detection ratio
NSString* vtDetectionRatio = nil;
//color
NSColor* textColor = nil;
//get status
if(nil != self.item.vtInfo[VT_RESULTS_URL])
{
//known
isKnown = YES;
}
//file status (known/unknown)
if(YES == isKnown)
{
//default color
textColor = NSColor.controlTextColor;
//set color to red if its flagged
if(0 != [self.item.vtInfo[VT_RESULTS_POSITIVES] unsignedIntegerValue])
{
//red
textColor = [NSColor redColor];
}
//generate detection ratio
vtDetectionRatio = [NSString stringWithFormat:@"%lu/%lu", (unsigned long)[self.item.vtInfo[VT_RESULTS_POSITIVES] unsignedIntegerValue], (unsigned long)[self.item.vtInfo[VT_RESULTS_TOTAL] unsignedIntegerValue]];
//set name
[self.fileName setStringValue:self.item.name];
//set color
self.fileName.textColor = textColor;
//detection ratio
[self.detectionRatio setStringValue:vtDetectionRatio];
//set color
self.detectionRatio.textColor = textColor;
//analysis url
[self.analysisURL setStringValue:@"VirusTotal report"];
//make analyis url a hyperlink
makeTextViewHyperlink(self.analysisURL, [NSURL URLWithString:self.item.vtInfo[VT_RESULTS_URL]]);
//set 'submit' button text to 'rescan'
self.submitButton.title = @"Rescan?";
}
//unknown file
else
{
//hide file name label
self.fileNameLabel.hidden = YES;
//hide file name
self.fileName.hidden = YES;
//hide detection ratio label
self.detectionRatioLabel.hidden = YES;
//hide detection ratio
self.detectionRatio.hidden = YES;
//hide analysis url label
self.analysisURLLabel.hidden = YES;
//hide analysis url
self.analysisURL.hidden = YES;
//set unknown file msg
[self.unknownFile setStringValue:[NSString stringWithFormat:@"no results found for '%@'", self.item.name]];
//show 'unknown file' msg
self.unknownFile.hidden = NO;
}
return;
}
//automatically invoked when user clicks 'close'
// ->just close window
-(IBAction)closeButtonHandler:(id)sender
{
//close
[self.window close];
return;
}
//automatically invoked when window is closing
// ->tell OS that we are done with window so it can (now) be freed
-(void)windowWillClose:(NSNotification *)notification
{
//make un-modal
//[[NSApplication sharedApplication] stopModal];
//stop spinner
// ->will hide too
[self.progressIndicator stopAnimation:nil];
return;
}
//automatically invoked when user clicks 'rescan'/'submit'
// ->rescan or upload to VT!
-(IBAction)vtButtonHandler:(id)sender
{
//VT object
VirusTotal* vtObj = nil;
//result(s) from VT
__block NSDictionary* result = nil;
//analyis URL
NSMutableAttributedString* hyperlinkString = nil;
//VT scan ID
__block NSString* scanID = nil;
//new report
__block NSURL* newReport = nil;
//alloc/init VT obj
vtObj = [[VirusTotal alloc] init];
//disable button
((NSButton*)sender).enabled = NO;
//disable close button
self.closeButton.enabled = NO;
//get current string
hyperlinkString = [self.analysisURL.attributedStringValue mutableCopy];
//start editing
[hyperlinkString beginEditing];
//remove url/link
[hyperlinkString removeAttribute:NSLinkAttributeName range:NSMakeRange(0, [hyperlinkString length])];
//done editing
[hyperlinkString endEditing];
//set text
// ->will look the same, but the URL will be disabled!
[self.analysisURL setAttributedStringValue:hyperlinkString];
//pre-req
[self.overlayView setWantsLayer:YES];
//dark mode
// set overlay to light
if(YES == isDarkMode())
{
//set overlay's view color to gray
self.overlayView.layer.backgroundColor = NSColor.lightGrayColor.CGColor;
}
//light mode
// set overlay to gray
else
{
//set to gray
self.overlayView.layer.backgroundColor = NSColor.grayColor.CGColor;
}
//make it semi-transparent
self.overlayView.alphaValue = 0.85;
//show it
self.overlayView.hidden = NO;
//show spinner
self.progressIndicator.hidden = NO;
//animate it
[self.progressIndicator startAnimation:nil];
//rescan file?
if(YES == [((NSButton*)sender).title isEqualToString:@"Rescan?"])
{
//set status msg
[self.statusMsg setStringValue:[NSString stringWithFormat:@"submitting re-scan request for %@", self.item.name]];
//show status msg
self.statusMsg.hidden = NO;
//submit rescan request in background
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//make request to VT
// ->will also update UI to show '...'
result = [vtObj reScan:self.item];
//got result
// ->update UI and launch browswer to show report
if(nil != result)
{
//grab scan ID
// ->need this for (re)queries
scanID = result[VT_RESULTS_SCANID];
//with a scan id can re-query VT
// ->will update VT button in UI once results are retrieved
if(nil != scanID)
{
//kick off task to re-query VT
// ->wait 60 seconds though to give VT servers some time to process
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 60 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[vtObj getInfoForItem:self.item scanID:scanID];
});
}
//nap so user can see msg 'submitting' msg
[NSThread sleepForTimeInterval:0.5];
//update status msg
dispatch_sync(dispatch_get_main_queue(), ^{
//update
[self.statusMsg setStringValue:@"request submitted"];
});
//nap so user can see msg
[NSThread sleepForTimeInterval:0.5];
//launch browser to show new report
dispatch_sync(dispatch_get_main_queue(), ^{
//sanity check
// then launch browser
if( (nil != result[@"permalink"]) &&
(nil != (newReport = [NSURL URLWithString:result[@"permalink"]])) )
{
//launch browser
[[NSWorkspace sharedWorkspace] openURL:newReport];
}
});
//wait to browser is up and happy
[NSThread sleepForTimeInterval:0.5];
//close window
dispatch_sync(dispatch_get_main_queue(), ^{
//close
[self.window close];
});
}
//error
else
{
//show error msg
dispatch_async(dispatch_get_main_queue(), ^{
//update status msg
[self.statusMsg setStringValue:@"failed to submit request :("];
//stop activity indicator
[self.progressIndicator stopAnimation:nil];
});
}
});
} //rescan file
//submit file
else
{
//set status msg
[self.statusMsg setStringValue:[NSString stringWithFormat:@"submitting %@", self.item.name]];
//show status msg
self.statusMsg.hidden = NO;
//submit rescan request in background
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//submit file to VT
// ->will also update UI to show '...'
result = [vtObj submit:self.item];
// ->need this for (re)queries
scanID = result[VT_RESULTS_SCANID];
//with a scan id can query VT
// ->will update VT button in UI once results are retrieved
if(nil != scanID)
{
//kick off task to re-query VT
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 60 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[vtObj getInfoForItem:self.item scanID:scanID];
});
}
//got response
// ->launch browswer to show report
if(nil != result)
{
//update status msg
dispatch_sync(dispatch_get_main_queue(), ^{
//update
[self.statusMsg setStringValue:@"file submitted"];
});
//nap so user can see msg
[NSThread sleepForTimeInterval:0.5];
//launch browser to show new report
dispatch_sync(dispatch_get_main_queue(), ^{
//sanity check
// then launch browser
if( (nil != result[@"permalink"]) &&
(nil != (newReport = [NSURL URLWithString:result[@"permalink"]])) )
{
//launch browser
[[NSWorkspace sharedWorkspace] openURL:newReport];
}
});
//wait to browser is up and happy
[NSThread sleepForTimeInterval:0.5];
//close window
dispatch_sync(dispatch_get_main_queue(), ^{
//close
[self.window close];
});
}//got result
//error
else
{
//show error msg
dispatch_sync(dispatch_get_main_queue(), ^{
//update status msg
[self.statusMsg setStringValue:@"failed to submit request :("];
//stop activity indicator
[self.progressIndicator stopAnimation:nil];
});
}
});
} //submit file
return;
}
@end