Index: com/tightvnc/vncviewer/SshTunneledSocketFactory.java =================================================================== --- com/tightvnc/vncviewer/SshTunneledSocketFactory.java (revision 3658) +++ com/tightvnc/vncviewer/SshTunneledSocketFactory.java (working copy) @@ -32,23 +32,54 @@ import java.net.*; import java.io.*; import javax.swing.*; +import java.util.HashMap; class SshTunneledSocketFactory implements SocketFactory { + // The ARG_ finals provide a consistent mapping for + // argument naming. This prevents typo's later in the code, too! + // If you extend this later, simply add an entry to the final ARG_ list, + // and also to the SshArguments[] array so it will be enumerated by the + // various createSocket() implementations---and made visible within "argMap". + + static final String + ARG_SSH_HOST = "SSHHOST", + ARG_SSH_KNOWN_HOSTS = "SSH_KNOWN_HOSTS", + ARG_SSH_IDENTITY = "SSH_IDENTITY", + ARG_SSH_IDENTITY_PUB = "SSH_IDENTITY_PUB" + ; + + static final String SshArguments[] = { + ARG_SSH_HOST, + ARG_SSH_KNOWN_HOSTS, + ARG_SSH_IDENTITY, + ARG_SSH_IDENTITY_PUB}; + public Socket createSocket(String host, int port, Applet applet) throws IOException { - - return createSocket(host, port, applet.getParameter("SSHHOST")); + // Automagically fill the argMap with the + // known Applet arguments (see ARG_*, above). + HashMap argMap = new HashMap(); + for (int i = 0; i < SshArguments.length; i++) + argMap.put(SshArguments[i], applet.getParameter(SshArguments[i])); + + return createSocket(host, port, argMap); } public Socket createSocket(String host, int port, String[] args) throws IOException { + // Automagically fill the argMap with the + // known console arguments (see ARG_*, above). + HashMap argMap = new HashMap(); + for (int i = 0; i < SshArguments.length; i++) + argMap.put(SshArguments[i], readArg(args, SshArguments[i])); - return createSocket(host, port, readArg(args, "SSHHOST")); + return createSocket(host, port, argMap); } public Socket createSocket(String host, int port, - String sshHost) throws IOException { + HashMap argMap) throws IOException { + String sshHost = argMap.get(ARG_SSH_HOST); if (localPort == 0) { if (sshHost == null) { sshHost = System.getProperty("user.name") + "@" + host; @@ -56,7 +87,7 @@ } System.out.println("Creating SSH tunnel to " + sshHost); try { - createTunnel(host, port, sshHost); + createTunnel(host, port, argMap); } catch (IOException e) { throw e; } catch (Exception e) { @@ -69,10 +100,39 @@ } private void createTunnel(String host, int port, - String sshHost) throws Exception { + HashMap argMap) throws Exception { + + String sshHost = argMap.get(ARG_SSH_HOST); + String sshKnownHosts = argMap.get(ARG_SSH_KNOWN_HOSTS); + String sshIdentity = argMap.get(ARG_SSH_IDENTITY); + String sshIdentityPub = argMap.get(ARG_SSH_IDENTITY_PUB); + try { JSch jsch = new JSch(); + if (sshKnownHosts != null) + { + // System.out.println("sshKnownHosts: " + sshKnownHosts); // DEBUG + InputStream in = new ByteArrayInputStream(sshKnownHosts.getBytes()); + jsch.setKnownHosts(in); + in.close(); + } + + if (sshIdentity != null + && sshIdentityPub != null) + { + + //System.out.println("using identity: " + sshIdentityPub); // DEBUG + jsch.addIdentity( + "id_vnc", // an arbitrary identity name + sshIdentity.getBytes(), + sshIdentityPub.getBytes(), + null // this will force a GUI prompt for passphrase. + ); + + System.out.println("Known Identities: " + jsch.getIdentityNames().toString()); + } + final int atIndex = sshHost.indexOf('@'); String user = ""; if (atIndex > 0) { @@ -133,11 +193,19 @@ } public String getPassphrase() { - return null; + return passphrase; } public boolean promptPassphrase(String message) { - return true; + SshPasswordRequester requester = new SshPasswordRequester(message + ":"); + try { + char[] passwdChars = requester.queryPassword(); + passphrase = new String(passwdChars); + java.util.Arrays.fill(passwdChars, '\0'); + return true; + } catch (Exception e) { + return false; + } } public boolean promptPassword(String message) { @@ -212,7 +280,7 @@ } } - private String passwd; + private String passwd, passphrase; } }