Updated to latest UnlegitLibrary version and implemented UDP

This commit is contained in:
Finn
2026-01-18 21:48:43 +01:00
parent da254a6c8e
commit 50cd7b57ac
40 changed files with 172 additions and 1503 deletions

View File

@@ -1,7 +1,10 @@
package org.openautonomousconnection.protocol.side.client;
import dev.unlegitdqrk.unlegitlibrary.network.system.client.NetworkClient;
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.ClientDisconnectedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.state.disconnect.ClientDisconnectedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.state.disconnect.ClientFullyDisconnectedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.Transport;
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.TransportPolicy;
import dev.unlegitdqrk.unlegitlibrary.network.utils.NetworkUtils;
import dev.unlegitdqrk.unlegitlibrary.utils.DefaultMethodsOverrider;
import lombok.Getter;
@@ -52,12 +55,20 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
*/
private ProtocolVersion serverVersion = null;
@Getter
private String insHost;
@Getter
private int insPort;
/**
* Initializes the ProtocolClient, setting up certificate folders and the INS client connection.
*/
public ProtocolClient() {
public ProtocolClient(String insHost, int insPort) {
// Initialize and verify certificate folders and files
folderStructure = new ClientCertificateFolderStructure();
this.insHost = insHost;
this.insPort = insPort;
}
/**
@@ -65,8 +76,8 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
*/
private void createNetworkClient() {
clientToINS = new NetworkClient.ClientBuilder().setLogger(protocolBridge.getLogger()).setProxy(protocolBridge.getProxy()).
setHost(protocolBridge.getProtocolSettings().host).setPort(protocolBridge.getProtocolSettings().port).
setPacketHandler(protocolBridge.getProtocolSettings().packetHandler).setEventManager(protocolBridge.getProtocolSettings().eventManager).
setHost(insHost).setTcpPort(insPort).setUdpPort(0).
setPacketHandler(protocolBridge.getProtocolValues().packetHandler).setEventManager(protocolBridge.getProtocolValues().eventManager).
setRootCAFolder(folderStructure.publicCAFolder).setClientCertificatesFolder(folderStructure.publicClientFolder, folderStructure.privateClientFolder).
build();
}
@@ -75,16 +86,18 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
* Connects to a Server.
*
* @param host Server host
* @param port Server port
* @param tcpPort TCP port
* @param udpPort UDP port
*/
public final void connectToServer(String host, int port) {
public final void connectToServer(String host, int tcpPort, int udpPort) {
if (!protocolBridge.isRunningAsClient())
throw new IllegalStateException("Not running as client");
if (clientToServer != null && clientToServer.isConnected())
if (clientToServer != null &&
(clientToServer.isFullyConnected() || clientToServer.isTcpConnected() || clientToServer.isUdpConnected()))
return;
createServerClient(host, port);
createServerClient(host, tcpPort, udpPort);
}
/**
@@ -100,16 +113,18 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
* Initialize connection to Server
*
* @param host Server host
* @param port Server port
* @param tcpPort Server port
* @param udpPort Server port
*/
private final void createServerClient(String host, int port) {
private final void createServerClient(String host, int tcpPort, int udpPort) {
clientToServer = new NetworkClient.ClientBuilder()
.setLogger(protocolBridge.getLogger())
.setProxy(protocolBridge.getProxy())
.setHost(host)
.setPort(port)
.setPacketHandler(protocolBridge.getProtocolSettings().packetHandler)
.setEventManager(protocolBridge.getProtocolSettings().eventManager)
.setTcpPort(tcpPort)
.setUdpPort(udpPort)
.setPacketHandler(protocolBridge.getProtocolValues().packetHandler)
.setEventManager(protocolBridge.getProtocolValues().eventManager)
.setRootCAFolder(folderStructure.publicCAFolder)
.setClientCertificatesFolder(
folderStructure.publicClientFolder,
@@ -190,7 +205,7 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
* @param serverVersion the ProtocolVersion to set for the server.
*/
public final void setServerVersion(ProtocolVersion serverVersion) {
if (serverVersion == null) this.serverVersion = insServerVersion;
if (serverVersion == null) this.serverVersion = serverVersion;
}
/**
@@ -218,12 +233,12 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
*/
public final void onDisconnect(ClientDisconnectedEvent event) {
// Reset server version on INS disconnect
if (!clientToINS.isConnected()) {
if (!clientToINS.isTcpConnected()) {
insServerVersion = null;
disconnectFromServer();
}
if (!clientToServer.isConnected()) serverVersion = null;
if (!clientToServer.isFullyConnected()) serverVersion = null;
}
/**
@@ -486,7 +501,7 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
public final void sendINSQuery(String tln, String name, String sub, INSRecordType type) throws IOException, ClassNotFoundException {
if (!protocolBridge.isRunningAsClient()) return;
getClientINSConnection().sendPacket(new INSQueryPacket(tln, name, sub, type, getClientINSConnection().getClientID()));
getClientINSConnection().sendPacket(new INSQueryPacket(tln, name, sub, type, getClientINSConnection().getClientId()), Transport.TCP);
onQuerySent(tln, name, sub, type);
}

View File

@@ -8,4 +8,13 @@ import org.openautonomousconnection.protocol.versions.ProtocolVersion;
*/
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.CLIENT)
public class ProtocolWebClient extends ProtocolClient {
/**
* Initializes the ProtocolClient, setting up certificate folders and the INS client connection.
*
* @param insHost
* @param insPort
*/
public ProtocolWebClient(String insHost, int insPort) {
super(insHost, insPort);
}
}

View File

@@ -1,6 +1,8 @@
package org.openautonomousconnection.protocol.side.ins;
import dev.unlegitdqrk.unlegitlibrary.file.ConfigurationManager;
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.TransportPolicy;
import lombok.Getter;
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
import org.openautonomousconnection.protocol.side.server.ProtocolCustomServer;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
@@ -17,49 +19,36 @@ import java.util.List;
*/
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.INS)
public abstract class ProtocolINSServer extends ProtocolCustomServer {
/**
* The configuration manager for handling server configurations.
*/
private final ConfigurationManager configurationManager;
/**
* Constructs a ProtocolINSServer with the specified configuration file.
*
* @param configFile The configuration file for the INS server.
* @throws IOException If an I/O error occurs.
* @throws CertificateException If a certificate error occurs.
*/
public ProtocolINSServer(File configFile) throws Exception {
super("ca_ins_", "cert_ins_");
// Ensure the configuration file exists
if (!configFile.exists()) configFile.createNewFile();
// Load the configuration properties
configurationManager = new ConfigurationManager(configFile);
configurationManager.loadProperties();
// Set default values for configuration properties if not already set
if (!configurationManager.isSet("server.site.info")) {
configurationManager.set("server.site.info", "INS-SERVER INFO SITE IP:PORT");
configurationManager.saveProperties();
}
if (!configurationManager.isSet("server.site.frontend")) {
configurationManager.set("server.site.frontend", "SERVER IP TO INS-FRONTEND:PORT");
configurationManager.saveProperties();
}
setCustomClient(ConnectedProtocolClient.class);
}
/**
* Gets the INS information site URL from the configuration.
*
* @return The INS information site URL.
*/
public final String getINSInfoSite() {
return configurationManager.getString("server.site.info");
@Getter
private final String insInfoSite;
/**
* Gets the INS registration site URL from the configuration.
*
* @return The INS registration site URL.
*/
@Getter
private String insFrontendSite;
/**
* Constructs a ProtocolINSServer with the specified configuration file.
*
* @param insInfoSize The INS-InfoSize (IP:PORT)
* @param insFrontendSite The INS-InfoSize (IP:PORT)
* @throws IOException If an I/O error occurs.
* @throws CertificateException If a certificate error occurs.
*/
public ProtocolINSServer(String insInfoSize, String insFrontendSite, int port) throws Exception {
super("ins", "ins", port, 0);
this.insInfoSite = insInfoSize;
this.insFrontendSite = insFrontendSite;
setCustomClient(ConnectedProtocolClient.class);
}
/**
@@ -147,13 +136,4 @@ public abstract class ProtocolINSServer extends ProtocolCustomServer {
* or <code>null</code> if the TLN has no registered info site.
*/
public abstract String resolveTLNInfoSite(String tln);
/**
* Gets the INS registration site URL from the configuration.
*
* @return The INS registration site URL.
*/
public final String getINSFrontendSite() {
return configurationManager.getString("server.site.frontend");
}
}

View File

@@ -8,7 +8,7 @@ import org.openautonomousconnection.protocol.versions.ProtocolVersion;
public abstract class CustomConnectedClient {
@Getter
private final ConnectionHandler pipelineConnection;
private final ConnectionHandler connection;
@Getter
private final ProtocolCustomServer server;
@@ -18,8 +18,8 @@ public abstract class CustomConnectedClient {
@Getter
private boolean clientVersionLoaded = false;
public CustomConnectedClient(ConnectionHandler pipelineConnection, ProtocolCustomServer protocolServer) {
this.pipelineConnection = pipelineConnection;
public CustomConnectedClient(ConnectionHandler connection, ProtocolCustomServer protocolServer) {
this.connection = connection;
this.server = protocolServer;
}
@@ -29,7 +29,7 @@ public abstract class CustomConnectedClient {
} catch (Exception ignored) {
}
try {
pipelineConnection.disconnect();
connection.disconnect();
} catch (Exception ignored) {
}
}

View File

@@ -1,6 +1,8 @@
package org.openautonomousconnection.protocol.side.server;
import dev.unlegitdqrk.unlegitlibrary.network.system.server.NetworkServer;
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.ClientID;
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.TransportPolicy;
import dev.unlegitdqrk.unlegitlibrary.network.utils.NetworkUtils;
import lombok.Getter;
import lombok.Setter;
@@ -50,17 +52,26 @@ public abstract class ProtocolCustomServer {
* The network server handling pipeline connections.
*/
@Getter
private NetworkServer pipelineServer;
private NetworkServer network;
@Getter
private int tcpPort;
@Getter
private int udpPort;
/**
* Initializes the web server with the given configuration, authentication, and rules files.
*
* @throws Exception If an error occurs during initialization.
*/
public ProtocolCustomServer(String caPrefix, String certPrefix) throws Exception {
public ProtocolCustomServer(String caPrefix, String certPrefix, int tcpPort, int udpPort) throws Exception {
// Initialize the list of connected clients
this.clients = new ArrayList<>();
this.tcpPort = tcpPort;
this.udpPort = udpPort;
// Set up folder structure for certificates
folderStructure = new ServerCertificateFolderStructure(caPrefix, certPrefix);
@@ -91,10 +102,11 @@ public abstract class ProtocolCustomServer {
* Initialize the pipeline server
*/
private void createNetworkServer() {
pipelineServer = new NetworkServer.ServerBuilder().
setPort(protocolBridge.getProtocolSettings().port).setTimeout(0).
setPacketHandler(protocolBridge.getProtocolSettings().packetHandler).setEventManager(protocolBridge.getProtocolSettings().eventManager).
setLogger(protocolBridge.getLogger()).
network = new NetworkServer.ServerBuilder().
setTcpPort(tcpPort).setUdpPort(udpPort).setTransportPolicy(TransportPolicy.bothRequired()).
setTimeout(0).
setPacketHandler(protocolBridge.getProtocolValues().packetHandler).
setEventManager(protocolBridge.getProtocolValues().eventManager).setLogger(protocolBridge.getLogger()).
setServerCertificate(certFile, keyFile).setRootCAFolder(folderStructure.publicCAFolder).
build();
}
@@ -105,9 +117,9 @@ public abstract class ProtocolCustomServer {
* @param clientID The client ID to search for.
* @return The connected web client with the specified ID, or null if not found.
*/
public final <T extends CustomConnectedClient> T getClientByID(int clientID) {
public final <T extends CustomConnectedClient> T getClientByID(ClientID clientID) {
for (CustomConnectedClient client : clients)
if (client.getPipelineConnection().getClientID() == clientID) return (T) client;
if (client.getConnection().getClientId() == clientID) return (T) client;
return null;
}
@@ -176,8 +188,8 @@ public abstract class ProtocolCustomServer {
private String certPrefix = "cert_server_";
public ServerCertificateFolderStructure(String caPrefix, String certPrefix) {
this.caPrefix = caPrefix;
this.certPrefix = certPrefix;
this.caPrefix = "ca_" + caPrefix + "_";
this.certPrefix = "cert_" + certPrefix + "_";
certificatesFolder = new File("certificates");

View File

@@ -9,11 +9,7 @@ import org.openautonomousconnection.protocol.side.server.CustomConnectedClient;
*/
public final class ConnectedWebClient extends CustomConnectedClient {
@Getter
private final ProtocolWebServer server;
public ConnectedWebClient(ConnectionHandler pipelineConnection, ProtocolWebServer webServer) {
super(pipelineConnection, webServer);
this.server = webServer;
}
}

View File

@@ -1,6 +1,7 @@
package org.openautonomousconnection.protocol.side.web;
import dev.unlegitdqrk.unlegitlibrary.file.FileUtils;
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.TransportPolicy;
import dev.unlegitdqrk.unlegitlibrary.string.RandomString;
import lombok.Getter;
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
@@ -30,31 +31,39 @@ public abstract class ProtocolWebServer extends ProtocolCustomServer {
*/
@Getter
private final File errorsFolder;
/**
* The configuration for the web server.
*/
@Getter
private final WebServerConfig serverConfig;
/**
* A unique secret for session management.
*/
@Getter
private String uniqueSessionString;
/**
* The expiration time of a Session in minutes
*/
@Getter
private int sessionExpire;
/**
* The max upload size in MB
*/
@Getter
private int maxUploadSize;
/**
* Initializes the web server with the given configuration, authentication, and rules files.
*
* @param serverConfig The configuration.
* @param authFile The authentication file.
* @param rulesFile The rules file.
* @param sessionExpire The expiration time of a Session in minutes
* @param uploadSize The max upload size in MB
* @throws Exception If an error occurs during initialization.
*/
public ProtocolWebServer(WebServerConfig serverConfig, File authFile, File rulesFile) throws Exception {
super("ca_server_", "cert_server_");
public ProtocolWebServer(File authFile, File rulesFile, int tcpPort, int udpPort,
int sessionExpire, int uploadSize) throws Exception {
super("server", "server", tcpPort, udpPort);
// Store the configuration file
this.serverConfig = serverConfig;
this.serverConfig.attachWebServer(this);
this.sessionExpire = sessionExpire;
this.maxUploadSize = uploadSize;
// Set up content and error folders
contentFolder = new File("content");

View File

@@ -1,51 +0,0 @@
package org.openautonomousconnection.protocol.side.web;
import dev.unlegitdqrk.unlegitlibrary.file.ConfigurationManager;
import java.io.File;
import java.io.IOException;
public class WebServerConfig {
private final File configFile;
private final ConfigurationManager configuration;
private ProtocolWebServer webServer;
public WebServerConfig(File configFile) throws IOException {
this.configFile = configFile;
if (!configFile.exists()) configFile.createNewFile();
configuration = new ConfigurationManager(configFile);
if (!configuration.isSet("sessionexpireMIN")) configuration.set("sessionexpireMIN", 60);
if (!configuration.isSet("maxuploadMB")) configuration.set("maxuploadMB", 10000);
if (!configuration.isSet("port")) configuration.set("port", 20);
configuration.saveProperties();
}
public void attachWebServer(ProtocolWebServer webServer) {
if (this.webServer != null) return;
this.webServer = webServer;
}
/**
* @return Web port
*/
public int getPort() {
return configuration.getInt("port");
}
/**
* @return The Session expiration time in minutes
*/
public int getSessionExpiration() {
return configuration.getInt("sessionexpireMIN");
}
/**
* @return Max upload size in MB
*/
public int getMaxUpload() {
return configuration.getInt("maxuploadMB");
}
}

View File

@@ -128,7 +128,7 @@ public final class SessionManager {
this.login = login;
this.ip = ip;
this.userAgent = userAgent;
this.expiresAt = System.currentTimeMillis() + (long) protocolWebServer.getServerConfig().getSessionExpiration() * 60 * 1000;
this.expiresAt = System.currentTimeMillis() + (long) protocolWebServer.getSessionExpire() * 60 * 1000;
}
/**
@@ -158,7 +158,7 @@ public final class SessionManager {
* @throws IOException If an I/O error occurs.
*/
void refresh(ProtocolWebServer protocolWebServer) throws IOException {
this.expiresAt = System.currentTimeMillis() + (long) protocolWebServer.getServerConfig().getSessionExpiration() * 60 * 1000;
this.expiresAt = System.currentTimeMillis() + (long) protocolWebServer.getSessionExpire() * 60 * 1000;
}
}
}