| | |
| | | import java.net.URL;
|
| | | import java.net.UnknownHostException;
|
| | | import java.security.ProtectionDomain;
|
| | | import java.text.MessageFormat;
|
| | | import java.util.ArrayList;
|
| | | import java.util.List;
|
| | |
|
| | |
| | | import org.eclipse.jetty.http.security.Constraint;
|
| | | import org.eclipse.jetty.security.ConstraintMapping;
|
| | | import org.eclipse.jetty.security.ConstraintSecurityHandler;
|
| | | import org.eclipse.jetty.security.HashLoginService;
|
| | | import org.eclipse.jetty.security.LoginService;
|
| | | import org.eclipse.jetty.security.authentication.BasicAuthenticator;
|
| | | import org.eclipse.jetty.server.Connector;
|
| | | import org.eclipse.jetty.server.Handler;
|
| | |
| | | import org.eclipse.jetty.webapp.WebAppContext;
|
| | | import org.eclipse.jgit.http.server.GitServlet;
|
| | |
|
| | |
|
| | | import com.beust.jcommander.JCommander;
|
| | | import com.beust.jcommander.Parameter;
|
| | | import com.beust.jcommander.ParameterException;
|
| | | import com.beust.jcommander.Parameters;
|
| | | import com.gitblit.utils.StringUtils;
|
| | | import com.gitblit.wicket.GitBlitWebApp;
|
| | |
|
| | | public class GitBlitServer {
|
| | |
|
| | | private final static Logger logger = Log.getLogger(GitBlitServer.class.getSimpleName());
|
| | | private final static String border_star = "***********************************************************";
|
| | | private static boolean debugMode = false;
|
| | |
|
| | | public static boolean isDebugMode() {
|
| | | return debugMode;
|
| | | }
|
| | | private final static FileSettings fileSettings = new FileSettings();
|
| | |
|
| | | public static void main(String[] args) {
|
| | | Params params = new Params();
|
| | |
| | | }
|
| | | if (jc != null) {
|
| | | jc.usage();
|
| | | System.out.println("\nExample:\n java -server -Xmx1024M -jar go-git-go.jar --repos c:\\git --port 80 --securePort 443");
|
| | | System.out.println("\nExample:\n java -server -Xmx1024M -jar gitblit.jar --repos c:\\git --port 80 --securePort 443");
|
| | | }
|
| | | System.exit(0);
|
| | | }
|
| | |
| | | * Start Server.
|
| | | */
|
| | | private static void start(Params params) {
|
| | | PatternLayout layout = new PatternLayout(StoredSettings.getString("log4jPattern", "%-5p %d{MM-dd HH:mm:ss.SSS} %-20.20c{1} %m%n"));
|
| | | PatternLayout layout = new PatternLayout(fileSettings.getString(Keys.server.log4jPattern, "%-5p %d{MM-dd HH:mm:ss.SSS} %-20.20c{1} %m%n"));
|
| | | org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
|
| | | rootLogger.addAppender(new ConsoleAppender(layout));
|
| | |
|
| | |
| | | String osversion = System.getProperty("os.version");
|
| | | logger.info("Running on " + osname + " (" + osversion + ")");
|
| | |
|
| | | if (params.debug) {
|
| | | logger.warn("DEBUG Mode");
|
| | | }
|
| | |
|
| | | // Determine port connectors
|
| | | List<Connector> connectors = new ArrayList<Connector>();
|
| | | if (params.port > 0) {
|
| | | Connector httpConnector = createConnector(params.useNIO, params.port);
|
| | | String bindInterface = fileSettings.getString(Keys.server.httpBindInterface, null);
|
| | | if (!StringUtils.isEmpty(bindInterface)) {
|
| | | logger.warn(MessageFormat.format("Binding port {0} to {1}", params.port, bindInterface));
|
| | | httpConnector.setHost(bindInterface);
|
| | | }
|
| | | connectors.add(httpConnector);
|
| | | }
|
| | | |
| | |
|
| | | if (params.securePort > 0) {
|
| | | if (new File("keystore").exists()) {
|
| | | Connector secureConnector = createSSLConnector(params.useNIO, params.securePort, params.storePassword);
|
| | | String bindInterface = fileSettings.getString(Keys.server.httpsBindInterface, null);
|
| | | if (!StringUtils.isEmpty(bindInterface)) {
|
| | | logger.warn(MessageFormat.format("Binding port {0} to {1}", params.port, bindInterface));
|
| | | secureConnector.setHost(bindInterface);
|
| | | }
|
| | | connectors.add(secureConnector);
|
| | | } else {
|
| | | logger.warn("Failed to find Keystore? Did you run \"makekeystore\"?");
|
| | |
| | | FilterHolder wicketFilter = new FilterHolder(WicketFilter.class);
|
| | | wicketFilter.setInitParameter(ContextParamWebApplicationFactory.APP_CLASS_PARAM, GitBlitWebApp.class.getName());
|
| | | wicketFilter.setInitParameter(WicketFilter.FILTER_MAPPING_PARAM, wicketPathSpec);
|
| | | wicketFilter.setInitParameter(WicketFilter.IGNORE_PATHS_PARAM, "git/");
|
| | | rootContext.addFilter(wicketFilter, wicketPathSpec, FilterMapping.DEFAULT);
|
| | |
|
| | | // GIT Servlet
|
| | | // Git Servlet
|
| | | ServletHolder gitServlet = null;
|
| | | String gitServletPathSpec = "/git/*";
|
| | | ServletHolder gitServlet = rootContext.addServlet(GitServlet.class, gitServletPathSpec);
|
| | | gitServlet.setInitParameter("base-path", params.repositoriesFolder);
|
| | | gitServlet.setInitParameter("export-all", params.exportAll ? "1" : "0");
|
| | | if (fileSettings.getBoolean(Keys.git.allowPushPull, true)) {
|
| | | gitServlet = rootContext.addServlet(GitServlet.class, gitServletPathSpec);
|
| | | gitServlet.setInitParameter("base-path", params.repositoriesFolder);
|
| | | gitServlet.setInitParameter("export-all", params.exportAll ? "1" : "0");
|
| | | }
|
| | |
|
| | | // Login Service
|
| | | LoginService loginService = null;
|
| | | String realmUsers = params.realmFile;
|
| | | if (realmUsers != null && new File(realmUsers).exists()) {
|
| | | logger.info("Setting up login service from " + realmUsers);
|
| | | JettyLoginService jettyLoginService = new JettyLoginService(realmUsers);
|
| | | GitBlit.self().setLoginService(jettyLoginService);
|
| | | loginService = jettyLoginService;
|
| | | }
|
| | |
|
| | | // Authentication Realm
|
| | | // Determine what handler to use
|
| | | Handler handler;
|
| | | if (realmUsers != null && new File(realmUsers).exists() && params.authenticateAccess) {
|
| | | List<String> list = StoredSettings.getStrings("gitRoles");
|
| | | String[] roles;
|
| | | if (list.size() == 0) {
|
| | | roles = new String[] { "*" };
|
| | | if (gitServlet != null) {
|
| | | if (loginService != null && params.authenticatePushPull) {
|
| | | // Authenticate Pull/Push
|
| | | String[] roles = new String[] { Constants.PULL_ROLE, Constants.PUSH_ROLE };
|
| | | logger.info("Authentication required for git servlet pull/push access");
|
| | |
|
| | | Constraint constraint = new Constraint();
|
| | | constraint.setName("auth");
|
| | | constraint.setAuthenticate(true);
|
| | | constraint.setRoles(roles);
|
| | |
|
| | | ConstraintMapping mapping = new ConstraintMapping();
|
| | | mapping.setPathSpec(gitServletPathSpec);
|
| | | mapping.setConstraint(constraint);
|
| | |
|
| | | ConstraintSecurityHandler security = new ConstraintSecurityHandler();
|
| | | security.addConstraintMapping(mapping);
|
| | | for (String role : roles) {
|
| | | security.addRole(role);
|
| | | }
|
| | | security.setAuthenticator(new BasicAuthenticator());
|
| | | security.setLoginService(loginService);
|
| | | security.setStrict(false);
|
| | |
|
| | | security.setHandler(rootContext);
|
| | |
|
| | | handler = security;
|
| | | } else {
|
| | | roles = list.toArray(new String[list.size()]);
|
| | | // Anonymous Pull/Push
|
| | | logger.info("Setting up anonymous git servlet pull/push access");
|
| | | handler = rootContext;
|
| | | }
|
| | | logger.info("Authentication required for GIT access");
|
| | | logger.info("Setting up realm from " + realmUsers);
|
| | | HashLoginService loginService = new HashLoginService(Constants.NAME, realmUsers);
|
| | |
|
| | | Constraint constraint = new Constraint();
|
| | | constraint.setName("auth");
|
| | | constraint.setAuthenticate(true);
|
| | | constraint.setRoles(roles);
|
| | |
|
| | | ConstraintMapping mapping = new ConstraintMapping();
|
| | | mapping.setPathSpec(gitServletPathSpec);
|
| | | mapping.setConstraint(constraint);
|
| | |
|
| | | ConstraintSecurityHandler security = new ConstraintSecurityHandler();
|
| | | security.addConstraintMapping(mapping);
|
| | | for (String role : roles) {
|
| | | security.addRole(role);
|
| | | }
|
| | | security.setAuthenticator(new BasicAuthenticator());
|
| | | security.setLoginService(loginService);
|
| | | security.setStrict(false);
|
| | |
|
| | | security.setHandler(rootContext);
|
| | |
|
| | | handler = security;
|
| | | } else {
|
| | | logger.info("Setting up anonymous access");
|
| | | logger.info("Git servlet pull/push disabled");
|
| | | handler = rootContext;
|
| | | }
|
| | |
|
| | | // Set the server's contexts
|
| | | server.setHandler(handler);
|
| | |
|
| | | // Setup the GitBlit context
|
| | | GitBlit gitblit = GitBlit.self();
|
| | | gitblit.setupContext(fileSettings);
|
| | | rootContext.addEventListener(gitblit);
|
| | |
|
| | | // Start the Server
|
| | | try {
|
| | |
| | | public Boolean stop = false;
|
| | |
|
| | | @Parameter(names = { "--temp" }, description = "Server temp folder")
|
| | | public String temp = StoredSettings.getString("tempFolder", "temp");
|
| | |
|
| | | @Parameter(names = { "--debug" }, description = "Run server in DEBUG mode")
|
| | | public Boolean debug = StoredSettings.getBoolean("debug", false);
|
| | | public String temp = fileSettings.getString(Keys.server.tempFolder, "temp");
|
| | |
|
| | | /*
|
| | | * GIT Servlet Parameters
|
| | | */
|
| | | @Parameter(names = { "--repos" }, description = "GIT Repositories Folder")
|
| | | public String repositoriesFolder = StoredSettings.getString("repositoriesFolder", "repos");
|
| | | @Parameter(names = { "--repos" }, description = "Git Repositories Folder")
|
| | | public String repositoriesFolder = fileSettings.getString(Keys.git.repositoriesFolder, "repos");
|
| | |
|
| | | @Parameter(names = { "--exportAll" }, description = "Export All Found Repositories")
|
| | | public Boolean exportAll = StoredSettings.getBoolean("exportAll", true);
|
| | | public Boolean exportAll = fileSettings.getBoolean(Keys.git.exportAll, true);
|
| | |
|
| | | /*
|
| | | * Authentication Parameters
|
| | | */
|
| | | @Parameter(names = { "--authenticateAccess" }, description = "Authenticate GIT access")
|
| | | public Boolean authenticateAccess = StoredSettings.getBoolean("authenticateAccess", true);
|
| | | @Parameter(names = { "--authenticatePushPull" }, description = "Authenticate Git Push/Pull access")
|
| | | public Boolean authenticatePushPull = fileSettings.getBoolean(Keys.git.authenticate, true);
|
| | |
|
| | | @Parameter(names = { "--realm" }, description = "Users Realm Hash File")
|
| | | public String realmFile = StoredSettings.getString("realmFile", "users.properties");
|
| | | public String realmFile = fileSettings.getString(Keys.server.realmFile, "users.properties");
|
| | |
|
| | | /*
|
| | | * JETTY Parameters
|
| | | */
|
| | | @Parameter(names = { "--nio" }, description = "Use NIO Connector else use Socket Connector.")
|
| | | public Boolean useNIO = StoredSettings.getBoolean("useNio", true);
|
| | | public Boolean useNIO = fileSettings.getBoolean(Keys.server.useNio, true);
|
| | |
|
| | | @Parameter(names = "--port", description = "HTTP port for to serve. (port <= 0 will disable this connector)")
|
| | | public Integer port = StoredSettings.getInteger("httpPort", 80);
|
| | | public Integer port = fileSettings.getInteger(Keys.server.httpPort, 80);
|
| | |
|
| | | @Parameter(names = "--securePort", description = "HTTPS port to serve. (port <= 0 will disable this connector)")
|
| | | public Integer securePort = StoredSettings.getInteger("httpsPort", 443);
|
| | | public Integer securePort = fileSettings.getInteger(Keys.server.httpsPort, 443);
|
| | |
|
| | | @Parameter(names = "--storePassword", description = "Password for SSL (https) keystore.")
|
| | | public String storePassword = StoredSettings.getString("storePassword", "");
|
| | | public String storePassword = fileSettings.getString(Keys.server.storePassword, "");
|
| | |
|
| | | @Parameter(names = "--shutdownPort", description = "Port for Shutdown Monitor to listen on. (port <= 0 will disable this monitor)")
|
| | | public Integer shutdownPort = StoredSettings.getInteger("shutdownPort", 8081);
|
| | | public Integer shutdownPort = fileSettings.getInteger(Keys.server.shutdownPort, 8081);
|
| | |
|
| | | }
|
| | | } |