Skip to content

Commit

Permalink
Delay initialization of sftp server
Browse files Browse the repository at this point in the history
  • Loading branch information
albertvaka committed Mar 19, 2023
1 parent 1ba9e59 commit 51dfa2d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
21 changes: 10 additions & 11 deletions src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -34,7 +31,7 @@
import org.kde.kdeconnect_tp.BuildConfig;
import org.kde.kdeconnect_tp.R;

import java.io.File;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -63,13 +60,7 @@ public String getDescription() {

@Override
public boolean onCreate() {
try {
server.init(context, device);
return true;
} catch (Exception e) {
Log.e("SFTP", "Exception in server.init()", e);
return false;
}
return true;
}

@Override
Expand Down Expand Up @@ -117,6 +108,14 @@ public void onDestroy() {
@Override
public boolean onPacketReceived(NetworkPacket np) {
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> pathNames = new ArrayList<>();

Expand Down
10 changes: 9 additions & 1 deletion src/org/kde/kdeconnect/Plugins/SftpPlugin/SimpleSftpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ class SimpleSftpServer {
SecurityUtils.setRegisterBouncyCastle(false);
}

boolean initialized = false;

private final SshServer sshd = SshServer.setUpDefaultServer();
private AndroidFileSystemFactory safFileSystemFactory;

public void setSafRoots(List<SftpPlugin.StorageInfo> storageInfoList) {
safFileSystemFactory.initRoots(storageInfoList);
}

void init(Context context, Device device) throws GeneralSecurityException {
void initialize(Context context, Device device) throws GeneralSecurityException {

sshd.setKeyExchangeFactories(Arrays.asList(
new ECDHP384.Factory(), // This is the best we have in mina-sshd 0.14.0 -- Upgrading is non-trivial
Expand Down Expand Up @@ -97,6 +99,8 @@ public Iterable<KeyPair> loadKeys() {

sshd.setPublickeyAuthenticator(keyAuth);
sshd.setPasswordAuthenticator(passwordAuth);

initialized = true;
}

public boolean start() {
Expand Down Expand Up @@ -177,6 +181,10 @@ String getLocalIpAddress() {
return ip6;
}

public boolean isInitialized() {
return initialized;
}

static class SimplePasswordAuthenticator implements PasswordAuthenticator {

String password;
Expand Down

0 comments on commit 51dfa2d

Please sign in to comment.