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);
}
}
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) {
}
}
}
Aggregations