Search in sources :

Example 1 with PemToKeyPairTask

use of com.amaze.filemanager.asynchronous.asynctasks.ssh.PemToKeyPairTask 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 PemToKeyPairTask

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

the class SftpConnectDialog method onActivityResult.

/**
 * Set the PEM key for authentication when the Intent to browse file returned.
 */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (SELECT_PEM_INTENT == requestCode && Activity.RESULT_OK == resultCode) {
        selectedPem = data.getData();
        Log.d(TAG, "Selected PEM: " + selectedPem.toString() + "/ " + selectedPem.getLastPathSegment());
        try {
            InputStream selectedKeyContent = context.getContentResolver().openInputStream(selectedPem);
            new PemToKeyPairTask(selectedKeyContent, result -> {
                if (result.result != null) {
                    selectedParsedKeyPair = result.result;
                    selectedParsedKeyPairName = selectedPem.getLastPathSegment().substring(selectedPem.getLastPathSegment().indexOf('/') + 1);
                    MDButton okBTN = ((MaterialDialog) getDialog()).getActionButton(DialogAction.POSITIVE);
                    okBTN.setEnabled(okBTN.isEnabled() || true);
                    Button selectPemBTN = getDialog().findViewById(R.id.selectPemBTN);
                    selectPemBTN.setText(selectedParsedKeyPairName);
                }
            }).execute();
        } catch (FileNotFoundException e) {
            Log.e(TAG, "File not found", e);
        } catch (IOException shouldNotHappen) {
        }
    }
}
Also used : Context(android.content.Context) KeyPair(java.security.KeyPair) BookSorter(com.amaze.filemanager.utils.BookSorter) Bundle(android.os.Bundle) AppConfig(com.amaze.filemanager.utils.application.AppConfig) ColorUsage(com.amaze.filemanager.utils.color.ColorUsage) Uri(android.net.Uri) MainFragment(com.amaze.filemanager.fragments.MainFragment) UtilsHandler(com.amaze.filemanager.database.UtilsHandler) Dialog(android.app.Dialog) Intent(android.content.Intent) AsyncTaskResult(com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult) Editable(android.text.Editable) MainActivity(com.amaze.filemanager.activities.MainActivity) MDButton(com.afollestad.materialdialogs.internal.MDButton) SecurityUtils(net.schmizz.sshj.common.SecurityUtils) SshClientUtils(com.amaze.filemanager.filesystem.ssh.SshClientUtils) GetSshHostFingerprintTask(com.amaze.filemanager.asynchronous.asynctasks.ssh.GetSshHostFingerprintTask) DataUtils(com.amaze.filemanager.utils.DataUtils) View(android.view.View) Button(android.widget.Button) DialogFragment(android.app.DialogFragment) Log(android.util.Log) UtilitiesProvider(com.amaze.filemanager.utils.provider.UtilitiesProvider) SshConnectionPool(com.amaze.filemanager.filesystem.ssh.SshConnectionPool) DialogAction(com.afollestad.materialdialogs.DialogAction) IOException(java.io.IOException) PublicKey(java.security.PublicKey) InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) AlertDialog(android.app.AlertDialog) SshAuthenticationTask(com.amaze.filemanager.asynchronous.asynctasks.ssh.SshAuthenticationTask) PemToKeyPairTask(com.amaze.filemanager.asynchronous.asynctasks.ssh.PemToKeyPairTask) SSHClient(net.schmizz.sshj.SSHClient) R(com.amaze.filemanager.R) BufferedReader(java.io.BufferedReader) Snackbar(android.support.design.widget.Snackbar) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Activity(android.app.Activity) SimpleTextWatcher(com.amaze.filemanager.utils.SimpleTextWatcher) OpenMode(com.amaze.filemanager.utils.OpenMode) Collections(java.util.Collections) EditText(android.widget.EditText) TextWatcher(android.text.TextWatcher) InputStream(java.io.InputStream) MDButton(com.afollestad.materialdialogs.internal.MDButton) MDButton(com.afollestad.materialdialogs.internal.MDButton) Button(android.widget.Button) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) PemToKeyPairTask(com.amaze.filemanager.asynchronous.asynctasks.ssh.PemToKeyPairTask) IOException(java.io.IOException)

Aggregations

Uri (android.net.Uri)2 Log (android.util.Log)2 MainActivity (com.amaze.filemanager.activities.MainActivity)2 AsyncTaskResult (com.amaze.filemanager.asynchronous.asynctasks.AsyncTaskResult)2 PemToKeyPairTask (com.amaze.filemanager.asynchronous.asynctasks.ssh.PemToKeyPairTask)2 SshAuthenticationTask (com.amaze.filemanager.asynchronous.asynctasks.ssh.SshAuthenticationTask)2 UtilsHandler (com.amaze.filemanager.database.UtilsHandler)2 AppConfig (com.amaze.filemanager.utils.application.AppConfig)2 IOException (java.io.IOException)2 Activity (android.app.Activity)1 AlertDialog (android.app.AlertDialog)1 Dialog (android.app.Dialog)1 DialogFragment (android.app.DialogFragment)1 Context (android.content.Context)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 NonNull (android.support.annotation.NonNull)1 Snackbar (android.support.design.widget.Snackbar)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1