Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for different post/host in the ssh://... url in the web interface. #268

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/distrib/data/gitblit.properties
Expand Up @@ -110,6 +110,22 @@ git.sshPort = 29418
# RESTART REQUIRED
git.sshBindInterface =

# Specifies the ssh postname to publish in the web interface.
# You may specify a hostname if you're forwarding from a different
# hostname than web.canonicalUrl points to
#
# SINCE 1.6.3
# RESTART REQUIRED
git.sshDisplayHost =

# Specifies the ssh port to publish in the web interface.
# You may specify a port that has been forwarded to git.sshPort
# Recommended value: 22
#
# SINCE 1.6.3
# RESTART REQUIRED
git.sshDisplayPort =

# Specify the SSH key manager to use for retrieving, storing, and removing
# SSH keys.
#
Expand Down
Expand Up @@ -83,6 +83,8 @@ protected void processRequest(javax.servlet.http.HttpServletRequest request,
response.getWriter().append("SSH is not active on this server!");
return;
}
int sshDisplayPort = settings.getInteger(Keys.git.sshDisplayPort, sshPort);

// extract repo name from request
String repoUrl = request.getPathInfo().substring(1);

Expand All @@ -106,6 +108,10 @@ protected void processRequest(javax.servlet.http.HttpServletRequest request,
if (!StringUtils.isEmpty(url) && url.indexOf("localhost") == -1) {
host = new URL(url).getHost();
}
String sshDisplayHost = settings.getString(Keys.git.sshDisplayHost, "");
if(sshDisplayHost.isEmpty()) {
sshDisplayHost = host;
}

UserModel user;
if (StringUtils.isEmpty(username)) {
Expand Down Expand Up @@ -135,7 +141,7 @@ protected void processRequest(javax.servlet.http.HttpServletRequest request,
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
sb.append("<sparkleshare><invite>\n");
sb.append(MessageFormat.format("<address>ssh://{0}@{1}:{2,number,0}/</address>\n", user.username, host, sshPort));
sb.append(MessageFormat.format("<address>ssh://{0}@{1}:{2,number,0}/</address>\n", user.username, sshDisplayHost, sshDisplayPort));
sb.append(MessageFormat.format("<remote_path>/{0}</remote_path>\n", model.name));
int fanoutPort = settings.getInteger(Keys.fanout.port, 0);
if (fanoutPort > 0) {
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/gitblit/transport/ssh/SshDaemon.java
Expand Up @@ -143,14 +143,22 @@ public SshDaemon(IGitblit gitblit, WorkQueue workQueue) {
}

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);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/gitblit/transport/ssh/WelcomeShell.java
Expand Up @@ -200,13 +200,18 @@ private String getHostname() {
}

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);
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/gitblit/transport/ssh/commands/SshCommand.java
Expand Up @@ -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;
Expand Down Expand Up @@ -73,15 +74,20 @@ protected String getHostname() {

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);
}
}

Expand Down