use of com.amaze.filemanager.asynchronous.asynctasks.ssh.SshAuthenticationTask 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.SshAuthenticationTask in project AmazeFileManager by TeamAmaze.
the class SftpConnectDialog method authenticateAndSaveSetup.
private boolean authenticateAndSaveSetup(String connectionName, String hostname, int port, String hostKeyFingerprint, String username, String password, String selectedParsedKeyPairName, KeyPair selectedParsedKeyPair, boolean isEdit) {
if (isEdit)
password = getArguments().getString("password", null);
final String path = deriveSftpPathFrom(hostname, port, username, password, selectedParsedKeyPair);
final String encryptedPath = SshClientUtils.encryptSshPathAsNecessary(path);
if (!isEdit) {
try {
AsyncTaskResult<SSHClient> taskResult = new SshAuthenticationTask(hostname, port, hostKeyFingerprint, username, password, selectedParsedKeyPair).execute().get();
SSHClient result = taskResult.result;
if (result != null) {
if (DataUtils.getInstance().containsServer(path) == -1) {
DataUtils.getInstance().addServer(new String[] { connectionName, path });
((MainActivity) getActivity()).getDrawer().refreshDrawer();
utilsHandler.addSsh(connectionName, encryptedPath, hostKeyFingerprint, selectedParsedKeyPairName, getPemContents());
MainFragment ma = ((MainActivity) getActivity()).getCurrentMainFragment();
ma.loadlist(path, false, OpenMode.UNKNOWN);
dismiss();
} else {
Snackbar.make(getActivity().findViewById(R.id.content_frame), getResources().getString(R.string.connection_exists), Snackbar.LENGTH_SHORT).show();
dismiss();
}
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
} else {
DataUtils.getInstance().removeServer(DataUtils.getInstance().containsServer(path));
DataUtils.getInstance().addServer(new String[] { connectionName, path });
Collections.sort(DataUtils.getInstance().getServers(), new BookSorter());
((MainActivity) getActivity()).getDrawer().refreshDrawer();
AppConfig.runInBackground(() -> {
utilsHandler.updateSsh(connectionName, getArguments().getString("name"), encryptedPath, selectedParsedKeyPairName, getPemContents());
});
dismiss();
return true;
}
}
Aggregations