forked from reuben/coccinellida
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TunnelController.m
537 lines (459 loc) · 18.5 KB
/
TunnelController.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
//
// TunnelController.m
// Coccinellida
//
// Licensed under GPL v3 Terms
//
// Created by Dmitry Geurkov on 6/8/10.
// Copyright 2010-2011. All rights reserved.
//
#import "TunnelController.h"
#import "Tunnel.h"
@implementation TunnelController
- (void) awakeFromNib {
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
onSound = [[NSSound alloc] initWithContentsOfFile: [[NSBundle bundleForClass:[self class]] pathForResource: @"on" ofType: @"mp3"] byReference: YES];
[onSound setVolume: 0.2];
offSound = [[NSSound alloc] initWithContentsOfFile: [[NSBundle bundleForClass:[self class]] pathForResource: @"off" ofType: @"mp3"] byReference: YES];
[offSound setVolume: 0.2];
NSData* tunnelData = [[NSUserDefaults standardUserDefaults] dataForKey: @"tunnels"];
if(tunnelData == nil)
tunnels = [NSMutableArray new];
else
tunnels = (NSMutableArray*) [NSKeyedUnarchiver unarchiveObjectWithData: tunnelData];
[tunnelsList setDataSource:(id<NSTableViewDataSource>)self];
[portForwardingList setDataSource:(id<NSTableViewDataSource>)self];
[self rebuildMenuList];
if( [tunnels count] > 0){
[editTunnelButton setEnabled: YES];
[deleteTunnelButton setEnabled: YES];
}else{
[editTunnelButton setEnabled: NO];
[deleteTunnelButton setEnabled: NO];
}
exitThread = NO;
[NSThread detachNewThreadSelector: @selector(checkTunnels) toTarget:self withObject:nil ];
}
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{
return YES;
}
-(void) checkTunnels {
// Autostart tunnels
for(Tunnel* t in tunnels){
if([t autostart] == YES)
[t start];
}
while(!exitThread){
@synchronized(self){
int i = 0;
for(Tunnel* t in tunnels){
if( [t delegate] == nil )
[t setDelegate: self];
[t readStatus];
if( [t running] == YES && [t checkProcess] == NO ){
[t stop];
[NSThread sleepForTimeInterval:2];
[t start];
}
[[[statusMenu itemArray] objectAtIndex: i+3] setState: [t running] ? NSOnState : NSOffState];
i++;
}
}
[NSThread sleepForTimeInterval:3];
}
}
-(void) tunnelStatusChanged: (Tunnel*) tunnel status: (NSString*) status {
if( [status isEqualToString: @"START"] ){
if ( [soundEffectsButton state] == NSOnState )
[onSound play];
if ( [notificationsButton state] == NSOnState )
[self notificationActionText:@"Tunnel started" notificationTitle:[NSString stringWithFormat: @"Tunnel %@", [tunnel name]]];
}
if( [status isEqualToString: @"STOP"] ){
if ( [soundEffectsButton state] == NSOnState )
[offSound play];
if ( [notificationsButton state] == NSOnState )
[self notificationActionText:@"Tunnel stopped" notificationTitle:[NSString stringWithFormat: @"Tunnel %@", [tunnel name]]];
}
if( [status isEqualToString: @"TIME_OUT"] ){
if ( [notificationsButton state] == NSOnState )
[self notificationActionText:@"Time out occured" notificationTitle:[NSString stringWithFormat: @"Tunnel %@", [tunnel name]]];
}
if( [status isEqualToString: @"CONNECTED"] ){
if ( [notificationsButton state] == NSOnState )
[self notificationActionText:@"Connection established" notificationTitle:[NSString stringWithFormat: @"Tunnel %@", [tunnel name]]];
}
if( [status isEqualToString: @"WRONG_PASSWORD"] ){
if ( [notificationsButton state] == NSOnState )
[self notificationActionText:@"Login/Password incorrect" notificationTitle:[NSString stringWithFormat: @"Tunnel %@", [tunnel name]]];
passwordChangeTunnel = tunnel;
[passwordChangeTextField setStringValue: @""];
[passwordWindow setTitle: [NSString stringWithFormat: @"%@ incorrect password", [tunnel name]]];
[passwordWindow center];
[NSApp runModalForWindow: passwordWindow];
[NSApp endSheet: passwordWindow];
if(passwordChanged){
[tunnel stop];
[tunnel start];
}else{
[tunnel stop];
NSAlert* alert = [NSAlert new];
[[alert window] center];
[alert addButtonWithTitle:@"Close"];
[alert setMessageText: [NSString stringWithFormat: @"Tunnel %@ Problem",[tunnel name]]];
[alert setInformativeText:@"Login/Password incorrect"];
[alert beginSheetModalForWindow: nil modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];
}
}
if( [status isEqualToString: @"CONNECTION_REFUSED"] ){
if ( [notificationsButton state] == NSOnState )
[self notificationActionText:@"Connection refused" notificationTitle:[NSString stringWithFormat: @"Tunnel %@", [tunnel name]]];
[tunnel stop];
NSAlert* alert = [NSAlert new];
[[alert window] center];
[alert addButtonWithTitle:@"Close"];
[alert setMessageText: [NSString stringWithFormat: @"Tunnel %@ Problem",[tunnel name]]];
[alert setInformativeText:@"Connection refused"];
[alert beginSheetModalForWindow: nil modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];
}
if( [status isEqualToString: @"CONNECTION_ERROR"] ){
if ( [notificationsButton state] == NSOnState )
[self notificationActionText:@"Connection error" notificationTitle:[NSString stringWithFormat: @"Tunnel %@", [tunnel name]]];
}
}
- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
}
- (void) applicationWillTerminate: (NSNotification*) notification {
exitThread = YES;
@synchronized(self){
for(Tunnel* t in tunnels){
if( [t running] )
[t stop];
}
}
}
- (IBAction) chooseIdentityFile: (id) sender {
NSOpenPanel* panel = [NSOpenPanel openPanel];
[panel setShowsHiddenFiles: YES];
[panel beginSheetForDirectory: @"~"
file:nil
modalForWindow: tunnelWindow
modalDelegate:self
didEndSelector:@selector(chooseIdentityFileDidEnd:
returnCode:
contextInfo:)
contextInfo:nil];
}
- (void)chooseIdentityFileDidEnd: (NSOpenPanel*)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
if (returnCode == NSOKButton)
{
[identityTextField setStringValue: [panel filename]];
[self prepareSSHCommand: nil];
}
}
- (IBAction) changePassword: (id) sender {
passwordChanged = YES;
[NSApp endSheet: passwordWindow];
[passwordWindow orderOut: self];
[passwordChangeTunnel setPassword: [passwordChangeTextField stringValue]];
passwordChangeTunnel = nil;
[self saveTunnelsData];
}
- (IBAction) cancelPasswordChange: (id) sender {
passwordChanged = NO;
passwordChangeTunnel = nil;
[NSApp endSheet: passwordWindow];
[passwordWindow orderOut: self];
}
- (IBAction) saveEditDialog: (id) sender {
if ( [[nameTextField stringValue] length] > 0){
[selectedTunnel setName: [nameTextField stringValue]];
[selectedTunnel setAutostart: [autostartCheckBox state] == NSOnState ? YES : NO];
[selectedTunnel setHost: [hostTextField stringValue]];
[selectedTunnel setPort: [[portTextField stringValue] intValue]];
[selectedTunnel setUser: [userTextField stringValue]];
[selectedTunnel setPassword: [passwordTextField stringValue]];
[selectedTunnel setIdentity: [identityTextField stringValue]];
[selectedTunnel setSocksHost: [socksHostTextField stringValue]];
[selectedTunnel setSocksPort: [[socksPortTextField stringValue] intValue]];
[selectedTunnel setConnectionTimeout: [[connectionTimeoutTextField stringValue] intValue]];
[selectedTunnel setAliveInterval: [[aliveIntervalTextField stringValue] intValue]];
[selectedTunnel setAliveCountMax: [[aliveCountMaxTextField stringValue] intValue]];
[selectedTunnel setTcpKeepAlive: [tcpKeepAliveCheckBox state] == NSOnState ? YES : NO];
[selectedTunnel setCompression: [compressionCheckBox state] == NSOnState ? YES : NO];
[selectedTunnel setAdditionalArgs: [additionalArgsTextField string]];
[selectedTunnel setPortForwardings: [NSMutableArray arrayWithArray: portForwardings]];
if(selectedTunnelIndex == -1){
[self addTunnel: selectedTunnel];
}else{
[self editTunnel: selectedTunnelIndex];
}
[self closeEditDialog: sender];
}
}
- (IBAction) closeEditDialog: (id) sender {
[tunnelWindow orderOut: self];
portForwardings = nil;
[NSApp stopModal];
selectedTunnel = nil;
selectedTunnelIndex = -1;
}
- (IBAction) buttonAddTunnel: (id) sender {
selectedTunnel = [Tunnel new];
selectedTunnelIndex = -1;
portForwardings = [NSMutableArray array];
[nameTextField setStringValue: @""];
[autostartCheckBox setState: NSOffState];
[hostTextField setStringValue: @""];
[portTextField setStringValue: @"22"];
[userTextField setStringValue: @""];
[passwordTextField setStringValue: @""];
[identityTextField setStringValue: @""];
[socksHostTextField setStringValue: @""];
[socksPortTextField setStringValue: @""];
[connectionTimeoutTextField setStringValue: @"15"];
[aliveIntervalTextField setStringValue: @"30"];
[aliveCountMaxTextField setStringValue: @"3"];
[tcpKeepAliveCheckBox setState: NSOffState];
[compressionCheckBox setState: NSOffState];
[additionalArgsTextField setString: @""];
[sshCommandTextField setString: @""];
[tabView selectTabViewItemAtIndex: 0];
[NSApp beginSheet: tunnelWindow
modalForWindow: prefWindow
modalDelegate: nil
didEndSelector: nil
contextInfo: nil];
[NSApp runModalForWindow: tunnelWindow];
[NSApp endSheet: tunnelWindow];
[tunnelWindow orderOut: self];
}
- (IBAction) buttonEditTunnel: (id) sender {
if( [tunnelsList selectedRow] != -1){
@synchronized(self){
selectedTunnel = [tunnels objectAtIndex: [tunnelsList selectedRow]];
}
selectedTunnelIndex = [tunnelsList selectedRow];
portForwardings = [selectedTunnel portForwardings];
[nameTextField setStringValue: [selectedTunnel name]];
[autostartCheckBox setState: [selectedTunnel autostart] == YES ? NSOnState : NSOffState];
[hostTextField setStringValue: [selectedTunnel host]];
[portTextField setStringValue: [[NSNumber numberWithInt: [selectedTunnel port]] stringValue]];
[userTextField setStringValue: [selectedTunnel user]];
[passwordTextField setStringValue: [selectedTunnel password]];
[identityTextField setStringValue: [selectedTunnel identity]];
[socksHostTextField setStringValue: [selectedTunnel socksHost]];
[socksPortTextField setStringValue: [[NSNumber numberWithInt: [selectedTunnel socksPort]] stringValue]];
[connectionTimeoutTextField setStringValue: [[NSNumber numberWithInt: [selectedTunnel connectionTimeout]] stringValue]];
[aliveIntervalTextField setStringValue: [[NSNumber numberWithInt: [selectedTunnel aliveInterval]] stringValue]];
[aliveCountMaxTextField setStringValue: [[NSNumber numberWithInt: [selectedTunnel aliveCountMax]] stringValue]];
[tcpKeepAliveCheckBox setState: [selectedTunnel tcpKeepAlive] == YES ? NSOnState : NSOffState];
[compressionCheckBox setState: [selectedTunnel compression] == YES ? NSOnState : NSOffState];
[additionalArgsTextField setString: [selectedTunnel additionalArgs]];
[sshCommandTextField setString: [[selectedTunnel prepareSSHCommandArgs] objectAtIndex: 0]];
[tabView selectTabViewItemAtIndex: 0];
[NSApp beginSheet: tunnelWindow
modalForWindow: prefWindow
modalDelegate: nil
didEndSelector: nil
contextInfo: nil];
[NSApp runModalForWindow: tunnelWindow];
[NSApp endSheet: tunnelWindow];
[tunnelWindow orderOut: self];
}
}
- (IBAction) buttonDeleteTunnel: (id) sender {
@synchronized(self){
if( [tunnels count] > 0)
[self deleteTunnel: [tunnelsList selectedRow]];
if( [tunnels count] > 0){
[editTunnelButton setEnabled: YES];
[deleteTunnelButton setEnabled: YES];
}else{
[editTunnelButton setEnabled: NO];
[deleteTunnelButton setEnabled: NO];
}
}
}
-(void) addTunnel: (Tunnel*) tunnel {
@synchronized(self){
[tunnels addObject: tunnel];
[self rebuildMenuList];
[tunnelsList reloadData];
if( [tunnels count] > 0){
[editTunnelButton setEnabled: YES];
[deleteTunnelButton setEnabled: YES];
}else{
[editTunnelButton setEnabled: NO];
[deleteTunnelButton setEnabled: NO];
}
NSData* tunnelData = [NSKeyedArchiver archivedDataWithRootObject: tunnels];
[[NSUserDefaults standardUserDefaults] setObject: tunnelData forKey: @"tunnels"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
-(void) editTunnel: (int) indexToEdit {
@synchronized(self){
[self saveTunnelsData];
}
}
-(void) saveTunnelsData {
[self rebuildMenuList];
[tunnelsList reloadData];
NSData* tunnelData = [NSKeyedArchiver archivedDataWithRootObject: tunnels];
[[NSUserDefaults standardUserDefaults] setObject: tunnelData forKey: @"tunnels"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
-(void) deleteTunnel: (int) indexToRemove {
@synchronized(self){
Tunnel* t = (Tunnel*)[tunnels objectAtIndex: indexToRemove];
if( [t running] )
[t stop];
[tunnels removeObjectAtIndex: indexToRemove];
[t tunnelRemoved];
[self rebuildMenuList];
[tunnelsList reloadData];
if( [tunnels count] > 0){
[editTunnelButton setEnabled: YES];
[deleteTunnelButton setEnabled: YES];
}else{
[editTunnelButton setEnabled: NO];
[deleteTunnelButton setEnabled: NO];
}
NSData* tunnelData = [NSKeyedArchiver archivedDataWithRootObject: tunnels];
[[NSUserDefaults standardUserDefaults] setObject: tunnelData forKey: @"tunnels"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
-(void) rebuildMenuList {
int i = 0;
int c = [[statusMenu itemArray] count];
for(i=0;i<(c-5);i++){
[statusMenu removeItemAtIndex: 3];
}
if( [tunnels count] > 0){
i = 0;
for(Tunnel* t in tunnels){
NSMenuItem* m = [NSMenuItem new];
[m setTitle: [t name]];
[m setAction: @selector(startStopTunnel:)];
[m setTarget: self];
[m setEnabled: YES];
[m setState: [t running] == YES ? NSOnState : NSOffState];
[statusMenu insertItem: m atIndex: 3+i];
i++;
}
}else{
NSMenuItem* m = [NSMenuItem new];
[m setTitle: @"No Tunnels"];
[m setEnabled: NO];
[statusMenu insertItem: m atIndex: 3];
}
}
-(IBAction) startStopTunnel: (id) sender {
NSMenuItem* m = (NSMenuItem*)sender;
passwordChanged = NO;
int i = 0;
int menuIndex = -1;
for(NSMenuItem* mi in [statusMenu itemArray]){
if(mi == m){
menuIndex = i-3;
break;
}
i++;
}
if(menuIndex != -1){
Tunnel* t = nil;
@synchronized(self){
t = [tunnels objectAtIndex: menuIndex];
}
if( t != nil){
if( [t running] )
[t stop];
else
[t start];
[m setState: [t running] == YES ? NSOnState : NSOffState];
}
}
}
- (IBAction) addPortForwardDialog: (id) sender {
[portForwardings insertObject: @"L:Type Port:localhost:Type Host:Type Port" atIndex: [portForwardings count]];
[portForwardingList reloadData];
[self prepareSSHCommand: nil];
}
- (IBAction) removePortForwardDialog: (id) sender {
if( [portForwardingList selectedRow] >= 0){
[portForwardings removeObjectAtIndex: [portForwardingList selectedRow]];
[portForwardingList reloadData];
[self prepareSSHCommand: nil];
}
}
- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn*) tableColumn row: (int) rowIndex {
if( tableView == tunnelsList){
return [[tunnels objectAtIndex: rowIndex] name];
}else{
NSString* portForwarding = [portForwardings objectAtIndex: rowIndex];
NSArray* arr = [portForwarding componentsSeparatedByString: @":"];
uint index = [[portForwardingList tableColumns] indexOfObject: tableColumn];
if( index == 0)
return [[arr objectAtIndex: 0] isEqualTo: @"L"] ? @"Local" : @"Remote";
else
return [arr objectAtIndex: index];
}
}
- (void)tableView:(NSTableView *) tableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn*) tableColumn row: (int)rowIndex {
if( tableView != tunnelsList){
NSString* portForwarding = [portForwardings objectAtIndex: rowIndex];
NSMutableArray* arr = [NSMutableArray arrayWithArray: [portForwarding componentsSeparatedByString: @":"]];
uint index = [[portForwardingList tableColumns] indexOfObject: tableColumn];
if(index == 0)
anObject = [anObject isEqualTo: @"Local"] ? @"L" : @"R";
else if(index == 1 || index == 4)
anObject = [NSString stringWithFormat:@"%i", [anObject intValue]];
[arr replaceObjectAtIndex: index withObject: anObject];
portForwarding = [arr componentsJoinedByString: @":"];
[portForwardings replaceObjectAtIndex: rowIndex withObject: portForwarding];
[portForwardingList reloadData];
[self prepareSSHCommand: nil];
}
}
- (int) numberOfRowsInTableView: (NSTableView*) tableView {
if( tableView == tunnelsList){
return [tunnels count];
}else{
return [portForwardings count];
}
}
- (IBAction) prepareSSHCommand: (id) sender {
Tunnel* tmp = [Tunnel new];
[tmp setName: [nameTextField stringValue]];
[tmp setHost: [hostTextField stringValue]];
[tmp setPort: [[portTextField stringValue] intValue]];
[tmp setUser: [userTextField stringValue]];
[tmp setPassword: [passwordTextField stringValue]];
[tmp setIdentity: [identityTextField stringValue]];
[tmp setSocksHost: [socksHostTextField stringValue]];
[tmp setSocksPort: [[socksPortTextField stringValue] intValue]];
[tmp setConnectionTimeout: [[connectionTimeoutTextField stringValue] intValue]];
[tmp setAliveInterval: [[aliveIntervalTextField stringValue] intValue]];
[tmp setAliveCountMax: [[aliveCountMaxTextField stringValue] intValue]];
[tmp setTcpKeepAlive: [tcpKeepAliveCheckBox state] == NSOnState ? YES : NO];
[tmp setCompression: [compressionCheckBox state] == NSOnState ? YES : NO];
[tmp setAdditionalArgs: [additionalArgsTextField string]];
[tmp setPortForwardings: [NSMutableArray arrayWithArray: portForwardings]];
[sshCommandTextField setString: [[tmp prepareSSHCommandArgs] objectAtIndex: 0]];
}
- (void) controlTextDidChange: (NSNotification*) notification {
[self prepareSSHCommand: nil];
}
- (void) textDidChange: (NSNotification*) notification {
[self prepareSSHCommand: nil];
}
- (void) notificationActionText: (NSString*) actionText notificationTitle: (NSString*) title {
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = title;
notification.informativeText = actionText;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}
@end