Search in sources :

Example 1 with CustomSshJConfig

use of com.amaze.filemanager.filesystem.ssh.CustomSshJConfig in project AmazeFileManager by TeamAmaze.

the class GetSshHostFingerprintTask method doInBackground.

@Override
protected AsyncTaskResult<PublicKey> doInBackground(Void... voids) {
    final AtomicReference<AsyncTaskResult<PublicKey>> holder = new AtomicReference<AsyncTaskResult<PublicKey>>();
    final CountDownLatch latch = new CountDownLatch(1);
    final SSHClient sshClient = new SSHClient(new CustomSshJConfig());
    sshClient.setConnectTimeout(SSH_CONNECT_TIMEOUT);
    sshClient.addHostKeyVerifier((hostname, port, key) -> {
        holder.set(new AsyncTaskResult<PublicKey>(key));
        latch.countDown();
        return true;
    });
    try {
        sshClient.connect(hostname, port);
        latch.await();
    } catch (IOException e) {
        e.printStackTrace();
        holder.set(new AsyncTaskResult<PublicKey>(e));
        latch.countDown();
    } catch (InterruptedException e) {
        e.printStackTrace();
        holder.set(new AsyncTaskResult<PublicKey>(e));
        latch.countDown();
    } finally {
        SshClientUtils.tryDisconnect(sshClient);
        return holder.get();
    }
}
Also used : CustomSshJConfig(com.amaze.filemanager.filesystem.ssh.CustomSshJConfig) PublicKey(java.security.PublicKey) AsyncTaskResult(com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult) SSHClient(net.schmizz.sshj.SSHClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with CustomSshJConfig

use of com.amaze.filemanager.filesystem.ssh.CustomSshJConfig in project AmazeFileManager by TeamAmaze.

the class SshAuthenticationTask method doInBackground.

@Override
protected AsyncTaskResult<SSHClient> doInBackground(Void... voids) {
    final SSHClient sshClient = new SSHClient(new CustomSshJConfig());
    sshClient.addHostKeyVerifier(hostKey);
    sshClient.setConnectTimeout(SSH_CONNECT_TIMEOUT);
    try {
        sshClient.connect(hostname, port);
        if (password != null && !"".equals(password)) {
            sshClient.authPassword(username, password);
            return new AsyncTaskResult<SSHClient>(sshClient);
        } else {
            sshClient.authPublickey(username, new KeyProvider() {

                @Override
                public PrivateKey getPrivate() throws IOException {
                    return privateKey.getPrivate();
                }

                @Override
                public PublicKey getPublic() throws IOException {
                    return privateKey.getPublic();
                }

                @Override
                public KeyType getType() throws IOException {
                    return KeyType.fromKey(getPublic());
                }
            });
            return new AsyncTaskResult<SSHClient>(sshClient);
        }
    } catch (UserAuthException e) {
        e.printStackTrace();
        return new AsyncTaskResult<SSHClient>(e);
    } catch (TransportException e) {
        e.printStackTrace();
        return new AsyncTaskResult<SSHClient>(e);
    } catch (IOException e) {
        e.printStackTrace();
        return new AsyncTaskResult<SSHClient>(e);
    }
}
Also used : CustomSshJConfig(com.amaze.filemanager.filesystem.ssh.CustomSshJConfig) KeyProvider(net.schmizz.sshj.userauth.keyprovider.KeyProvider) PrivateKey(java.security.PrivateKey) KeyType(net.schmizz.sshj.common.KeyType) PublicKey(java.security.PublicKey) SSHClient(net.schmizz.sshj.SSHClient) AsyncTaskResult(com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult) IOException(java.io.IOException) UserAuthException(net.schmizz.sshj.userauth.UserAuthException) TransportException(net.schmizz.sshj.transport.TransportException)

Aggregations

AsyncTaskResult (com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult)2 CustomSshJConfig (com.amaze.filemanager.filesystem.ssh.CustomSshJConfig)2 IOException (java.io.IOException)2 PublicKey (java.security.PublicKey)2 SSHClient (net.schmizz.sshj.SSHClient)2 PrivateKey (java.security.PrivateKey)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 KeyType (net.schmizz.sshj.common.KeyType)1 TransportException (net.schmizz.sshj.transport.TransportException)1 UserAuthException (net.schmizz.sshj.userauth.UserAuthException)1 KeyProvider (net.schmizz.sshj.userauth.keyprovider.KeyProvider)1