(refs #115)Start and stop sshd at the system settings

This commit is contained in:
takezoe
2014-03-10 00:44:25 +09:00
parent 525edbab80
commit 845f2d6faa
5 changed files with 66 additions and 19 deletions

View File

@@ -82,7 +82,7 @@ abstract class GitCommand(val command: String) extends Command {
}
}
class GitUploadPack(override val command: String) extends GitCommand(command: String) {
class GitUploadPack(context: ServletContext, override val command: String) extends GitCommand(command: String) {
override protected def runTask(user: String): Unit = {
using(Git.open(getRepositoryDir(owner, repositoryName))) {
@@ -119,7 +119,7 @@ class GitCommandFactory(context: ServletContext) extends CommandFactory {
override def createCommand(command: String): Command = {
logger.debug(s"command: $command")
command match {
case GitCommand.CommandRegex("upload", owner, repoName) => new GitUploadPack(command)
case GitCommand.CommandRegex("upload", owner, repoName) => new GitUploadPack(context, command)
case GitCommand.CommandRegex("receive", owner, repoName) => new GitReceivePack(context, command)
case _ => new UnknownCommand(command)
}

View File

@@ -4,36 +4,36 @@ import javax.servlet.{ServletContext, ServletContextEvent, ServletContextListene
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider
import org.slf4j.LoggerFactory
import util.Directory
import service.SystemSettingsService
import java.util.concurrent.atomic.AtomicBoolean
object SshServer {
private val logger = LoggerFactory.getLogger(SshServer.getClass)
val DEFAULT_PORT: Int = 29418
// TODO read from config
val SSH_SERVICE_ENABLE = true // TODO read from config
private val server = org.apache.sshd.SshServer.setUpDefaultServer()
private val active = new AtomicBoolean(false);
private def configure(context: ServletContext) = {
server.setPort(DEFAULT_PORT) // TODO read from config
private def configure(context: ServletContext, port: Int) = {
server.setPort(port)
server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(s"${Directory.GitBucketHome}/gitbucket.ser"))
server.setPublickeyAuthenticator(new PublicKeyAuthenticator(context))
server.setCommandFactory(new GitCommandFactory(context))
}
def start(context: ServletContext) = this.synchronized {
if (SSH_SERVICE_ENABLE) {
configure(context)
def start(context: ServletContext, port: Int) = {
if(active.compareAndSet(false, true)){
configure(context, port)
server.start()
logger.info(s"Start SSH Server Listen on ${server.getPort}")
}
}
def stop() = {
server.stop(true)
if(active.compareAndSet(true, false)){
server.stop(true)
}
}
def isActive = active.get
}
/*
@@ -41,16 +41,20 @@ object SshServer {
*
* How to use:
* git clone ssh://username@host_or_ip:29418/owner/repository_name.git
*
*/
class SshServerListener extends ServletContextListener {
class SshServerListener extends ServletContextListener with SystemSettingsService {
override def contextInitialized(sce: ServletContextEvent): Unit = {
SshServer.start(sce.getServletContext)
val settings = loadSystemSettings()
if(settings.ssh){
SshServer.start(sce.getServletContext, settings.sshPort.getOrElse(SystemSettingsService.DefaultSshPort))
}
}
override def contextDestroyed(sce: ServletContextEvent): Unit = {
SshServer.stop()
if(loadSystemSettings().ssh){
SshServer.stop()
}
}
}