Factorize some creation logic

This commit is contained in:
2025-12-06 13:08:48 +01:00
parent 60889b668a
commit ddf1def881
4 changed files with 77 additions and 24 deletions

View File

@@ -1,14 +1,17 @@
package acq.acq.src;
import common.common.src.crypto.KeyLoader;
import common.common.src.logger.Logger;
import common.common.src.requestHandlers.SSLServerFactory;
import common.common.src.socket.SocketManager;
import javax.net.ssl.*;
import java.io.*;
import java.security.KeyStore;
import static common.common.src.ports.Ports.*;
public class Main {
private static final String HOST = "127.0.0.1";
private static final String KEY_STORE_PATH = "assets/certs/acq.p12";
private static final String KEY_STORE_PWD = "hepl";
@@ -16,8 +19,6 @@ public class Main {
private static final String TRUST_STORE_PWD = "heplhepl";
public static void main(String[] args) throws Exception {
String host = "localhost";
int port = 8443;
KeyLoader loader = new KeyLoader(KEY_STORE_PATH, KEY_STORE_PWD, TRUST_STORE_PATH, TRUST_STORE_PWD);
@@ -27,8 +28,15 @@ public class Main {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
requestACS(ctx);
Thread ACQServer = SSLServerFactory.createServer(ctx, ACQ_SERVER_PORT, Main::handleRequest);
ACQServer.start();
}
public static void requestACS(SSLContext ctx) {
SSLSocketFactory factory = ctx.getSocketFactory();
try (SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) {
try (SSLSocket socket = (SSLSocket) factory.createSocket(Main.HOST, ACS_SERVER_PORT)) {
socket.startHandshake();
String message = "Hello ACS";
@@ -38,7 +46,11 @@ public class Main {
String response = SocketManager.readResponse(socket);
Logger.displayReceived(response);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void handleRequest(SSLSocket serverSocket) {
Logger.displayInfo("Request handled");
}
}

View File

@@ -2,11 +2,14 @@ package acs.acs.src;
import common.common.src.crypto.KeyLoader;
import common.common.src.logger.Logger;
import common.common.src.requestHandlers.SSLServerFactory;
import common.common.src.socket.SocketManager;
import javax.net.ssl.*;
import java.io.IOException;
import static common.common.src.ports.Ports.ACS_SERVER_PORT;
public class Main {
@@ -17,7 +20,7 @@ public class Main {
private static final String TRUST_STORE_PWD = "heplhepl";
public static void main(String[] args) throws Exception {
int port = 8443;
KeyLoader loader = new KeyLoader(KEY_STORE_PATH, KEY_STORE_PWD, TRUST_STORE_PATH, TRUST_STORE_PWD);
KeyManagerFactory kmf = loader.loadKeyStore();
@@ -26,15 +29,12 @@ public class Main {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
Thread serverThread = SSLServerFactory.createServer(ctx, ACS_SERVER_PORT, Main::handleRequest);
serverThread.start();
}
SSLServerSocketFactory factory = ctx.getServerSocketFactory();
try (SSLServerSocket serverSocket = (SSLServerSocket) factory.createServerSocket(port)) {
serverSocket.setNeedClientAuth(true);
Logger.displayInfo("ACS listening on port " + port);
while (true) {
try (SSLSocket clientSocket = (SSLSocket) serverSocket.accept()) {
private static void handleRequest(SSLSocket clientSocket) {
try{
String response = SocketManager.readResponse(clientSocket);
Logger.displayReceived(response);
@@ -46,6 +46,5 @@ public class Main {
throw new RuntimeException(e);
}
}
}
}
}

View File

@@ -5,5 +5,8 @@ public class Ports {
* Port d'écoute du service ACS pour la communication avec l'application externe.
*/
public static int PORT_AUTH = 8786;
public static final int HTTP_SERVER_PORT = 8043;
public static final int ACS_SERVER_PORT = 8443;
public static final int ACQ_SERVER_PORT = 8543;
public static String ACS_HOST = "127.0.0.1";
}

View File

@@ -0,0 +1,39 @@
package common.common.src.requestHandlers;
import common.common.src.logger.Logger;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.util.function.Consumer;
public class SSLServerFactory {
/**
* Create a thread that will run a method each time a request is received on the specified port
* @param ctx the ssl context
* @param listeningPort the listening port
* @param consumer the action to be run
* @return the created thread
*/
public static Thread createServer(SSLContext ctx, int listeningPort, Consumer<SSLSocket> 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);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
});
}
}