From 8b63e0aaf044b36627e9ce02a1d73618e50700e4 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 10 Apr 2014 18:58:08 -0400
Subject: [PATCH] Listen for ssh clients on all interfaces, by default
---
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java | 92 +++++++++++++++++++++++++++++++++++++---------
1 files changed, 74 insertions(+), 18 deletions(-)
diff --git a/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
index 0d614a0..597b9ea 100644
--- a/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
+++ b/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
@@ -27,7 +27,12 @@
import org.apache.sshd.server.Environment;
import org.kohsuke.args4j.Argument;
+import com.gitblit.git.GitblitReceivePackFactory;
+import com.gitblit.git.GitblitUploadPackFactory;
+import com.gitblit.git.RepositoryResolver;
+import com.gitblit.transport.ssh.AbstractGitCommand;
import com.gitblit.transport.ssh.CommandMetaData;
+import com.gitblit.transport.ssh.SshSession;
import com.gitblit.utils.cli.SubcommandHandler;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
@@ -44,9 +49,17 @@
private Set<Class<? extends Command>> commands;
private Map<String, Class<? extends Command>> map;
+ private Map<String, Command> root;
public DispatchCommand() {
commands = new HashSet<Class<? extends Command>>();
+ }
+
+ public void registerDispatcher(String name, Command cmd) {
+ if (root == null) {
+ root = Maps.newHashMap();
+ }
+ root.put(name, cmd);
}
public void registerCommand(Class<? extends Command> cmd) {
@@ -78,20 +91,7 @@
throw new UnloggedFailure(1, msg.toString());
}
- final Class<? extends Command> c = getMap().get(commandName);
- if (c == null) {
- String msg =
- (getName().isEmpty() ? "Gitblit" : getName()) + ": "
- + commandName + ": not found";
- throw new UnloggedFailure(1, msg);
- }
-
- Command cmd = null;
- try {
- cmd = c.newInstance();
- } catch (Exception e) {
- throw new UnloggedFailure(1, MessageFormat.format("Failed to instantiate {0} command", commandName));
- }
+ Command cmd = getCommand();
if (cmd instanceof BaseCommand) {
BaseCommand bc = (BaseCommand) cmd;
if (getName().isEmpty()) {
@@ -100,11 +100,11 @@
bc.setName(getName() + " " + commandName);
}
bc.setArguments(args.toArray(new String[args.size()]));
- } else if (!args.isEmpty()) {
- throw new UnloggedFailure(1, commandName + " does not take arguments");
}
- provideStateTo(cmd);
+ provideBaseStateTo(cmd);
+ provideGitState(cmd);
+ reset();
//atomicCmd.set(cmd);
cmd.start(env);
@@ -119,8 +119,29 @@
}
}
+ private Command getCommand() throws UnloggedFailure {
+ if (root != null && root.containsKey(commandName)) {
+ return root.get(commandName);
+ }
+ final Class<? extends Command> c = getMap().get(commandName);
+ if (c == null) {
+ String msg =
+ (getName().isEmpty() ? "Gitblit" : getName()) + ": "
+ + commandName + ": not found";
+ throw new UnloggedFailure(1, msg);
+ }
+
+ Command cmd = null;
+ try {
+ cmd = c.newInstance();
+ } catch (Exception e) {
+ throw new UnloggedFailure(1, MessageFormat.format("Failed to instantiate {0} command", commandName));
+ }
+ return cmd;
+ }
+
@Override
-protected String usage() {
+ protected String usage() {
final StringBuilder usage = new StringBuilder();
usage.append("Available commands");
if (!getName().isEmpty()) {
@@ -157,4 +178,39 @@
usage.append("\n");
return usage.toString();
}
+
+ // This is needed because we are not using provider or
+ // clazz.newInstance() for DispatchCommand
+ private void reset() {
+ args = new ArrayList<String>();
+ }
+
+ private void provideGitState(Command cmd) {
+ if (cmd instanceof AbstractGitCommand) {
+ AbstractGitCommand a = (AbstractGitCommand) cmd;
+ a.setRepositoryResolver(repositoryResolver);
+ a.setUploadPackFactory(gitblitUploadPackFactory);
+ a.setReceivePackFactory(gitblitReceivePackFactory);
+ } else if (cmd instanceof DispatchCommand) {
+ DispatchCommand d = (DispatchCommand)cmd;
+ d.setRepositoryResolver(repositoryResolver);
+ d.setUploadPackFactory(gitblitUploadPackFactory);
+ d.setReceivePackFactory(gitblitReceivePackFactory);
+ }
+ }
+
+ private RepositoryResolver<SshSession> repositoryResolver;
+ public void setRepositoryResolver(RepositoryResolver<SshSession> repositoryResolver) {
+ this.repositoryResolver = repositoryResolver;
+ }
+
+ private GitblitUploadPackFactory<SshSession> gitblitUploadPackFactory;
+ public void setUploadPackFactory(GitblitUploadPackFactory<SshSession> gitblitUploadPackFactory) {
+ this.gitblitUploadPackFactory = gitblitUploadPackFactory;
+ }
+
+ private GitblitReceivePackFactory<SshSession> gitblitReceivePackFactory;
+ public void setReceivePackFactory(GitblitReceivePackFactory<SshSession> gitblitReceivePackFactory) {
+ this.gitblitReceivePackFactory = gitblitReceivePackFactory;
+ }
}
--
Gitblit v1.9.1