-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathVTInfoWindowController.m
More file actions
executable file
·335 lines (250 loc) · 9.02 KB
/
VTInfoWindowController.m
File metadata and controls
executable file
·335 lines (250 loc) · 9.02 KB
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
//
// VTInfoWindow.m
// KnockKnock
//
// 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"
@interface VTInfoWindowController ()
@end
@implementation VTInfoWindowController
@synthesize windowController;
//init method
// ->save item and load nib
-(id)initWithItem:(File*)selectedItem
{
self = [super init];
if(nil != self)
{
//load nib
self.windowController = [[VTInfoWindowController alloc] initWithWindowNibName:@"VTInfoWindow"];
//save item
self.windowController.fileObj = selectedItem;
}
return self;
}
//automatically invoked
// ->make it white
-(void)windowDidLoad
{
//super
[super windowDidLoad];
//not in 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.fileObj.vtInfo[VT_RESULTS_URL])
{
//known
isKnown = YES;
}
//file status (known/unknown)
if(YES == isKnown)
{
//reset
textColor = NSColor.controlTextColor;
//set color to red if its flagged
if(0 != [self.fileObj.vtInfo[VT_RESULTS_POSITIVES] unsignedIntegerValue])
{
//red
textColor = [NSColor redColor];
}
//generate detection ratio
vtDetectionRatio = [NSString stringWithFormat:@"%lu/%lu", (unsigned long)[self.fileObj.vtInfo[VT_RESULTS_POSITIVES] unsignedIntegerValue], (unsigned long)[self.fileObj.vtInfo[VT_RESULTS_TOTAL] unsignedIntegerValue]];
//set name
self.fileName.stringValue = self.fileObj.name;
//set color
self.fileName.textColor = textColor;
//detection ratio
self.detectionRatio.stringValue = vtDetectionRatio;
//set color
self.detectionRatio.textColor = textColor;
//analysis url
self.analysisURL.stringValue = NSLocalizedString(@"VirusTotal Report", @"VirusTotal Report");
//make analysis url a hyperlink
makeTextViewHyperlink(self.analysisURL, [NSURL URLWithString:self.fileObj.vtInfo[VT_RESULTS_URL]]);
//disable scan button
self.submitButton.enabled = NO;
}
//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:NSLocalizedString(@"'%@' isn't known to VirusTotal", @"'%@' isn't known to VirusTotal"), self.fileObj.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
{
//stop spinner
// ->will hide too
[self.progressIndicator stopAnimation:nil];
return;
}
//invoked when user clicks 'submit'
// upload file to VT, open VT scan, etc...
-(IBAction)vtButtonHandler:(id)sender
{
//VT object
VirusTotal* vtObj = nil;
//analyis URL
NSMutableAttributedString* hyperlinkString = 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];
//set status msg
self.statusMsg.stringValue = [NSString stringWithFormat:NSLocalizedString(@"submitting '%@'", @"submitting '%@'"), self.fileObj.name];
//show status msg
self.statusMsg.hidden = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[vtObj submitFile:self.fileObj.path completion:^(NSDictionary *result) {
//completion on main thread
if(!result[VT_ERROR]) {
//NSLog(@"response: %@", result);
//got response?
// launch browser to show user
if(nil != result[VT_RESULTS_URL])
{
//reset file's VT info
self.fileObj.vtInfo = nil;
//new report URL
newReport = [NSURL URLWithString:result[VT_RESULTS_URL]];
//update status msg
dispatch_async(dispatch_get_main_queue(), ^{
//set item's VT status in UI to pending (...)
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) itemProcessed:self.fileObj];
//update
[self.statusMsg setStringValue:[NSString stringWithFormat:NSLocalizedString(@"submitted '%@'", @"submitted '%@'"), self.fileObj.name]];
});
//nap
// allows msg to show up, and give VT some time
[NSThread sleepForTimeInterval:2.0];
//launch browser to show new report
dispatch_async(dispatch_get_main_queue(), ^{
//launch browser
[NSWorkspace.sharedWorkspace openURL:newReport];
});
//wait to browser is up and happy
[NSThread sleepForTimeInterval:0.5];
//close window
dispatch_async(dispatch_get_main_queue(), ^{
//close
[self.window close];
});
}
}
else {
//err msg
//NSLog(@"ERROR: %@", result[VT_ERROR]);
//show error msg
dispatch_async(dispatch_get_main_queue(), ^{
//update status msg
[self.statusMsg setStringValue:[NSString stringWithFormat:NSLocalizedString(@"ERROR: %@", @"ERROR: %@"), result[VT_ERROR]]];
//stop activity indicator
[self.progressIndicator stopAnimation:nil];
});
}
}];
});
bail:
return;
}
@end