2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-09-01 06:35:09 +00:00

Delay initialization of sftp server

This commit is contained in:
Albert Vaca Cintora
2023-03-19 11:56:25 +01:00
committed by Albert Vaca Cintora
parent 1ba9e59872
commit 51dfa2dd8c
2 changed files with 19 additions and 12 deletions

View File

@@ -8,7 +8,6 @@ package org.kde.kdeconnect.Plugins.SftpPlugin;
import android.app.Activity; import android.app.Activity;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
@@ -16,10 +15,8 @@ import android.os.Environment;
import android.os.storage.StorageManager; import android.os.storage.StorageManager;
import android.os.storage.StorageVolume; import android.os.storage.StorageVolume;
import android.provider.Settings; import android.provider.Settings;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@@ -34,7 +31,7 @@ import org.kde.kdeconnect.UserInterface.StartActivityAlertDialogFragment;
import org.kde.kdeconnect_tp.BuildConfig; import org.kde.kdeconnect_tp.BuildConfig;
import org.kde.kdeconnect_tp.R; import org.kde.kdeconnect_tp.R;
import java.io.File; import java.security.GeneralSecurityException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@@ -63,13 +60,7 @@ public class SftpPlugin extends Plugin implements SharedPreferences.OnSharedPref
@Override @Override
public boolean onCreate() { public boolean onCreate() {
try {
server.init(context, device);
return true; return true;
} catch (Exception e) {
Log.e("SFTP", "Exception in server.init()", e);
return false;
}
} }
@Override @Override
@@ -117,6 +108,14 @@ public class SftpPlugin extends Plugin implements SharedPreferences.OnSharedPref
@Override @Override
public boolean onPacketReceived(NetworkPacket np) { public boolean onPacketReceived(NetworkPacket np) {
if (np.getBoolean("startBrowsing")) { if (np.getBoolean("startBrowsing")) {
if (!server.isInitialized()) {
try {
server.initialize(context, device);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
ArrayList<String> paths = new ArrayList<>(); ArrayList<String> paths = new ArrayList<>();
ArrayList<String> pathNames = new ArrayList<>(); ArrayList<String> pathNames = new ArrayList<>();

View File

@@ -58,6 +58,8 @@ class SimpleSftpServer {
SecurityUtils.setRegisterBouncyCastle(false); SecurityUtils.setRegisterBouncyCastle(false);
} }
boolean initialized = false;
private final SshServer sshd = SshServer.setUpDefaultServer(); private final SshServer sshd = SshServer.setUpDefaultServer();
private AndroidFileSystemFactory safFileSystemFactory; private AndroidFileSystemFactory safFileSystemFactory;
@@ -65,7 +67,7 @@ class SimpleSftpServer {
safFileSystemFactory.initRoots(storageInfoList); safFileSystemFactory.initRoots(storageInfoList);
} }
void init(Context context, Device device) throws GeneralSecurityException { void initialize(Context context, Device device) throws GeneralSecurityException {
sshd.setKeyExchangeFactories(Arrays.asList( sshd.setKeyExchangeFactories(Arrays.asList(
new ECDHP384.Factory(), // This is the best we have in mina-sshd 0.14.0 -- Upgrading is non-trivial new ECDHP384.Factory(), // This is the best we have in mina-sshd 0.14.0 -- Upgrading is non-trivial
@@ -97,6 +99,8 @@ class SimpleSftpServer {
sshd.setPublickeyAuthenticator(keyAuth); sshd.setPublickeyAuthenticator(keyAuth);
sshd.setPasswordAuthenticator(passwordAuth); sshd.setPasswordAuthenticator(passwordAuth);
initialized = true;
} }
public boolean start() { public boolean start() {
@@ -177,6 +181,10 @@ class SimpleSftpServer {
return ip6; return ip6;
} }
public boolean isInitialized() {
return initialized;
}
static class SimplePasswordAuthenticator implements PasswordAuthenticator { static class SimplePasswordAuthenticator implements PasswordAuthenticator {
String password; String password;