diff --git a/server/src/main/java/ctbrec/recorder/server/HttpServer.java b/server/src/main/java/ctbrec/recorder/server/HttpServer.java index 5a8de8e0..bfe7f385 100644 --- a/server/src/main/java/ctbrec/recorder/server/HttpServer.java +++ b/server/src/main/java/ctbrec/recorder/server/HttpServer.java @@ -61,7 +61,7 @@ import ctbrec.sites.streamate.Streamate; public class HttpServer { - private static final transient Logger LOG = LoggerFactory.getLogger(HttpServer.class); + private static final Logger LOG = LoggerFactory.getLogger(HttpServer.class); private Recorder recorder; private OnlineMonitor onlineMonitor; private Config config; @@ -142,11 +142,11 @@ public class HttpServer { private void startHttpServer() throws Exception { server = new Server(); - HttpConfiguration config = new HttpConfiguration(); - config.setSendServerVersion(false); - config.setSecurePort(this.config.getSettings().httpSecurePort); - config.setSecureScheme("https"); - HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(config); + HttpConfiguration httpConfig = new HttpConfiguration(); + httpConfig.setSendServerVersion(false); + httpConfig.setSecurePort(this.config.getSettings().httpSecurePort); + httpConfig.setSecureScheme("https"); + HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(httpConfig); SslContextFactory sslContextFactory = new SslContextFactory.Server(); URL keyStoreUrl = getClass().getResource("/keystore.pkcs12"); @@ -157,76 +157,82 @@ public class HttpServer { sslContextFactory.setTrustStorePath(keyStoreSrc); sslContextFactory.setTrustStorePassword(keyStorePassword); - // connector for http - ServerConnector http = new ServerConnector(server, httpConnectionFactory); - http.setPort(this.config.getSettings().httpPort); - http.setIdleTimeout(this.config.getSettings().httpTimeout); + try (ServerConnector http = new ServerConnector(server, httpConnectionFactory); + ServerConnector https = new ServerConnector(server, sslContextFactory, httpConnectionFactory)) { - // connector for https (TLS) - ServerConnector https = new ServerConnector(server, sslContextFactory, httpConnectionFactory); - https.setPort(this.config.getSettings().httpSecurePort); - https.setIdleTimeout(this.config.getSettings().httpTimeout); + // connector for http + http.setPort(this.config.getSettings().httpPort); + http.setIdleTimeout(this.config.getSettings().httpTimeout); - String contextPath = Config.getInstance().getContextPath(); - ServletContextHandler basicAuthContext = new ServletContextHandler(ServletContextHandler.SESSIONS); - basicAuthContext.setContextPath(contextPath + "/secured"); + // connector for https (TLS) + https.setPort(this.config.getSettings().httpSecurePort); + https.setIdleTimeout(this.config.getSettings().httpTimeout); - ServletContextHandler defaultContext = new ServletContextHandler(ServletContextHandler.SESSIONS); - defaultContext.setContextPath(contextPath); + String contextPath = Config.getInstance().getContextPath(); + ServletContextHandler basicAuthContext = new ServletContextHandler(ServletContextHandler.SESSIONS); + basicAuthContext.setContextPath(contextPath + "/secured"); - RecorderServlet recorderServlet = new RecorderServlet(recorder, sites); - ServletHolder holder = new ServletHolder(recorderServlet); - defaultContext.addServlet(holder, "/rec"); + ServletContextHandler defaultContext = new ServletContextHandler(ServletContextHandler.SESSIONS); + defaultContext.setContextPath(contextPath); - HlsServlet hlsServlet = new HlsServlet(this.config); - holder = new ServletHolder(hlsServlet); - defaultContext.addServlet(holder, "/hls/*"); + RecorderServlet recorderServlet = new RecorderServlet(recorder, sites); + ServletHolder holder = new ServletHolder(recorderServlet); + defaultContext.addServlet(holder, "/rec"); - if (this.config.getSettings().webinterface) { - StaticFileServlet staticFileServlet = new StaticFileServlet("/html"); - holder = new ServletHolder(staticFileServlet); - String staticFileContext = "/static/*"; - defaultContext.addServlet(holder, staticFileContext); - LOG.info("Register static file servlet under {}", defaultContext.getContextPath()+staticFileContext); + HlsServlet hlsServlet = new HlsServlet(this.config); + holder = new ServletHolder(hlsServlet); + defaultContext.addServlet(holder, "/hls/*"); - // servlet to retrieve the HMAC secured by basic auth - String username = this.config.getSettings().webinterfaceUsername; - String password = this.config.getSettings().webinterfacePassword; - basicAuthContext.setSecurityHandler(basicAuth(username, password, "CTB Recorder")); - basicAuthContext.addServlet(new ServletHolder(new HttpServlet() { - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - if (Objects.equal(username, req.getRemoteUser())) { - resp.setStatus(HttpServletResponse.SC_OK); - resp.setContentType("application/json"); - byte[] hmac = Optional.ofNullable(HttpServer.this.config.getSettings().key).orElse(new byte[0]); - JSONObject response = new JSONObject(); - response.put("hmac", new String(hmac, "utf-8")); - resp.getOutputStream().println(response.toString()); + if (this.config.getSettings().webinterface) { + StaticFileServlet staticFileServlet = new StaticFileServlet("/html"); + holder = new ServletHolder(staticFileServlet); + String staticFileContext = "/static/*"; + defaultContext.addServlet(holder, staticFileContext); + LOG.info("Register static file servlet under {}", defaultContext.getContextPath()+staticFileContext); + + // servlet to retrieve the HMAC secured by basic auth + String username = this.config.getSettings().webinterfaceUsername; + String password = this.config.getSettings().webinterfacePassword; + basicAuthContext.setSecurityHandler(basicAuth(username, password, "CTB Recorder")); + basicAuthContext.addServlet(new ServletHolder(new HttpServlet() { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException { + if (Objects.equal(username, req.getRemoteUser())) { + resp.setStatus(HttpServletResponse.SC_OK); + resp.setContentType("application/json"); + byte[] hmac = Optional.ofNullable(HttpServer.this.config.getSettings().key).orElse(new byte[0]); + try { + JSONObject response = new JSONObject(); + response.put("hmac", new String(hmac, "utf-8")); + resp.getOutputStream().println(response.toString()); + } catch (Exception e) { + resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } + } } - } - }), "/hmac"); - } + }), "/hmac"); + } - server.addConnector(http); - HandlerList handlers = new HandlerList(); - if (this.config.getSettings().transportLayerSecurity) { - server.addConnector(https); - handlers.setHandlers(new Handler[] { new SecuredRedirectHandler(), basicAuthContext, defaultContext }); - } else { - handlers.setHandlers(new Handler[] { basicAuthContext, defaultContext }); - } - server.setHandler(handlers); + server.addConnector(http); + HandlerList handlers = new HandlerList(); + if (this.config.getSettings().transportLayerSecurity) { + server.addConnector(https); + handlers.setHandlers(new Handler[] { new SecuredRedirectHandler(), basicAuthContext, defaultContext }); + } else { + handlers.setHandlers(new Handler[] { basicAuthContext, defaultContext }); + } + server.setHandler(handlers); - try { - server.start(); - server.join(); - } catch (BindException e) { - LOG.error("Port {} is already in use", http.getPort(), e); - System.exit(1); - } catch (Exception e) { - LOG.error("Server start failed", e); - System.exit(1); + try { + server.start(); + server.join(); + } catch (BindException e) { + LOG.error("Port {} is already in use", http.getPort(), e); + System.exit(1); + } catch (Exception e) { + LOG.error("Server start failed", e); + System.exit(1); + } } } @@ -256,10 +262,10 @@ public class HttpServer { } private void registerAlertSystem() { - for (EventHandlerConfiguration config : Config.getInstance().getSettings().eventHandlers) { - EventHandler handler = new EventHandler(config); + for (EventHandlerConfiguration ehconfig : Config.getInstance().getSettings().eventHandlers) { + EventHandler handler = new EventHandler(ehconfig); EventBusHolder.register(handler); - LOG.debug("Registered event handler for {} {}", config.getEvent(), config.getName()); + LOG.debug("Registered event handler for {} {}", ehconfig.getEvent(), ehconfig.getName()); } LOG.debug("Alert System registered"); } @@ -268,8 +274,9 @@ public class HttpServer { LOG.debug("OS:\t{} {}", System.getProperty("os.name"), System.getProperty("os.version")); LOG.debug("Java:\t{} {} {}", System.getProperty("java.vendor"), System.getProperty("java.vm.name"), System.getProperty("java.version")); try { - LOG.debug("ctbrec server {}", getVersion().toString()); + LOG.debug("ctbrec server {}", getVersion()); } catch (IOException e) { + // nothing to do here } }