diff --git a/src/main/java/common/common/src/requestHandlers/SSLServerFactory.java b/src/main/java/common/common/src/requestHandlers/SSLServerFactory.java index 8acc3f9..b7e9c4a 100644 --- a/src/main/java/common/common/src/requestHandlers/SSLServerFactory.java +++ b/src/main/java/common/common/src/requestHandlers/SSLServerFactory.java @@ -20,10 +20,13 @@ public class SSLServerFactory { */ public static Thread createServer(SSLContext ctx, int listeningPort, Consumer consumer) { return new Thread(() -> { + SSLServerSocketFactory serverSocketFactory = ctx.getServerSocketFactory(); + try (SSLServerSocket serverSocket = (SSLServerSocket) serverSocketFactory.createServerSocket(listeningPort)) { serverSocket.setNeedClientAuth(true); Logger.displayInfo("Server listening on port " + listeningPort); + while (true) { try (SSLSocket clientSocket = (SSLSocket) serverSocket.accept()) { consumer.accept(clientSocket); @@ -31,6 +34,7 @@ public class SSLServerFactory { throw new RuntimeException(e); } } + } catch (IOException ioe) { throw new RuntimeException(ioe); } diff --git a/src/main/java/httpServer/httpServer/src/Main.java b/src/main/java/httpServer/httpServer/src/Main.java index b19b755..111843f 100644 --- a/src/main/java/httpServer/httpServer/src/Main.java +++ b/src/main/java/httpServer/httpServer/src/Main.java @@ -1,38 +1,73 @@ package httpServer.httpServer.src; -import com.sun.net.httpserver.HttpServer; +import com.sun.net.httpserver.HttpsConfigurator; +import com.sun.net.httpserver.HttpsParameters; +import com.sun.net.httpserver.HttpsServer; +import common.common.src.crypto.KeyLoader; import common.common.src.logger.Logger; import httpServer.httpServer.src.handlers.IRequestHandler; import httpServer.httpServer.src.handlers.RequestHandler; import httpServer.httpServer.src.interceptors.RequestInterceptor; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.TrustManagerFactory; import java.io.*; import java.lang.reflect.Proxy; import java.net.InetSocketAddress; +import static common.common.src.ports.Ports.HTTP_SERVER_PORT; + public class Main { - public static void main(String[] args) throws IOException { + private static final String KEY_STORE_PATH = "assets/certs/https.p12"; + private static final String KEY_STORE_PWD = "hepl"; - final int port = 8043; + private static final String TRUST_STORE_PATH = "assets/certs/https-trust.jks"; + private static final String TRUST_STORE_PWD = "heplhepl"; - HttpServer server = HttpServer.create(new InetSocketAddress(port), 5); + public static void main(String[] args) { - IRequestHandler requestHandler = new RequestHandler(); + try { + HttpsServer server = HttpsServer.create(new InetSocketAddress(HTTP_SERVER_PORT), 5); - IRequestHandler proxy = (IRequestHandler) Proxy.newProxyInstance( - requestHandler.getClass().getClassLoader(), - new Class[]{ IRequestHandler.class }, - new RequestInterceptor(requestHandler) - ); + KeyLoader loader = new KeyLoader(KEY_STORE_PATH, KEY_STORE_PWD, TRUST_STORE_PATH, TRUST_STORE_PWD); + KeyManagerFactory kmf = loader.loadKeyStore(); - server.createContext("/", proxy::handleRoot); + SSLContext ctx = SSLContext.getInstance("TLS"); + ctx.init(kmf.getKeyManagers(), null, null); - server.createContext("/payment", proxy::handlePayment); + server.setHttpsConfigurator(new HttpsConfigurator(ctx) { + @Override + public void configure(HttpsParameters params) { + SSLContext context = getSSLContext(); + SSLEngine engine = context.createSSLEngine(); + params.setNeedClientAuth(false); + params.setCipherSuites(engine.getEnabledCipherSuites()); + params.setProtocols(engine.getEnabledProtocols()); + params.setSSLParameters(context.getDefaultSSLParameters()); + } + }); - server.createContext("/login", proxy::handleLogin); + IRequestHandler requestHandler = new RequestHandler(); - server.start(); - Logger.displayInfo("Server started on port " + port); + IRequestHandler proxy = (IRequestHandler) Proxy.newProxyInstance( + requestHandler.getClass().getClassLoader(), + new Class[]{IRequestHandler.class}, + new RequestInterceptor(requestHandler) + ); + + server.createContext("/", proxy::handleRoot); + + server.createContext("/payment", proxy::handlePayment); + + server.createContext("/login", proxy::handleLogin); + + server.start(); + Logger.displayInfo("Server started on port " + HTTP_SERVER_PORT); + } catch (Exception e) { + throw new RuntimeException(e); + } } } diff --git a/src/main/java/httpServer/httpServer/src/interceptors/RequestInterceptor.java b/src/main/java/httpServer/httpServer/src/interceptors/RequestInterceptor.java index 104411b..38aa514 100644 --- a/src/main/java/httpServer/httpServer/src/interceptors/RequestInterceptor.java +++ b/src/main/java/httpServer/httpServer/src/interceptors/RequestInterceptor.java @@ -1,6 +1,7 @@ package httpServer.httpServer.src.interceptors; import com.sun.net.httpserver.HttpExchange; +import common.common.src.logger.Logger; import httpServer.httpServer.src.annotations.AllowedVerb; import httpServer.httpServer.src.annotations.OnlyAuthorizedClients; import httpServer.httpServer.src.authorization.AuthorizedClients; @@ -24,6 +25,9 @@ public class RequestInterceptor implements InvocationHandler { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { HttpExchange exchange = (HttpExchange) args[0]; try{ + + Logger.displayReceived("Request to " + exchange.getRequestURI() + " received"); + Method realMethod = target.getClass().getMethod(method.getName(), method.getParameterTypes()); checkAuthorizedVerb(realMethod, exchange.getRequestMethod());