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

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