-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvancedWindowController.m
615 lines (552 loc) · 25.7 KB
/
AdvancedWindowController.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//
// AdvancedWindowController.m
// V2RayX
//
//
#import "AdvancedWindowController.h"
#import "MutableDeepCopying.h"
#include <stdio.h>
@interface AdvancedWindowController () {
ConfigWindowController* configWindowController;
//outbound
}
@property (strong) NSPopover* popover;
@property NSInteger selectedOutbound;
@property (atomic) NSInteger selectedRuleSet;
@property (atomic) NSInteger selectedRule;
@end
@implementation AdvancedWindowController
- (instancetype)initWithWindowNibName:(NSNibName)windowNibName parentController:(ConfigWindowController*)parent {
self = [super initWithWindowNibName:windowNibName];
if (self) {
configWindowController = parent;
}
return self;
}
- (void)removeObservers {
[self removeObserver:self forKeyPath:@"selectedOutbound"];
[self removeObserver:self forKeyPath:@"selectedRuleSet"];
[self removeObserver:self forKeyPath:@"selectedRule"];
}
- (void)windowDidLoad {
[super windowDidLoad];
// initialize UI
[_outboundTable setFocusRingType:NSFocusRingTypeNone];
[_ruleTable setFocusRingType:NSFocusRingTypeNone];
[_ruleSetTable setFocusRingType:NSFocusRingTypeNone];
[_configTable setFocusRingType:NSFocusRingTypeNone];
[_subscriptionTable setFocusRingType:NSFocusRingTypeNone];
[[self domainStrategyButton] removeAllItems];
for(NSString* strategy in DOMAIN_STRATEGY_LIST) {
[[self domainStrategyButton] addItemWithTitle:strategy];
}
[[self networkListButton] removeAllItems];
for(NSString* network in ROUTING_NETWORK_LIST) {
[[self networkListButton] addItemWithTitle:network];
}
[_protocolButton removeAllItems];
for (NSString* p in SNIFFING_PROTOCOL) {
[_protocolButton addItemWithTitle:p];
}
_outboundJsonView.automaticQuoteSubstitutionEnabled = false;
_domainTextView.automaticQuoteSubstitutionEnabled = false;
[_routeToBox removeAllItems];
[_routeToBox addItemsWithObjectValues:RESERVED_TAGS];
[_inboundTagBox removeAllItems];
[_inboundTagBox addItemsWithObjectValues:@[@"socksinbound", @"httpinbound"]];
// outbound
[_outboundJsonView setFont:[NSFont fontWithName:@"Menlo" size:13]];
[self addObserver:self
forKeyPath:@"selectedOutbound"
options:NSKeyValueObservingOptionNew
context:nil];
// rule
[self addObserver:self
forKeyPath:@"selectedRuleSet"
options:NSKeyValueObservingOptionNew
context:nil];
[self addObserver:self
forKeyPath:@"selectedRule"
options:NSKeyValueObservingOptionNew
context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldDidChange:)
name:NSControlTextDidChangeNotification
object:_ruleSetNameField];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldDidChange:)
name:NSControlTextDidChangeNotification
object:_routeToBox];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldDidChange:)
name:NSControlTextDidChangeNotification
object:_portField];
// core path
self.corePathField.stringValue = [NSString stringWithFormat:@"%@/Library/Application Support/V2RayX/v2ray-core/",NSHomeDirectory()];
self.enableRestore = configWindowController.enableRestore;
self.enableEncryption = configWindowController.enableEncryption;
self.encryptionKey = [[NSString alloc] initWithString:configWindowController.encryptionKey];
// encryption
_changeIndicatorField.stringValue = @"";
[self fillData];
}
- (void)fillData {
// outbound
self.outbounds = [configWindowController.outbounds mutableCopy];
_outboundJsonView.editable = self.outbounds.count > 0;
if (self.outbounds.count > 0) {
self.selectedOutbound = 0;
} else {
self.selectedOutbound = -1;
}
[_outboundTable reloadData];
// subscriptions
self.subscriptions = [configWindowController.subscriptions mutableCopy];
[_subscriptionTable reloadData];
// rules
self.selectedRuleSet = 0;
self.selectedRule = 0;
self.routingRuleSets = [configWindowController.routingRuleSets mutableDeepCopy];
// configs
self.configs = [configWindowController.cusProfiles mutableDeepCopy];
[_configTable reloadData];
// core
[_enableRestoreButton selectItemAtIndex:_enableRestore?1:0];
}
- (IBAction)ok:(id)sender {
if (![self checkOutbound]) {
return;
}
if (![self checkConfig]) {
return;
}
[self removeObservers];
[self.window.sheetParent endSheet:self.window returnCode:NSModalResponseOK];
}
- (IBAction)cancel:(id)sender {
[self removeObservers];
[self.window.sheetParent endSheet:self.window returnCode:NSModalResponseCancel];
}
// table data
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
if (tableView == _outboundTable) {
return [self.outbounds count];
}
if (tableView == _configTable) {
return [self.configs count];
}
if (tableView == _ruleSetTable) {
return self.routingRuleSets.count;
}
if (tableView == _ruleTable) {
return [self.routingRuleSets[_selectedRuleSet][@"rules"] count];
}
if (tableView == _subscriptionTable) {
return [self.subscriptions count];
}
return 0;
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
if (tableView == _outboundTable) {
return self.outbounds[row][@"tag"];
}
if (tableView == _configTable) {
return self.configs[row];
}
if (tableView == _ruleSetTable) {
return self.routingRuleSets[row][@"name"];
}
if(tableView == _ruleTable) {
NSUInteger ruleCount = [self.routingRuleSets[_selectedRuleSet][@"rules"] count];
NSDictionary* rule = self.routingRuleSets[_selectedRuleSet][@"rules"][row];
NSString* routeTo = rule[@"outboundTag"] ? rule[@"outboundTag"] : rule[@"balancerTag"];
return row + 1 == ruleCount ? [NSString stringWithFormat:@"final:%@", routeTo] : [NSString stringWithFormat:@"%lu:%@", row, routeTo] ;
}
if (tableView == _subscriptionTable) {
return self.subscriptions[row];
}
return @"";
}
// table delegate
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
if (notification.object == _outboundTable) {
if (_outboundTable.selectedRow != _selectedOutbound) {
[self checkOutbound];
} else {
NSLog(@"do nothing");
}
}
if (notification.object == _ruleSetTable) {
self.selectedRuleSet = [_ruleSetTable selectedRow];
[_ruleTable reloadData];
}
if (notification.object == _ruleTable) {
self.selectedRule = [_ruleTable selectedRow];
}
}
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
if (tableView == _configTable) {
self.configs[row] = object;
} else if (tableView == _subscriptionTable) {
self.subscriptions[row] = object;
}
}
// bound
- (BOOL)checkOutbound {
if (_outbounds.count == 0) {
return YES;
}
NSError *e;
NSDictionary* newOutboud = [NSJSONSerialization JSONObjectWithData:[_outboundJsonView.string dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&e];
if (e) {
[self showAlert:@"NOT a valid json"];
[_outboundTable selectRowIndexes:[NSIndexSet indexSetWithIndex:_selectedOutbound] byExtendingSelection:NO];
return NO;
} else {
self.outbounds[_selectedOutbound] = newOutboud;
self.selectedOutbound = _outboundTable.selectedRow;
[_outboundTable reloadData];
return YES;
}
}
- (IBAction)addRemoveOutbound:(id)sender {
if ([sender selectedSegment] == 0) {
NSString* tagName = [NSString stringWithFormat:@"tag%lu", self.outbounds.count];
[self.outbounds addObject:@{
@"sendThrough": @"0.0.0.0",
@"protocol": @"protocol name",
@"settings": @{},
@"tag": tagName,
@"streamSettings": @{},
@"mux": @{}
}];
if (_selectedOutbound == -1) {
_selectedOutbound = 0;
self.selectedOutbound = 0;
}
} else {
if (_selectedOutbound >= 0 && _selectedOutbound < _outbounds.count) {
[_outbounds removeObjectAtIndex:_selectedOutbound];
self.selectedOutbound = MIN((NSInteger)_outbounds.count - 1, _selectedOutbound);
}
}
[_outboundTable reloadData];
_outboundJsonView.editable = _outbounds.count > 0;
}
// rules
-(void)textFieldDidChange:(NSNotification *)notification {
if (notification.object == _ruleSetNameField) {
self.routingRuleSets[_selectedRuleSet][@"name"] = _ruleSetNameField.stringValue;
}
if (notification.object == _routeToBox) {
NSMutableDictionary* rule =
self.routingRuleSets[_selectedRuleSet][@"rules"][_selectedRule];
if ([@"balance" isEqualToString:_routeToBox.stringValue]) {
rule[@"balancerTag"] = @"balance";
[rule removeObjectForKey:@"outboundTag"];
} else {
rule[@"outboundTag"] = _routeToBox.stringValue;
[rule removeObjectForKey:@"balancerTag"];
}
}
if (notification.object == _portField) {
NSMutableDictionary* rule =
self.routingRuleSets[_selectedRuleSet][@"rules"][_selectedRule];
NSString* trimedInput = [_portField.stringValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSNumberFormatter* f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *port = [f numberFromString:trimedInput];
rule[@"port"] = nilCoalescing(port, trimedInput);
}
if (notification.object == _inboundTagBox) {
self.routingRuleSets[_selectedRuleSet][@"rules"][_selectedRule][@"inboundTag"] = _inboundTagBox.stringValue;
}
}
- (IBAction)addRemoveRuleSet:(id)sender {
[[self window] makeFirstResponder:_ruleSetTable];
if ([sender selectedSegment] == 0) {
NSMutableDictionary* newRuleSet = [ROUTING_DIRECT mutableDeepCopy];
newRuleSet[@"name"] = @"new_rule_set";
[_routingRuleSets addObject:newRuleSet];
[_ruleSetTable reloadData];
[_ruleSetTable selectRowIndexes:[NSIndexSet indexSetWithIndex:_routingRuleSets.count - 1] byExtendingSelection:NO]; // toggle
} else if([sender selectedSegment] == 1 && _selectedRuleSet > 0 && _selectedRuleSet < _routingRuleSets.count){
[_routingRuleSets removeObjectAtIndex:_selectedRuleSet];
NSUInteger originalIndex = _ruleSetTable.selectedRow;
[_ruleSetTable selectRowIndexes:[NSIndexSet indexSetWithIndex:MIN(_selectedRuleSet, _routingRuleSets.count - 1)] byExtendingSelection:NO]; // toggle
if (originalIndex == _ruleSetTable.selectedRow) {
[self tableViewSelectionDidChange:[NSNotification notificationWithName:NSTableViewSelectionDidChangeNotification object:_ruleSetTable]];
}
[_ruleSetTable reloadData];
} else if ([sender selectedSegment] == 2) {
NSAlert* alert = [[NSAlert alloc] init];
alert.messageText = @"Do you want to reset rule sets to original three ones?";
[alert addButtonWithTitle:@"OK"];
[alert addButtonWithTitle:@"Cancel"];
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
if (returnCode == NSAlertFirstButtonReturn) {
NSLog(@"will rest");
self->_routingRuleSets = [@[ROUTING_GLOBAL, ROUTING_BYPASSCN_PRIVATE_APPLE, ROUTING_DIRECT] mutableDeepCopy];
NSUInteger originalIndex = self->_ruleSetTable.selectedRow;
[self->_ruleSetTable selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO]; // toggle
if (originalIndex == self->_ruleSetTable.selectedRow) {
[self tableViewSelectionDidChange:[NSNotification notificationWithName:NSTableViewSelectionDidChangeNotification object:self->_ruleSetTable]];
}
[self->_ruleSetTable reloadData];
}
}];
} else if ([sender selectedSegment] == 3 && _selectedRuleSet >= 0 && _selectedRuleSet < _routingRuleSets.count) {
[_routingRuleSets addObject:[[_routingRuleSets objectAtIndex:_selectedRuleSet] mutableDeepCopy]];
[_ruleSetTable reloadData];
[_ruleSetTable selectRowIndexes:[NSIndexSet indexSetWithIndex:_routingRuleSets.count - 1] byExtendingSelection:NO]; // toggle
}
}
- (IBAction)didSelectStrategy:(id)sender {
if (_selectedRuleSet < _routingRuleSets.count) {
_routingRuleSets[_selectedRuleSet][@"domainStrategy"] = _domainStrategyButton.selectedItem.title;
}
}
- (IBAction)addRemoveRule:(id)sender {
[[self window] makeFirstResponder:_ruleTable];
NSMutableArray* rules = _routingRuleSets[_selectedRuleSet][@"rules"];
if ([sender selectedSegment] == 0) {
[rules insertObject:[@{
@"type": @"field",
@"outboundTag": @"direct"} mutableDeepCopy] atIndex:rules.count-1];
} else {
if (_selectedRule + 1 == [rules count]) {
return;
}
[rules removeObjectAtIndex:_selectedRule];
}
[_ruleTable reloadData];
self.selectedRule = _selectedRule; // toggle
}
- (IBAction)didClickEnable:(id)sender {
#define enableFilter(enableButton, field, key) \
field.enabled = enableButton.state; \
if (!enableButton.state) { \
[self.routingRuleSets[_selectedRuleSet][@"rules"][_selectedRule] removeObjectForKey:key];\
}
if (sender == _domainEnableButton) {
enableFilter(_domainEnableButton, _editDomainButton, @"domain")
if (_domainEnableButton.state) {
[self showEditView:_editDomainButton];
} else if (self.popover.shown) {
[self.popover close];
}
} else if (sender == _ipEnableButton) {
enableFilter(_ipEnableButton, _editIpButton, @"ip")
if (_ipEnableButton.state) {
[self showEditView:_editIpButton];
} else if (self.popover.shown) {
[self.popover close];
}
} else if (sender == _inboundEnableButton) {
enableFilter(_inboundEnableButton, _inboundTagBox, @"inboundTag")
if (_inboundEnableButton.state) {
[self.routingRuleSets[_selectedRuleSet][@"rules"][_selectedRule] setObject:_inboundTagBox.stringValue forKey:@"inboundTag"];
}
} else if (sender == _protocolEnableButton) {
enableFilter(_protocolEnableButton, _protocolButton, @"protocol")
if (_protocolEnableButton.state) {
[self didSelectRuleItem:_protocolButton];
}
} else if (sender == _networkEnableButton) {
enableFilter(_networkEnableButton, _networkListButton, @"network")
if (_networkEnableButton.state) {
[self didSelectRuleItem:_networkListButton];
}
} else if (sender == _portEnableButton) {
enableFilter(_portEnableButton, _portField, @"port")
if (_portEnableButton.state) {
[self textFieldDidChange:[[NSNotification alloc] initWithName:NSTextDidChangeNotification object:_portField userInfo:nil]];
}
}
}
- (IBAction)didSelectRuleItem:(id)sender {
if (sender == _networkListButton && _networkEnableButton.state) {
[self.routingRuleSets[_selectedRuleSet][@"rules"][_selectedRule] setObject:_networkListButton.selectedItem.title forKey:@"network"];
} else if (sender == _protocolButton && _protocolEnableButton.state) {
[self.routingRuleSets[_selectedRuleSet][@"rules"][_selectedRule] setObject:_protocolButton.selectedItem.title forKey:@"protocol"];
} else if ((sender == _inboundTagBox && _inboundEnableButton.state) || sender == _routeToBox) {
[self textFieldDidChange:[[NSNotification alloc] initWithName:NSTextDidChangeNotification object:sender userInfo:nil]];
}
}
- (IBAction)showEditView:(id)sender {
if (self.popover.shown) {
[self.popover close];
}
self.popover = [[NSPopover alloc] init];
self.popover.behavior = NSPopoverBehaviorApplicationDefined;
self.popover.contentViewController = [[NSViewController alloc] init];
if (sender == _editDomainButton) {
self.popover.contentViewController.view = _domainListEditView;
} else if (sender == _editIpButton) {
self.popover.contentViewController.view = _ipListEditView;
}
[self.popover showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge];
}
- (IBAction)saveDomainOrIPList:(id)sender {
[self.popover close];
if (sender == _saveIPListButton) {
self.routingRuleSets[_selectedRuleSet][@"rules"][_selectedRule][@"ip"] = [[_ipTextView string] componentsSeparatedByString:@"\n"];
NSLog(@"%@", self.routingRuleSets[_selectedRuleSet][@"rules"][_selectedRule][@"ip"]);
} else {
self.routingRuleSets[_selectedRuleSet][@"rules"][_selectedRule][@"domain"] = [[_ipTextView string] componentsSeparatedByString:@"\n"];
}
}
- (IBAction)closeDomainOrIPList:(id)sender {
[self.popover close];
}
//
- (IBAction)addRemoveSubscription:(id)sender {
NSLog(@"%@", sender);
if ([sender selectedSegment] == 0) {
[_subscriptions addObject:@"enter your subscription link here"];
[_subscriptionTable reloadData];
} else if ([sender selectedSegment] == 1 && [_subscriptionTable selectedRow] >= 0 && [_subscriptionTable selectedRow] < _subscriptions.count) {
[_subscriptions removeObjectAtIndex:[_subscriptionTable selectedRow]];
[_subscriptionTable reloadData];
}
NSLog(@"%@", _subscriptions);
}
// configs
- (IBAction)addRemoveConfig:(id)sender {
if ([sender selectedSegment] == 0) {
[_configs addObject:@"/path/to/your/config.json"];
[_configTable reloadData];
// [_configTable selectRowIndexes:[NSIndexSet indexSetWithIndex:[_configs count] -1] byExtendingSelection:NO];
// [_configTable setFocusedColumn:[_configs count] - 1];
} else if ([sender selectedSegment] == 1 && [_configTable selectedRow] >= 0 && [_configTable selectedRow] < _configs.count) {
[_configs removeObjectAtIndex:[_configTable selectedRow]];
[_configTable reloadData];
}
}
- (BOOL)checkConfig {
[_checkLabel setHidden:NO];
NSString* v2rayBinPath = [configWindowController.appDelegate getV2rayPath];
for (NSString* filePath in _configs) {
int returnCode = runCommandLine(v2rayBinPath, @[@"-test", @"-config", filePath]);
if (returnCode != 0) {
[_checkLabel setHidden:YES];
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:[NSString stringWithFormat:@"%@ is not a valid v2ray config file", filePath]];
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
return;
}];
return NO;
}
}
return YES;
}
// core
- (IBAction)showCorePath:(id)sender {
if (![[NSFileManager defaultManager] fileExistsAtPath:self.corePathField.stringValue]) {
[[NSFileManager defaultManager] createDirectoryAtPath:self.corePathField.stringValue withIntermediateDirectories:YES attributes:nil error:nil];
}
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:@[[NSURL fileURLWithPath:self.corePathField.stringValue]]];
}
- (IBAction)changeEnableRestore:(NSPopUpButton*)sender {
_enableRestore = [sender indexOfSelectedItem];
}
- (IBAction)showInformation:(id)sender {
self.popover = [[NSPopover alloc] init];
self.popover.behavior = NSPopoverBehaviorTransient;
self.popover.contentViewController = [[NSViewController alloc] init];
if (sender == _domainIpHelpButton) {
self.popover.contentViewController.view = _domainIpHelpView;
} else if (sender == _routeToHelpButton) {
self.popover.contentViewController.view = _routingTagHelpView;
}
[self.popover showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge];
}
- (IBAction)showHelp:(id)sender {
NSString* tabTitle = _mainTabView.selectedTabViewItem.label;
// NSLog(@"%@", _mainTabView.selectedTabViewItem.label);
if ([@"Rules" isEqualToString:tabTitle]) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.v2ray.com/chapter_02/03_routing.html"]];
} else if ([@"Outbounds" isEqualToString:tabTitle]) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.v2ray.com/chapter_02/01_overview.html#outboundobject"]];
} else if ([@"Configs" isEqualToString:tabTitle]) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.v2ray.com/chapter_02/01_overview.html"]];
} else if ([@"V2Ray Core" isEqualToString:tabTitle]) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.v2ray.com/chapter_00/install.html#download"]];
}
}
- (void)showAlert:(NSString*)text {
NSAlert* alert = [[NSAlert alloc] init];
[alert setInformativeText:text];
[alert beginSheetModalForWindow:self.window completionHandler:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
// NSLog(@"keypath=%@", keyPath);
// outboud
if ([@"selectedOutbound" isEqualToString:keyPath]) {
if (_selectedOutbound > -1) {
[_outboundTable selectRowIndexes:[NSIndexSet indexSetWithIndex:_selectedOutbound] byExtendingSelection:NO];
_outboundJsonView.string = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:_outbounds[_selectedOutbound] options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
} else {
_outboundJsonView.string = @"";
}
}
// rule
if ([@"selectedRuleSet" isEqualToString:keyPath] && _selectedRuleSet < _routingRuleSets.count) {
_ruleSetNameField.stringValue = _routingRuleSets[_selectedRuleSet][@"name"];
[_domainStrategyButton selectItemAtIndex:searchInArray(_routingRuleSets[_selectedRuleSet][@"domainStrategy"], DOMAIN_STRATEGY_LIST)];
self.selectedRule = _selectedRule; //toggle
}
if ([@"selectedRule" isEqualToString:keyPath] && _selectedRuleSet < _routingRuleSets.count && _selectedRule < [_routingRuleSets[_selectedRuleSet][@"rules"] count] ) {
if (self.popover.shown) {
[self.popover close];
}
NSDictionary* rules = _routingRuleSets[_selectedRuleSet][@"rules"][_selectedRule];
BOOL selectedLastRule = _selectedRule + 1 == [_routingRuleSets[_selectedRuleSet][@"rules"] count];
_domainEnableButton.enabled = !selectedLastRule;
_domainEnableButton.state = rules[@"domain"] != NULL;
_editDomainButton.enabled = _domainEnableButton.state;
if (rules[@"domain"]) {
_domainTextView.string = [rules[@"domain"] componentsJoinedByString:@"\n"];
} else {
_domainTextView.string = @"";
}
_ipEnableButton.enabled = !selectedLastRule;
_ipEnableButton.state = rules[@"ip"] != NULL;
_editIpButton.enabled = _ipEnableButton.state;
if (rules[@"ip"]) {
_ipTextView.string = [rules[@"ip"] componentsJoinedByString:@"\n"];
} else {
_ipTextView.string = @"";
}
_inboundEnableButton.enabled = !selectedLastRule;
_inboundEnableButton.state = rules[@"inboundTag"] != NULL;
_inboundTagBox.enabled = _inboundEnableButton.state;
_inboundTagBox.stringValue = nilCoalescing(rules[@"inboundTag"], @"");
_protocolEnableButton.enabled = !selectedLastRule;
_protocolEnableButton.state = rules[@"protocol"] != NULL;
_protocolButton.enabled = _protocolEnableButton.state;
[_protocolButton selectItemAtIndex:searchInArray(rules[@"protocol"], SNIFFING_PROTOCOL)];
_portEnableButton.enabled = !selectedLastRule;
_portEnableButton.state = rules[@"port"] != NULL;
_portField.enabled = _portEnableButton.state && !selectedLastRule;
_portField.objectValue = nilCoalescing(rules[@"port"], @"0-65535");
_networkEnableButton.enabled = !selectedLastRule;
_networkEnableButton.state = rules[@"network"] != NULL;
_networkListButton.enabled = _networkEnableButton.state;
[_networkListButton selectItemAtIndex:searchInArray(rules[@"network"], ROUTING_NETWORK_LIST)];
_routeToBox.stringValue = rules[@"outboundTag"] ? rules[@"outboundTag"] : rules[@"balancerTag"];
}
}
- (IBAction)changePassword:(id)sender {
if ([[_encryptionKeyField stringValue] isEqualToString:[_encryptionKeyConfirmField stringValue]]) {
if ([[_encryptionKeyField stringValue] length] > 32) {
self.encryptionKey = [_encryptionKeyField.stringValue substringToIndex:32];
} else {
self.encryptionKey = [_encryptionKeyConfirmField.stringValue stringByPaddingToLength:32 withString:@"-" startingAtIndex:0];
}
[_changeIndicatorField setStringValue:@"success"];
} else {
[_changeIndicatorField setStringValue:@"two password do not match each other."];
}
}
@end