-
Notifications
You must be signed in to change notification settings - Fork 0
/
SXRS.java
1008 lines (931 loc) · 32.2 KB
/
SXRS.java
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
/** Java application for making pretty GUI interface to
Secure eXperiment remote shift text terminal and VNC viewer.
Notes to developers:
* Code in SXRS.java is for GUI up to separator lines "// *+*+*+*+*+...".
Code after the separator line is for actually launching the
SXRS terminal and vnc viewers.
@author Glenn Horton-Smith
@version 2006-05-20 original KamLAND Remote Shift tool (KLRS)
@version 2009-08-13 extended to Secure eXperiment Remote Shift tool (SXRS)
@version 2011-04-25 add option for arbitrary logo
@version 2012-06-06 guarantee unique temporary file dir for each session
@version 2013-10-24 use applet params or app args to pass settings
@version 2014-12-09 force gui to run in Swing event dispatch thread
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.prefs.*;
import java.util.*;
public class SXRS extends Applet
implements WindowListener, ActionListener {
public static final String version="3.011";
TextField tf_userId; // default user ID to use
JCheckBox ckb_viewonly; // view only flag for vncviewer
TextField tf_loginChain; // user@host opts ! user@host opts...
JComboBox cb_vncserverHostDisplays; // e.g. vncsrv01:1 vncsrv02:1
TextField tf_localVncPortStart; // port on localhost for VNC tunnel
TextField tf_localLoginPortStart; // port on localhost for ssh tunnel
JComboBox cb_xtermCommandName; // terminal command name
JComboBox cb_vncviewerCommandName; // vncviewer command name
Button b_advancedSettingsToggle; // show/hide advanced settings
Box pnl_advancedSettings; // the advanced settings panel
Box pnl_startTerminalButtons; // the panel of "start ssh #n" buttons
Box pnl_startViewerButtons; // the panel of "start vnc #n" buttons
Button[] ba_startTerminal; // the "start ssh #n" buttons
Button[] ba_startViewer; // the "start vnc #n" buttons
Button b_help; // the help button
JFrame helpFrame; // the help window
String userId; // validated default user ID from tf_userId
int n_loginHosts=0; // number of hosts parsed from loginChain
String[] loginHosts; // hosts parsed from loginChain
String[] loginUsers; // users parsed from loginChain
String[] loginOptions; // options parsed from loginChain
int [] loginPorts; // local port set up by session i for next login
// e.g., loginPorts[0] is the port to use to log in to the 2nd session.
// Note for now loginPorts[i]= i+localLoginPortStart.
// Someday if automatic port selection were implemented, then
// the tf_localLoginPortStart text field will go away.
Preferences myprefs; // preferences database
String[] args; // arguments from main, if any
HashMap argument_pairs; // name=value pairs from args
String os; // operating system
String tmpdir; // directory for temporary files
ClassLoader classLoader; // global class loader to use
int n_vncserverHostDisplays=0; // number of vnc servers for login
String[] vncserverHosts; // individual vnc hosts
int[] vncserverDisplays; // individual vnc display numbers
int localVncPortStart=5909; // value of vnc port opened on last login
// note that localVncPort should reflect the value in effect when the last
// login terminal was opened, so startViewer() uses the right one even
// if user changes the port after opening last login terminal.
static final String BUNDLED_PUTTY_WINEXE= "(putty from SXRS_winexe.jar)";
// ^= constant used to indicate bundled putty.exe
static final String BUNDLED_VNCVIEWER_WINEXE= "(vncviewer from SXRS_winexe.jar)";
// ^= constant used to indicate bundled vncviewer.exe
static final String BUNDLED_VNCVIEWER_JAVA= "(pure Java VncViewer)";
// ^= constant used to indicate bundled vncviewer.exe
static final String MANUAL_VNCVIEWER_PLEASE= "(please start manually)";
// ^= constant used to indicate manual VNC is preferred
////////////////////////////////////////////////////////////////
// Applet methods
public void init() {
// preferences store for persistent user prefereces
try {
myprefs= Preferences.userRoot().node("SXRS");
}
catch (Exception e) { myprefs= null; }
// possible command line arguments from main
if (args != null) {
argument_pairs = new HashMap();
for (int i=0; i<args.length; i++) {
String[] pair = args[i].split("=", 2);
if (pair.length == 2) {
argument_pairs.put( pair[0], pair[1] );
}
else {
argument_pairs.put( pair[0], null );
}
}
}
// os
os="unknown";
try { os= System.getProperty("os.name");}
catch (Exception e) {}
// tmpdir
String subdir = "/SXRS_Files-" + UUID.randomUUID();
try {
tmpdir = System.getProperty("java.io.tmpdir");
}
catch (Exception e) {
showMessageDialog
( null, // can't use "this" since we're still in init()
"Problem retrieving tmpdir from java system properties:\n " +
e + "\nWill attempt to use current directory instead.",
"tmpdir problem",
JOptionPane.ERROR_MESSAGE );
tmpdir = "."; /* Note we must not delete contents of this dir. */
}
try {
File tf= new File(tmpdir+subdir);
if ( tf.isDirectory() || tf.mkdirs() )
tmpdir= tmpdir+subdir;
else {
throw new Exception("Path " + tmpdir+subdir +
" is not a directory and can't be made.");
}
}
catch (Exception e) {
showMessageDialog
( null, // can't use "this" yet, still in init()
"Problem opening/creating " + subdir + " in " + tmpdir
+ ":\n " +
e + "\nCannot proceed.",
"tmpdir problem",
JOptionPane.ERROR_MESSAGE );
System.exit(1);
}
// class loader
classLoader= this.getClass().getClassLoader();
if (classLoader==null)
classLoader= ClassLoader.getSystemClassLoader();
// set up all our widgets
this.setLayout(new BorderLayout());
// icon on west
ImageIcon sxrs_icon= null;
String icon_url_string= getPrefParam("sxrs.iconURL","");
if (icon_url_string.length()>0) {
try {
URL icon_url= new URL(icon_url_string);
sxrs_icon= new ImageIcon( icon_url, icon_url_string );
} catch (Exception e) {
showMessageDialog
( null, // can't use "this" yet, still in init()
"This is harmless, but I thought you should know...\n"+
"while trying to open image:\n"+e,
"Icon image load problem",
JOptionPane.ERROR_MESSAGE );
sxrs_icon= null;
}
}
if (sxrs_icon == null) {
sxrs_icon= new ImageIcon
(classLoader.getResource("images/SXRS-icon.gif"),
"Secure eXperiment logo");
}
add(new JLabel(sxrs_icon), BorderLayout.WEST);
// input frame on east
Box f1= new Box(BoxLayout.Y_AXIS);
f1.add( Box.createGlue());
// userId
tf_userId= new TextField(10);
userId= null;
try {
try { userId=System.getProperty("user.name"); }
catch (Exception e) {}
try { if (myprefs != null) userId= myprefs.get("userId", userId); }
catch (Exception e) {}
} catch (Exception e) { userId="your_userid"; }
tf_userId.setText( userId );
f1.add( makeInputPanel("Your userid:", tf_userId) );
// viewonly option
ckb_viewonly= new JCheckBox();
f1.add( makeInputPanel("Start VNC in \"view only\" mode:", ckb_viewonly) );
// advanced setting panel in this box
pnl_advancedSettings= new Box(BoxLayout.Y_AXIS);
//pnl_advancedSettings.setBorder(new BevelBorder(BevelBorder.LOWERED));
// loginChain
tf_loginChain= new TextField
( getPrefParam
( "sxrs.loginChain", "( loginChain not set )" ), 30);
pnl_advancedSettings.add( makeInputPanel("Login chain:", tf_loginChain));
// vncserverHostDisplays
cb_vncserverHostDisplays= new JComboBox( );
String default_vncservers= getPrefParam("sxrs.vncserverHostDisplay","");
cb_vncserverHostDisplays.addItem(default_vncservers);
for (int i=1; i<100; i++) {
String alt= getPrefParam("sxrs.vncserverHostDisplay_"+i,"");
if (alt.length()<=0)
break;
cb_vncserverHostDisplays.addItem( alt );
}
cb_vncserverHostDisplays.setSelectedItem(default_vncservers);
cb_vncserverHostDisplays.setEditable(true);
pnl_advancedSettings.add( makeInputPanel("VNC server host:display [...]:",
cb_vncserverHostDisplays));
// localVncPort
tf_localVncPortStart= new TextField( getPrefParam("sxrs.localVncPort","5909") );
pnl_advancedSettings.add( makeInputPanel("Local VNC port:",
tf_localVncPortStart));
// localLoginPortStart
tf_localLoginPortStart= new TextField( getPrefParam("sxrs.localLoginPortStart","10022") );
pnl_advancedSettings.add( makeInputPanel("Local login port start:",
tf_localLoginPortStart));
// xtermCommandName
String default_xtermCommandName= "xterm";
if ( os.indexOf("Windows")>=0 )
default_xtermCommandName= BUNDLED_PUTTY_WINEXE;
if ( os.indexOf("Mac")>=0 )
default_xtermCommandName= "/usr/X11R6/bin/xterm -display :0";
String xtermCommandName= getPrefParam("sxrs.xtermCommandName", default_xtermCommandName);
cb_xtermCommandName= new JComboBox();
cb_xtermCommandName.addItem(xtermCommandName);
if ( !xtermCommandName.equals(default_xtermCommandName) )
cb_xtermCommandName.addItem(default_xtermCommandName);
cb_xtermCommandName.setSelectedItem(xtermCommandName);
cb_xtermCommandName.setEditable(true);
pnl_advancedSettings.add( makeInputPanel("xterm command name:",
cb_xtermCommandName));
// vncviewerCommandName
String default_vncviewerCommandName= "vncviewer";
if ( os.indexOf("Windows")>=0 )
default_vncviewerCommandName= BUNDLED_VNCVIEWER_WINEXE;
if ( os.indexOf("Mac")>=0 )
default_vncviewerCommandName= BUNDLED_VNCVIEWER_JAVA;
String vncviewerCommandName= getPrefParam("sxrs.vncviewerCommandName", default_vncviewerCommandName);
cb_vncviewerCommandName= new JComboBox();
cb_vncviewerCommandName.addItem(vncviewerCommandName);
if ( !vncviewerCommandName.equals(default_vncviewerCommandName) )
cb_vncviewerCommandName.addItem(default_vncviewerCommandName);
if ( !default_vncviewerCommandName.equals(BUNDLED_VNCVIEWER_JAVA) )
cb_vncviewerCommandName.addItem(BUNDLED_VNCVIEWER_JAVA);
cb_vncviewerCommandName.setSelectedItem(vncviewerCommandName);
cb_vncviewerCommandName.setEditable(true);
pnl_advancedSettings.add( makeInputPanel("vncviewer command name:",
cb_vncviewerCommandName));
pnl_advancedSettings.setVisible( false );
f1.add( pnl_advancedSettings );
b_advancedSettingsToggle= new Button("Show advanced settings");
f1.add( b_advancedSettingsToggle );
add(f1, BorderLayout.EAST);
// button frame on bottom
Box f2= new Box(BoxLayout.X_AXIS);
// start button box
pnl_startTerminalButtons= new Box(BoxLayout.X_AXIS);
f2.add(pnl_startTerminalButtons);
updateFromLoginChain();
// start vnc button
pnl_startViewerButtons= new Box(BoxLayout.X_AXIS);
f2.add(pnl_startViewerButtons);
updateFromVncserverHostDisplays();
// help button
b_help= new Button("Help!");
f2.add(b_help);
add(f2, BorderLayout.SOUTH);
// help pane -- invisible until called for
JEditorPane helpText= new JEditorPane( );
helpText.setContentType("text/html");
helpText.setEditable(false);
try {
helpText.setPage( classLoader.getResource("html/helpJavaSXRS.html") );
} catch (Exception e) {
helpText.setText("<H1>Error loading help!</H1>"+
"The help text resource could not be found.<P>"+e+
"This should never happen if you are running from a valid jar file." );
}
JScrollPane helpObject= new JScrollPane(helpText);
helpFrame= new JFrame("Help for Secure eXperiment Remote Shift helper");
helpFrame.getContentPane().add(helpObject); // 1.4 compatible
helpFrame.setSize(500,250);
// attach ActionListeners
tf_loginChain.addActionListener(this);
tf_userId.addActionListener(this);
b_help.addActionListener(this);
b_advancedSettingsToggle.addActionListener(this);
// succesful init and debug info if requested
if ( getPrefParam("sxrs.debug",null) != null ) {
StringBuffer buff = new StringBuffer();
buff.append("Debug info requested by sxrs.debug set.\n");
buff.append("Values from system properties or preferences:\n"+
" os="+os+"\n"+
" tmpdir="+tmpdir+"\n"+
" userID="+userId+"\n" );
buff.append("Argument pairs: " + argument_pairs);
String debugMessage = buff.toString();
showMessageDialog
( null, // can't use "this" yet, still in init()
debugMessage,
"Debug message at end of SXRS.init()",
JOptionPane.INFORMATION_MESSAGE);
}
}
Panel makeInputPanel( String prompt, Component tf )
{
Panel panel= new Panel();
panel.add( new Label(prompt) );
panel.add( tf );
return panel;
}
void showMessageDialog(final Component parentComponent,
final Object message,
final String title,
final int messageType) {
// thread-safe call to showMessageDialog, using invokeLater
// with an immediate console log message too
System.out.println(message);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(parentComponent,
message,
title,
messageType);
}
});
}
void validateUserId() {
// get userId and check for validity
userId= tf_userId.getText();
if (userId.length()<=0 || userId.indexOf(' ')>=0) {
showMessageDialog
( this,
"User ID must not contain spaces and must not be null.\n"
+ "Please enter a valid user ID and try again.",
"Error",
JOptionPane.ERROR_MESSAGE );
return;
}
}
void updateFromLoginChain() {
int nh=0;
int i1,i2,i3;
String s= tf_loginChain.getText();
int l= s.length();
// first count number of '!' to determine nh
nh=1;
for (i1=0; i1<l; i1++)
if (s.charAt(i1)=='!')
nh++;
// [re]initialize arrays iff size changes (otherwise re-use)
if (nh != n_loginHosts) {
n_loginHosts= nh;
loginHosts= new String[nh];
loginUsers= new String[nh];
loginOptions= new String[nh];
loginPorts= new int[nh];
ba_startTerminal= new Button[nh];
pnl_startTerminalButtons.removeAll();
for (int ih=0; ih<nh; ih++) {
ba_startTerminal[ih]= new Button("SSH #"+(ih+1));
if (ih>0)
ba_startTerminal[ih].setEnabled(false);
ba_startTerminal[ih].addActionListener(this);
pnl_startTerminalButtons.add(ba_startTerminal[ih]);
}
this.validate();
}
validateUserId();
int ih= 0;
i1=i2=0;
while (ih < nh && i1<l) {
// extract [user@]host
while (i1<l && s.charAt(i1)==' ')
i1++;
i2=i1;
while (i2<l && s.charAt(i2)!='!' && s.charAt(i2)!=' ')
i2++;
// copy user and host
i3= s.indexOf('@',i1);
if (i3>i1 && i3<i2) {
loginUsers[ih]= s.substring(i1,i3);
loginHosts[ih]= s.substring(i3+1,i2);
}
else {
loginUsers[ih]= userId;
loginHosts[ih]= s.substring(i1,i2);
}
//System.out.println("host "+ih+" "+loginHosts[ih]);
//System.out.println("user "+ih+" "+loginUsers[ih]);
// extract options, if any
i1=i2;
while (i2<l && s.charAt(i2)!='!')
i2++;
if (i2>i1)
loginOptions[ih]= s.substring(i1,i2);
else
loginOptions[ih]= "";
//System.out.println("option "+ih+" "+loginOptions[ih]);
ih++;
i1=i2+1;
}
if (ih != nh) {
System.err.println("Logic error in parsing option string! ih="+ih
+" nh="+nh);
}
}
void updateFromVncserverHostDisplays()
{
// get and validate vncserverHost and vncserverPort list,
// update button panel
String hds =((String)(cb_vncserverHostDisplays.getSelectedItem())).trim();
int ihash= hds.indexOf("#");
if (ihash > 0)
hds= hds.substring(0,ihash).trim(); // ignore comment
StringTokenizer tk= new StringTokenizer(hds);
int n= tk.countTokens();
// [re]initialize arrays iff size changes (otherwise re-use)
if (n != n_vncserverHostDisplays) {
n_vncserverHostDisplays= n;
vncserverHosts= new String[n];
vncserverDisplays= new int[n];
ba_startViewer= new Button[n];
pnl_startViewerButtons.removeAll();
for (int i=0; i<n; i++) {
ba_startViewer[i]= new Button("VNC #"+(i+1));
ba_startViewer[i].setEnabled(false);
ba_startViewer[i].addActionListener(this);
pnl_startViewerButtons.add(ba_startViewer[i]);
}
this.validate();
}
int i= 0;
for (i=0; tk.hasMoreTokens() && i<n; i++) {
String hd= tk.nextToken();
String vncserverHost;
int vncserverDisplay= 1;
try {
int icolon= hd.indexOf(":");
if (icolon<0) {
vncserverHost= hd;
}
else {
vncserverHost= hd.substring(0,icolon);
vncserverDisplay= Integer.decode(hd.substring(icolon+1)).intValue();
}
vncserverHosts[i]= vncserverHost;
vncserverDisplays[i]= vncserverDisplay;
}
catch (Exception e) {
showMessageDialog
( this,
"Could not parse integer from vncserver Host:Display.\n"
+e,
"Error",
JOptionPane.ERROR_MESSAGE );
return;
}
}
}
////////////////////////////////////////////////////////////////
// utility methods
String getPrefParam( String paramName, String defaultValue )
{
String rv=null;
try {
if (myprefs != null)
try { rv= myprefs.get(paramName, null); } catch (Exception e) {}
if (rv==null && argument_pairs != null)
rv= (String)(argument_pairs.get(paramName));
if (rv==null)
rv= getParameter(paramName);
if (rv==null)
return defaultValue;
else
return rv;
} catch (Exception e) {
return defaultValue;
}
}
////////////////////////////////////////////////////////////////
// ActionListener methods for responding to events
public void actionPerformed(ActionEvent evt) {
Object source= evt.getSource();
for (int ih=0; ih<n_loginHosts; ih++) {
if (source == ba_startTerminal[ih]) {
// make sure buttons are up to date, hopefully not belatedly
// (only a problem if the button user is hitting disappeared)
updateFromLoginChain();
updateFromVncserverHostDisplays();
if (ih >= n_loginHosts) {
showMessageDialog
( this,
"It looks like you changed the login chain string but didn't hit return to update the button bar.\nPlease try again.",
"Oops.",
JOptionPane.ERROR_MESSAGE );
return;
}
TerminalRunner r= new TerminalRunner(this,ih);
r.start();
return;
}
}
for (int id=0; id<n_vncserverHostDisplays; id++) {
if (source == ba_startViewer[id]) {
try {
startViewer(id);
} catch (Exception e) {
showMessageDialog
( this,
"While trying to start vncviewer:\n"+e,
"Start vncviewer failure",
JOptionPane.ERROR_MESSAGE );
}
return;
}
}
if (source == tf_userId) {
validateUserId();
}
else if (source == tf_loginChain) {
updateFromLoginChain();
updateFromVncserverHostDisplays();
}
else if (source == cb_vncserverHostDisplays) {
updateFromLoginChain();
updateFromVncserverHostDisplays();
}
else if (source == b_help) {
helpFrame.setVisible(true);
helpFrame.setState(helpFrame.NORMAL);
}
else if (source == b_advancedSettingsToggle) {
Container p= this.getParent();
if (p==null) p= this;
if (pnl_advancedSettings.isVisible()) {
pnl_advancedSettings.setVisible(false);
b_advancedSettingsToggle.setLabel("Show advanced settings");
}
else {
pnl_advancedSettings.setVisible(true);
b_advancedSettingsToggle.setLabel("Hide advanced settings");
}
updateFromLoginChain();
updateFromVncserverHostDisplays();
this.validate();
p.setSize( p.getPreferredSize() );
p.validate();
}
else {
showMessageDialog
( this,
"Unhandled action: "+evt+"\n"+
"Unimplemented widget, or actionListener was set for a widget that doesn't need it?",
"Harmless internal error",
JOptionPane.ERROR_MESSAGE );
}
}
////////////////////////////////////////////////////////////////
// quit method for leaving when window closed
public void quit()
{
/* clean up temporary directory */
clearResourcesCopied();
// note the >0 instead of >=0 in next line is deliberate
if (tmpdir.indexOf("/SXRS_Files-")>0) {
try {
File tf = new File(tmpdir);
tf.delete();
}
catch (Exception e) {
}
}
/* exit */
System.exit(0);
}
////////////////////////////////////////////////////////////////
// WindowListener methods
public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
quit();
}
public void windowClosed(WindowEvent e) {
quit();
}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
////////////////////////////////////////////////////////////////
// main() so we can run standalone
public static void main(final String[] args)
{
// Since some javax.swing classes are used in the SXRS gui,
// and by design swing is not thread-safe,
// we launch using SwingUtilities.invokeLater().
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SXRS sxrs= new SXRS();
sxrs.args = args;
sxrs.init();
Frame f= new Frame("Secure eXperiment Remote Shift Client "+version);
f.add(sxrs);
f.addWindowListener(sxrs);
f.pack();
f.setVisible(true);
sxrs.start();
}
});
}
// *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
// *+* End of GUI code *+*
// *+* Start of code to run external SXRS connection applications *+*
// *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
////////////////////////////////////////////////////////////////
// TerminalRunner embedded class
class TerminalRunner extends Thread {
SXRS appf;
int istage;
public TerminalRunner(SXRS arg_appf, int arg_istage) {
appf=arg_appf;
istage= arg_istage;
}
public void run()
{
try {
runTerminal();
}
catch (Exception e) {
showMessageDialog
( appf,
"While trying to start terminal:\n"+e,
"Start terminal failure",
JOptionPane.ERROR_MESSAGE );
e.printStackTrace();
}
}
public void runTerminal() throws Exception
{
// set thisLoginPort
int thisLoginPort=-1;
if (istage>0)
thisLoginPort= loginPorts[istage-1];
// set nextLoginPort
int nextLoginPort;
try {
nextLoginPort= Integer.decode(tf_localLoginPortStart.getText()).intValue() + istage;
}
catch (Exception e) {
showMessageDialog
( appf,
"Could not parse integer from \"local login port\" advanced setting.\n"
+ "Please enter an integer and try again.",
"Error",
JOptionPane.ERROR_MESSAGE );
return;
}
loginPorts[istage]= nextLoginPort;
// get and check localVncPort
// strictly only needed for last login, but check each time anyway
try {
localVncPortStart= Integer.decode(tf_localVncPortStart.getText()).intValue();
}
catch (Exception e) {
showMessageDialog
( appf,
"Could not parse integer from \"local VNC port\" advanced setting.\n"
+ "Please enter an integer and try again.",
"Error",
JOptionPane.ERROR_MESSAGE );
return;
}
// set nexthost
String nexthost;
if (istage < n_loginHosts-1)
nexthost= loginHosts[istage+1];
else
nexthost= null;
// set options
String options;
{
StringBuffer optionbuff= new StringBuffer();
optionbuff.append( loginOptions[istage]
+ " -l " + loginUsers[istage] );
if (nexthost != null)
optionbuff.append(" -L " + nextLoginPort + ":" + nexthost + ":22 ");
else {
for (int id=0; id<n_vncserverHostDisplays; id++)
optionbuff.append
( " -L " + (localVncPortStart+id) + ":"
+ vncserverHosts[id] + ":"
+ (vncserverDisplays[id]+5900) + " " );
}
options= optionbuff.toString();
}
// get other settings
String xtermCommandName= (String)(cb_xtermCommandName.getSelectedItem());
// prepare command line for SSH process of choice
String[] command;
if ( xtermCommandName.indexOf("putty")<0 ) {
// xterm and ssh must be on the command search path
String scriptName= tmpdir+"/sxrs_ssh.bash";
copyFromResource(null, "bash/sxrs_ssh.bash", scriptName);
String[] tmpcommand1= xtermCommandName.split(" ");
String[] tmpcommand2=
{ "-e", "bash", scriptName,
options + ( istage > 0 ? " -p "+thisLoginPort+" localhost"
: loginHosts[0] ) };
command= new String[tmpcommand1.length + tmpcommand2.length];
for (int i=0; i<tmpcommand1.length; i++)
command[i]= tmpcommand1[i];
for (int i=0; i<tmpcommand2.length; i++)
command[i+tmpcommand1.length]= tmpcommand2[i];
}
else /* using putty */ {
String exename= xtermCommandName;
if ( exename.equals(BUNDLED_PUTTY_WINEXE) ) {
exename= tmpdir+"/sxrs_putty.exe";
copyFromResource("SXRS_winexe.jar", "win_exe/putty.exe",
exename);
}
String tmpcmd= exename + " -ssh -t " + options +
(istage>0 ? " -P "+thisLoginPort+" localhost" : loginHosts[0]);
// note -P instead of -p for putty
command= tmpcmd.split(" ");
// putty doesn't understand -Y, use -X -- a convenience for users
for (int i=0; i<command.length; i++)
if (command[i].equals("-Y"))
command[i]= "-X";
}
// execute command
Runtime rt= Runtime.getRuntime();
Process p;
try {
p= rt.exec( command );
p.getOutputStream().close(); // this closes stdin of the process
// (note "getOutputStream" gives the process's standard *input*)
} catch (Exception e) {
StringBuffer cmdstring= new StringBuffer();
for (int i=0; i<command.length; i++)
cmdstring.append(command[i]+" ");
showMessageDialog
( appf,
"Execution of command failed! Command was\n"
+cmdstring + "\nException: "+e+ "\nOs: "+os+
(os.indexOf("Mac")>=0 ? "\n\n** Is X running? **" : ""),
"Error",
JOptionPane.ERROR_MESSAGE );
return;
}
// if successful, enable the "startViewer" button
if (istage < n_loginHosts-1)
ba_startTerminal[istage+1].setEnabled(true);
else {
for (int i=0; i<n_vncserverHostDisplays; i++)
if (ba_startViewer[i] != null)
ba_startViewer[i].setEnabled(true);
}
// temporary...
//System.out.println("Created terminal process "+p+ "\nOs: "+os);
try {
p.waitFor();
} catch(Exception e) {
System.err.println("When waiting for process, had exception "+e);
}
try {
InputStream is= p.getInputStream();
InputStream es= p.getErrorStream();
byte[] buffer= new byte[100];
while (is.available()>0 || es.available()>0) {
int n;
if (es.available()>0)
n= es.read(buffer);
else
n= is.read(buffer);
if (n <= 0)
break;
System.out.write(buffer,0,n);
}
} catch(Exception e) {
System.err.println("When reading from process, had exception "+e);
}
//System.out.println("terminal process ended with code "+p.exitValue());
// ... end temporary
int exitValue= p.exitValue();
if (exitValue != 0) {
StringBuffer cmdstring= new StringBuffer();
for (int i=0; i<command.length; i++)
cmdstring.append(command[i]+" ");
showMessageDialog
( appf,
"Process ended with code "+exitValue
+ "\nCommand was\n"
+cmdstring+ "\nOs: "+os+
(os.indexOf("Mac")>=0 ? "\n\n** Is X running? **" : ""),
"Error at terminal exit",
JOptionPane.ERROR_MESSAGE );
}
// save userId preference on success
if (exitValue==0 && myprefs != null)
try {
myprefs.put("userId", userId);
myprefs.put("sxrs.xtermCommandName", xtermCommandName);
}
catch (Exception e) {}
}
}
////////////////////////////////////////////////////////////////
// startViewer() method
void startViewer(int id) throws Exception
{
// build and execute vncviewer command
int localVncPort= localVncPortStart + id;
String vncviewerCommandName= (String)cb_vncviewerCommandName.getSelectedItem();
String exename= vncviewerCommandName;
if ( vncviewerCommandName.equals(MANUAL_VNCVIEWER_PLEASE) ) {
String message = "On your OS, "+os
+ ", it is recommended that you start your own vnc viewer.\n"
+ "Please start your vncviewer using localhost for the host and 9 for the display.";
if ( ckb_viewonly.isSelected() )
message = message + "\nNote: you will also have to select ViewOnly mode manually.";
showMessageDialog
( this,
message,
"Please start your VNCviewer now.",
JOptionPane.INFORMATION_MESSAGE );
return;
}
if ( vncviewerCommandName.equals(BUNDLED_VNCVIEWER_JAVA) ) {
// the following code is quite specific to TightVnc VncViewer class
VncViewer v = new VncViewer();
String [] args = { "HOST", "localhost", "PORT", ""+localVncPort,
"Share desktop", "Yes",
"Offer Relogin", "No",
"View only",
ckb_viewonly.isSelected() ? "Yes" : "No"};
v.mainArgs = args;
v.inAnApplet = false;
v.inSeparateFrame = true;
v.init();
v.inAnApplet = true;
v.start();
return;
}
if ( vncviewerCommandName.equals(BUNDLED_VNCVIEWER_WINEXE) ) {
exename= tmpdir+"/sxrs_vncviewer.exe";
copyFromResource("SXRS_winexe.jar", "win_exe/vncviewer.exe",
exename);
}
String command;
if (! ckb_viewonly.isSelected() )
command= exename+" -Shared localhost:"+localVncPort;
else
command= exename+" -Shared -ViewOnly localhost:"+localVncPort;
Runtime rt= Runtime.getRuntime();
Process p;
try {
p= rt.exec( command );
p.getOutputStream().close(); // this closes stdin of the process
// (note "getOutputStream" gives the process's standard *input*)
} catch (Exception e) {
showMessageDialog
( this,
"Execution of command failed! Command was\n"
+command + "\nException: "+e+ "\nOs: "+os,
"Error",
JOptionPane.ERROR_MESSAGE );
return;
}
if (myprefs != null)
try {
myprefs.put("sxrs.vncviewerCommandName", vncviewerCommandName);
}
catch (Exception e) {}
}
////////////////////////////////////////////////////////////////
// copyFromResource() method for copying a file from a resource
// (needed for copying external executable files)
Vector resourcesCopied= new Vector();
void copyFromResource(String resourceJar,
String resourceName,
String destFileName) throws Exception {
String resourceId= resourceJar+"?"+resourceName+"?"+destFileName;
if (resourcesCopied.contains(resourceId))
return;
InputStream xi;
if (resourceJar == null) {
xi= classLoader.getResourceAsStream( resourceName );
if (xi==null) {
throw new Exception("Could not find resource "+resourceName);
}
}
else {
String resourceURL=null;
try {
String base= classLoader.getResource("SXRS.class").toString();
int ijar= base.lastIndexOf(".jar");
int istart= base.lastIndexOf("/", ijar-1);
resourceURL= base.substring(0,istart+1)+resourceJar+"!/"+resourceName;
URL url= new URL(resourceURL);
URLConnection conn= url.openConnection();
xi= conn.getInputStream();
} catch (Exception e) {
// try falling back on normal class loader
xi= classLoader.getResourceAsStream( resourceName );
if (xi==null) {
throw new Exception("Could not find resource "+resourceName
+" in "+resourceJar
+"\n ["+resourceURL+"]\n"+e);
}
}
}
try {
FileOutputStream xo= new FileOutputStream( destFileName );
byte[]b= new byte[16384];
int ir=0;
while ( (ir=xi.read(b))>0 ) {
xo.write(b,0,ir);
}
xo.close();
resourcesCopied.add(resourceId);
} catch (Exception e) {
throw new Exception("Exception while attempting to copy resource "
+resourceName + " to file " +destFileName+":\n"
+e);
}
} // end copyFromResource
void clearResourcesCopied() {
for (int i=0; i < resourcesCopied.size(); i++) {
try {
String rc = resourcesCopied.get(i).toString();
int j = rc.lastIndexOf('?');