Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
21 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,30 @@ | ||
package giter8 | ||
|
||
import java.nio.file.{Files, Paths} | ||
import org.eclipse.jgit.transport.sshd.SshdSessionFactory | ||
import java.{util => ju} | ||
import java.io.File | ||
import java.nio.file.{Path, Files, Paths} | ||
import scala.collection.JavaConverters._ | ||
|
||
import com.jcraft.jsch.agentproxy.{ConnectorFactory, RemoteIdentityRepository} | ||
import com.jcraft.jsch.{JSch, Session} | ||
import org.eclipse.jgit.transport.{JschConfigSessionFactory, OpenSshConfig} | ||
import org.eclipse.jgit.util.FS | ||
class SshAgentSessionFactory(knownHosts: Option[String]) extends SshdSessionFactory() { | ||
|
||
class SshAgentSessionFactory(knownHosts: Option[String]) extends JschConfigSessionFactory() { | ||
private val home = Option(System.getProperty("user.home")).getOrElse("") | ||
private val programData = Option(System.getenv("PROGRAMDATA")).getOrElse("") | ||
|
||
private val Home = Option(System.getProperty("user.home")).getOrElse("") | ||
private val ProgramData = Option(System.getenv("PROGRAMDATA")).getOrElse("") | ||
|
||
private val KnownHostsPaths: List[String] = List( | ||
s"$Home/.ssh/known_hosts", | ||
private val defaultKnownHosts: List[String] = List( | ||
s"$home/.ssh/known_hosts", | ||
s"$home/.ssh/known_hosts2", | ||
"/etc/ssh/ssh_known_hosts", | ||
s"$ProgramData/ssh/ssh_known_hosts" | ||
s"$programData/ssh/ssh_known_hosts" | ||
) | ||
|
||
override protected def configure(host: OpenSshConfig.Host, session: Session): Unit = {} | ||
|
||
override protected def createDefaultJSch(fs: FS): JSch = { | ||
val jsch = new JSch() | ||
knownHostsWithDefault.foreach(jsch.setKnownHosts) | ||
jsch.setIdentityRepository(identityRepository) | ||
jsch | ||
override def getDefaultKnownHostsFiles(sshDir: File): ju.List[Path] = { | ||
knownHosts | ||
.map(_ :: Nil) | ||
.getOrElse(defaultKnownHosts) | ||
.map(Paths.get(_)) | ||
.filter(Files.exists(_)) | ||
.asJava | ||
} | ||
|
||
private def knownHostsWithDefault = { | ||
knownHosts.orElse(KnownHostsPaths.find(path => Files.exists(Paths.get(path)))) | ||
} | ||
|
||
private def identityRepository = { | ||
val factory = ConnectorFactory.getDefault | ||
val connector = factory.createConnector | ||
new RemoteIdentityRepository(connector) | ||
} | ||
} |