Search in sources :

Example 1 with AsyncTaskResult

use of com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult in project AmazeFileManager by TeamAmaze.

the class SshConnectionPool method create.

// Logic for creating SSH connection. Depends on password existence in given Uri password or
// key-based authentication
private SSHClient create(@NonNull Uri uri) throws IOException {
    String host = uri.getHost();
    int port = uri.getPort();
    // If the uri is fetched from the app's database storage, we assume it will never be empty
    String[] userInfo = uri.getUserInfo().split(":");
    String username = userInfo[0];
    String password = userInfo.length > 1 ? userInfo[1] : null;
    if (port < 0)
        port = SSH_DEFAULT_PORT;
    UtilsHandler utilsHandler = AppConfig.getInstance().getUtilsHandler();
    try {
        String pem = utilsHandler.getSshAuthPrivateKey(uri.toString());
        AtomicReference<KeyPair> keyPair = new AtomicReference<>(null);
        if (pem != null && !"".equals(pem)) {
            CountDownLatch latch = new CountDownLatch(1);
            new PemToKeyPairTask(pem, result -> {
                if (result.result != null) {
                    keyPair.set(result.result);
                    latch.countDown();
                }
            }).execute();
            latch.await();
        }
        AsyncTaskResult<SSHClient> taskResult = new SshAuthenticationTask(host, port, utilsHandler.getSshHostKey(uri.toString()), username, password, keyPair.get()).execute().get();
        SSHClient client = taskResult.result;
        return client;
    } catch (InterruptedException e) {
        // FIXME: proper handling
        throw new RuntimeException(e);
    } catch (ExecutionException e) {
        // FIXME: proper handling
        throw new RuntimeException(e);
    }
}
Also used : KeyPair(java.security.KeyPair) AppConfig(com.amaze.filemanager.utils.application.AppConfig) Uri(android.net.Uri) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UtilsHandler(com.amaze.filemanager.database.UtilsHandler) IOException(java.io.IOException) NonNull(android.support.annotation.NonNull) AsyncTaskResult(com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) MainActivity(com.amaze.filemanager.activities.MainActivity) ExecutionException(java.util.concurrent.ExecutionException) CountDownLatch(java.util.concurrent.CountDownLatch) SshAuthenticationTask(com.amaze.filemanager.asynchronous.asynctasks.ssh.SshAuthenticationTask) PemToKeyPairTask(com.amaze.filemanager.asynchronous.asynctasks.ssh.PemToKeyPairTask) SSHClient(net.schmizz.sshj.SSHClient) Map(java.util.Map) Log(android.util.Log) KeyPair(java.security.KeyPair) AtomicReference(java.util.concurrent.atomic.AtomicReference) PemToKeyPairTask(com.amaze.filemanager.asynchronous.asynctasks.ssh.PemToKeyPairTask) SshAuthenticationTask(com.amaze.filemanager.asynchronous.asynctasks.ssh.SshAuthenticationTask) CountDownLatch(java.util.concurrent.CountDownLatch) SSHClient(net.schmizz.sshj.SSHClient) ExecutionException(java.util.concurrent.ExecutionException) UtilsHandler(com.amaze.filemanager.database.UtilsHandler)

Example 2 with AsyncTaskResult

use of com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult 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 3 with AsyncTaskResult

use of com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult in project AmazeFileManager by TeamAmaze.

the class PemToKeyPairTask method onPostExecute.

@Override
protected void onPostExecute(AsyncTaskResult<KeyPair> result) {
    if (result.exception != null) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(AppConfig.getInstance().getActivityContext());
        EditText textfield = new EditText(builder.getContext());
        textfield.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        builder.customView(textfield, false).title(R.string.ssh_key_prompt_passphrase).positiveText(R.string.ok).onPositive(((dialog, which) -> {
            new PemToKeyPairTask(pemFile, callback, textfield.getText().toString()).execute();
            dialog.dismiss();
        })).negativeText(R.string.cancel).onNegative(((dialog, which) -> {
            dialog.dismiss();
            toastOnParseError(result);
        }));
        builder.show();
    }
    if (callback != null) {
        callback.onResult(result);
    }
}
Also used : EditText(android.widget.EditText) KeyPair(java.security.KeyPair) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) AppConfig(com.amaze.filemanager.utils.application.AppConfig) Uri(android.net.Uri) Security(java.security.Security) NonNull(android.support.annotation.NonNull) AsyncTaskResult(com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult) Toast(android.widget.Toast) OpenSSHKeyFile(net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile) PasswordFinder(net.schmizz.sshj.userauth.password.PasswordFinder) Log(android.util.Log) AsyncTask(android.os.AsyncTask) OpenSSHKeyV1KeyFile(com.hierynomus.sshj.userauth.keyprovider.OpenSSHKeyV1KeyFile) PEMParser(org.bouncycastle.openssl.PEMParser) InputType(android.text.InputType) IOUtils(net.schmizz.sshj.common.IOUtils) IOException(java.io.IOException) KeyProvider(net.schmizz.sshj.userauth.keyprovider.KeyProvider) Provider(java.security.Provider) PuTTYKeyFile(net.schmizz.sshj.userauth.keyprovider.PuTTYKeyFile) Resource(net.schmizz.sshj.userauth.password.Resource) StringReader(java.io.StringReader) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) R(com.amaze.filemanager.R) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) EditText(android.widget.EditText) InputStream(java.io.InputStream) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog)

Example 4 with AsyncTaskResult

use of com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult 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)4 IOException (java.io.IOException)4 SSHClient (net.schmizz.sshj.SSHClient)3 Uri (android.net.Uri)2 NonNull (android.support.annotation.NonNull)2 Log (android.util.Log)2 CustomSshJConfig (com.amaze.filemanager.filesystem.ssh.CustomSshJConfig)2 AppConfig (com.amaze.filemanager.utils.application.AppConfig)2 KeyPair (java.security.KeyPair)2 PublicKey (java.security.PublicKey)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 KeyProvider (net.schmizz.sshj.userauth.keyprovider.KeyProvider)2 AsyncTask (android.os.AsyncTask)1 InputType (android.text.InputType)1 EditText (android.widget.EditText)1 Toast (android.widget.Toast)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 R (com.amaze.filemanager.R)1 MainActivity (com.amaze.filemanager.activities.MainActivity)1