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 c5f24ea
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 516 deletions.
314 changes: 18 additions & 296 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 @@ -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 c5f24ea

Please sign in to comment.