Skip to content

Commit

Permalink
versao SSL
Browse files Browse the repository at this point in the history
  • Loading branch information
José René Campanario committed Oct 17, 2019
1 parent 2005452 commit 4b533f4
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 28 deletions.
8 changes: 2 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
service/server/target/*
timesheet-canada.mv.db
timesheet.mv.db
timesheet.trace.db
timesheet-old.mv.db
timesheet-copy.db
timesheet-copy.mv.db
*.db
arquivos/*
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"request": "launch",
"mainClass": "com.timesheet.Main",
"projectName": "server",
"vmArgs": "-DDB_URL=jdbc:h2:./timesheet -DDB_USER=sa -DDB_PASS=sa -DSERVICE_PORT=8000 -DSERVICE_BIND=localhost"
"vmArgs": "-DXms512m -DXmx512m -DKEYSTORE_PATH=./arquivos/certs/keystore.jks -DROOT_PATH=. -DDB_URL=jdbc:h2:./timesheet -DDB_USER=sa -DDB_PASS=sa -DSERVICE_PORT=8000 -DSERVICE_BIND=localhost"
}
]
}
5 changes: 4 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ function homeme(idSubGroup, hora, minuto, endHour, endMinute) {
addItem(idSubGroup, hora, minuto, undefined, undefined, 'Home', false, undefined);
}
function adicionar(name, sector, horaInicial, minutoInicial, horaFinal, minutoFinal) {
if (typeof name === 'undefined' || name == null || name.length <= 0) {
return;
}
var idSubGroup = groups.add({ employeeName: name, order: 0, checked: false })[0];
var c = montarNome(idSubGroup, name, false);
groups.update({ id: idSubGroup, content: c });
Expand Down Expand Up @@ -557,7 +560,7 @@ function updateDateItem(item) {

var socket;
if (window.WebSocket) {
var url = "ws://" + location.hostname + ":" + (parseInt(location.port) + 1) + "/";
var url = "wss://" + location.hostname + ":444/";
socket = new WebSocket(url);
socket.onmessage = function (event) {
var action = JSON.parse(event.data);
Expand Down
103 changes: 83 additions & 20 deletions service/server/src/main/java/com/timesheet/Main.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,51 @@
package com.timesheet;

import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.resource.PathResourceManager;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;
import io.undertow.servlet.api.ListenerInfo;
import io.undertow.servlet.api.ServletInfo;
import io.undertow.websockets.WebSocketProtocolHandshakeHandler;
import static io.undertow.Handlers.path;
import static io.undertow.servlet.Servlets.defaultContainer;
import static io.undertow.servlet.Servlets.deployment;
import static io.undertow.servlet.Servlets.listener;
import static io.undertow.servlet.Servlets.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.servlet.ServletException;

import org.h2.server.web.DbStarter;
import static io.undertow.servlet.Servlets.defaultContainer;
import static io.undertow.servlet.Servlets.deployment;
import static io.undertow.servlet.Servlets.servlet;
import static io.undertow.servlet.Servlets.listener;

import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.resource.PathResourceManager;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;
import io.undertow.servlet.api.ListenerInfo;
import io.undertow.servlet.api.ServletInfo;
import io.undertow.websockets.WebSocketConnectionCallback;
import io.undertow.websockets.WebSocketProtocolHandshakeHandler;
import io.undertow.websockets.core.AbstractReceiveListener;
import io.undertow.websockets.core.BufferedTextMessage;
import io.undertow.websockets.core.WebSocketChannel;
import io.undertow.websockets.core.WebSockets;
import io.undertow.websockets.WebSocketConnectionCallback;
import io.undertow.websockets.spi.WebSocketHttpExchange;
import static io.undertow.Handlers.path;

public class Main {

Expand All @@ -37,32 +54,37 @@ public class Main {
private static String DB_PASS = null;
private static String SERVICE_PORT = null;
private static String SERVICE_BIND = null;
private static String ROOT_PATH = ".";
private static String KEYSTORE_PATH = null;

public static void main(String[] args) {
public static void main(String[] args) throws Throwable {
initializeVariables();
initializeDatabase();
initializeServers();
}

private static void initializeServers() {
private static void initializeServers() throws Throwable {
ServletInfo timeSheetServlet = servlet("TimeSheetServlet", TimeSheetServlet.class).addMapping("/ts");
ListenerInfo databaseListener = listener(DbStarter.class);
DeploymentInfo servletBuilder = deployment().setClassLoader(Main.class.getClassLoader()).setContextPath("/")
.setDeploymentName("timesheet.war").setResourceManager(new PathResourceManager(Paths.get("."), 100))
.setDeploymentName("timesheet.war").setResourceManager(new PathResourceManager(Paths.get(Main.ROOT_PATH), 100))
.addListener(databaseListener).addInitParameter("db.url", DB_URL).addInitParameter("db.user", DB_USER)
.addInitParameter("db.password", DB_PASS).addServlets(timeSheetServlet);
.addInitParameter("db.password", DB_PASS).addWelcomePage("index.html")
.addServlets(timeSheetServlet);
DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);
manager.deploy();
HttpHandler servletHandler = null;
try {
servletHandler = manager.start();
} catch (ServletException e) {
}
Undertow httpServer = Undertow.builder().addHttpListener(Integer.parseInt(SERVICE_PORT), SERVICE_BIND)
int port = Integer.parseInt(SERVICE_PORT);
String host = SERVICE_BIND;
Undertow httpServer = Undertow.builder().addHttpsListener(port, host, Main.getSSLContext())
.setHandler(servletHandler).build();
httpServer.start();

Undertow webSockerServer = Undertow.builder().addHttpListener(Integer.parseInt(SERVICE_PORT) + 1, SERVICE_BIND)
Undertow webSockerServer = Undertow.builder().addHttpsListener(Integer.parseInt(SERVICE_PORT) + 1, SERVICE_BIND, Main.getSSLContext())
.setHandler(path().addPrefixPath("/",
new WebSocketProtocolHandshakeHandler(new WebSocketConnectionCallback() {
Set<WebSocketChannel> channels = new HashSet<WebSocketChannel>();
Expand Down Expand Up @@ -136,12 +158,53 @@ private static String initializeVariables(String variable) {
return result;
}

private static SSLContext getSSLContext() throws Throwable {
SSLContext sslContext = SSLContext.getDefault();
sslContext = SSLContext.getInstance("TLSv1.2");
String defaultAlgorithm = KeyManagerFactory.getDefaultAlgorithm();

KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(defaultAlgorithm);

KeyStore localKeyStore = KeyStore.getInstance("JKS");

InputStream is = new FileInputStream(new File(Main.KEYSTORE_PATH));
localKeyStore.load(is, "".toCharArray());

keyManagerFactory.init(localKeyStore, "".toCharArray());

KeyManager[] km = keyManagerFactory.getKeyManagers();

TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(X509Certificate[] c, String a) throws CertificateException {
}

public void checkServerTrusted(X509Certificate[] c, String a) throws CertificateException {
}
} };
SecureRandom sr = new SecureRandom();
sslContext.init(km, tm, sr);
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String h, SSLSession s) {
return true;
}
});

return sslContext;
}


private static void initializeVariables() {
Main.DB_URL = initializeVariables("DB_URL");
Main.DB_USER = initializeVariables("DB_USER");
Main.DB_PASS = initializeVariables("DB_PASS");
Main.SERVICE_PORT = initializeVariables("SERVICE_PORT");
Main.SERVICE_BIND = initializeVariables("SERVICE_BIND");
Main.ROOT_PATH = initializeVariables("ROOT_PATH");
Main.KEYSTORE_PATH = initializeVariables("KEYSTORE_PATH");
}

}

0 comments on commit 4b533f4

Please sign in to comment.