-
Notifications
You must be signed in to change notification settings - Fork 32
/
AppController.m
1232 lines (870 loc) · 35.1 KB
/
AppController.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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// AppController.m
// Sidestep
//
// Created by Chetan Surpur on 11/18/10.
// Copyright 2010 Chetan Surpur. All rights reserved.
//
#import "AppController.h"
#import "GrowlMessage.h"
@implementation AppController
/*
* Constants
*******************************************************************************
*/
NSString *noNetworkConnectionStatusText = @"No wireless connection";
NSString *determiningConnectionStatusText = @"Determining connection status...";
NSString *connectingConnectionStatusText = @"Connecting...";
NSString *retryingConnectionStatusText = @"Reconnecting...";
NSString *proxyConnectedConnectionStatusText = @"Secure connection";
NSString *protectedConnectionStatusText = @"Secure network";
NSString *openConnectionStatusText = @"Unsecure network";
NSString *notConnectedServerStatusText = @"Not connected";
NSString *authorizationErrorServerStatusText = @"Failed connecting - authorization failure";
NSString *connectionErrorServerStatusText = @"Failed connecting - network failure";
NSString *connectedServerStatusText = @"Connected to SSH";
NSString *connectedVPNText = @"Connected to VPN";
NSString *disconnectedVPNText = @"Disconnected from VPN";
NSString *unknownVPNText = @"VPN service not found";
NSString *noVPNText = @"No VPN service selected";
NSString *testingConnectionStatusText = @"Testing connection...";
NSString *authFailedTestingConnectionStatusText = @"Failed connecting - authorization failure";
NSString *reachFailedTestingConnectionStatusText = @"Failed connecting - network failure";
NSString *sucessTestingConnectionStatusText = @"Connection succeeded!";
NSString *restoredDirectConnectionStatusText = @"Disconnected";
NSString *rerouteConnectionButtonTitle = @"Connect";
NSString *restoreConnectionButtonTitle = @"Disconnect";
NSString *helpWithProxyURL = @"http://chetansurpur.com/projects/sidestep/#proxy-servers";
/* Growl spam reduction
* Growl outputs 10 - 12 error messages simulatenously when connecting to an unsecured network.
* This hack only allows the notification to occur once.
*/
NSInteger GrowlSpam_ConnectionType = 0;
NSInteger GrowlSpam_ConnectingToProxy = 0;
NSInteger GrowlSpam_TestConnection = 0;
/*
* Class methods
*******************************************************************************
*/
- (id)init {
self = [super init];
if (self != nil)
{
SSHconnector = [[SSHConnector alloc] init];
defaultsController = [[DefaultsController alloc] init];
networkNotifier = [[NetworkNotifier alloc] init];
proxySetter = [[ProxySetter alloc] init];
vpnInterfacer = [[VPNInterfacer alloc] init];
growl = [[GrowlMessage alloc] init];
initiatedDelayedConnectionAttempt = FALSE;
currentDelay = 0;
retryCounter = 0;
SSHConnection = nil;
SSHConnecting = FALSE;
SSHConnected = FALSE;
currentNetworkSecurityType = nil;
SInt32 version = 0;
Gestalt( gestaltSystemVersion, &version );
lion = ( version >= 0x1070 );
}
return self;
}
- (void)dealloc {
[SSHconnector release];
[defaultsController release];
[networkNotifier release];
[proxySetter release];
[vpnInterfacer release];
[statusImageDirectInsecure release];
[statusImageDirectSecure release];
[statusImageReroutedSecure release];
[growl release];
[super dealloc];
}
/*
* UI Event Handlers
*******************************************************************************
*/
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
int previousPID = [defaultsController getSSHConnectionPID];
if (previousPID != 0) {
XLog(self, @"Turning proxy off");
[self turnWirelessProxyOffThread];
// Terminate previous SSH connection attempt if still running
[NSThread detachNewThreadSelector:@selector(terminateSSHConnectionAttemptThread)
toTarget:self
withObject:nil];
// Kill previous SSH connection
[SSHconnector killSSHConnectionForPID:previousPID];
}
[networkNotifier listenForAirportConnectionAndNotifyObject:self
withSelector:@selector(connectedToAirportNetwork)];
// Check if current network type is insecure
if (![networkNotifier getNetworkSecurityTypeAndNotifyObject :self
withSelector:@selector(connectedToAirportNetworkWithSecurityType:)]) {
[self showRestartSidestepDialog];
}
//These notifications are filed on NSWorkspace's notification center, not the default
// notification center. You will not receive sleep/wake notifications if you file
//with the default notification center.
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self
selector: @selector(receiveSleepNote:)
name: NSWorkspaceWillSleepNotification object: NULL];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self
selector: @selector(receiveWakeNote:)
name: NSWorkspaceDidWakeNotification object: NULL];
}
- (void)awakeFromNib {
// Set selected proxy if not already set
if ([defaultsController selectedProxy] == nil || [[defaultsController selectedProxy] isEqualToString:@""]) {
[defaultsController setSelectedProxy:@"1"];
}
// Update VPN service lists
[self updateUIForVPNServiceList];
// Update UI for the selected proxy
[self updateUIForSelectedProxy];
// Growl
[GrowlApplicationBridge setGrowlDelegate:self];
// Create status menu item
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
[statusItem setMenu:statusMenu];
[statusItem setHighlightMode:YES];
// Allocates and loads the images into the application which will be used for our NSStatusItem
statusImageDirectInsecure = [NSImage imageNamed:@"direct-insecure-icon"];
statusImageDirectSecure = [NSImage imageNamed:@"direct-secure-icon"];
statusImageReroutedSecure = [NSImage imageNamed:@"rerouted-secure-icon"];
// Sets the default images in our NSStatusItem
[statusItem setImage:statusImageDirectSecure];
[statusItem setAlternateImage:statusImageDirectSecure];
// Check for updates if not first run and check for updates is enabled
if ([defaultsController ranAtleastOnce] && [[SUUpdater sharedUpdater] automaticallyChecksForUpdates]) {
XLog(self, @"Checking for updates");
[[SUUpdater sharedUpdater] checkForUpdatesInBackground];
}
// Set first-run default preferences
if (![defaultsController ranAtleastOnce]) {
[defaultsController setRerouteAutomatically:TRUE];
[defaultsController setRanAtleastOnce:TRUE];
[defaultsController setRunOnLogin:TRUE];
[self setRunOnLogin:TRUE];
[defaultsController setGrowlSetting:TRUE];
[defaultsController setCompressSSHConnection:FALSE];
// Show welcome window
[welcomeWindow center];
[welcomeTabs selectFirstTabViewItem:self];
[welcomeWindow setIsVisible:TRUE];
[welcomeWindow makeKeyAndOrderFront:self];
}
// Set default remote port number if not already set
if ([defaultsController getRemotePortNumber] == nil || [[defaultsController getRemotePortNumber] isEqualToString:@""]) {
[defaultsController setRemotePortNumber:@"22"];
}
// Enable Growl if preference is not found (user updated from previous version / has already completed 1st run)
if (![defaultsController getGrowlSetting]) {
[defaultsController setGrowlSetting:TRUE];
}
// Set default local port number if not already set
if ([defaultsController getLocalPortNumber] == nil || [[defaultsController getLocalPortNumber] isEqualToString:@""]) {
[defaultsController setLocalPortNumber:@"9050"];
}
// Update connection status
[connectionStatus setTitle:determiningConnectionStatusText];
// Update proxy server status
[proxyServerStatus setTitle:notConnectedServerStatusText];
// Set reroute or restore button title
[rerouteOrRestoreConnectionButton setTitle:rerouteConnectionButtonTitle];
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
XLog(self, @"User clicked Quit");
if (SSHConnecting || SSHConnected) {
[NSApp activateIgnoringOtherApps:YES]; // Allows windows of this app to become front
// Ask if user really wants to quit
int decision = NSRunCriticalAlertPanel (@"Do you want to disconnect before you quit?",
@"If you quit without disconnecting, your internet "
"will continue to be routed through the proxy.",
@"Yes",
@"No",
nil,
nil);
// Operate based on user's decision
if (decision == NSAlertDefaultReturn) { // Answer was "Yes"
XLog(self, @"Turning proxy off");
[self turnWirelessProxyOffThread];
if (SSHConnection) {
XLog(self, @"Killing current SSH connection");
[SSHConnection terminate];
}
XLog(self, @"Terminating current SSH connection attempt");
[NSThread detachNewThreadSelector:@selector(terminateSSHConnectionAttemptThread)
toTarget:self
withObject:nil];
}
}
return NSTerminateNow;
}
/*
* Functions
*******************************************************************************
*/
- (void)openSSHConnectionAfterDelay :(int)delay {
if (!SSHConnected || testingConnection) {
if (SSHConnecting) {
if (SSHConnection) {
XLog(self, @"Killing current SSH connection");
[SSHConnection terminate];
}
XLog(self, @"Terminating current SSH connection attempt");
[NSThread detachNewThreadSelector:@selector(terminateSSHConnectionAttemptThread)
toTarget:self
withObject:nil];
SSHConnecting = FALSE;
}
// Reset current delay
currentDelay = delay;
// Reset retry counter
retryCounter = 0;
// Initiate connection attempt after delay if not already initiated
if (!initiatedDelayedConnectionAttempt && !SSHConnecting) {
XLog(self, @"Opening new SSH connection after delay");
initiatedDelayedConnectionAttempt = TRUE;
[NSThread detachNewThreadSelector:@selector(openSSHConnectionAfterDelayThread)
toTarget:self
withObject:nil];
}
}
}
- (void)testSSHConnection {
testingConnection = TRUE;
[self openSSHConnectionAfterDelay:0];
}
- (void)closeSSHConnection {
if (SSHConnection) {
XLog(self, @"Turning proxy off");
[self turnWirelessProxyOffThread];
XLog(self, @"Killing current SSH connection");
[SSHConnection terminate];
}
if (SSHConnecting) {
XLog(self, @"Terminating current SSH connection attempt");
[NSThread detachNewThreadSelector:@selector(terminateSSHConnectionAttemptThread)
toTarget:self
withObject:nil];
}
SSHConnecting = FALSE;
SSHConnected = FALSE;
/* Set process to 0.
* Otherwise, next launch of Sidestep will read this variable and think it crashed.
* Sidestep would then try to kill the PID stored the last time it ran, potentially killing an unintended process.
*/
[defaultsController saveSSHConnectionPID:0];
}
- (void)openVPNConnectionAfterDelay :(int)delay {
// Reset current delay
currentDelay = delay;
// Initiate connection attempt after delay if not already initiated
if (!initiatedDelayedConnectionAttempt) {
XLog(self, @"Opening VPN connection after delay");
initiatedDelayedConnectionAttempt = TRUE;
if (![[defaultsController selectedVPNService] isEqualToString:@"None"]) {
[NSThread detachNewThreadSelector:@selector(openVPNConnectionAfterDelayThread)
toTarget:self
withObject:nil];
}
else {
[growl message:noVPNText];
}
}
}
- (void)closeVPNConnection {
XLog(self, @"Closing VPN connection");
if (![[defaultsController selectedVPNService] isEqualToString:@"None"]) {
[NSThread detachNewThreadSelector:@selector(closeVPNConnectionThread)
toTarget:self
withObject:nil];
}
else {
[growl message:noVPNText];
}
}
- (void)setRunOnLogin :(BOOL)value {
[self willChangeValueForKey:@"startAtLogin"];
NSURL *appURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
if (value) {
XLog(self, @"Enabling run on login");
[LoginItemController setStartAtLogin:appURL enabled:TRUE];
}
else {
XLog(self, @"Disabling run on login");
[LoginItemController setStartAtLogin:appURL enabled:FALSE];
}
[self didChangeValueForKey:@"startAtLogin"];
}
/*
- (void)setGrowlSetting :(BOOL)value {
if (value) {
XLog(self, @"Enabling Growl Notification");
[defaultsController setGrowlSetting:TRUE];
[self setGrowlSetting:TRUE];
}
else {
XLog(self, @"Disabling Growl Notification");
}
}
*/
- (NSDictionary *) registrationDictionaryForGrowl {
NSArray *notifications;
notifications = [NSArray arrayWithObject:@"GrowlNotification"];
NSDictionary *dict;
dict = [NSDictionary dictionaryWithObjectsAndKeys:
notifications, GROWL_NOTIFICATIONS_ALL,
notifications, GROWL_NOTIFICATIONS_DEFAULT, nil];
return (dict);
}
/*
* Threads
*******************************************************************************
*/
- (void)openSSHConnectionAfterDelayThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
while (currentDelay > 0) {
XLog(self, @"Opening SSH connection in %d seconds", currentDelay);
// Sleep for one second
NSDate *future = [NSDate dateWithTimeIntervalSinceNow:1];
[NSThread sleepUntilDate:future];
// Decrement current delay
currentDelay--;
}
NSString *username = [defaultsController getServerUsername];
NSString *hostname = [defaultsController getServerHostname];
NSString *remoteport = [defaultsController getRemotePortNumber];
NSNumber *localport = (NSNumber *)[defaultsController getLocalPortNumber];
NSString *additionalargs = [defaultsController getAdditionalArguments];
BOOL sshCompression = [defaultsController getCompressSSHConnection];
if (username && hostname) {
if (![SSHconnector openSSHConnectionAndNotifyObject:self
withOpeningSelector:@selector(SSHConnectionOpening:)
withSuccessSelector:@selector(SSHConnectionOpened:)
withFailureSelector:@selector(SSHConnectionFailed:)
withUsername:username
withHostname:hostname
withRemotePort:(NSString *)remoteport
withLocalBindPort:(NSNumber *)localport
withAdditionalArguments:additionalargs
withSSHCompression:sshCompression]) {
[self showRestartSidestepDialog];
}
}
else {
XLog(self, @"No username or hostname found");
}
initiatedDelayedConnectionAttempt = FALSE;
[pool release];
}
- (void)watchSSHConnectionForCloseThread :(NSTask *)connection {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[SSHconnector watchSSHConnectionAndOnCloseNotifyObject:self
withSelector:@selector(SSHConnectionClosed)
withConnection:connection];
[pool release];
}
- (void)terminateSSHConnectionAttemptThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[SSHconnector terminateSSHConnectionAttempt];
[pool release];
}
- (void)turnWirelessProxyOnThread :(NSNumber *)port {
@autoreleasepool {
if(lion) {
if (![proxySetter toggleProxy:TRUE interface:@"Wi-Fi" port:port]) {
[self showAuthorizationErrorSidestepDialog];
}
} else {
if (![proxySetter toggleProxy:TRUE interface:@"Airport" port:port]) {
[self showAuthorizationErrorSidestepDialog];
}
}
}
}
- (void)turnWirelessProxyOffThread {
@autoreleasepool {
if(lion) {
if (![proxySetter toggleProxy:FALSE interface:@"Wi-Fi" port:0]) {
[self showAuthorizationErrorSidestepDialog];
}
} else {
if (![proxySetter toggleProxy:FALSE interface:@"Airport" port:0]) {
[self showAuthorizationErrorSidestepDialog];
}
}
}
}
- (void)openVPNConnectionAfterDelayThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
while (currentDelay > 0) {
XLog(self, @"Opening VPN connection in %d seconds", currentDelay);
// Sleep for one second
NSDate *future = [NSDate dateWithTimeIntervalSinceNow:1];
[NSThread sleepUntilDate:future];
// Decrement current delay
currentDelay--;
}
int result = [vpnInterfacer turnVPNOnOrOff:[defaultsController selectedVPNService] withState:TRUE];
if (!result) {
[self showRestartSidestepDialog];
}
else {
if (result == 1) {
[growl message:connectedVPNText];
}
else {
[growl message:unknownVPNText];
}
}
initiatedDelayedConnectionAttempt = FALSE;
[pool release];
}
- (void)closeVPNConnectionThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int result = [vpnInterfacer turnVPNOnOrOff:[defaultsController selectedVPNService] withState:FALSE];
if (!result) {
[self showRestartSidestepDialog];
}
else {
if (result == 1) {
[growl message:disconnectedVPNText];
}
else {
[growl message:unknownVPNText];
}
}
[pool release];
}
/*
* Event handlers
*******************************************************************************
*/
- (void)SSHConnectionOpening :(NSTask *)connection {
XLog(self, @"Called SSHConnectionOpening. Connection task PID: %d", [connection processIdentifier]);
[defaultsController saveSSHConnectionPID:[connection processIdentifier]];
if (testingConnection) {
[self performSelectorOnMainThread:@selector(updateUIForTestingSSHConnectionOpening) withObject:nil waitUntilDone:FALSE];
}
else {
[self performSelectorOnMainThread:@selector(updateUIForSSHConnectionOpening) withObject:nil waitUntilDone:FALSE];
}
[NSThread detachNewThreadSelector:@selector(watchSSHConnectionForCloseThread:)
toTarget:self
withObject:connection];
SSHConnection = connection;
SSHConnecting = TRUE;
}
- (void)SSHConnectionOpened :(NSTask *)connection {
XLog(self, @"Called SSHConnectionOpened. Connection task PID: %d", [connection processIdentifier]);
SSHConnected = TRUE;
SSHConnecting = FALSE;
if (testingConnection) {
if (SSHConnection) {
XLog(self, @"Killing current SSH connection");
[SSHConnection terminate];
}
XLog(self, @"Terminating current SSH connection attempt");
[NSThread detachNewThreadSelector:@selector(terminateSSHConnectionAttemptThread)
toTarget:self
withObject:nil];
testingConnection = FALSE;
[self performSelectorOnMainThread:@selector(updateUIForTestingSSHConnectionSucceeded) withObject:nil waitUntilDone:FALSE];
}
else {
XLog(self, @"Turning proxy on");
NSNumber *localport = (NSNumber *)[defaultsController getLocalPortNumber];
[self turnWirelessProxyOnThread:[NSNumber numberWithInt:[localport intValue]]];
[self performSelectorOnMainThread:@selector(updateUIForSSHConnectionOpened) withObject:nil waitUntilDone:FALSE];
}
}
- (void)SSHConnectionFailed :(NSString *)errorCode {
XLog(self, @"Called SSHConnectionFailed.");
SSHConnection = nil;
SSHConnected = FALSE;
SSHConnecting = FALSE;
XLog(self, @"Resetting keychain entry");
if ([errorCode isEqualToString:@"2"]) {
BOOL result = [PasswordController deleteKeychainEntryForHost:[defaultsController getServerHostname] user:[defaultsController getServerUsername]];
XLog(self, @"Result of trying to delete keychain entry: %d", result);
}
if (testingConnection) {
testingConnection = FALSE;
[self performSelectorOnMainThread:@selector(updateUIForTestingSSHConnectionFailedWithError:) withObject:errorCode waitUntilDone:FALSE];
}
else {
[self performSelectorOnMainThread:@selector(updateUIForSSHConnectionFailedWithError:) withObject:errorCode waitUntilDone:FALSE];
if (![errorCode isEqualToString:@"2"] && ![errorCode isEqualToString:@"5"] && retryCounter < 2) {
XLog(self, @"Retrying connection attempt after delay. Retry counter: %d", retryCounter);
[self performSelectorOnMainThread:@selector(updateUIForSSHConnectionRetrying) withObject:nil waitUntilDone:FALSE];
currentDelay = 3;
initiatedDelayedConnectionAttempt = TRUE;
[NSThread detachNewThreadSelector:@selector(openSSHConnectionAfterDelayThread)
toTarget:self
withObject:nil];
retryCounter++;
}
else if (retryCounter >= 2) {
[self performSelectorOnMainThread:@selector(updateConnectionStatusForCurrentNetwork) withObject:nil waitUntilDone:FALSE];
}
}
}
- (void)SSHConnectionClosed {
XLog(self, @"SSH Connection was closed.");
if (SSHConnecting) { // Connection was closed while connecting before SSH connection watcher started
// Terminate current SSH connection attempt if still running
[NSThread detachNewThreadSelector:@selector(terminateSSHConnectionAttemptThread)
toTarget:self
withObject:nil];
}
if (SSHConnected) {
XLog(self, @"Turning proxy off");
[self turnWirelessProxyOffThread];
}
if (!testingConnection) {
[self performSelectorOnMainThread:@selector(updateUIForSSHConnectionClosed) withObject:nil waitUntilDone:FALSE];
}
[growl message:restoredDirectConnectionStatusText];
SSHConnection = nil;
SSHConnected = FALSE;
SSHConnecting = FALSE;
}
- (void)connectedToAirportNetwork {
if (![networkNotifier getNetworkSecurityTypeAndNotifyObject :self
withSelector:@selector(connectedToAirportNetworkWithSecurityType:)]) {
[self showRestartSidestepDialog];
}
}
- (void)connectedToAirportNetworkWithSecurityType:(NSString *)security {
XLog(self, @"Network security type: %@", security);
currentNetworkSecurityType = security;
[self performSelectorOnMainThread:@selector(updateConnectionStatusForCurrentNetwork) withObject:nil waitUntilDone:FALSE];
/* Kill process if there's one running
*/
if ([defaultsController getSSHConnectionPID] != 0) {
[self closeSSHConnection];
}
if ([[defaultsController selectedProxy] isEqualToString:@"0"]) {
[self closeVPNConnection];
}
/* Launch new process if needed
*/
if ([security isEqualToString:@"none"] && [defaultsController rerouteAutomaticallyEnabled]) {
if ([[defaultsController selectedProxy] isEqualToString:@"1"]) {
[self openSSHConnectionAfterDelay:3];
}
else {
[self openVPNConnectionAfterDelay:3];
}
}
}
- (void) receiveSleepNote: (NSNotification*) note
{
NSLog(@"receiveSleepNote: %@", [note name]);
}
- (void) receiveWakeNote: (NSNotification*) note
{
NSLog(@"receiveSleepNote: %@", [note name]);
}
/*
* UI Functions
*******************************************************************************
*/
- (void)updateConnectionStatusForCurrentNetwork {
XLog(self, @"Called updateConnectionStatusForCurrentNetwork");
if ([currentNetworkSecurityType isEqualToString:@""]) {
[connectionStatus setTitle:noNetworkConnectionStatusText];
if (GrowlSpam_ConnectionType != 1 && GrowlSpam_TestConnection != 1) {
[growl message:noNetworkConnectionStatusText];
GrowlSpam_ConnectionType = 1;
GrowlSpam_TestConnection = 0;
}
// Update the images in our NSStatusItem
[statusItem setImage:statusImageDirectSecure];
[statusItem setAlternateImage:statusImageDirectSecure];
}
else if ([currentNetworkSecurityType isEqualToString:@"none"]) {
[connectionStatus setTitle:openConnectionStatusText];
if (GrowlSpam_ConnectionType != 2) {
[growl message:openConnectionStatusText];
GrowlSpam_ConnectionType = 2;
}
// Update the images in our NSStatusItem
[statusItem setImage:statusImageDirectInsecure];
[statusItem setAlternateImage:statusImageDirectInsecure];
}
else {
[connectionStatus setTitle:protectedConnectionStatusText];
if (GrowlSpam_ConnectionType != 3) {
[growl message:protectedConnectionStatusText];
GrowlSpam_ConnectionType = 3;
}
// Update the images in our NSStatusItem
[statusItem setImage:statusImageDirectSecure];
[statusItem setAlternateImage:statusImageDirectSecure];
}
}
- (void)updateUIForSSHConnectionRetrying {
XLog(self, @"Called updateUIForSSHConnectionRetrying");
// Update connection status
[connectionStatus setTitle:retryingConnectionStatusText];
}
- (void)updateUIForSSHConnectionOpening {
XLog(self, @"Called updateUIForSSHConnectionOpening");
// Update connection status
[connectionStatus setTitle:connectingConnectionStatusText];
if (GrowlSpam_ConnectingToProxy == 0) {
[growl message:connectingConnectionStatusText];
GrowlSpam_ConnectingToProxy = 1;
}
// Disable reroute or restore button
[rerouteOrRestoreConnectionButton setEnabled:FALSE];
}
- (void)updateUIForSSHConnectionOpened {
XLog(self, @"Called updateUIForSSHConnectionOpened");
// Update connection status
[connectionStatus setTitle:proxyConnectedConnectionStatusText];
[growl message:proxyConnectedConnectionStatusText];
// Reset GrowlSpam variable to allow notifications now that spam should have ended
GrowlSpam_ConnectingToProxy = 0;
// Update proxy server status
// Proxy server status is updated in another growl message. No need to add one here.
[proxyServerStatus setTitle:connectedServerStatusText];
// Set reroute or restore button title
[rerouteOrRestoreConnectionButton setTitle:restoreConnectionButtonTitle];
// Enable reroute or restore button
[rerouteOrRestoreConnectionButton setEnabled:TRUE];
// Update the images in our NSStatusItem
[statusItem setImage:statusImageReroutedSecure];
[statusItem setAlternateImage:statusImageReroutedSecure];
}
- (void)updateUIForSSHConnectionFailedWithError :(NSString *)errorCode {
XLog(self, @"Called updateUIForSSHConnectionFailedWithError");
if ([errorCode isEqualToString:@"2"]) {
// Update proxy server status
[proxyServerStatus setTitle:authorizationErrorServerStatusText];
}
else if ([errorCode isEqualToString:@"3"] || [errorCode isEqualToString:@"4"]) {
// Update proxy server status
[proxyServerStatus setTitle:connectionErrorServerStatusText];
}
// Update connection status
[self updateConnectionStatusForCurrentNetwork];
// Set reroute or restore button title
[rerouteOrRestoreConnectionButton setTitle:rerouteConnectionButtonTitle];
// Enable reroute or restore button
[rerouteOrRestoreConnectionButton setEnabled:TRUE];
}
- (void)updateUIForSSHConnectionClosed {
XLog(self, @"Called updateUIForSSHConnectionClosed");
// Update connection status
[self updateConnectionStatusForCurrentNetwork];
// Update proxy server status
[proxyServerStatus setTitle:notConnectedServerStatusText];
// Update reroute or restore button title
[rerouteOrRestoreConnectionButton setTitle:rerouteConnectionButtonTitle];
}
- (void)updateUIForTestingSSHConnectionOpening {
XLog(self, @"Called updateUIForTestingSSHConnectionOpening");
// Update testing connection status
[testConnectionStatusField setStringValue:testingConnectionStatusText];
[growl message:testingConnectionStatusText];
// Prevent wireless status from appearing after test.
GrowlSpam_TestConnection = 1;
}
- (void)updateUIForTestingSSHConnectionSucceeded {
XLog(self, @"Called updateUIForTestingSSHConnectionSucceeded");
// Update testing connection status
[testConnectionStatusField setStringValue:sucessTestingConnectionStatusText];
[growl message:sucessTestingConnectionStatusText];
// Growl Spam Reduction reset. This allows messages to appear if the user connects to a different network of the same type.
GrowlSpam_ConnectionType = 0;
}
- (void)updateUIForTestingSSHConnectionFailedWithError :(NSString *)errorCode {
XLog(self, @"Called updateUIForTestingSSHConnectionFailedWithError");
if ([errorCode isEqualToString:@"2"]) {
// Update testing connection status
[testConnectionStatusField setStringValue:authFailedTestingConnectionStatusText];
[growl message:authFailedTestingConnectionStatusText];
}
else if ([errorCode isEqualToString:@"3"] || [errorCode isEqualToString:@"4"] || [errorCode isEqualToString:@"5"]) {
// Update testing connection status
[testConnectionStatusField setStringValue:reachFailedTestingConnectionStatusText];
[growl message:reachFailedTestingConnectionStatusText];
}
// Growl Spam Reduction reset. This allows messages to appear if the user connects to a different network of the same type.
GrowlSpam_ConnectionType = 0;
}
- (void)showAuthorizationErrorSidestepDialog {
XLog(self, @"Showing user Authorization Error Sidestep dialog");
[NSApp activateIgnoringOtherApps:YES]; // Allows windows of this app to become front
NSRunCriticalAlertPanel( @"Error accessing your System Preferences",
@"It seems that you didn't allow Sidestep to modify your System Preferences.\n\n"
"Please close and open Sidestep again in order to ensure smooth running,"
"by authorizing Sidestep to modify your System.\n\n"
"Until you do so, you will not benefit from Sidestep functionalities.",
@"OK",
nil,
nil,
nil);
}
- (void)showRestartSidestepDialog {
XLog(self, @"Showing user restart Sidestep dialog");
[NSApp activateIgnoringOtherApps:YES]; // Allows windows of this app to become front
NSRunCriticalAlertPanel( @"Please restart Sidestep",
@"It seems that you've moved Sidestep to somewhere else on your computer "
"or have renamed the application.\n\n"
"Please close and open Sidestep again in order to ensure smooth running.\n\n"
"Until you do so, you might experience problems with Sidestep and your "
"Internet connection.",
@"OK",
nil,