aboutsummaryrefslogtreecommitdiffstats
path: root/java/com/tigervnc/vncviewer/Parameters.java
blob: 88d68e14b9eeadec3c7c7866e190822d04fba831 (plain)
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
/* Copyright (C) 2016-2019 Brian P. Hinz
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA.
 */

package com.tigervnc.vncviewer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import com.tigervnc.rfb.*;
import com.tigervnc.rfb.Exception;

public class Parameters {

  public static BoolParameter noLionFS
  = new BoolParameter("NoLionFS",
    "On Mac systems, setting this parameter will force the use of the old "+
    "(pre-Lion) full-screen mode, even if the viewer is running on OS X 10.7 "+
    "Lion or later.",
    false);

  public static BoolParameter dotWhenNoCursor
  = new BoolParameter("DotWhenNoCursor",
    "Show the dot cursor when the server sends an invisible cursor",
    false);

  public static BoolParameter sendLocalUsername
  = new BoolParameter("SendLocalUsername",
    "Send the local username for SecurityTypes "+
    "such as Plain rather than prompting",
    true);

  public static StringParameter passwordFile
  = new StringParameter("PasswordFile",
    "Password file for VNC authentication",
    "");

  public static AliasParameter passwd
  = new AliasParameter("passwd",
    "Alias for PasswordFile",
    passwordFile);

  public static BoolParameter autoSelect
  = new BoolParameter("AutoSelect",
    "Auto select pixel format and encoding",
    true);

  public static BoolParameter fullColor
  = new BoolParameter("FullColor",
    "Use full color - otherwise 6-bit colour is used "+
    "until AutoSelect decides the link is fast enough",
    true);

  public static AliasParameter fullColorAlias
  = new AliasParameter("FullColour",
    "Alias for FullColor",
    Parameters.fullColor);

  public static IntParameter lowColorLevel
  = new IntParameter("LowColorLevel",
    "Color level to use on slow connections. "+
    "0 = Very Low, 1 = Low, 2 = Medium",
    2);

  public static AliasParameter lowColorLevelAlias
  = new AliasParameter("LowColourLevel",
    "Alias for LowColorLevel",
    lowColorLevel);

  public static StringParameter preferredEncoding
  = new StringParameter("PreferredEncoding",
    "Preferred encoding to use (Tight, ZRLE, "+
    "hextile or raw) - implies AutoSelect=0",
    "Tight");

  public static BoolParameter remoteResize
  = new BoolParameter("RemoteResize",
    "Dynamically resize the remote desktop size as "+
    "the size of the local client window changes. "+
    "(Does not work with all servers)",
    true);

  public static BoolParameter viewOnly
  = new BoolParameter("ViewOnly",
    "Don't send any mouse or keyboard events to the server",
    false);

  public static BoolParameter shared
  = new BoolParameter("Shared",
    "Don't disconnect other viewers upon "+
    "connection - share the desktop instead",
    false);

  public static BoolParameter maximize
  = new BoolParameter("Maximize",
    "Maximize viewer window",
    false);

  public static BoolParameter fullScreen
  = new BoolParameter("FullScreen",
    "Full Screen Mode",
    false);

  public static BoolParameter fullScreenAllMonitors
  = new BoolParameter("FullScreenAllMonitors",
    "Enable full screen over all monitors",
    true);

  public static BoolParameter acceptClipboard
  = new BoolParameter("AcceptClipboard",
    "Accept clipboard changes from the server",
    true);

  public static BoolParameter sendClipboard
  = new BoolParameter("SendClipboard",
    "Send clipboard changes to the server",
    true);

  public static IntParameter maxCutText
  = new IntParameter("MaxCutText",
    "Maximum permitted length of an outgoing clipboard update",
    262144);

  public static StringParameter menuKey
  = new StringParameter("MenuKey",
    "The key which brings up the popup menu",
    "F8");

  public static StringParameter desktopSize
  = new StringParameter("DesktopSize",
    "Reconfigure desktop size on the server on connect (if possible)",
    "");

  public static BoolParameter listenMode
  = new BoolParameter("listen",
    "Listen for connections from VNC servers",
    false);

  public static StringParameter scalingFactor
  = new StringParameter("ScalingFactor",
    "Reduce or enlarge the remote desktop image. "+
    "The value is interpreted as a scaling factor "+
    "in percent. If the parameter is set to "+
    "\"Auto\", then automatic scaling is "+
    "performed. Auto-scaling tries to choose a "+
    "scaling factor in such a way that the whole "+
    "remote desktop will fit on the local screen. "+
    "If the parameter is set to \"FixedRatio\", "+
    "then automatic scaling is performed, but the "+
    "original aspect ratio is preserved.",
    "100");

  public static BoolParameter alwaysShowServerDialog
  = new BoolParameter("AlwaysShowServerDialog",
    "Always show the server dialog even if a server has been "+
    "specified in an applet parameter or on the command line",
    false);

  public static BoolParameter acceptBell
  = new BoolParameter("AcceptBell",
    "Produce a system beep when requested to by the server.",
    true);

  public static StringParameter via
  = new StringParameter("Via",
    "Automatically create an encrypted TCP tunnel to "+
    "the gateway machine, then connect to the VNC host "+
    "through that tunnel. By default, this option invokes "+
    "SSH local port forwarding using the embedded JSch "+
    "client, however an external SSH client may be specified "+
    "using the \"-extSSH\" parameter. Note that when using "+
    "the -via option, the VNC host machine name should be "+
    "specified from the point of view of the gateway machine, "+
    "e.g. \"localhost\" denotes the gateway, "+
    "not the machine on which the viewer was launched. "+
    "See the System Properties section below for "+
    "information on configuring the -Via option.", "");

  public static BoolParameter tunnel
  = new BoolParameter("Tunnel",
    "The -Tunnel command is basically a shorthand for the "+
    "-via command when the VNC server and SSH gateway are "+
    "one and the same. -Tunnel creates an SSH connection "+
    "to the server and forwards the VNC through the tunnel "+
    "without the need to specify anything else.", false);

  public static BoolParameter extSSH
  = new BoolParameter("extSSH",
    "By default, SSH tunneling uses the embedded JSch client "+
    "for tunnel creation. This option causes the client to "+
    "invoke an external SSH client application for all tunneling "+
    "operations. By default, \"/usr/bin/ssh\" is used, however "+
    "the path to the external application may be specified using "+
    "the -SSHClient option.", false);

  public static StringParameter extSSHClient
  = new StringParameter("extSSHClient",
    "Specifies the path to an external SSH client application "+
    "that is to be used for tunneling operations when the -extSSH "+
    "option is in effect.", "/usr/bin/ssh");

  public static StringParameter extSSHArgs
  = new StringParameter("extSSHArgs",
    "Specifies the arguments string or command template to be used "+
    "by the external SSH client application when the -extSSH option "+
    "is in effect. The string will be processed according to the same "+
    "pattern substitution rules as the VNC_TUNNEL_CMD and VNC_VIA_CMD "+
    "system properties, and can be used to override those in a more "+
    "command-line friendly way. If not specified, then the appropriate "+
    "VNC_TUNNEL_CMD or VNC_VIA_CMD command template will be used.", "");

  public static StringParameter sshConfig
  = new StringParameter("SSHConfig",
    "Specifies the path to an OpenSSH configuration file that to "+
    "be parsed by the embedded JSch SSH client during tunneling "+
    "operations.", FileUtils.getHomeDir()+".ssh/config");

  public static StringParameter sshKey
  = new StringParameter("SSHKey",
    "When using the Via or Tunnel options with the embedded SSH client, "+
    "this parameter specifies the text of the SSH private key to use when "+
    "authenticating with the SSH server. You can use \\n within the string "+
    "to specify a new line.", "");

  public static StringParameter sshKeyFile
  = new StringParameter("SSHKeyFile",
    "When using the Via or Tunnel options with the embedded SSH client, "+
    "this parameter specifies a file that contains an SSH private key "+
    "(or keys) to use when authenticating with the SSH server. If not "+
    "specified, ~/.ssh/id_dsa or ~/.ssh/id_rsa will be used (if they exist). "+
    "Otherwise, the client will fallback to prompting for an SSH password.",
    "");

  public static StringParameter sshKeyPass
  = new StringParameter("SSHKeyPass",
    "When using the Via or Tunnel options with the embedded SSH client, "+
    "this parameter specifies the passphrase for the SSH key.", "");

  public static BoolParameter customCompressLevel
  = new BoolParameter("CustomCompressLevel",
    "Use custom compression level. Default if CompressLevel is specified.",
    false);

  public static IntParameter compressLevel
  = new IntParameter("CompressLevel",
    "Use specified compression level. 0 = Low, 6 = High",
    1);

  public static BoolParameter noJpeg
  = new BoolParameter("NoJPEG",
    "Disable lossy JPEG compression in Tight encoding.",
    false);

  public static IntParameter qualityLevel
  = new IntParameter("QualityLevel",
    "JPEG quality level. 0 = Low, 9 = High",
    8);

  private static final String IDENTIFIER_STRING
  = "TigerVNC Configuration file Version 1.0";

  static VoidParameter[] parameterArray = {
    CSecurityTLS.X509CA,
    CSecurityTLS.X509CRL,
    SecurityClient.secTypes,
    dotWhenNoCursor,
    autoSelect,
    fullColor,
    lowColorLevel,
    preferredEncoding,
    customCompressLevel,
    compressLevel,
    noJpeg,
    qualityLevel,
    maximize,
    fullScreen,
    fullScreenAllMonitors,
    desktopSize,
    remoteResize,
    viewOnly,
    shared,
    acceptClipboard,
    sendClipboard,
    menuKey,
    noLionFS,
    sendLocalUsername,
    maxCutText,
    scalingFactor,
    acceptBell,
    via,
    tunnel,
    extSSH,
    extSSHClient,
    extSSHArgs,
    sshConfig,
    sshKeyFile,
  };


  static LogWriter vlog = new LogWriter("Parameters");

	public static void saveViewerParameters(String filename, String servername) {

	  // Write to the registry or a predefined file if no filename was specified.
    String filepath;
    if (filename == null || filename.isEmpty()) {
      saveToReg(servername);
      return;
      /*
      String homeDir = FileUtils.getVncHomeDir();
      if (homeDir == null)
        throw new Exception("Failed to read configuration file, "+
                            "can't obtain home directory path.");
      filepath = homeDir.concat("default.tigervnc");
      */
    } else {
      filepath = filename;
    }

	  /* Write parameters to file */
    File f = new File(filepath);
    if (f.exists() && !f.canWrite())
	    throw new Exception(String.format("Failed to write configuration file,"+
                                        "can't open %s", filepath)); 

    PrintWriter pw = null;
    try {
      pw = new PrintWriter(f, "UTF-8");
    } catch (java.lang.Exception e)	{
      throw new Exception(e.getMessage());
    }

    pw.println(IDENTIFIER_STRING);
    pw.println("");

	  if (servername != null && !servername.isEmpty()) {
	    pw.println(String.format("ServerName=%s\n", servername));
      updateConnHistory(servername);
    }

    for (int i = 0; i < parameterArray.length; i++) {
      if (parameterArray[i] instanceof StringParameter) {
        //if (line.substring(0,idx).trim().equalsIgnoreCase(parameterArray[i].getName()))
          pw.println(String.format("%s=%s",parameterArray[i].getName(),
                                   parameterArray[i].getValueStr()));
      } else if (parameterArray[i] instanceof IntParameter) {
        //if (line.substring(0,idx).trim().equalsIgnoreCase(parameterArray[i].getName()))
          pw.println(String.format("%s=%s",parameterArray[i].getName(),
                                   parameterArray[i].getValueStr()));
      } else if (parameterArray[i] instanceof BoolParameter) {
        //if (line.substring(0,idx).trim().equalsIgnoreCase(parameterArray[i].getName()))
          pw.println(String.format("%s=%s",parameterArray[i].getName(),
                                   parameterArray[i].getValueStr()));
      } else {
        vlog.error(String.format("Unknown parameter type for parameter %s",
                   parameterArray[i].getName()));
      }
    }

    pw.flush();
    pw.close();
	}

  public static String loadViewerParameters(String filename) throws Exception {
    String servername = "";

    String filepath;
    if (filename == null) {

      return loadFromReg(); 

      /*
      String homeDir = FileUtils.getVncHomeDir();
      if (homeDir == null)
        throw new Exception("Failed to read configuration file, "+
                            "can't obtain home directory path.");
      filepath = homeDir.concat("default.tigervnc");
      */
    } else {
      filepath = filename;
    }

    /* Read parameters from file */
    File f = new File(filepath);
    if (!f.exists() || !f.canRead()) {
      if (filename == null || filename.isEmpty())
        return null;
      throw new Exception(String.format("Failed to read configuration file, can't open %s",
                          filepath)); 
    }

    String line = "";
    LineNumberReader reader;
    try {
      reader = new LineNumberReader(new FileReader(f));
    } catch (FileNotFoundException e) {
      throw new Exception(e.getMessage());
    }

    int lineNr = 0;
    while (line != null) {

      // Read the next line
      try {
        line = reader.readLine();
        lineNr = reader.getLineNumber();
        if (line == null)
          break;
      } catch (IOException e) {
        throw new Exception(String.format("Failed to read line %d in file %s: %s",
          lineNr, filepath, e.getMessage()));
      }

      // Make sure that the first line of the file has the file identifier string
      if(lineNr == 1) {
        if(line.equals(IDENTIFIER_STRING))
          continue;
        else
          throw new Exception(String.format("Configuration file %s is in an invalid format", filename));
      }

      // Skip empty lines and comments
      if (line.trim().isEmpty() || line.trim().startsWith("#"))
        continue;

      // Find the parameter value
      int idx = line.indexOf("=");
      if (idx == -1) {
        vlog.error(String.format("Failed to read line %d in file %s: %s",
                   lineNr, filename, "Invalid format"));
        continue;
      }
      String value = line.substring(idx+1).trim();
      boolean invalidParameterName = true; // Will be set to false below if 
                                           // the line contains a valid name.

      if (line.substring(0,idx).trim().equalsIgnoreCase("ServerName")) {
        if (value.length() > 256) {
          vlog.error(String.format("Failed to read line %d in file %s: %s",
                     lineNr, filepath, "Invalid format or too large value"));
          continue;
        }
        servername = value;
        invalidParameterName = false;
      } else {
        for (int i = 0; i < parameterArray.length; i++) {
          if (parameterArray[i] instanceof StringParameter) {
            if (line.substring(0,idx).trim().equalsIgnoreCase(parameterArray[i].getName())) {
              if (value.length() > 256) {
                vlog.error(String.format("Failed to read line %d in file %s: %s",
                           lineNr, filepath, "Invalid format or too large value"));
                continue;
              }
              ((StringParameter)parameterArray[i]).setParam(value);
              invalidParameterName = false;
            }
          } else if (parameterArray[i] instanceof IntParameter) {
            if (line.substring(0,idx).trim().equalsIgnoreCase(parameterArray[i].getName())) {
              ((IntParameter)parameterArray[i]).setParam(value);
              invalidParameterName = false;
            }
          } else if (parameterArray[i] instanceof BoolParameter) {
            if (line.substring(0,idx).trim().equalsIgnoreCase(parameterArray[i].getName())) {
              ((BoolParameter)parameterArray[i]).setParam(value);
              invalidParameterName = false;
            }
          } else {
            vlog.error(String.format("Unknown parameter type for parameter %s",
                       parameterArray[i].getName()));

          }
        }
      }

      if (invalidParameterName)
        vlog.info(String.format("Unknown parameter %s on line %d in file %s",
                  line, lineNr, filepath));
    }
    try {
      reader.close();
    } catch (IOException e) {
      vlog.info(e.getMessage());
    } finally {
      try {
        if (reader != null)
          reader.close();
        } catch (IOException e) { }
    }

    return servername;
  }

  public static void saveToReg(String servername) {
    String hKey = "global";

    if (servername != null && !servername.isEmpty()) {
      UserPreferences.set(hKey, "ServerName", servername);
      updateConnHistory(servername);
    }

    for (int i = 0; i < parameterArray.length; i++) {
      if (parameterArray[i] instanceof StringParameter) {
        UserPreferences.set(hKey, parameterArray[i].getName(),
                                  parameterArray[i].getValueStr());
      } else if (parameterArray[i] instanceof IntParameter) {
        UserPreferences.set(hKey, parameterArray[i].getName(),
                            ((IntParameter)parameterArray[i]).getValue());
      } else if (parameterArray[i] instanceof BoolParameter) {
        UserPreferences.set(hKey, parameterArray[i].getName(),
                            ((BoolParameter)parameterArray[i]).getValue());
      } else {
        vlog.error(String.format("Unknown parameter type for parameter %s",
                   parameterArray[i].getName()));
      }
    }

    UserPreferences.save(hKey);
  }

  public static String loadFromReg() {

    String hKey = "global";

    String servername = UserPreferences.get(hKey, "ServerName");
    if (servername == null)
      servername = "";

    for (int i = 0; i < parameterArray.length; i++) {
      if (parameterArray[i] instanceof StringParameter) {
        if (UserPreferences.get(hKey, parameterArray[i].getName()) != null) {
          String stringValue =
            UserPreferences.get(hKey, parameterArray[i].getName());
          ((StringParameter)parameterArray[i]).setParam(stringValue);
        }
      } else if (parameterArray[i] instanceof IntParameter) {
        if (UserPreferences.get(hKey, parameterArray[i].getName()) != null) {
          int intValue =
            UserPreferences.getInt(hKey, parameterArray[i].getName());
          ((IntParameter)parameterArray[i]).setParam(intValue);
        }
      } else if (parameterArray[i] instanceof BoolParameter) {
        if (UserPreferences.get(hKey, parameterArray[i].getName()) != null) {
          boolean booleanValue =
            UserPreferences.getBool(hKey, parameterArray[i].getName());
          ((BoolParameter)parameterArray[i]).setParam(booleanValue);
        }
      } else {
        vlog.error(String.format("Unknown parameter type for parameter %s",
                   parameterArray[i].getName()));
      }
    }

    return servername;
  }

  private static void updateConnHistory(String serverName) {
    String hKey = "ServerDialog";
    if (serverName != null && !serverName.isEmpty()) {
      String valueStr = UserPreferences.get(hKey, "history");
      String t = (valueStr == null) ? "" : valueStr;
      StringTokenizer st = new StringTokenizer(t, ",");
      StringBuffer sb = new StringBuffer().append(serverName);
      while (st.hasMoreTokens()) {
        String str = st.nextToken();
        if (!str.equals(serverName) && !str.equals(""))
          sb.append(',').append(str);
      }
      UserPreferences.set(hKey, "history", sb.toString());
      UserPreferences.save(hKey);
    }
  }

}