| | |
| | | package com.gitblit.manager; |
| | | |
| | | import java.io.IOException; |
| | | import java.net.URI; |
| | | import java.text.MessageFormat; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | |
| | | import java.util.concurrent.ScheduledExecutorService; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import javax.inject.Named; |
| | | import javax.inject.Singleton; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | import org.apache.sshd.server.Command; |
| | | import org.eclipse.jgit.transport.resolver.ReceivePackFactory; |
| | | import org.eclipse.jgit.transport.resolver.UploadPackFactory; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | |
| | | import com.gitblit.fanout.FanoutNioService; |
| | | import com.gitblit.fanout.FanoutService; |
| | | import com.gitblit.fanout.FanoutSocketService; |
| | | import com.gitblit.git.GitDaemon; |
| | | import com.gitblit.git.GitblitReceivePackFactory; |
| | | import com.gitblit.git.GitblitUploadPackFactory; |
| | | import com.gitblit.git.RepositoryResolver; |
| | | import com.gitblit.models.FederationModel; |
| | | import com.gitblit.models.RepositoryModel; |
| | | import com.gitblit.models.UserModel; |
| | | import com.gitblit.service.FederationPullService; |
| | | import com.gitblit.transport.ssh.SshCommandFactory; |
| | | import com.gitblit.transport.git.GitDaemon; |
| | | import com.gitblit.transport.ssh.SshDaemon; |
| | | import com.gitblit.transport.ssh.SshSession; |
| | | import com.gitblit.transport.ssh.commands.CreateRepository; |
| | | import com.gitblit.transport.ssh.commands.VersionCommand; |
| | | import com.gitblit.utils.IdGenerator; |
| | | import com.gitblit.utils.StringUtils; |
| | | import com.gitblit.utils.TimeUtils; |
| | | |
| | | import dagger.Module; |
| | | import dagger.ObjectGraph; |
| | | import dagger.Provides; |
| | | |
| | | /** |
| | | * Services manager manages long-running services/processes that either have no |
| | |
| | | } |
| | | if (gitDaemon != null) { |
| | | gitDaemon.stop(); |
| | | } |
| | | if (sshDaemon != null) { |
| | | sshDaemon.stop(); |
| | | } |
| | | return this; |
| | | } |
| | |
| | | String bindInterface = settings.getString(Keys.git.sshBindInterface, "localhost"); |
| | | if (port > 0) { |
| | | try { |
| | | sshDaemon = ObjectGraph.create(new SshModule()).get(SshDaemon.class); |
| | | sshDaemon = new SshDaemon(gitblit, new IdGenerator()); |
| | | sshDaemon.start(); |
| | | } catch (IOException e) { |
| | | sshDaemon = null; |
| | |
| | | return null; |
| | | } |
| | | if (user.canClone(repository)) { |
| | | String servername = request.getServerName(); |
| | | String url = gitDaemon.formatUrl(servername, repository.name); |
| | | String hostname = getHostname(request); |
| | | String url = gitDaemon.formatUrl(hostname, repository.name); |
| | | return url; |
| | | } |
| | | } |
| | |
| | | return AccessPermission.NONE; |
| | | } |
| | | |
| | | public String getSshDaemonUrl(HttpServletRequest request, UserModel user, RepositoryModel repository) { |
| | | if (sshDaemon != null) { |
| | | String bindInterface = settings.getString(Keys.git.sshBindInterface, "localhost"); |
| | | if (bindInterface.equals("localhost") |
| | | && (!request.getServerName().equals("localhost") && !request.getServerName().equals("127.0.0.1"))) { |
| | | // ssh daemon is bound to localhost and the request is from elsewhere |
| | | return null; |
| | | } |
| | | if (user.canClone(repository)) { |
| | | String hostname = getHostname(request); |
| | | String url = sshDaemon.formatUrl(user.username, hostname, repository.name); |
| | | return url; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public AccessPermission getSshDaemonAccessPermission(UserModel user, RepositoryModel repository) { |
| | | if (sshDaemon != null && user.canClone(repository)) { |
| | | AccessPermission sshDaemonPermission = user.getRepositoryPermission(repository).permission; |
| | | if (sshDaemonPermission.atLeast(AccessPermission.CLONE)) { |
| | | if (repository.accessRestriction.atLeast(AccessRestrictionType.CLONE)) { |
| | | // can not authenticate clone via anonymous ssh protocol |
| | | sshDaemonPermission = AccessPermission.NONE; |
| | | } else if (repository.accessRestriction.atLeast(AccessRestrictionType.PUSH)) { |
| | | // can not authenticate push via anonymous ssh protocol |
| | | sshDaemonPermission = AccessPermission.CLONE; |
| | | } else { |
| | | // normal user permission |
| | | } |
| | | } |
| | | return sshDaemonPermission; |
| | | } |
| | | return AccessPermission.NONE; |
| | | } |
| | | |
| | | /** |
| | | * Extract the hostname from the canonical url or return the |
| | | * hostname from the servlet request. |
| | | * |
| | | * @param request |
| | | * @return |
| | | */ |
| | | protected String getHostname(HttpServletRequest request) { |
| | | String hostname = request.getServerName(); |
| | | String canonicalUrl = gitblit.getSettings().getString(Keys.web.canonicalUrl, null); |
| | | if (!StringUtils.isEmpty(canonicalUrl)) { |
| | | try { |
| | | URI uri = new URI(canonicalUrl); |
| | | String host = uri.getHost(); |
| | | if (!StringUtils.isEmpty(host) && !"localhost".equals(host)) { |
| | | hostname = host; |
| | | } |
| | | } catch (Exception e) { |
| | | } |
| | | } |
| | | return hostname; |
| | | } |
| | | |
| | | private class FederationPuller extends FederationPullService { |
| | | |
| | |
| | | "Next pull of {0} @ {1} scheduled for {2,date,yyyy-MM-dd HH:mm}", |
| | | registration.name, registration.url, registration.nextPull)); |
| | | } |
| | | |
| | | } |
| | | |
| | | @Module(library = true, |
| | | injects = { |
| | | IGitblit.class, |
| | | SshCommandFactory.class, |
| | | SshDaemon.class, |
| | | }) |
| | | public class SshModule { |
| | | @Provides @Named("create-repository") Command provideCreateRepository() { |
| | | return new CreateRepository(); |
| | | } |
| | | |
| | | @Provides @Named("version") Command provideVersion() { |
| | | return new VersionCommand(); |
| | | } |
| | | |
| | | @Provides @Singleton IdGenerator provideIdGenerator() { |
| | | return new IdGenerator(); |
| | | } |
| | | |
| | | @Provides @Singleton RepositoryResolver<SshSession> provideRepositoryResolver() { |
| | | return new RepositoryResolver<SshSession>(provideGitblit()); |
| | | } |
| | | |
| | | @Provides @Singleton UploadPackFactory<SshSession> provideUploadPackFactory() { |
| | | return new GitblitUploadPackFactory<SshSession>(provideGitblit()); |
| | | } |
| | | |
| | | @Provides @Singleton ReceivePackFactory<SshSession> provideReceivePackFactory() { |
| | | return new GitblitReceivePackFactory<SshSession>(provideGitblit()); |
| | | } |
| | | |
| | | @Provides @Singleton IGitblit provideGitblit() { |
| | | return ServicesManager.this.gitblit; |
| | | } |
| | | } |
| | | } |