]> source.dussan.org Git - gitblit.git/commitdiff
Added git.sshDisplay{Port|Host} to hide port forward.
authorMorten Bøgeskov <source@bogeskov.dk>
Thu, 4 Jun 2015 06:24:34 +0000 (08:24 +0200)
committerJames Moger <james.moger@gitblit.com>
Mon, 15 Jun 2015 16:51:52 +0000 (12:51 -0400)
Running gitblit in a container it's easy to expose the ssh on the default port.
Using git.sshDisplayPort/git.sshDisplayHost you can expose the forwarded address
as the official location.

src/main/distrib/data/defaults.properties
src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java
src/main/java/com/gitblit/transport/ssh/SshDaemon.java
src/main/java/com/gitblit/transport/ssh/WelcomeShell.java
src/main/java/com/gitblit/transport/ssh/commands/SshCommand.java

index cc42d907156877901e3f958f53aec282da8637fa..272eec0864fca967ddf021ce9f94fb0cbda07e2f 100644 (file)
@@ -112,6 +112,18 @@ git.sshPort = 29418
 # RESTART REQUIRED
 git.sshBindInterface = 
 
+# Manually specify the hostname to use in advertised SSH repository urls.
+# This may be useful in complex forwarding setups.
+#
+# SINCE 1.7.0
+git.sshDisplayHost = 
+
+# Manually specify the port to use in advertised SSH repository urls.
+# This may be useful in complex forwarding setups.
+#
+# SINCE 1.7.0
+git.sshDisplayPort = 
+
 # Specify the SSH key manager to use for retrieving, storing, and removing
 # SSH keys.
 #
index 69c254cb1f5b5a6b78ea5166c6dab37dd09e5f8e..d2ddc2c8bf63d886a6a6350f5523ae301db28fc0 100644 (file)
@@ -89,6 +89,8 @@ public class SparkleShareInviteServlet extends HttpServlet {
                        response.getWriter().append("SSH is not active on this server!");\r
                        return;\r
                }\r
+               int sshDisplayPort = settings.getInteger(Keys.git.sshDisplayPort, sshPort);\r
+\r
                // extract repo name from request\r
                String repoUrl = request.getPathInfo().substring(1);\r
 \r
@@ -112,6 +114,10 @@ public class SparkleShareInviteServlet extends HttpServlet {
                if (!StringUtils.isEmpty(url) && url.indexOf("localhost") == -1) {\r
                        host = new URL(url).getHost();\r
                }\r
+               String sshDisplayHost = settings.getString(Keys.git.sshDisplayHost, "");\r
+               if(sshDisplayHost.isEmpty()) {\r
+                       sshDisplayHost = host;\r
+               }\r
 \r
                UserModel user;\r
                if (StringUtils.isEmpty(username)) {\r
@@ -141,7 +147,7 @@ public class SparkleShareInviteServlet extends HttpServlet {
                StringBuilder sb = new StringBuilder();\r
                sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");\r
                sb.append("<sparkleshare><invite>\n");\r
-               sb.append(MessageFormat.format("<address>ssh://{0}@{1}:{2,number,0}/</address>\n", user.username, host, sshPort));\r
+               sb.append(MessageFormat.format("<address>ssh://{0}@{1}:{2,number,0}/</address>\n", user.username, sshDisplayHost, sshDisplayPort));\r
                sb.append(MessageFormat.format("<remote_path>/{0}</remote_path>\n", model.name));\r
                int fanoutPort = settings.getInteger(Keys.fanout.port, 0);\r
                if (fanoutPort > 0) {\r
index 0ff5c28485de9b80d9ff228012397bf2e5136b96..4756d967407b2c299136a277ef9a4652726e9667 100644 (file)
@@ -200,14 +200,22 @@ public class SshDaemon {
        }
 
        public String formatUrl(String gituser, String servername, String repository) {
-               if (sshd.getPort() == DEFAULT_PORT) {
+               IStoredSettings settings = gitblit.getSettings();
+
+               int port = sshd.getPort();
+               int displayPort = settings.getInteger(Keys.git.sshDisplayPort, port);
+               String displayServername = settings.getString(Keys.git.sshDisplayHost, "");
+               if(displayServername.isEmpty()) {
+                       displayServername = servername;
+               }
+               if (displayPort == DEFAULT_PORT) {
                        // standard port
-                       return MessageFormat.format("ssh://{0}@{1}/{2}", gituser, servername,
+                       return MessageFormat.format("ssh://{0}@{1}/{2}", gituser, displayServername,
                                        repository);
                } else {
                        // non-standard port
                        return MessageFormat.format("ssh://{0}@{1}:{2,number,0}/{3}",
-                                       gituser, servername, sshd.getPort(), repository);
+                                       gituser, displayServername, displayPort, repository);
                }
        }
 
index 852756a70776aa9642b41d2677b1ec178bbb2cbd..267082ac0a9ff1dc4fd450d7eef797b95dbcc289 100644 (file)
@@ -200,13 +200,18 @@ public class WelcomeShell implements Factory<Command> {
                }
 
                private String formatUrl(String hostname, int port, String username) {
-                       if (port == 22) {
+                       int displayPort = settings.getInteger(Keys.git.sshDisplayPort, port);
+                       String displayHostname = settings.getString(Keys.git.sshDisplayHost, "");
+                       if(displayHostname.isEmpty()) {
+                               displayHostname = hostname;
+                       }
+                       if (displayPort == 22) {
                                // standard port
-                               return MessageFormat.format("{0}@{1}/REPOSITORY.git", username, hostname);
+                               return MessageFormat.format("{0}@{1}/REPOSITORY.git", username, displayHostname);
                        } else {
                                // non-standard port
                                return MessageFormat.format("ssh://{0}@{1}:{2,number,0}/REPOSITORY.git",
-                                               username, hostname, port);
+                                               username, displayHostname, displayPort);
                        }
                }
        }
index f18b99bc2dd67d7dd1c21aaf0bfcd3bf359a6061..06b0e8d8850d1032767100a028b416456abd5a4a 100644 (file)
@@ -16,6 +16,7 @@
  */
 package com.gitblit.transport.ssh.commands;
 
+import com.gitblit.IStoredSettings;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.net.MalformedURLException;
@@ -73,15 +74,20 @@ public abstract class SshCommand extends BaseCommand {
 
        protected String getRepositoryUrl(String repository) {
                String username = getContext().getClient().getUsername();
-               String hostname = getHostname();
-               int port = getContext().getGitblit().getSettings().getInteger(Keys.git.sshPort, 0);
-               if (port == 22) {
+               IStoredSettings settings = getContext().getGitblit().getSettings();
+               String displayHostname = settings.getString(Keys.git.sshDisplayHost, "");
+               if(displayHostname.isEmpty()) {
+                       displayHostname = getHostname();
+               }
+               int port = settings.getInteger(Keys.git.sshPort, 0);
+               int displayPort = settings.getInteger(Keys.git.sshDisplayPort, port);
+               if (displayPort == 22) {
                        // standard port
-                       return MessageFormat.format("{0}@{1}/{2}.git", username, hostname, repository);
+                       return MessageFormat.format("{0}@{1}/{2}.git", username, displayHostname, repository);
                } else {
                        // non-standard port
                        return MessageFormat.format("ssh://{0}@{1}:{2,number,0}/{3}",
-                                       username, hostname, port, repository);
+                                       username, displayHostname, displayPort, repository);
                }
        }