Skip to content

Commit

Permalink
Use a single SSH session for all operations
Browse files Browse the repository at this point in the history
And clean up code which is no longer called.
  • Loading branch information
amberin committed Sep 1, 2024
1 parent f038578 commit 0dc3f38
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 518 deletions.
321 changes: 24 additions & 297 deletions app/src/main/java/com/orgzly/android/git/GitFileSynchronizer.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.orgzly.android.prefs.AppPreferences;
import com.orgzly.android.prefs.RepoPreferences;

import org.eclipse.jgit.api.TransportCommand;

public class GitPreferencesFromRepoPrefs implements GitPreferences {
private RepoPreferences repoPreferences;

Expand All @@ -22,7 +24,14 @@ public GitTransportSetter createTransportSetter() {
String password = repoPreferences.getStringValue(R.string.pref_key_git_https_password, "");
return new HTTPSTransportSetter(username, password);
case "file":
return tc -> tc;
return new GitTransportSetter() {
@Override
public TransportCommand setTransport(TransportCommand tc) {
return tc;
}
@Override
public void close() {}
};
default:
return new GitSshKeyTransportSetter();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,73 @@ package com.orgzly.android.git
import android.os.Build
import androidx.annotation.RequiresApi
import com.orgzly.android.App
import org.eclipse.jgit.annotations.NonNull
import org.eclipse.jgit.api.TransportCommand
import org.eclipse.jgit.api.TransportConfigCallback
import org.eclipse.jgit.internal.transport.sshd.OpenSshServerKeyDatabase
import org.eclipse.jgit.transport.CredentialsProvider
import org.eclipse.jgit.transport.RemoteSession
import org.eclipse.jgit.transport.SshSessionFactory
import org.eclipse.jgit.transport.SshTransport
import org.eclipse.jgit.transport.Transport
import org.eclipse.jgit.transport.URIish
import org.eclipse.jgit.transport.sshd.ServerKeyDatabase
import org.eclipse.jgit.transport.sshd.SshdSession
import org.eclipse.jgit.transport.sshd.SshdSessionFactory
import org.eclipse.jgit.util.FS
import java.io.File
import java.security.KeyPair

class GitSshKeyTransportSetter: GitTransportSetter {
private val configCallback: TransportConfigCallback
private val context = App.getAppContext()

init {
val factory: SshSessionFactory = object : SshdSessionFactory(null, null) {
private val factory = object: SshdSessionFactory(null, null) {
private lateinit var session: SshdSession

override fun getHomeDirectory(): File { return context.filesDir }
override fun getDefaultPreferredAuthentications(): String { return "publickey" }

override fun getDefaultPreferredAuthentications(): String { return "publickey" }
override fun createServerKeyDatabase(
homeDir: File,
sshDir: File
): ServerKeyDatabase {
// We override this method because we want to set "askAboutNewFile" to False.
return OpenSshServerKeyDatabase(
false,
getDefaultKnownHostsFiles(sshDir)
)
}

override fun createServerKeyDatabase(
@NonNull homeDir: File,
@NonNull sshDir: File
): ServerKeyDatabase {
// We override this method because we want to set "askAboutNewFile" to False.
return OpenSshServerKeyDatabase(
false,
getDefaultKnownHostsFiles(sshDir)
)
@RequiresApi(Build.VERSION_CODES.N)
override fun getDefaultKeys(sshDir: File): Iterable<KeyPair>? {
return if (SshKey.exists) {
listOf(SshKey.getKeyPair())
} else {
SshKey.promptForKeyGeneration()
null
}
}

@RequiresApi(Build.VERSION_CODES.N)
override fun getDefaultKeys(@NonNull sshDir: File): Iterable<KeyPair>? {
return if (SshKey.exists) {
listOf(SshKey.getKeyPair())
} else {
SshKey.promptForKeyGeneration()
null
}
}
override fun releaseSession(session: RemoteSession) {
// Do nothing. We want to leave SSH sessions open.
}

override fun getSession(
uri: URIish?,
credentialsProvider: CredentialsProvider?,
fs: FS?,
tms: Int
): SshdSession {
if (this::session.isInitialized) { return session }
session = super.getSession(uri, credentialsProvider, fs, tms)
return session
}

fun disconnect() {
session.disconnect()
}
}

init {
SshSessionFactory.setInstance(factory)

// org.apache.sshd.common.config.keys.IdentityUtils freaks out if user.home is not set
Expand All @@ -59,10 +81,13 @@ class GitSshKeyTransportSetter: GitTransportSetter {
}
}

override fun close() {
factory.disconnect()
}

override fun setTransport(tc: TransportCommand<*, *>): TransportCommand<*, *> {
tc.setTransportConfigCallback(configCallback)
tc.setCredentialsProvider(SshCredentialsProvider())
return tc
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import org.eclipse.jgit.api.TransportCommand;

public interface GitTransportSetter {
public interface GitTransportSetter extends AutoCloseable {
public TransportCommand setTransport(TransportCommand tc);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public TransportCommand setTransport(TransportCommand tc) {
tc.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
return tc;
}

public void close() {
// Nothing to do here; the HTTPS transport does not use persistent connections.
}
}
Loading

0 comments on commit 0dc3f38

Please sign in to comment.