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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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-2019 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. It behaves as much as possible like the native
  23. // viewers.
  24. //
  25. // Unfortunately, because of the way Java classes are loaded on demand, only
  26. // configuration parameters defined in this file can be set from the command
  27. // line.
  28. package com.tigervnc.vncviewer;
  29. import java.awt.*;
  30. import java.awt.event.*;
  31. import java.awt.Color;
  32. import java.awt.Graphics;
  33. import java.awt.Image;
  34. import java.io.BufferedReader;
  35. import java.io.InputStream;
  36. import java.io.InputStreamReader;
  37. import java.io.IOException;
  38. import java.io.File;
  39. import java.lang.Character;
  40. import java.lang.reflect.*;
  41. import java.net.URL;
  42. import java.nio.CharBuffer;
  43. import java.util.*;
  44. import java.util.jar.Attributes;
  45. import java.util.jar.Manifest;
  46. import javax.swing.*;
  47. import javax.swing.border.*;
  48. import javax.swing.plaf.FontUIResource;
  49. import javax.swing.SwingUtilities;
  50. import javax.swing.UIManager.*;
  51. import com.tigervnc.rdr.*;
  52. import com.tigervnc.rfb.*;
  53. import com.tigervnc.network.*;
  54. import static com.tigervnc.vncviewer.Parameters.*;
  55. public class VncViewer implements Runnable {
  56. public static final String aboutText =
  57. new String("TigerVNC Java Viewer v%s (%s)%n"+
  58. "Built on %s at %s%n"+
  59. "Copyright (C) 1999-2020 TigerVNC Team and many others (see README.rst)%n"+
  60. "See https://www.tigervnc.org for information on TigerVNC.");
  61. public static String version = null;
  62. public static String build = null;
  63. public static String buildDate = null;
  64. public static String buildTime = null;
  65. static ImageIcon frameIconSrc =
  66. new ImageIcon(VncViewer.class.getResource("tigervnc.ico"));
  67. public static final Image frameIcon = frameIconSrc.getImage();
  68. public static final ImageIcon logoIcon =
  69. new ImageIcon(VncViewer.class.getResource("tigervnc.png"));
  70. public static final Image logoImage = logoIcon.getImage();
  71. public static final InputStream timestamp =
  72. VncViewer.class.getResourceAsStream("timestamp");
  73. public static final String os =
  74. System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
  75. private String defaultServerName;
  76. int VNCSERVERNAMELEN = 256;
  77. CharBuffer vncServerName = CharBuffer.allocate(VNCSERVERNAMELEN);
  78. public static void setLookAndFeel() {
  79. try {
  80. if (os.startsWith("mac os x")) {
  81. Class appClass = Class.forName("com.apple.eawt.Application");
  82. Method getApplication =
  83. appClass.getMethod("getApplication", (Class[])null);
  84. Object app = getApplication.invoke(appClass);
  85. Class paramTypes[] = new Class[1];
  86. paramTypes[0] = Image.class;
  87. Method setDockIconImage =
  88. appClass.getMethod("setDockIconImage", paramTypes);
  89. setDockIconImage.invoke(app, VncViewer.logoImage);
  90. }
  91. // Use Nimbus LookAndFeel if it's available, otherwise fallback
  92. // to the native laf, or Metal if no native laf is available.
  93. String laf = System.getProperty("swing.defaultlaf");
  94. if (laf == null) {
  95. LookAndFeelInfo[] installedLafs = UIManager.getInstalledLookAndFeels();
  96. for (int i = 0; i < installedLafs.length; i++) {
  97. if (installedLafs[i].getName().equals("Nimbus"))
  98. laf = installedLafs[i].getClassName();
  99. }
  100. if (laf == null)
  101. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  102. }
  103. UIManager.setLookAndFeel(laf);
  104. if (UIManager.getLookAndFeel().getName().equals("Metal")) {
  105. UIManager.put("swing.boldMetal", Boolean.FALSE);
  106. Enumeration<Object> keys = UIManager.getDefaults().keys();
  107. while (keys.hasMoreElements()) {
  108. Object key = keys.nextElement();
  109. Object value = UIManager.get(key);
  110. if (value instanceof FontUIResource) {
  111. String name = ((FontUIResource)value).getName();
  112. int style = ((FontUIResource)value).getStyle();
  113. int size = ((FontUIResource)value).getSize()-1;
  114. FontUIResource f = new FontUIResource(name, style, size);
  115. UIManager.put(key, f);
  116. }
  117. }
  118. } else if (UIManager.getLookAndFeel().getName().equals("Nimbus")) {
  119. Font f = UIManager.getFont("TitledBorder.font");
  120. String name = f.getName();
  121. int style = f.getStyle();
  122. int size = f.getSize()-2;
  123. FontUIResource r = new FontUIResource(name, style, size);
  124. UIManager.put("TitledBorder.font", r);
  125. }
  126. } catch (java.lang.Exception e) {
  127. vlog.info(e.toString());
  128. }
  129. }
  130. public static void main(String[] argv) {
  131. setLookAndFeel();
  132. VncViewer viewer = new VncViewer(argv);
  133. viewer.start();
  134. }
  135. public VncViewer(String[] argv) {
  136. SecurityClient.setDefaults();
  137. // Write about text to console, still using normal locale codeset
  138. getTimestamp();
  139. System.err.format("%n");
  140. System.err.format(aboutText, version, build, buildDate, buildTime);
  141. System.err.format("%n");
  142. Configuration.enableViewerParams();
  143. /* Load the default parameter settings */
  144. try {
  145. defaultServerName = loadViewerParameters(null);
  146. } catch (com.tigervnc.rfb.Exception e) {
  147. defaultServerName = "";
  148. vlog.info(e.getMessage());
  149. }
  150. // Override defaults with command-line options
  151. int i = 0;
  152. for (; i < argv.length; i++) {
  153. if (argv[i].length() == 0)
  154. continue;
  155. if (argv[i].equalsIgnoreCase("-config")) {
  156. if (++i >= argv.length)
  157. usage();
  158. defaultServerName = loadViewerParameters(argv[i]);
  159. continue;
  160. }
  161. if (argv[i].equalsIgnoreCase("-log")) {
  162. if (++i >= argv.length) usage();
  163. System.err.println("Log setting: "+argv[i]);
  164. LogWriter.setLogParams(argv[i]);
  165. continue;
  166. }
  167. if (argv[i].charAt(0) == '-') {
  168. if (i+1 < argv.length) {
  169. if (Configuration.setParam(argv[i].substring(1), argv[i+1])) {
  170. i++;
  171. continue;
  172. }
  173. }
  174. if (Configuration.setParam(argv[i]))
  175. continue;
  176. usage();
  177. }
  178. vncServerName.put(argv[i].toCharArray()).flip();
  179. }
  180. // Check if the server name in reality is a configuration file
  181. potentiallyLoadConfigurationFile(vncServerName);
  182. }
  183. public static void usage() {
  184. String usage = ("\nusage: vncviewer [options/parameters] "+
  185. "[host:displayNum]\n"+
  186. " vncviewer [options/parameters] -listen [port] "+
  187. "[options/parameters]\n"+
  188. " vncviewer [options/parameters] [.tigervnc file]\n"+
  189. "\n"+
  190. "Options:\n"+
  191. " -log <level> configure logging level\n"+
  192. "\n"+
  193. "Parameters can be turned on with -<param> or off with "+
  194. "-<param>=0\n"+
  195. "Parameters which take a value can be specified as "+
  196. "-<param> <value>\n"+
  197. "Other valid forms are <param>=<value> -<param>=<value> "+
  198. "--<param>=<value>\n"+
  199. "Parameter names are case-insensitive. The parameters "+
  200. "are:\n"+
  201. "\n");
  202. System.err.print(usage);
  203. Configuration.listParams(79, 14);
  204. String propertiesString = ("\n"+
  205. "System Properties (adapted from the TurboVNC vncviewer man page)\n"+
  206. " When started with the -via option, vncviewer reads the VNC_VIA_CMD\n"+
  207. " System property, expands patterns beginning with the \"%\" character,\n"+
  208. " and uses the resulting command line to establish the secure tunnel\n"+
  209. " to the VNC gateway. If VNC_VIA_CMD is not set, this command line\n"+
  210. " defaults to \"/usr/bin/ssh -f -L %L:%H:%R %G sleep 20\".\n"+
  211. "\n"+
  212. " The following patterns are recognized in the VNC_VIA_CMD property\n"+
  213. " (note that all of the patterns %G, %H, %L and %R must be present in \n"+
  214. " the command template):\n"+
  215. "\n"+
  216. " \t%% A literal \"%\";\n"+
  217. "\n"+
  218. " \t%G gateway machine name;\n"+
  219. "\n"+
  220. " \t%H remote VNC machine name, (as known to the gateway);\n"+
  221. "\n"+
  222. " \t%L local TCP port number;\n"+
  223. "\n"+
  224. " \t%R remote TCP port number.\n"+
  225. "\n"+
  226. " When started with the -tunnel option, vncviewer reads the VNC_TUNNEL_CMD\n"+
  227. " System property, expands patterns beginning with the \"%\" character, and\n"+
  228. " uses the resulting command line to establish the secure tunnel to the\n"+
  229. " VNC server. If VNC_TUNNEL_CMD is not set, this command line defaults\n"+
  230. " to \"/usr/bin/ssh -f -L %L:localhost:%R %H sleep 20\".\n"+
  231. "\n"+
  232. " The following patterns are recognized in the VNC_TUNNEL_CMD property\n"+
  233. " (note that all of the patterns %H, %L and %R must be present in \n"+
  234. " the command template):\n"+
  235. "\n"+
  236. " \t%% A literal \"%\";\n"+
  237. "\n"+
  238. " \t%H remote VNC machine name (as known to the client);\n"+
  239. "\n"+
  240. " \t%L local TCP port number;\n"+
  241. "\n"+
  242. " \t%R remote TCP port number.\n"+
  243. "\n");
  244. System.err.print(propertiesString);
  245. // Technically, we shouldn't use System.exit here but if there is a parameter
  246. // error then the problem is in the index/html file anyway.
  247. System.exit(1);
  248. }
  249. public static void potentiallyLoadConfigurationFile(CharBuffer vncServerName) {
  250. String serverName = vncServerName.toString();
  251. boolean hasPathSeparator = (serverName.indexOf('/') != -1 ||
  252. (serverName.indexOf('\\')) != -1);
  253. if (hasPathSeparator) {
  254. try {
  255. serverName = loadViewerParameters(vncServerName.toString());
  256. if (serverName == "") {
  257. vlog.info("Unable to load the server name from given file");
  258. System.exit(1);
  259. }
  260. vncServerName.clear();
  261. vncServerName.put(serverName).flip();
  262. } catch (com.tigervnc.rfb.Exception e) {
  263. vlog.info(e.getMessage());
  264. System.exit(1);
  265. }
  266. }
  267. }
  268. public static void newViewer() {
  269. String cmd = "java -jar ";
  270. try {
  271. URL url =
  272. VncViewer.class.getProtectionDomain().getCodeSource().getLocation();
  273. File f = new File(url.toURI());
  274. if (!f.exists() || !f.canRead()) {
  275. String msg = new String("The jar file "+f.getAbsolutePath()+
  276. " does not exist or cannot be read.");
  277. JOptionPane.showMessageDialog(null, msg, "ERROR",
  278. JOptionPane.ERROR_MESSAGE);
  279. return;
  280. }
  281. cmd = cmd.concat(f.getAbsolutePath());
  282. Thread t = new Thread(new ExtProcess(cmd, vlog));
  283. t.start();
  284. } catch (java.net.URISyntaxException e) {
  285. vlog.info(e.getMessage());
  286. } catch (java.lang.Exception e) {
  287. vlog.info(e.getMessage());
  288. }
  289. }
  290. private static void getTimestamp() {
  291. if (version == null || build == null) {
  292. try {
  293. Manifest manifest = new Manifest(timestamp);
  294. Attributes attributes = manifest.getMainAttributes();
  295. version = attributes.getValue("Version");
  296. build = attributes.getValue("Build");
  297. buildDate = attributes.getValue("Package-Date");
  298. buildTime = attributes.getValue("Package-Time");
  299. } catch (java.lang.Exception e) { }
  300. }
  301. }
  302. public static void about_vncviewer(Container parent) {
  303. String pkgDate = "";
  304. String pkgTime = "";
  305. try {
  306. Manifest manifest = new Manifest(VncViewer.timestamp);
  307. Attributes attributes = manifest.getMainAttributes();
  308. pkgDate = attributes.getValue("Package-Date");
  309. pkgTime = attributes.getValue("Package-Time");
  310. } catch (java.lang.Exception e) { }
  311. Window fullScreenWindow = DesktopWindow.getFullScreenWindow();
  312. if (fullScreenWindow != null)
  313. DesktopWindow.setFullScreenWindow(null);
  314. String msg =
  315. String.format(VncViewer.aboutText, VncViewer.version, VncViewer.build,
  316. VncViewer.buildDate, VncViewer.buildTime);
  317. Object[] options = {"Close \u21B5"};
  318. JOptionPane op =
  319. new JOptionPane(msg, JOptionPane.INFORMATION_MESSAGE,
  320. JOptionPane.DEFAULT_OPTION, VncViewer.logoIcon, options);
  321. JDialog dlg = op.createDialog(parent, "About TigerVNC Viewer for Java");
  322. dlg.setIconImage(VncViewer.frameIcon);
  323. dlg.setAlwaysOnTop(true);
  324. dlg.setVisible(true);
  325. if (fullScreenWindow != null)
  326. DesktopWindow.setFullScreenWindow(fullScreenWindow);
  327. }
  328. public void start() {
  329. (new Thread(this, "VncViewer Thread")).start();
  330. }
  331. public void exit(int n) {
  332. System.exit(n);
  333. }
  334. void reportException(java.lang.Exception e) {
  335. String title, msg = e.getMessage();
  336. int msgType = JOptionPane.ERROR_MESSAGE;
  337. title = "TigerVNC Viewer : Error";
  338. e.printStackTrace();
  339. JOptionPane.showMessageDialog(null, msg, title, msgType);
  340. }
  341. public void run() {
  342. cc = null;
  343. UserDialog dlg = new UserDialog();
  344. CSecurity.upg = dlg;
  345. CSecurityTLS.msg = dlg;
  346. Socket sock = null;
  347. /* Specifying -via and -listen together is nonsense */
  348. if (listenMode.getValue() && !via.getValueStr().isEmpty()) {
  349. vlog.error("Parameters -listen and -via are incompatible");
  350. String msg =
  351. new String("Parameters -listen and -via are incompatible");
  352. JOptionPane.showMessageDialog(null, msg, "ERROR",
  353. JOptionPane.ERROR_MESSAGE);
  354. exit(1);
  355. }
  356. if (listenMode.getValue()) {
  357. int port = 5500;
  358. if (vncServerName.charAt(0) != 0 &&
  359. Character.isDigit(vncServerName.charAt(0)))
  360. port = Integer.parseInt(vncServerName.toString());
  361. TcpListener listener = null;
  362. try {
  363. listener = new TcpListener(null, port);
  364. } catch (java.lang.Exception e) {
  365. reportException(e);
  366. exit(1);
  367. }
  368. vlog.info("Listening on port "+port);
  369. while (sock == null)
  370. sock = listener.accept();
  371. } else {
  372. if (vncServerName.charAt(0) == 0) {
  373. try {
  374. SwingUtilities.invokeAndWait(
  375. new ServerDialog(defaultServerName, vncServerName));
  376. } catch (InvocationTargetException e) {
  377. reportException(e);
  378. } catch (InterruptedException e) {
  379. reportException(e);
  380. }
  381. if (vncServerName.charAt(0) == 0)
  382. exit(0);
  383. }
  384. }
  385. try {
  386. cc = new CConn(vncServerName.toString(), sock);
  387. while (!cc.shuttingDown)
  388. cc.processMsg();
  389. exit(0);
  390. } catch (java.lang.Exception e) {
  391. if (cc == null || !cc.shuttingDown) {
  392. reportException(e);
  393. if (cc != null)
  394. cc.close();
  395. }
  396. exit(1);
  397. }
  398. }
  399. public static CConn cc;
  400. public static StringParameter config
  401. = new StringParameter("Config",
  402. "Specifies a configuration file to load.", null);
  403. static LogWriter vlog = new LogWriter("VncViewer");
  404. }