forked from j62/ctbrec
1
0
Fork 0

Fix SonarLint issues

This commit is contained in:
0xboobface 2019-12-21 14:46:59 +01:00
parent a4b805c038
commit ce103853a3
1 changed files with 77 additions and 70 deletions

View File

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