Improve SSH client.
[lgpl/argeo-commons.git] / org.argeo.core / src / org / argeo / ssh / AbstractSsh.java
index 9c4ec567acf4206c1d1b6eed58e0ab0b7f9ee7f4..588c16b01bd426a8a08e20aca5d051a8d9686cbb 100644 (file)
@@ -1,18 +1,17 @@
 package org.argeo.ssh;
 
+import java.io.Console;
 import java.io.IOException;
 import java.net.URI;
-import java.security.GeneralSecurityException;
-import java.security.KeyPair;
+import java.util.Arrays;
+import java.util.Scanner;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.sshd.client.SshClient;
-import org.apache.sshd.client.config.keys.ClientIdentityLoader;
 import org.apache.sshd.client.future.ConnectFuture;
 import org.apache.sshd.client.session.ClientSession;
-import org.apache.sshd.client.subsystem.sftp.SftpFileSystemProvider;
-import org.apache.sshd.common.config.keys.FilePasswordProvider;
+import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystemProvider;
 
 abstract class AbstractSsh {
        private final static Log log = LogFactory.getLog(AbstractSsh.class);
@@ -23,6 +22,8 @@ abstract class AbstractSsh {
        private boolean passwordSet = false;
        private ClientSession session;
 
+       private SshKeyPair sshKeyPair;
+
        synchronized SshClient getSshClient() {
                if (sshClient == null) {
                        long begin = System.currentTimeMillis();
@@ -35,16 +36,39 @@ abstract class AbstractSsh {
                }
                return sshClient;
        }
-       
+
        synchronized SftpFileSystemProvider getSftpFileSystemProvider() {
-               if(sftpFileSystemProvider==null) {
+               if (sftpFileSystemProvider == null) {
                        sftpFileSystemProvider = new SftpFileSystemProvider(sshClient);
                }
                return sftpFileSystemProvider;
        }
 
+       @SuppressWarnings("restriction")
        void authenticate() {
                try {
+                       if (sshKeyPair != null) {
+                               session.addPublicKeyIdentity(sshKeyPair.asKeyPair());
+                       } else {
+
+                               if (!passwordSet) {
+                                       String password;
+                                       Console console = System.console();
+                                       if (console == null) {// IDE
+                                               System.out.print("Password: ");
+                                               try (Scanner s = new Scanner(System.in)) {
+                                                       password = s.next();
+                                               }
+                                       } else {
+                                               console.printf("Password: ");
+                                               char[] pwd = console.readPassword();
+                                               password = new String(pwd);
+                                               Arrays.fill(pwd, ' ');
+                                       }
+                                       session.addPasswordIdentity(password);
+                                       passwordSet = true;
+                               }
+                       }
                        session.auth().verify(1000l);
                } catch (IOException e) {
                        throw new IllegalStateException(e);
@@ -60,19 +84,20 @@ abstract class AbstractSsh {
        }
 
        void loadKey(String password, String keyPath) {
-               try {
-                       KeyPair keyPair = ClientIdentityLoader.DEFAULT.loadClientIdentity(keyPath,
-                                       FilePasswordProvider.of(password));
-                       session.addPublicKeyIdentity(keyPair);
-               } catch (IOException | GeneralSecurityException e) {
-                       throw new IllegalStateException(e);
-               }
+//             try {
+//                     KeyPair keyPair = ClientIdentityLoader.DEFAULT.loadClientIdentity(keyPath,
+//                                     FilePasswordProvider.of(password));
+//                     session.addPublicKeyIdentity(keyPair);
+//             } catch (IOException | GeneralSecurityException e) {
+//                     throw new IllegalStateException(e);
+//             }
        }
 
        void openSession(URI uri) {
                openSession(uri.getUserInfo(), uri.getHost(), uri.getPort() > 0 ? uri.getPort() : null);
        }
 
+       @SuppressWarnings("restriction")
        void openSession(String login, String host, Integer port) {
                if (session != null)
                        throw new IllegalStateException("Session is already open");
@@ -106,8 +131,9 @@ abstract class AbstractSsh {
                }
        }
 
+       @SuppressWarnings("restriction")
        void closeSession() {
-               if (session != null)
+               if (session == null)
                        throw new IllegalStateException("No session is open");
                try {
                        session.close();