From 53269096a6823628a54bb850ba8db6cca063529b Mon Sep 17 00:00:00 2001 From: Jiri Tyr Date: Mon, 21 Oct 2013 14:32:53 +0100 Subject: [PATCH 1/3] Allow to force HTTPS scheme If the standalone GitBucket instance runs behind HTTPS proxy, the repository URL always shows HTTP scheme dispute the fact that the connection is HTTPS. This patch is adding a command line option which allows to force the HTTPS scheme. --- build.xml | 2 +- contrib/redhat/gitbucket.conf | 3 +++ contrib/redhat/gitbucket.init | 3 +++ src/main/java/JettyLauncher.java | 38 +++++++++++++++++++++++++------- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/build.xml b/build.xml index 187359e51..d39324e99 100644 --- a/build.xml +++ b/build.xml @@ -46,7 +46,7 @@ + includes="JettyLauncher.class,SslConnector.class"/> diff --git a/contrib/redhat/gitbucket.conf b/contrib/redhat/gitbucket.conf index fb14ebe9c..26e4201fa 100644 --- a/contrib/redhat/gitbucket.conf +++ b/contrib/redhat/gitbucket.conf @@ -1,6 +1,9 @@ # Server port #GITBUCKET_PORT=8080 +# Force HTTPS scheme +#GITBUCKET_HTTPS=false + # Data directory (GITBUCKET_HOME/gitbucket) #GITBUCKET_HOME=/var/lib/gitbucket diff --git a/contrib/redhat/gitbucket.init b/contrib/redhat/gitbucket.init index 6ab01f850..390318cd1 100644 --- a/contrib/redhat/gitbucket.init +++ b/contrib/redhat/gitbucket.init @@ -35,6 +35,9 @@ start() { if [ $GITBUCKET_PREFIX ]; then START_OPTS="${START_OPTS} --prefix ${GITBUCKET_PREFIX}" fi + if [ $GITBUCKET_HTTPS ]; then + START_OPTS="${START_OPTS} --https=true" + fi # Run the Java process GITBUCKET_HOME="${GITBUCKET_HOME}" java $GITBUCKET_JVM_OPTS -jar $GITBUCKET_WAR_FILE $START_OPTS >>$LOG_FILE 2>&1 & diff --git a/src/main/java/JettyLauncher.java b/src/main/java/JettyLauncher.java index 356da213b..c56398b98 100644 --- a/src/main/java/JettyLauncher.java +++ b/src/main/java/JettyLauncher.java @@ -1,7 +1,10 @@ +import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.webapp.WebAppContext; +import java.io.IOException; import java.net.URL; import java.security.ProtectionDomain; @@ -10,17 +13,20 @@ public class JettyLauncher { String host = null; int port = 8080; String contextPath = "/"; + boolean httpsScheme = false; - for(String arg: args){ - if(arg.startsWith("--") && arg.contains("=")){ + for(String arg: args) { + if(arg.startsWith("--") && arg.contains("=")) { String[] dim = arg.split("="); - if(dim.length >= 2){ - if(dim[0].equals("--host")){ + if(dim.length >= 2) { + if(dim[0].equals("--host")) { host = dim[1]; - } else if(dim[0].equals("--port")){ + } else if(dim[0].equals("--port")) { port = Integer.parseInt(dim[1]); - } else if(dim[0].equals("--prefix")){ + } else if(dim[0].equals("--prefix")) { contextPath = dim[1]; + } else if(dim[0].equals("--https") && (dim[1].equals("1") || dim[1].equals("true"))) { + httpsScheme = true; } } } @@ -28,8 +34,8 @@ public class JettyLauncher { Server server = new Server(); - SelectChannelConnector connector = new SelectChannelConnector(); - if(host != null){ + SslConnector connector = new SslConnector(httpsScheme); + if(host != null) { connector.setHost(host); } connector.setMaxIdleTime(1000 * 60 * 60); @@ -51,3 +57,19 @@ public class JettyLauncher { server.join(); } } + +class SslConnector extends SelectChannelConnector { + boolean myHttpsScheme; + + public SslConnector(boolean httpsScheme) { + myHttpsScheme = httpsScheme; + } + + @Override + public void customize(final EndPoint endpoint, final Request request) throws IOException { + if (myHttpsScheme) { + request.setScheme("https"); + super.customize(endpoint, request); + } + } +} From 2e239d16d44c43276aaf9ecc0ac1a6731e568428 Mon Sep 17 00:00:00 2001 From: Jiri Tyr Date: Mon, 21 Oct 2013 22:45:34 +0100 Subject: [PATCH 2/3] Refactorization of the https command line option Renaming the flag variable and the Connector class. --- build.xml | 2 +- src/main/java/JettyLauncher.java | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/build.xml b/build.xml index d39324e99..de766bd8e 100644 --- a/build.xml +++ b/build.xml @@ -46,7 +46,7 @@ + includes="JettyLauncher.class,CustomConnector.class"/> diff --git a/src/main/java/JettyLauncher.java b/src/main/java/JettyLauncher.java index c56398b98..5edba71c8 100644 --- a/src/main/java/JettyLauncher.java +++ b/src/main/java/JettyLauncher.java @@ -13,7 +13,7 @@ public class JettyLauncher { String host = null; int port = 8080; String contextPath = "/"; - boolean httpsScheme = false; + boolean forceHttps = false; for(String arg: args) { if(arg.startsWith("--") && arg.contains("=")) { @@ -26,7 +26,7 @@ public class JettyLauncher { } else if(dim[0].equals("--prefix")) { contextPath = dim[1]; } else if(dim[0].equals("--https") && (dim[1].equals("1") || dim[1].equals("true"))) { - httpsScheme = true; + forceHttps = true; } } } @@ -34,7 +34,7 @@ public class JettyLauncher { Server server = new Server(); - SslConnector connector = new SslConnector(httpsScheme); + CustomConnector connector = new CustomConnector(forceHttps); if(host != null) { connector.setHost(host); } @@ -58,16 +58,16 @@ public class JettyLauncher { } } -class SslConnector extends SelectChannelConnector { - boolean myHttpsScheme; +class CustomConnector extends SelectChannelConnector { + boolean mForceHttps; - public SslConnector(boolean httpsScheme) { - myHttpsScheme = httpsScheme; + public CustomConnector(boolean forceHttps) { + mForceHttps = forceHttps; } @Override public void customize(final EndPoint endpoint, final Request request) throws IOException { - if (myHttpsScheme) { + if (mForceHttps) { request.setScheme("https"); super.customize(endpoint, request); } From 99517fa5087f9722ebf87e448b34f94ce6955da2 Mon Sep 17 00:00:00 2001 From: Jiri Tyr Date: Mon, 21 Oct 2013 22:47:27 +0100 Subject: [PATCH 3/3] Fixing command line options in init.d script Adding missing "--host" option and fixing the declaration of other command line options. --- contrib/redhat/gitbucket.conf | 3 +++ contrib/redhat/gitbucket.init | 10 +++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/contrib/redhat/gitbucket.conf b/contrib/redhat/gitbucket.conf index 26e4201fa..c3959f36e 100644 --- a/contrib/redhat/gitbucket.conf +++ b/contrib/redhat/gitbucket.conf @@ -1,3 +1,6 @@ +# Bind host +#GITBUCKET_HOST=0.0.0.0 + # Server port #GITBUCKET_PORT=8080 diff --git a/contrib/redhat/gitbucket.init b/contrib/redhat/gitbucket.init index 390318cd1..3aed80262 100644 --- a/contrib/redhat/gitbucket.init +++ b/contrib/redhat/gitbucket.init @@ -12,7 +12,6 @@ . /etc/rc.d/init.d/functions # Default values -GITBUCKET_PORT=8080 GITBUCKET_HOME=/var/lib/gitbucket GITBUCKET_WAR_FILE=/usr/share/gitbucket/lib/gitbucket.war @@ -31,9 +30,14 @@ start() { echo -n $"Starting GitBucket server: " # Compile statup parameters - START_OPTS="--port=${GITBUCKET_PORT}" + if [ $GITBUCKET_PORT ]; then + START_OPTS="${START_OPTS} --port=${GITBUCKET_PORT}" + fi if [ $GITBUCKET_PREFIX ]; then - START_OPTS="${START_OPTS} --prefix ${GITBUCKET_PREFIX}" + START_OPTS="${START_OPTS} --prefix=${GITBUCKET_PREFIX}" + fi + if [ $GITBUCKET_HOST ]; then + START_OPTS="${START_OPTS} --host=${GITBUCKET_HOST}" fi if [ $GITBUCKET_HTTPS ]; then START_OPTS="${START_OPTS} --https=true"