| | |
| | | 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.server.Server;
|
| | | import org.eclipse.jetty.server.bio.SocketConnector;
|
| | | import org.eclipse.jetty.server.nio.SelectChannelConnector;
|
| | | import org.eclipse.jetty.server.session.HashSessionManager;
|
| | | import org.eclipse.jetty.server.ssl.SslConnector;
|
| | | import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
|
| | | import org.eclipse.jetty.server.ssl.SslSocketConnector;
|
| | |
| | | import org.eclipse.jetty.util.log.Logger;
|
| | | import org.eclipse.jetty.util.thread.QueuedThreadPool;
|
| | | 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 final static FileSettings fileSettings = new FileSettings();
|
| | |
|
| | | public static void main(String[] args) {
|
| | | Params params = new Params();
|
| | |
| | | * Start Server.
|
| | | */
|
| | | private static void start(Params params) {
|
| | | // instantiate GitBlit
|
| | | GitBlit.self();
|
| | | String pattern = fileSettings.getString(Keys.server.log4jPattern, "%-5p %d{MM-dd HH:mm:ss.SSS} %-20.20c{1} %m%n");
|
| | |
|
| | | PatternLayout layout = new PatternLayout(StoredSettings.getString(Keys.server_log4jPattern, "%-5p %d{MM-dd HH:mm:ss.SSS} %-20.20c{1} %m%n"));
|
| | | // allow os override of logging pattern
|
| | | String os = System.getProperty("os.name").toLowerCase();
|
| | | if (os.indexOf("windows") > -1) {
|
| | | String winPattern = fileSettings.getString(Keys.server.log4jPattern_windows, pattern);
|
| | | if (!StringUtils.isEmpty(winPattern)) {
|
| | | pattern = winPattern;
|
| | | }
|
| | | } else if (os.indexOf("linux") > -1) {
|
| | | String linuxPattern = fileSettings.getString(Keys.server.log4jPattern_linux, pattern);
|
| | | if (!StringUtils.isEmpty(linuxPattern)) {
|
| | | pattern = linuxPattern;
|
| | | }
|
| | | }
|
| | |
|
| | | PatternLayout layout = new PatternLayout(pattern);
|
| | | 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 (StoredSettings.getBoolean(Keys.server_debugMode, false)) {
|
| | | 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 connector on 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);
|
| | | File keystore = new File("keystore");
|
| | | if (!keystore.exists()) {
|
| | | logger.info("Generating self-signed SSL certificate");
|
| | | MakeCertificate.generateSelfSignedCertificate("localhost", keystore, params.storePassword);
|
| | | }
|
| | | if (keystore.exists()) {
|
| | | Connector secureConnector = createSSLConnector(keystore, params.storePassword, params.useNIO, params.securePort);
|
| | | String bindInterface = fileSettings.getString(Keys.server.httpsBindInterface, null);
|
| | | if (!StringUtils.isEmpty(bindInterface)) {
|
| | | logger.warn(MessageFormat.format("Binding ssl connector on port {0} to {1}", params.securePort, bindInterface));
|
| | | secureConnector.setHost(bindInterface);
|
| | | }
|
| | | connectors.add(secureConnector);
|
| | | } else {
|
| | | logger.warn("Failed to find Keystore? Did you run \"makekeystore\"?");
|
| | | logger.warn("Failed to find or load Keystore?");
|
| | | logger.warn("SSL connector DISABLED.");
|
| | | }
|
| | | }
|
| | |
| | | rootContext.setServer(server);
|
| | | rootContext.setWar(location.toExternalForm());
|
| | | rootContext.setTempDirectory(tempDir);
|
| | | |
| | | // Mark all cookies HttpOnly so they are not accessible to JavaScript
|
| | | // engines.
|
| | | // http://erlend.oftedal.no/blog/?blogid=33
|
| | | // https://www.owasp.org/index.php/HttpOnly#Browsers_Supporting_HttpOnly
|
| | | HashSessionManager sessionManager = new HashSessionManager();
|
| | | sessionManager.setHttpOnly(true);
|
| | | // Use secure cookies if only serving https
|
| | | sessionManager.setSecureCookies(params.port <= 0 && params.securePort > 0);
|
| | | rootContext.getSessionHandler().setSessionManager(sessionManager);
|
| | |
|
| | | // Wicket Filter
|
| | | String wicketPathSpec = "/*";
|
| | |
| | | // Git Servlet
|
| | | ServletHolder gitServlet = null;
|
| | | String gitServletPathSpec = "/git/*";
|
| | | if (StoredSettings.getBoolean(Keys.git_allowPushPull, true)) {
|
| | | gitServlet = rootContext.addServlet(GitServlet.class, gitServletPathSpec);
|
| | | if (fileSettings.getBoolean(Keys.git.enableGitServlet, true)) {
|
| | | gitServlet = rootContext.addServlet(GitBlitServlet.class, gitServletPathSpec);
|
| | | gitServlet.setInitParameter("base-path", params.repositoriesFolder);
|
| | | gitServlet.setInitParameter("export-all", params.exportAll ? "1" : "0");
|
| | | gitServlet.setInitParameter("export-all", fileSettings.getBoolean(Keys.git.exportAll, true) ? "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;
|
| | | String realmUsers = params.realmFile; |
| | | if (!StringUtils.isEmpty(realmUsers)) { |
| | | File realmFile = new File(realmUsers);
|
| | | if (realmFile.exists()) { |
| | | logger.info("Setting up login service from " + realmUsers);
|
| | | JettyLoginService jettyLoginService = new JettyLoginService(realmFile);
|
| | | GitBlit.self().setLoginService(jettyLoginService);
|
| | | loginService = jettyLoginService;
|
| | | }
|
| | | }
|
| | |
|
| | | // Determine what handler to use
|
| | | Handler handler;
|
| | | 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");
|
| | | if (loginService != null) {
|
| | | // Authenticate Clone/Push
|
| | | logger.info("Setting up authenticated git servlet clone/push access");
|
| | |
|
| | | Constraint constraint = new Constraint();
|
| | | constraint.setName("auth");
|
| | | constraint.setAuthenticate(true);
|
| | | constraint.setRoles(roles);
|
| | | constraint.setRoles(new String [] { "*" });
|
| | |
|
| | | ConstraintMapping mapping = new ConstraintMapping();
|
| | | mapping.setPathSpec(gitServletPathSpec);
|
| | | mapping.setConstraint(constraint);
|
| | |
|
| | | ConstraintSecurityHandler security = new ConstraintSecurityHandler();
|
| | | ConstraintSecurityHandler security = new ConstraintSecurityHandler(); |
| | | security.addConstraintMapping(mapping);
|
| | | for (String role : roles) {
|
| | | security.addRole(role);
|
| | | }
|
| | | security.setAuthenticator(new BasicAuthenticator());
|
| | | security.setLoginService(loginService);
|
| | | security.setStrict(false);
|
| | |
| | | handler = rootContext;
|
| | | }
|
| | | } else {
|
| | | logger.info("Git servlet pull/push disabled");
|
| | | logger.info("Git servlet clone/push disabled");
|
| | | handler = rootContext;
|
| | | }
|
| | |
|
| | | // Set the server's contexts
|
| | | server.setHandler(handler);
|
| | |
|
| | | // Setup the GitBlit context
|
| | | GitBlit gitblit = GitBlit.self();
|
| | | gitblit.configureContext(fileSettings);
|
| | | rootContext.addEventListener(gitblit);
|
| | |
|
| | | // Start the Server
|
| | | try {
|
| | |
| | | return connector;
|
| | | }
|
| | |
|
| | | private static Connector createSSLConnector(boolean useNIO, int port, String password) {
|
| | | private static Connector createSSLConnector(File keystore, String password, boolean useNIO, int port) {
|
| | | SslConnector connector;
|
| | | if (useNIO) {
|
| | | logger.info("Setting up NIO SslSelectChannelConnector on port " + port);
|
| | | SslSelectChannelConnector ssl = new SslSelectChannelConnector();
|
| | | ssl.setSoLingerTime(-1);
|
| | | ssl.setThreadPool(new QueuedThreadPool(20));
|
| | | ssl.setThreadPool(new QueuedThreadPool(20)); |
| | | connector = ssl;
|
| | | } else {
|
| | | logger.info("Setting up NIO SslSocketConnector on port " + port);
|
| | | SslSocketConnector ssl = new SslSocketConnector();
|
| | | connector = ssl;
|
| | | }
|
| | | connector.setKeystore("keystore");
|
| | | connector.setAllowRenegotiate(false);
|
| | | connector.setKeystore(keystore.getAbsolutePath());
|
| | | connector.setPassword(password);
|
| | | connector.setPort(port);
|
| | | connector.setMaxIdleTime(30000);
|
| | | return connector;
|
| | | }
|
| | |
|
| | | |
| | | /**
|
| | | * Recursively delete a folder and its contents.
|
| | | *
|
| | |
| | | @Parameter(names = { "--stop" }, description = "Stop Server")
|
| | | public Boolean stop = false;
|
| | |
|
| | | @Parameter(names = { "--temp" }, description = "Server temp folder")
|
| | | public String temp = StoredSettings.getString(Keys.server_tempFolder, "temp");
|
| | | @Parameter(names = { "--tempFolder" }, description = "Server temp folder")
|
| | | public String temp = fileSettings.getString(Keys.server.tempFolder, "temp");
|
| | |
|
| | | /*
|
| | | * GIT Servlet Parameters
|
| | | */
|
| | | @Parameter(names = { "--repos" }, description = "Git Repositories Folder")
|
| | | public String repositoriesFolder = StoredSettings.getString(Keys.git_repositoriesFolder, "repos");
|
| | |
|
| | | @Parameter(names = { "--exportAll" }, description = "Export All Found Repositories")
|
| | | public Boolean exportAll = StoredSettings.getBoolean(Keys.git_exportAll, true);
|
| | | @Parameter(names = { "--repositoriesFolder" }, description = "Git Repositories Folder")
|
| | | public String repositoriesFolder = fileSettings.getString(Keys.git.repositoriesFolder, "repos");
|
| | |
|
| | | /*
|
| | | * Authentication Parameters
|
| | | */
|
| | | @Parameter(names = { "--authenticatePushPull" }, description = "Authenticate Git Push/Pull access")
|
| | | public Boolean authenticatePushPull = StoredSettings.getBoolean(Keys.git_authenticate, true);
|
| | |
|
| | | @Parameter(names = { "--realm" }, description = "Users Realm Hash File")
|
| | | public String realmFile = StoredSettings.getString(Keys.server_realmFile, "users.properties");
|
| | | @Parameter(names = { "--realmFile" }, description = "Users Realm Hash File")
|
| | | public String realmFile = fileSettings.getString(Keys.realm.realmFile, "users.properties");
|
| | |
|
| | | /*
|
| | | * JETTY Parameters
|
| | | */
|
| | | @Parameter(names = { "--nio" }, description = "Use NIO Connector else use Socket Connector.")
|
| | | public Boolean useNIO = StoredSettings.getBoolean(Keys.server_useNio, true);
|
| | | @Parameter(names = { "--useNio" }, description = "Use NIO Connector else use Socket Connector.")
|
| | | 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(Keys.server_httpPort, 80);
|
| | | @Parameter(names = "--httpPort", description = "HTTP port for to serve. (port <= 0 will disable this connector)")
|
| | | 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(Keys.server_httpsPort, 443);
|
| | | @Parameter(names = "--httpsPort", description = "HTTPS port to serve. (port <= 0 will disable this connector)")
|
| | | public Integer securePort = fileSettings.getInteger(Keys.server.httpsPort, 443);
|
| | |
|
| | | @Parameter(names = "--storePassword", description = "Password for SSL (https) keystore.")
|
| | | public String storePassword = StoredSettings.getString(Keys.server_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(Keys.server_shutdownPort, 8081);
|
| | | public Integer shutdownPort = fileSettings.getInteger(Keys.server.shutdownPort, 8081);
|
| | |
|
| | | }
|
| | | } |