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