VncViewer.java 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. // Copyright (C) 2002-2005 Ultr@VNC Team. All Rights Reserved.
  2. // Copyright (C) 2004 Kenn Min Chong, John Witchel. All Rights Reserved.
  3. // Copyright (C) 2004 Alban Chazot. All Rights Reserved.
  4. // Copyright (C) 2001,2002 HorizonLive.com, Inc. All Rights Reserved.
  5. // Copyright (C) 2002 Constantin Kaplinsky. All Rights Reserved.
  6. // Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
  7. //
  8. // This is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation; either version 2 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This software is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this software; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  21. // USA.
  22. //
  23. //
  24. // VncViewer.java - the VNC viewer applet. This class mainly just sets up the
  25. // user interface, leaving it to the VncCanvas to do the actual rendering of
  26. // a VNC desktop.
  27. //
  28. // Alban Chazot - Carmi Grenoble July 5th 2004
  29. // * Add support for Ultr@VNC mslogon feature.
  30. // You can now be connected to a Ultr@VNC box with mslogon required.
  31. // Thanks to Wim Vandersmissen, who provide a TightVNC viewer patch do to so.
  32. // That give me the idea to provide it in the java viewer too.
  33. //
  34. // * Add ScrollPanel to applet mode
  35. //
  36. import java.awt.*;
  37. import java.awt.event.*;
  38. import java.io.*;
  39. import java.net.*;
  40. import javax.swing.*;
  41. public class VncViewer extends java.applet.Applet implements java.lang.Runnable, WindowListener {
  42. boolean inAnApplet = true;
  43. boolean inSeparateFrame = false;
  44. // mslogon support
  45. boolean mslogon = false;
  46. // mslogon support end
  47. //
  48. // main() is called when run as a java program from the command line.
  49. // It simply runs the applet inside a newly-created frame.
  50. //
  51. public static void main(String[] argv) {
  52. VncViewer v = new VncViewer();
  53. v.mainArgs = argv;
  54. v.inAnApplet = false;
  55. v.inSeparateFrame = true;
  56. v.init();
  57. v.start();
  58. }
  59. String[] mainArgs;
  60. RfbProto rfb;
  61. Thread rfbThread;
  62. Frame vncFrame;
  63. Container vncContainer;
  64. ScrollPane desktopScrollPane;
  65. GridBagLayout gridbag;
  66. ButtonPanel buttonPanel;
  67. //AuthPanel authenticator;
  68. VncCanvas vc;
  69. OptionsFrame options;
  70. ClipboardFrame clipboard;
  71. RecordingFrame rec;
  72. FTPFrame ftp; // KMC: FTP Frame declaration
  73. // Control session recording.
  74. Object recordingSync;
  75. String sessionFileName;
  76. boolean recordingActive;
  77. boolean recordingStatusChanged;
  78. String cursorUpdatesDef;
  79. String eightBitColorsDef;
  80. // Variables read from parameter values.
  81. String host;
  82. int port;
  83. String passwordParam;
  84. String encPasswordParam;
  85. boolean showControls;
  86. boolean showOfflineDesktop;
  87. int deferScreenUpdates;
  88. int deferCursorUpdates;
  89. int deferUpdateRequests;
  90. String serverID;
  91. // mslogon support 2
  92. String usernameParam;
  93. String encUsernameParam;
  94. String dm;
  95. byte[] domain = new byte[256];
  96. byte[] user = new byte[256];
  97. byte[] passwd = new byte[32];
  98. int i;
  99. // mslogon support 2 end
  100. //
  101. // init()
  102. //
  103. public void init() {
  104. readParameters();
  105. if (inSeparateFrame) {
  106. vncFrame = new Frame("Ultr@VNC");
  107. if (!inAnApplet) {
  108. vncFrame.add("Center", this);
  109. }
  110. vncContainer = vncFrame;
  111. } else {
  112. vncContainer = this;
  113. }
  114. recordingSync = new Object();
  115. options = new OptionsFrame(this);
  116. clipboard = new ClipboardFrame(this);
  117. // authenticator = new AuthPanel(false); // mslogon support : go to connectAndAuthenticate()
  118. if (RecordingFrame.checkSecurity())
  119. rec = new RecordingFrame(this);
  120. sessionFileName = null;
  121. recordingActive = false;
  122. recordingStatusChanged = false;
  123. cursorUpdatesDef = null;
  124. eightBitColorsDef = null;
  125. if (inSeparateFrame)
  126. vncFrame.addWindowListener(this);
  127. ftp = new FTPFrame(this); // KMC: FTPFrame creation
  128. rfbThread = new Thread(this);
  129. rfbThread.start();
  130. }
  131. public void update(Graphics g) {
  132. }
  133. //
  134. // run() - executed by the rfbThread to deal with the RFB socket.
  135. //
  136. public void run() {
  137. gridbag = new GridBagLayout();
  138. vncContainer.setLayout(gridbag);
  139. GridBagConstraints gbc = new GridBagConstraints();
  140. gbc.gridwidth = GridBagConstraints.REMAINDER;
  141. gbc.anchor = GridBagConstraints.NORTHWEST;
  142. // modif appshare pvandermaesen@noctis.be, never show control
  143. /*
  144. if (showControls) {
  145. buttonPanel = new ButtonPanel(this);
  146. gridbag.setConstraints(buttonPanel, gbc);
  147. vncContainer.add(buttonPanel);
  148. }
  149. */
  150. try {
  151. connectAndAuthenticate();
  152. doProtocolInitialisation();
  153. vc = new VncCanvas(this);
  154. gbc.weightx = 1.0;
  155. gbc.weighty = 1.0;
  156. // Add ScrollPanel to applet mode
  157. // Create a panel which itself is resizeable and can hold
  158. // non-resizeable VncCanvas component at the top left corner.
  159. Panel canvasPanel = new Panel();
  160. canvasPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
  161. canvasPanel.add(vc);
  162. // Create a ScrollPane which will hold a panel with VncCanvas
  163. // inside.
  164. desktopScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
  165. gbc.fill = GridBagConstraints.BOTH;
  166. gridbag.setConstraints(desktopScrollPane, gbc);
  167. desktopScrollPane.add(canvasPanel);
  168. if (inSeparateFrame) {
  169. // Finally, add our ScrollPane to the Frame window.
  170. vncFrame.add(desktopScrollPane);
  171. vncFrame.setTitle(rfb.desktopName);
  172. vncFrame.pack();
  173. vc.resizeDesktopFrame();
  174. } else {
  175. // Finally, add the scrollable panel component to the Applet.
  176. gridbag.setConstraints(desktopScrollPane, gbc);
  177. add(desktopScrollPane);
  178. // Add ScrollPanel to applet mode end
  179. validate();
  180. }
  181. moveFocusToDesktop();
  182. vc.processNormalProtocol();
  183. } catch (NoRouteToHostException e) {
  184. e.printStackTrace();
  185. fatalError("Network error: no route to server: " + host);
  186. } catch (UnknownHostException e) {
  187. e.printStackTrace();
  188. fatalError("Network error: server name unknown: " + host);
  189. } catch (ConnectException e) {
  190. e.printStackTrace();
  191. fatalError("Network error: could not connect to server: " + host + ":" + port);
  192. } catch (EOFException e) {
  193. e.printStackTrace();
  194. if (showOfflineDesktop) {
  195. System.out.println("Network error: remote side closed connection");
  196. if (vc != null) {
  197. vc.enableInput(false);
  198. }
  199. if (inSeparateFrame) {
  200. vncFrame.setTitle(rfb.desktopName + " [disconnected]");
  201. }
  202. if (rfb != null) {
  203. rfb.close();
  204. rfb = null;
  205. }
  206. if (showControls && buttonPanel != null) {
  207. buttonPanel.disableButtonsOnDisconnect();
  208. if (inSeparateFrame) {
  209. vncFrame.pack();
  210. } else {
  211. validate();
  212. }
  213. }
  214. } else {
  215. fatalError("Network error: remote side closed connection");
  216. }
  217. } catch (IOException e) {
  218. String str = e.getMessage();
  219. e.printStackTrace();
  220. if (str != null && str.length() != 0) {
  221. fatalError("Network Error: " + str);
  222. } else {
  223. fatalError(e.toString());
  224. }
  225. } catch (Exception e) {
  226. String str = e.getMessage();
  227. e.printStackTrace();
  228. if (str != null && str.length() != 0) {
  229. fatalError("Error: " + str);
  230. } else {
  231. fatalError(e.toString());
  232. }
  233. }
  234. }
  235. //
  236. // Connect to the RFB server and authenticate the user.
  237. //
  238. void connectAndAuthenticate() throws Exception {
  239. // If "ENCPASSWORD" parameter is set, decrypt the password into
  240. // the passwordParam string.
  241. if (encPasswordParam != null) {
  242. // ENCPASSWORD is hexascii-encoded. Decode.
  243. byte[] pw = { 0, 0, 0, 0, 0, 0, 0, 0 };
  244. int len = encPasswordParam.length() / 2;
  245. if (len > 8)
  246. len = 8;
  247. for (int i = 0; i < len; i++) {
  248. String hex = encPasswordParam.substring(i * 2, i * 2 + 2);
  249. Integer x = new Integer(Integer.parseInt(hex, 16));
  250. pw[i] = x.byteValue();
  251. }
  252. // Decrypt the password.
  253. byte[] key = { 23, 82, 107, 6, 35, 78, 88, 7 };
  254. DesCipher des = new DesCipher(key);
  255. des.decrypt(pw, 0, pw, 0);
  256. passwordParam = new String(pw);
  257. }
  258. // If a password parameter ("PASSWORD" or "ENCPASSWORD") is set,
  259. // don't ask user a password, get one from passwordParam instead.
  260. // Authentication failures would be fatal.
  261. if (passwordParam != null) {
  262. if (inSeparateFrame) {
  263. vncFrame.pack();
  264. vncFrame.show();
  265. } else {
  266. validate();
  267. }
  268. /* modif for autologin for dokeos appshare (noctis) */
  269. /* end modif */
  270. if (!tryAuthenticate(usernameParam, passwordParam)) {
  271. throw new Exception("VNC authentication failed");
  272. }
  273. return;
  274. }
  275. // There is no "PASSWORD" or "ENCPASSWORD" parameters -- ask user
  276. // for a password, try to authenticate, retry on authentication
  277. // failures.
  278. // mslogon support
  279. //
  280. // Detect Auth Protocol (Ultr@VNC or the standard One)
  281. // To know if we must show the username box
  282. //
  283. prologueDetectAuthProtocol();
  284. //authenticator = new AuthPanel(mslogon);
  285. // mslogon support end
  286. GridBagConstraints gbc = new GridBagConstraints();
  287. gbc.gridwidth = GridBagConstraints.REMAINDER;
  288. gbc.anchor = GridBagConstraints.NORTHWEST;
  289. gbc.weightx = 1.0;
  290. gbc.weighty = 1.0;
  291. gbc.ipadx = 100;
  292. gbc.ipady = 50;
  293. //gridbag.setConstraints(authenticator, gbc);
  294. //vncContainer.add(authenticator);
  295. if (inSeparateFrame) {
  296. vncFrame.pack();
  297. vncFrame.show();
  298. } else {
  299. validate();
  300. // FIXME: here moveFocusToPasswordField() does not always work
  301. // under Netscape 4.7x/Java 1.1.5/Linux. It seems like this call
  302. // is being executed before the password field of the
  303. // authenticator is fully drawn and activated, therefore
  304. // requestFocus() does not work. Currently, I don't know how to
  305. // solve this problem.
  306. // -- const
  307. // mslogon support
  308. //authenticator.moveFocusToUsernameField();
  309. // mslogon support end
  310. }
  311. while (true) {
  312. // Wait for user entering a password, or a username and a password
  313. // Try to authenticate with a given password.
  314. // mslogon support
  315. String us="";
  316. if (mslogon) {
  317. //us = authenticator.username.getText();
  318. } else {
  319. us = "";
  320. }
  321. /* add for dokeos appShare (noctis) */
  322. if (tryAuthenticate(us, "dokeos"))
  323. break;
  324. }
  325. }
  326. // mslogon support
  327. //
  328. // Detect Server rfb Protocol to know the auth Method
  329. // Perform a connexion to detect the Serverminor
  330. //
  331. void prologueDetectAuthProtocol() throws Exception {
  332. rfb = new RfbProto(host, port, this, serverID);
  333. rfb.readVersionMsg();
  334. System.out.println("RFB server supports protocol version " + rfb.serverMajor + "." + rfb.serverMinor);
  335. // Mslogon support
  336. if (rfb.serverMinor == 4) {
  337. mslogon = true;
  338. System.out.println("Ultr@VNC mslogon detected");
  339. }
  340. rfb.writeVersionMsg();
  341. }
  342. // mslogon support end
  343. //
  344. // Try to authenticate with a given password.
  345. //
  346. boolean tryAuthenticate(String us, String pw) throws Exception {
  347. rfb = new RfbProto(host, port, this, serverID);
  348. rfb.readVersionMsg();
  349. System.out.println("RFB server supports protocol version " + rfb.serverMajor + "." + rfb.serverMinor);
  350. rfb.writeVersionMsg();
  351. int authScheme = rfb.readAuthScheme();
  352. switch (authScheme) {
  353. case RfbProto.NoAuth:
  354. System.out.println("No authentication needed");
  355. return true;
  356. case RfbProto.VncAuth:
  357. if (mslogon) {
  358. System.out.println("showing JOptionPane warning.");
  359. int n = JOptionPane.showConfirmDialog(vncFrame, "The current authentication method does not transfer your password securely." + "Do you want to continue?", "Warning",
  360. JOptionPane.YES_NO_OPTION);
  361. if (n != JOptionPane.YES_OPTION) {
  362. throw new Exception("User cancelled insecure MS-Logon");
  363. }
  364. }
  365. // mslogon support
  366. byte[] challengems = new byte[64];
  367. if (mslogon) {
  368. // copy the us (user) parameter into the user Byte formated variable
  369. System.arraycopy(us.getBytes(), 0, user, 0, us.length());
  370. // and pad it with Null
  371. if (us.length() < 256) {
  372. for (i = us.length(); i < 256; i++) {
  373. user[i] = 0;
  374. }
  375. }
  376. dm = ".";
  377. // copy the dm (domain) parameter into the domain Byte formated variable
  378. System.arraycopy(dm.getBytes(), 0, domain, 0, dm.length());
  379. // and pad it with Null
  380. if (dm.length() < 256) {
  381. for (i = dm.length(); i < 256; i++) {
  382. domain[i] = 0;
  383. }
  384. }
  385. // equivalent of vncEncryptPasswdMS
  386. // copy the pw (password) parameter into the password Byte formated variable
  387. System.arraycopy(pw.getBytes(), 0, passwd, 0, pw.length());
  388. // and pad it with Null
  389. if (pw.length() < 32) {
  390. for (i = pw.length(); i < 32; i++) {
  391. passwd[i] = 0;
  392. }
  393. }
  394. // Encrypt the full given password
  395. byte[] fixedkey = { 23, 82, 107, 6, 35, 78, 88, 7 };
  396. DesCipher desme = new DesCipher(fixedkey);
  397. desme.encrypt(passwd, 0, passwd, 0);
  398. // end equivalent of vncEncryptPasswdMS
  399. // get the mslogon Challenge from server
  400. rfb.is.readFully(challengems);
  401. }
  402. // mslogon support end
  403. byte[] challenge = new byte[16];
  404. rfb.is.readFully(challenge);
  405. if (pw.length() > 8)
  406. pw = pw.substring(0, 8); // Truncate to 8 chars
  407. // vncEncryptBytes in the UNIX libvncauth truncates password
  408. // after the first zero byte. We do to.
  409. int firstZero = pw.indexOf(0);
  410. if (firstZero != -1)
  411. pw = pw.substring(0, firstZero);
  412. // mslogon support
  413. if (mslogon) {
  414. for (i = 0; i < 32; i++) {
  415. challengems[i] = (byte) (passwd[i] ^ challengems[i]);
  416. }
  417. rfb.os.write(user);
  418. rfb.os.write(domain);
  419. rfb.os.write(challengems);
  420. }
  421. // mslogon support end
  422. byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 };
  423. System.arraycopy(pw.getBytes(), 0, key, 0, pw.length());
  424. DesCipher des = new DesCipher(key);
  425. des.encrypt(challenge, 0, challenge, 0);
  426. des.encrypt(challenge, 8, challenge, 8);
  427. rfb.os.write(challenge);
  428. int authResult = rfb.is.readInt();
  429. switch (authResult) {
  430. case RfbProto.VncAuthOK:
  431. System.out.println("VNC authentication succeeded");
  432. return true;
  433. case RfbProto.VncAuthFailed:
  434. System.out.println("VNC authentication failed");
  435. break;
  436. case RfbProto.VncAuthTooMany:
  437. throw new Exception("VNC authentication failed - too many tries");
  438. default:
  439. throw new Exception("Unknown VNC authentication result " + authResult);
  440. }
  441. break;
  442. case RfbProto.MsLogon:
  443. System.out.println("MS-Logon (DH) detected");
  444. if (AuthMsLogon(us, pw)) {
  445. return true;
  446. }
  447. break;
  448. default:
  449. throw new Exception("Unknown VNC authentication scheme " + authScheme);
  450. }
  451. return false;
  452. }
  453. // marscha@2006: Try to better hide the windows password.
  454. // I know that this is no breakthrough in modern cryptography.
  455. // It's just a patch/kludge/workaround.
  456. boolean AuthMsLogon(String us, String pw) throws Exception {
  457. byte user[] = new byte[256];
  458. byte passwd[] = new byte[64];
  459. long gen = rfb.is.readLong();
  460. long mod = rfb.is.readLong();
  461. long resp = rfb.is.readLong();
  462. DH dh = new DH(gen, mod);
  463. long pub = dh.createInterKey();
  464. rfb.os.write(DH.longToBytes(pub));
  465. long key = dh.createEncryptionKey(resp);
  466. System.out.println("gen=" + gen + ", mod=" + mod + ", pub=" + pub + ", key=" + key);
  467. DesCipher des = new DesCipher(DH.longToBytes(key));
  468. System.arraycopy(us.getBytes(), 0, user, 0, us.length());
  469. // and pad it with Null
  470. if (us.length() < 256) {
  471. for (i = us.length(); i < 256; i++) {
  472. user[i] = 0;
  473. }
  474. }
  475. // copy the pw (password) parameter into the password Byte formated variable
  476. System.arraycopy(pw.getBytes(), 0, passwd, 0, pw.length());
  477. // and pad it with Null
  478. if (pw.length() < 32) {
  479. for (i = pw.length(); i < 32; i++) {
  480. passwd[i] = 0;
  481. }
  482. }
  483. // user = domain + "\\" + user;
  484. des.encryptText(user, user, DH.longToBytes(key));
  485. des.encryptText(passwd, passwd, DH.longToBytes(key));
  486. rfb.os.write(user);
  487. rfb.os.write(passwd);
  488. int authResult = rfb.is.readInt();
  489. switch (authResult) {
  490. case RfbProto.VncAuthOK:
  491. System.out.println("MS-Logon (DH) authentication succeeded");
  492. return true;
  493. case RfbProto.VncAuthFailed:
  494. System.out.println("MS-Logon (DH) authentication failed");
  495. break;
  496. case RfbProto.VncAuthTooMany:
  497. throw new Exception("MS-Logon (DH) authentication failed - too many tries");
  498. default:
  499. throw new Exception("Unknown MS-Logon (DH) authentication result " + authResult);
  500. }
  501. return false;
  502. }
  503. //
  504. // Do the rest of the protocol initialisation.
  505. //
  506. void doProtocolInitialisation() throws IOException {
  507. rfb.writeClientInit();
  508. rfb.readServerInit();
  509. System.out.println("Desktop name is " + rfb.desktopName);
  510. System.out.println("Desktop size is " + rfb.framebufferWidth + " x " + rfb.framebufferHeight);
  511. setEncodings();
  512. }
  513. //
  514. // Send current encoding list to the RFB server.
  515. //
  516. void setEncodings() {
  517. try {
  518. if (rfb != null && rfb.inNormalProtocol) {
  519. rfb.writeSetEncodings(options.encodings, options.nEncodings);
  520. if (vc != null) {
  521. vc.softCursorFree();
  522. }
  523. }
  524. } catch (Exception e) {
  525. e.printStackTrace();
  526. }
  527. }
  528. //
  529. // setCutText() - send the given cut text to the RFB server.
  530. //
  531. void setCutText(String text) {
  532. try {
  533. if (rfb != null && rfb.inNormalProtocol) {
  534. rfb.writeClientCutText(text);
  535. }
  536. } catch (Exception e) {
  537. e.printStackTrace();
  538. }
  539. }
  540. //
  541. // Order change in session recording status. To stop recording, pass
  542. // null in place of the fname argument.
  543. //
  544. void setRecordingStatus(String fname) {
  545. synchronized (recordingSync) {
  546. sessionFileName = fname;
  547. recordingStatusChanged = true;
  548. }
  549. }
  550. //
  551. // Start or stop session recording. Returns true if this method call
  552. // causes recording of a new session.
  553. //
  554. boolean checkRecordingStatus() throws IOException {
  555. synchronized (recordingSync) {
  556. if (recordingStatusChanged) {
  557. recordingStatusChanged = false;
  558. if (sessionFileName != null) {
  559. startRecording();
  560. return true;
  561. } else {
  562. stopRecording();
  563. }
  564. }
  565. }
  566. return false;
  567. }
  568. //
  569. // Start session recording.
  570. //
  571. protected void startRecording() throws IOException {
  572. synchronized (recordingSync) {
  573. if (!recordingActive) {
  574. // Save settings to restore them after recording the session.
  575. cursorUpdatesDef = options.choices[options.cursorUpdatesIndex].getSelectedItem();
  576. eightBitColorsDef = options.choices[options.eightBitColorsIndex].getSelectedItem();
  577. // Set options to values suitable for recording.
  578. options.choices[options.cursorUpdatesIndex].select("Disable");
  579. options.choices[options.cursorUpdatesIndex].setEnabled(false);
  580. options.setEncodings();
  581. options.choices[options.eightBitColorsIndex].select("Full");
  582. options.choices[options.eightBitColorsIndex].setEnabled(false);
  583. options.setColorFormat();
  584. } else {
  585. rfb.closeSession();
  586. }
  587. System.out.println("Recording the session in " + sessionFileName);
  588. rfb.startSession(sessionFileName);
  589. recordingActive = true;
  590. }
  591. }
  592. //
  593. // Stop session recording.
  594. //
  595. protected void stopRecording() throws IOException {
  596. synchronized (recordingSync) {
  597. if (recordingActive) {
  598. // Restore options.
  599. options.choices[options.cursorUpdatesIndex].select(cursorUpdatesDef);
  600. options.choices[options.cursorUpdatesIndex].setEnabled(true);
  601. options.setEncodings();
  602. options.choices[options.eightBitColorsIndex].select(eightBitColorsDef);
  603. options.choices[options.eightBitColorsIndex].setEnabled(true);
  604. options.setColorFormat();
  605. rfb.closeSession();
  606. System.out.println("Session recording stopped.");
  607. }
  608. sessionFileName = null;
  609. recordingActive = false;
  610. }
  611. }
  612. //
  613. // readParameters() - read parameters from the html source or from the
  614. // command line. On the command line, the arguments are just a sequence of
  615. // param_name/param_value pairs where the names and values correspond to
  616. // those expected in the html applet tag source.
  617. //
  618. public void readParameters() {
  619. host = readParameter("HOST", !inAnApplet);
  620. if (host == null) {
  621. host = getCodeBase().getHost();
  622. if (host.equals("")) {
  623. fatalError("HOST parameter not specified");
  624. }
  625. }
  626. serverID = readParameter("SERVERID", true);
  627. String str = readParameter("PORT", true);
  628. port = Integer.parseInt(str);
  629. if (inAnApplet) {
  630. str = readParameter("Open New Window", false);
  631. if (str != null && str.equalsIgnoreCase("Yes"))
  632. inSeparateFrame = true;
  633. }
  634. encPasswordParam = readParameter("ENCPASSWORD", false);
  635. if (encPasswordParam == null)
  636. passwordParam = readParameter("PASSWORD", false);
  637. // "Show Controls" set to "No" disables button panel.
  638. showControls = true;
  639. str = readParameter("Show Controls", false);
  640. if (str != null && str.equalsIgnoreCase("No"))
  641. showControls = false;
  642. // Do we continue showing desktop on remote disconnect?
  643. showOfflineDesktop = false;
  644. str = readParameter("Show Offline Desktop", false);
  645. if (str != null && str.equalsIgnoreCase("Yes"))
  646. showOfflineDesktop = true;
  647. // Fine tuning options.
  648. deferScreenUpdates = readIntParameter("Defer screen updates", 20);
  649. deferCursorUpdates = readIntParameter("Defer cursor updates", 10);
  650. deferUpdateRequests = readIntParameter("Defer update requests", 50);
  651. }
  652. public String readParameter(String name, boolean required) {
  653. if (inAnApplet) {
  654. String s = getParameter(name);
  655. if ((s == null) && required) {
  656. fatalError(name + " parameter not specified");
  657. }
  658. return s;
  659. }
  660. for (int i = 0; i < mainArgs.length; i += 2) {
  661. if (mainArgs[i].equalsIgnoreCase(name)) {
  662. try {
  663. return mainArgs[i + 1];
  664. } catch (Exception e) {
  665. if (required) {
  666. fatalError(name + " parameter not specified");
  667. }
  668. return null;
  669. }
  670. }
  671. }
  672. if (required) {
  673. fatalError(name + " parameter not specified");
  674. }
  675. return null;
  676. }
  677. int readIntParameter(String name, int defaultValue) {
  678. String str = readParameter(name, false);
  679. int result = defaultValue;
  680. if (str != null) {
  681. try {
  682. result = Integer.parseInt(str);
  683. } catch (NumberFormatException e) {
  684. }
  685. }
  686. return result;
  687. }
  688. //
  689. // moveFocusToDesktop() - move keyboard focus either to the
  690. // VncCanvas or to the AuthPanel.
  691. //
  692. void moveFocusToDesktop() {
  693. if (vncContainer != null) {
  694. if (vc != null && vncContainer.isAncestorOf(vc)) {
  695. vc.requestFocus();
  696. }
  697. }
  698. }
  699. //
  700. // disconnect() - close connection to server.
  701. //
  702. boolean disconnectRequested = false;
  703. synchronized public void disconnect() {
  704. disconnectRequested = true;
  705. if (rfb != null) {
  706. rfb.close();
  707. rfb = null;
  708. }
  709. System.out.println("Disconnect");
  710. options.dispose();
  711. clipboard.dispose();
  712. if (rec != null)
  713. rec.dispose();
  714. if (inAnApplet) {
  715. vncContainer.removeAll();
  716. Label errLabel = new Label("Disconnected");
  717. errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12));
  718. vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
  719. vncContainer.add(errLabel);
  720. if (inSeparateFrame) {
  721. vncFrame.pack();
  722. } else {
  723. validate();
  724. }
  725. rfbThread.stop();
  726. } else {
  727. System.exit(0);
  728. }
  729. }
  730. //
  731. // fatalError() - print out a fatal error message.
  732. //
  733. synchronized public void fatalError(String str) {
  734. if (rfb != null) {
  735. rfb.close();
  736. rfb = null;
  737. }
  738. System.out.println(str);
  739. if (disconnectRequested) {
  740. // Not necessary to show error message if the error was caused
  741. // by I/O problems after the disconnect() method call.
  742. disconnectRequested = false;
  743. return;
  744. }
  745. if (inAnApplet) {
  746. vncContainer.removeAll();
  747. Label errLabel = new Label(str);
  748. errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12));
  749. vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
  750. vncContainer.add(errLabel);
  751. if (inSeparateFrame) {
  752. vncFrame.pack();
  753. } else {
  754. validate();
  755. }
  756. Thread.currentThread().stop();
  757. } else {
  758. System.exit(1);
  759. }
  760. }
  761. //
  762. // This method is called before the applet is destroyed.
  763. //
  764. public void destroy() {
  765. vncContainer.removeAll();
  766. options.dispose();
  767. clipboard.dispose();
  768. if (ftp != null)
  769. ftp.dispose();
  770. if (rec != null)
  771. rec.dispose();
  772. if (rfb != null)
  773. rfb.close();
  774. if (inSeparateFrame)
  775. vncFrame.dispose();
  776. }
  777. //
  778. // Close application properly on window close event.
  779. //
  780. public void windowClosing(WindowEvent evt) {
  781. if (rfb != null)
  782. disconnect();
  783. vncFrame.dispose();
  784. if (!inAnApplet) {
  785. System.exit(0);
  786. }
  787. }
  788. //
  789. // Move the keyboard focus to the password field on window activation.
  790. //
  791. public void windowActivated(WindowEvent evt) {
  792. }
  793. //
  794. // Ignore window events we're not interested in.
  795. //
  796. public void windowDeactivated(WindowEvent evt) {
  797. }
  798. public void windowOpened(WindowEvent evt) {
  799. }
  800. public void windowClosed(WindowEvent evt) {
  801. }
  802. public void windowIconified(WindowEvent evt) {
  803. }
  804. public void windowDeiconified(WindowEvent evt) {
  805. }
  806. }