You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VncViewer.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
  3. * Copyright (C) 2011-2013 D. R. Commander. All Rights Reserved.
  4. * Copyright (C) 2011-2016 Brian P. Hinz
  5. *
  6. * This is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This software is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this software; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  19. * USA.
  20. */
  21. //
  22. // VncViewer - the VNC viewer applet. It can also be run from the
  23. // command-line, when it behaves as much as possibly like the windows and unix
  24. // viewers.
  25. //
  26. // Unfortunately, because of the way Java classes are loaded on demand, only
  27. // configuration parameters defined in this file can be set from the command
  28. // line or in applet parameters.
  29. package com.tigervnc.vncviewer;
  30. import java.awt.*;
  31. import java.awt.event.*;
  32. import java.awt.Color;
  33. import java.awt.Graphics;
  34. import java.awt.Image;
  35. import java.io.BufferedReader;
  36. import java.io.InputStream;
  37. import java.io.InputStreamReader;
  38. import java.io.IOException;
  39. import java.io.File;
  40. import java.lang.Character;
  41. import java.lang.reflect.*;
  42. import java.net.URL;
  43. import java.nio.CharBuffer;
  44. import java.util.*;
  45. import java.util.jar.Attributes;
  46. import java.util.jar.Manifest;
  47. import javax.swing.*;
  48. import javax.swing.border.*;
  49. import javax.swing.plaf.FontUIResource;
  50. import javax.swing.SwingUtilities;
  51. import javax.swing.UIManager.*;
  52. import com.tigervnc.rdr.*;
  53. import com.tigervnc.rfb.*;
  54. import com.tigervnc.network.*;
  55. import static com.tigervnc.vncviewer.Parameters.*;
  56. public class VncViewer extends javax.swing.JApplet
  57. implements Runnable, ActionListener {
  58. public static final String aboutText =
  59. new String("TigerVNC Java Viewer v%s (%s)%n"+
  60. "Built on %s at %s%n"+
  61. "Copyright (C) 1999-2018 TigerVNC Team and many others (see README.rst)%n"+
  62. "See http://www.tigervnc.org for information on TigerVNC.");
  63. public static String version = null;
  64. public static String build = null;
  65. public static String buildDate = null;
  66. public static String buildTime = null;
  67. static ImageIcon frameIconSrc =
  68. new ImageIcon(VncViewer.class.getResource("tigervnc.ico"));
  69. public static final Image frameIcon = frameIconSrc.getImage();
  70. public static final ImageIcon logoIcon =
  71. new ImageIcon(VncViewer.class.getResource("tigervnc.png"));
  72. public static final Image logoImage = logoIcon.getImage();
  73. public static final InputStream timestamp =
  74. VncViewer.class.getResourceAsStream("timestamp");
  75. public static final String os =
  76. System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
  77. private static VncViewer applet;
  78. private String defaultServerName;
  79. int VNCSERVERNAMELEN = 64;
  80. CharBuffer vncServerName = CharBuffer.allocate(VNCSERVERNAMELEN);
  81. public static void setLookAndFeel() {
  82. try {
  83. if (os.startsWith("mac os x")) {
  84. Class appClass = Class.forName("com.apple.eawt.Application");
  85. Method getApplication =
  86. appClass.getMethod("getApplication", (Class[])null);
  87. Object app = getApplication.invoke(appClass);
  88. Class paramTypes[] = new Class[1];
  89. paramTypes[0] = Image.class;
  90. Method setDockIconImage =
  91. appClass.getMethod("setDockIconImage", paramTypes);
  92. setDockIconImage.invoke(app, VncViewer.logoImage);
  93. }
  94. // Use Nimbus LookAndFeel if it's available, otherwise fallback
  95. // to the native laf, or Metal if no native laf is available.
  96. String laf = System.getProperty("swing.defaultlaf");
  97. if (laf == null) {
  98. LookAndFeelInfo[] installedLafs = UIManager.getInstalledLookAndFeels();
  99. for (int i = 0; i < installedLafs.length; i++) {
  100. if (installedLafs[i].getName().equals("Nimbus"))
  101. laf = installedLafs[i].getClassName();
  102. }
  103. if (laf == null)
  104. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  105. }
  106. UIManager.setLookAndFeel(laf);
  107. if (UIManager.getLookAndFeel().getName().equals("Metal")) {
  108. UIManager.put("swing.boldMetal", Boolean.FALSE);
  109. Enumeration<Object> keys = UIManager.getDefaults().keys();
  110. while (keys.hasMoreElements()) {
  111. Object key = keys.nextElement();
  112. Object value = UIManager.get(key);
  113. if (value instanceof FontUIResource) {
  114. String name = ((FontUIResource)value).getName();
  115. int style = ((FontUIResource)value).getStyle();
  116. int size = ((FontUIResource)value).getSize()-1;
  117. FontUIResource f = new FontUIResource(name, style, size);
  118. UIManager.put(key, f);
  119. }
  120. }
  121. } else if (UIManager.getLookAndFeel().getName().equals("Nimbus")) {
  122. Font f = UIManager.getFont("TitledBorder.font");
  123. String name = f.getName();
  124. int style = f.getStyle();
  125. int size = f.getSize()-2;
  126. FontUIResource r = new FontUIResource(name, style, size);
  127. UIManager.put("TitledBorder.font", r);
  128. }
  129. } catch (java.lang.Exception e) {
  130. vlog.info(e.toString());
  131. }
  132. }
  133. public static void main(String[] argv) {
  134. setLookAndFeel();
  135. VncViewer viewer = new VncViewer(argv);
  136. viewer.start();
  137. }
  138. public VncViewer() {
  139. // Only called in applet mode
  140. this(new String[0]);
  141. }
  142. public VncViewer(String[] argv) {
  143. SecurityClient.setDefaults();
  144. // Write about text to console, still using normal locale codeset
  145. getTimestamp();
  146. System.err.format("%n");
  147. System.err.format(aboutText, version, build, buildDate, buildTime);
  148. System.err.format("%n");
  149. Configuration.enableViewerParams();
  150. /* Load the default parameter settings */
  151. try {
  152. defaultServerName = loadViewerParameters(null);
  153. } catch (com.tigervnc.rfb.Exception e) {
  154. defaultServerName = "";
  155. vlog.info(e.getMessage());
  156. }
  157. // Override defaults with command-line options
  158. int i = 0;
  159. for (; i < argv.length; i++) {
  160. if (argv[i].length() == 0)
  161. continue;
  162. if (argv[i].equalsIgnoreCase("-config")) {
  163. if (++i >= argv.length)
  164. usage();
  165. defaultServerName = loadViewerParameters(argv[i]);
  166. continue;
  167. }
  168. if (argv[i].equalsIgnoreCase("-log")) {
  169. if (++i >= argv.length) usage();
  170. System.err.println("Log setting: "+argv[i]);
  171. LogWriter.setLogParams(argv[i]);
  172. continue;
  173. }
  174. if (argv[i].charAt(0) == '-') {
  175. if (i+1 < argv.length) {
  176. if (Configuration.setParam(argv[i].substring(1), argv[i+1])) {
  177. i++;
  178. continue;
  179. }
  180. }
  181. if (Configuration.setParam(argv[i]))
  182. continue;
  183. usage();
  184. }
  185. vncServerName.put(argv[i].toCharArray()).flip();
  186. }
  187. }
  188. public static void usage() {
  189. String usage = ("\nusage: vncviewer [options/parameters] "+
  190. "[host:displayNum]\n"+
  191. " vncviewer [options/parameters] -listen [port] "+
  192. "[options/parameters]\n"+
  193. "\n"+
  194. "Options:\n"+
  195. " -log <level> configure logging level\n"+
  196. "\n"+
  197. "Parameters can be turned on with -<param> or off with "+
  198. "-<param>=0\n"+
  199. "Parameters which take a value can be specified as "+
  200. "-<param> <value>\n"+
  201. "Other valid forms are <param>=<value> -<param>=<value> "+
  202. "--<param>=<value>\n"+
  203. "Parameter names are case-insensitive. The parameters "+
  204. "are:\n"+
  205. "\n");
  206. System.err.print(usage);
  207. Configuration.listParams(79, 14);
  208. String propertiesString = ("\n"+
  209. "System Properties (adapted from the TurboVNC vncviewer man page)\n"+
  210. " When started with the -via option, vncviewer reads the VNC_VIA_CMD\n"+
  211. " System property, expands patterns beginning with the \"%\" character,\n"+
  212. " and uses the resulting command line to establish the secure tunnel\n"+
  213. " to the VNC gateway. If VNC_VIA_CMD is not set, this command line\n"+
  214. " defaults to \"/usr/bin/ssh -f -L %L:%H:%R %G sleep 20\".\n"+
  215. "\n"+
  216. " The following patterns are recognized in the VNC_VIA_CMD property\n"+
  217. " (note that all of the patterns %G, %H, %L and %R must be present in \n"+
  218. " the command template):\n"+
  219. "\n"+
  220. " \t%% A literal \"%\";\n"+
  221. "\n"+
  222. " \t%G gateway machine name;\n"+
  223. "\n"+
  224. " \t%H remote VNC machine name, (as known to the gateway);\n"+
  225. "\n"+
  226. " \t%L local TCP port number;\n"+
  227. "\n"+
  228. " \t%R remote TCP port number.\n"+
  229. "\n"+
  230. " When started with the -tunnel option, vncviewer reads the VNC_TUNNEL_CMD\n"+
  231. " System property, expands patterns beginning with the \"%\" character, and\n"+
  232. " uses the resulting command line to establish the secure tunnel to the\n"+
  233. " VNC server. If VNC_TUNNEL_CMD is not set, this command line defaults\n"+
  234. " to \"/usr/bin/ssh -f -L %L:localhost:%R %H sleep 20\".\n"+
  235. "\n"+
  236. " The following patterns are recognized in the VNC_TUNNEL_CMD property\n"+
  237. " (note that all of the patterns %H, %L and %R must be present in \n"+
  238. " the command template):\n"+
  239. "\n"+
  240. " \t%% A literal \"%\";\n"+
  241. "\n"+
  242. " \t%H remote VNC machine name (as known to the client);\n"+
  243. "\n"+
  244. " \t%L local TCP port number;\n"+
  245. "\n"+
  246. " \t%R remote TCP port number.\n"+
  247. "\n");
  248. System.err.print(propertiesString);
  249. // Technically, we shouldn't use System.exit here but if there is a parameter
  250. // error then the problem is in the index/html file anyway.
  251. System.exit(1);
  252. }
  253. public static void newViewer() {
  254. String cmd = "java -jar ";
  255. try {
  256. URL url =
  257. VncViewer.class.getProtectionDomain().getCodeSource().getLocation();
  258. File f = new File(url.toURI());
  259. if (!f.exists() || !f.canRead()) {
  260. String msg = new String("The jar file "+f.getAbsolutePath()+
  261. " does not exist or cannot be read.");
  262. JOptionPane.showMessageDialog(null, msg, "ERROR",
  263. JOptionPane.ERROR_MESSAGE);
  264. return;
  265. }
  266. cmd = cmd.concat(f.getAbsolutePath());
  267. Thread t = new Thread(new ExtProcess(cmd, vlog));
  268. t.start();
  269. } catch (java.net.URISyntaxException e) {
  270. vlog.info(e.getMessage());
  271. } catch (java.lang.Exception e) {
  272. vlog.info(e.getMessage());
  273. }
  274. }
  275. public boolean isAppletDragStart(MouseEvent e) {
  276. if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
  277. // Drag undocking on Mac works, but introduces a host of
  278. // problems so disable it for now.
  279. if (os.startsWith("mac os x"))
  280. return false;
  281. else if (os.startsWith("windows"))
  282. return (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) != 0;
  283. else
  284. return (e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0;
  285. } else {
  286. return false;
  287. }
  288. }
  289. public void appletDragStarted() {
  290. embed.setParam(false);
  291. //cc.recreateViewport();
  292. JFrame f = (JFrame)JOptionPane.getFrameForComponent(this);
  293. // The default JFrame created by the drag event will be
  294. // visible briefly between appletDragStarted and Finished.
  295. if (f != null)
  296. f.setSize(0, 0);
  297. }
  298. public void appletDragFinished() {
  299. JFrame f = (JFrame)JOptionPane.getFrameForComponent(this);
  300. if (f != null)
  301. f.dispose();
  302. }
  303. public void setAppletCloseListener(ActionListener cl) {
  304. cc.setCloseListener(cl);
  305. }
  306. public void appletRestored() {
  307. cc.setCloseListener(null);
  308. }
  309. public static void setupEmbeddedFrame(JScrollPane sp) {
  310. InputMap im = sp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
  311. int ctrlAltShiftMask = Event.SHIFT_MASK | Event.CTRL_MASK | Event.ALT_MASK;
  312. if (im != null) {
  313. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, ctrlAltShiftMask),
  314. "unitScrollUp");
  315. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, ctrlAltShiftMask),
  316. "unitScrollDown");
  317. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, ctrlAltShiftMask),
  318. "unitScrollLeft");
  319. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ctrlAltShiftMask),
  320. "unitScrollRight");
  321. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, ctrlAltShiftMask),
  322. "scrollUp");
  323. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, ctrlAltShiftMask),
  324. "scrollDown");
  325. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, ctrlAltShiftMask),
  326. "scrollLeft");
  327. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_END, ctrlAltShiftMask),
  328. "scrollRight");
  329. }
  330. applet.getContentPane().removeAll();
  331. applet.getContentPane().add(sp);
  332. applet.validate();
  333. }
  334. public void init() {
  335. // Called right after zero-arg constructor in applet mode
  336. setLookAndFeel();
  337. setBackground(Color.white);
  338. applet = this;
  339. vncServerName.put(loadAppletParameters(applet).toCharArray()).flip();
  340. if (embed.getValue()) {
  341. fullScreen.setParam(false);
  342. remoteResize.setParam(false);
  343. maximize.setParam(false);
  344. scalingFactor.setParam("100");
  345. }
  346. setFocusTraversalKeysEnabled(false);
  347. addFocusListener(new FocusAdapter() {
  348. public void focusGained(FocusEvent e) {
  349. if (cc != null && cc.desktop != null)
  350. cc.desktop.viewport.requestFocusInWindow();
  351. }
  352. });
  353. Frame frame = (Frame)getFocusCycleRootAncestor();
  354. frame.setFocusTraversalKeysEnabled(false);
  355. frame.addWindowListener(new WindowAdapter() {
  356. // Transfer focus to scrollpane when browser receives it
  357. public void windowActivated(WindowEvent e) {
  358. if (cc != null && cc.desktop != null)
  359. cc.desktop.viewport.requestFocusInWindow();
  360. }
  361. public void windowDeactivated(WindowEvent e) {
  362. if (cc != null)
  363. cc.desktop.viewport.releaseDownKeys();
  364. }
  365. });
  366. }
  367. private static void getTimestamp() {
  368. if (version == null || build == null) {
  369. try {
  370. Manifest manifest = new Manifest(timestamp);
  371. Attributes attributes = manifest.getMainAttributes();
  372. version = attributes.getValue("Version");
  373. build = attributes.getValue("Build");
  374. buildDate = attributes.getValue("Package-Date");
  375. buildTime = attributes.getValue("Package-Time");
  376. } catch (java.lang.Exception e) { }
  377. }
  378. }
  379. public static void about_vncviewer(Container parent) {
  380. String pkgDate = "";
  381. String pkgTime = "";
  382. try {
  383. Manifest manifest = new Manifest(VncViewer.timestamp);
  384. Attributes attributes = manifest.getMainAttributes();
  385. pkgDate = attributes.getValue("Package-Date");
  386. pkgTime = attributes.getValue("Package-Time");
  387. } catch (java.lang.Exception e) { }
  388. Window fullScreenWindow = DesktopWindow.getFullScreenWindow();
  389. if (fullScreenWindow != null)
  390. DesktopWindow.setFullScreenWindow(null);
  391. String msg =
  392. String.format(VncViewer.aboutText, VncViewer.version, VncViewer.build,
  393. VncViewer.buildDate, VncViewer.buildTime);
  394. Object[] options = {"Close \u21B5"};
  395. JOptionPane op =
  396. new JOptionPane(msg, JOptionPane.INFORMATION_MESSAGE,
  397. JOptionPane.DEFAULT_OPTION, VncViewer.logoIcon, options);
  398. JDialog dlg = op.createDialog(parent, "About TigerVNC Viewer for Java");
  399. dlg.setIconImage(VncViewer.frameIcon);
  400. dlg.setAlwaysOnTop(true);
  401. dlg.setVisible(true);
  402. if (fullScreenWindow != null)
  403. DesktopWindow.setFullScreenWindow(fullScreenWindow);
  404. }
  405. public void start() {
  406. (new Thread(this, "VncViewer Thread")).start();
  407. }
  408. public void exit(int n) {
  409. if (embed.getValue())
  410. destroy();
  411. else
  412. System.exit(n);
  413. }
  414. // If "Reconnect" button is pressed
  415. public void actionPerformed(ActionEvent e) {
  416. getContentPane().removeAll();
  417. start();
  418. }
  419. void reportException(java.lang.Exception e) {
  420. String title, msg = e.getMessage();
  421. int msgType = JOptionPane.ERROR_MESSAGE;
  422. title = "TigerVNC Viewer : Error";
  423. e.printStackTrace();
  424. if (embed.getValue()) {
  425. getContentPane().removeAll();
  426. JLabel label = new JLabel("<html><center><b>" + title + "</b><p><i>" +
  427. msg + "</i></center></html>", JLabel.CENTER);
  428. label.setFont(new Font("Helvetica", Font.PLAIN, 24));
  429. label.setMaximumSize(new Dimension(getSize().width, 100));
  430. label.setVerticalAlignment(JLabel.CENTER);
  431. label.setAlignmentX(Component.CENTER_ALIGNMENT);
  432. JButton button = new JButton("Reconnect");
  433. button.addActionListener(this);
  434. button.setMaximumSize(new Dimension(200, 30));
  435. button.setAlignmentX(Component.CENTER_ALIGNMENT);
  436. setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
  437. add(label);
  438. add(button);
  439. validate();
  440. repaint();
  441. } else {
  442. JOptionPane.showMessageDialog(null, msg, title, msgType);
  443. }
  444. }
  445. public void run() {
  446. cc = null;
  447. UserDialog dlg = new UserDialog();
  448. CSecurity.upg = dlg;
  449. CSecurityTLS.msg = dlg;
  450. Socket sock = null;
  451. /* Specifying -via and -listen together is nonsense */
  452. if (listenMode.getValue() && !via.getValueStr().isEmpty()) {
  453. vlog.error("Parameters -listen and -via are incompatible");
  454. String msg =
  455. new String("Parameters -listen and -via are incompatible");
  456. JOptionPane.showMessageDialog(null, msg, "ERROR",
  457. JOptionPane.ERROR_MESSAGE);
  458. exit(1);
  459. }
  460. if (listenMode.getValue()) {
  461. int port = 5500;
  462. if (vncServerName.charAt(0) != 0 &&
  463. Character.isDigit(vncServerName.charAt(0)))
  464. port = Integer.parseInt(vncServerName.toString());
  465. TcpListener listener = null;
  466. try {
  467. listener = new TcpListener(null, port);
  468. } catch (java.lang.Exception e) {
  469. reportException(e);
  470. exit(1);
  471. }
  472. vlog.info("Listening on port "+port);
  473. while (sock == null)
  474. sock = listener.accept();
  475. } else {
  476. if (vncServerName.charAt(0) == 0) {
  477. try {
  478. SwingUtilities.invokeAndWait(
  479. new ServerDialog(defaultServerName, vncServerName));
  480. } catch (InvocationTargetException e) {
  481. reportException(e);
  482. } catch (InterruptedException e) {
  483. reportException(e);
  484. }
  485. if (vncServerName.charAt(0) == 0)
  486. exit(0);
  487. }
  488. }
  489. try {
  490. cc = new CConn(vncServerName.toString(), sock);
  491. while (!cc.shuttingDown)
  492. cc.processMsg();
  493. exit(0);
  494. } catch (java.lang.Exception e) {
  495. if (cc == null || !cc.shuttingDown) {
  496. reportException(e);
  497. if (cc != null)
  498. cc.close();
  499. } else if (embed.getValue()) {
  500. reportException(new java.lang.Exception("Connection closed"));
  501. exit(0);
  502. }
  503. exit(1);
  504. }
  505. }
  506. public static CConn cc;
  507. public static StringParameter config
  508. = new StringParameter("Config",
  509. "Specifies a configuration file to load.", null);
  510. static LogWriter vlog = new LogWriter("VncViewer");
  511. }