HTTP server has evolved in HTTPS server and ACQ now runs its own server
This commit is contained in:
@@ -20,10 +20,13 @@ public class SSLServerFactory {
|
|||||||
*/
|
*/
|
||||||
public static Thread createServer(SSLContext ctx, int listeningPort, Consumer<SSLSocket> consumer) {
|
public static Thread createServer(SSLContext ctx, int listeningPort, Consumer<SSLSocket> consumer) {
|
||||||
return new Thread(() -> {
|
return new Thread(() -> {
|
||||||
|
|
||||||
SSLServerSocketFactory serverSocketFactory = ctx.getServerSocketFactory();
|
SSLServerSocketFactory serverSocketFactory = ctx.getServerSocketFactory();
|
||||||
|
|
||||||
try (SSLServerSocket serverSocket = (SSLServerSocket) serverSocketFactory.createServerSocket(listeningPort)) {
|
try (SSLServerSocket serverSocket = (SSLServerSocket) serverSocketFactory.createServerSocket(listeningPort)) {
|
||||||
serverSocket.setNeedClientAuth(true);
|
serverSocket.setNeedClientAuth(true);
|
||||||
Logger.displayInfo("Server listening on port " + listeningPort);
|
Logger.displayInfo("Server listening on port " + listeningPort);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
try (SSLSocket clientSocket = (SSLSocket) serverSocket.accept()) {
|
try (SSLSocket clientSocket = (SSLSocket) serverSocket.accept()) {
|
||||||
consumer.accept(clientSocket);
|
consumer.accept(clientSocket);
|
||||||
@@ -31,6 +34,7 @@ public class SSLServerFactory {
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
throw new RuntimeException(ioe);
|
throw new RuntimeException(ioe);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,54 @@
|
|||||||
package httpServer.httpServer.src;
|
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 common.common.src.logger.Logger;
|
||||||
import httpServer.httpServer.src.handlers.IRequestHandler;
|
import httpServer.httpServer.src.handlers.IRequestHandler;
|
||||||
import httpServer.httpServer.src.handlers.RequestHandler;
|
import httpServer.httpServer.src.handlers.RequestHandler;
|
||||||
import httpServer.httpServer.src.interceptors.RequestInterceptor;
|
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.io.*;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
import static common.common.src.ports.Ports.HTTP_SERVER_PORT;
|
||||||
|
|
||||||
public class Main {
|
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) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
HttpsServer server = HttpsServer.create(new InetSocketAddress(HTTP_SERVER_PORT), 5);
|
||||||
|
|
||||||
|
KeyLoader loader = new KeyLoader(KEY_STORE_PATH, KEY_STORE_PWD, TRUST_STORE_PATH, TRUST_STORE_PWD);
|
||||||
|
KeyManagerFactory kmf = loader.loadKeyStore();
|
||||||
|
|
||||||
|
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||||
|
ctx.init(kmf.getKeyManagers(), null, null);
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
IRequestHandler requestHandler = new RequestHandler();
|
IRequestHandler requestHandler = new RequestHandler();
|
||||||
|
|
||||||
@@ -33,6 +65,9 @@ public class Main {
|
|||||||
server.createContext("/login", proxy::handleLogin);
|
server.createContext("/login", proxy::handleLogin);
|
||||||
|
|
||||||
server.start();
|
server.start();
|
||||||
Logger.displayInfo("Server started on port " + port);
|
Logger.displayInfo("Server started on port " + HTTP_SERVER_PORT);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package httpServer.httpServer.src.interceptors;
|
package httpServer.httpServer.src.interceptors;
|
||||||
|
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
|
import common.common.src.logger.Logger;
|
||||||
import httpServer.httpServer.src.annotations.AllowedVerb;
|
import httpServer.httpServer.src.annotations.AllowedVerb;
|
||||||
import httpServer.httpServer.src.annotations.OnlyAuthorizedClients;
|
import httpServer.httpServer.src.annotations.OnlyAuthorizedClients;
|
||||||
import httpServer.httpServer.src.authorization.AuthorizedClients;
|
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 {
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||||
HttpExchange exchange = (HttpExchange) args[0];
|
HttpExchange exchange = (HttpExchange) args[0];
|
||||||
try{
|
try{
|
||||||
|
|
||||||
|
Logger.displayReceived("Request to " + exchange.getRequestURI() + " received");
|
||||||
|
|
||||||
Method realMethod = target.getClass().getMethod(method.getName(), method.getParameterTypes());
|
Method realMethod = target.getClass().getMethod(method.getName(), method.getParameterTypes());
|
||||||
|
|
||||||
checkAuthorizedVerb(realMethod, exchange.getRequestMethod());
|
checkAuthorizedVerb(realMethod, exchange.getRequestMethod());
|
||||||
|
|||||||
Reference in New Issue
Block a user