diff --git a/android/app/src/main/java/com/example/journal/CustomSshSessionFactory.java b/android/app/src/main/java/com/example/journal/CustomSshSessionFactory.java new file mode 100644 index 00000000..591d0abc --- /dev/null +++ b/android/app/src/main/java/com/example/journal/CustomSshSessionFactory.java @@ -0,0 +1,58 @@ +package com.example.journal; + +import android.util.Log; + +import com.jcraft.jsch.JSch; +import com.jcraft.jsch.JSchException; +import com.jcraft.jsch.Session; + +import org.eclipse.jgit.transport.JschConfigSessionFactory; +import org.eclipse.jgit.transport.OpenSshConfig; +import org.eclipse.jgit.util.FS; + +public class CustomSshSessionFactory extends JschConfigSessionFactory { + private String privateKeyPath; + + public CustomSshSessionFactory(String privateKeyPath_) { + privateKeyPath = privateKeyPath_; + } + + protected void configure(OpenSshConfig.Host host, Session session) { + session.setConfig("StrictHostKeyChecking", "no"); + } + + protected JSch createDefaultJSch(FS fs) throws JSchException { + + class MyLogger implements com.jcraft.jsch.Logger { + java.util.Hashtable name; + + MyLogger() { + name = new java.util.Hashtable(); + name.put(new Integer(DEBUG), "DEBUG: "); + name.put(new Integer(INFO), "INFO: "); + name.put(new Integer(WARN), "WARN: "); + name.put(new Integer(ERROR), "ERROR: "); + name.put(new Integer(FATAL), "FATAL: "); + } + + + public boolean isEnabled(int level) { + return true; + } + + public void log(int level, String message) { + System.err.print(name.get(new Integer(level))); + System.err.println(message); + } + } + JSch.setLogger(new MyLogger()); + + JSch defaultJSch = super.createDefaultJSch(fs); + defaultJSch.addIdentity(privateKeyPath); + + JSch.setConfig("PreferredAuthentications", "publickey"); + + Log.d("identityNames", defaultJSch.getIdentityNames().toString()); + return defaultJSch; + } +} diff --git a/android/app/src/main/java/com/example/journal/GitCloneTask.java b/android/app/src/main/java/com/example/journal/GitCloneTask.java index 94bb048e..37fc18e2 100644 --- a/android/app/src/main/java/com/example/journal/GitCloneTask.java +++ b/android/app/src/main/java/com/example/journal/GitCloneTask.java @@ -11,18 +11,9 @@ import org.eclipse.jgit.api.errors.TransportException; import org.eclipse.jgit.transport.Transport; import org.eclipse.jgit.transport.SshTransport; -import org.eclipse.jgit.transport.JschConfigSessionFactory; -import org.eclipse.jgit.transport.SshSessionFactory; -import org.eclipse.jgit.transport.OpenSshConfig.Host; -import org.eclipse.jgit.util.FS; - import org.eclipse.jgit.lib.TextProgressMonitor; import java.io.PrintWriter; - -import com.jcraft.jsch.Session; -import com.jcraft.jsch.*; - import java.io.File; import io.flutter.plugin.common.MethodChannel.Result; @@ -43,47 +34,6 @@ public class GitCloneTask extends AsyncTask { Log.d("GitClone Directory", cloneDirPath); try { - final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() { - protected void configure(Host host, Session session) { - session.setConfig("StrictHostKeyChecking", "no"); - } - - protected JSch createDefaultJSch(FS fs) throws JSchException { - - class MyLogger implements com.jcraft.jsch.Logger { - java.util.Hashtable name; - - MyLogger() { - name = new java.util.Hashtable(); - name.put(new Integer(DEBUG), "DEBUG: "); - name.put(new Integer(INFO), "INFO: "); - name.put(new Integer(WARN), "WARN: "); - name.put(new Integer(ERROR), "ERROR: "); - name.put(new Integer(FATAL), "FATAL: "); - } - - - public boolean isEnabled(int level) { - return true; - } - - public void log(int level, String message) { - System.err.print(name.get(new Integer(level))); - System.err.println(message); - } - } - JSch.setLogger(new MyLogger()); - - JSch defaultJSch = super.createDefaultJSch(fs); - defaultJSch.addIdentity(privateKeyPath); - - JSch.setConfig("PreferredAuthentications", "publickey"); - - Log.d("identityNames", defaultJSch.getIdentityNames().toString()); - return defaultJSch; - } - }; - CloneCommand cloneCommand = Git.cloneRepository() .setURI(url) .setDirectory(cloneDir) @@ -93,7 +43,7 @@ public class GitCloneTask extends AsyncTask { @Override public void configure(Transport transport) { SshTransport sshTransport = (SshTransport) transport; - sshTransport.setSshSessionFactory(sshSessionFactory); + sshTransport.setSshSessionFactory(new CustomSshSessionFactory(privateKeyPath)); } }); diff --git a/android/app/src/main/java/com/example/journal/GitPullTask.java b/android/app/src/main/java/com/example/journal/GitPullTask.java index 901a7613..8b2aca27 100644 --- a/android/app/src/main/java/com/example/journal/GitPullTask.java +++ b/android/app/src/main/java/com/example/journal/GitPullTask.java @@ -5,7 +5,6 @@ import android.util.Log; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; -import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.api.PullCommand; import org.eclipse.jgit.api.TransportConfigCallback; @@ -13,14 +12,6 @@ import org.eclipse.jgit.api.errors.TransportException; import org.eclipse.jgit.transport.Transport; import org.eclipse.jgit.transport.SshTransport; -import org.eclipse.jgit.transport.JschConfigSessionFactory; -import org.eclipse.jgit.transport.SshSessionFactory; -import org.eclipse.jgit.transport.OpenSshConfig.Host; -import org.eclipse.jgit.util.FS; - -import com.jcraft.jsch.Session; -import com.jcraft.jsch.*; - import java.io.File; import io.flutter.plugin.common.MethodChannel.Result; @@ -41,47 +32,6 @@ public class GitPullTask extends AsyncTask { Log.d("GitClone Directory", cloneDirPath); try { - final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() { - protected void configure(Host host, Session session) { - session.setConfig("StrictHostKeyChecking", "no"); - } - - protected JSch createDefaultJSch(FS fs) throws JSchException { - - class MyLogger implements com.jcraft.jsch.Logger { - java.util.Hashtable name; - - MyLogger() { - name = new java.util.Hashtable(); - name.put(new Integer(DEBUG), "DEBUG: "); - name.put(new Integer(INFO), "INFO: "); - name.put(new Integer(WARN), "WARN: "); - name.put(new Integer(ERROR), "ERROR: "); - name.put(new Integer(FATAL), "FATAL: "); - } - - - public boolean isEnabled(int level) { - return true; - } - - public void log(int level, String message) { - System.err.print(name.get(new Integer(level))); - System.err.println(message); - } - } - JSch.setLogger(new MyLogger()); - - JSch defaultJSch = super.createDefaultJSch(fs); - defaultJSch.addIdentity(privateKeyPath); - - JSch.setConfig("PreferredAuthentications", "publickey"); - - Log.d("identityNames", defaultJSch.getIdentityNames().toString()); - return defaultJSch; - } - }; - Git git = Git.open(cloneDir); PullCommand pullCommand = git.pull(); @@ -89,7 +39,7 @@ public class GitPullTask extends AsyncTask { @Override public void configure(Transport transport) { SshTransport sshTransport = (SshTransport) transport; - sshTransport.setSshSessionFactory(sshSessionFactory); + sshTransport.setSshSessionFactory(new CustomSshSessionFactory(privateKeyPath)); } });