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

@@ -6,7 +6,7 @@
<groupId>org.openautonomousconnection</groupId>
<artifactId>Protocol</artifactId>
<version>1.0.0-BETA.5.4</version>
<version>1.0.0-BETA.6.0</version>
<organization>
<name>Open Autonomous Connection</name>
<url>https://open-autonomous-connection.org/</url>
@@ -119,7 +119,7 @@
<dependency>
<groupId>dev.unlegitdqrk</groupId>
<artifactId>unlegitlibrary</artifactId>
<version>1.6.9</version>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>

View File

@@ -10,24 +10,16 @@ import org.openautonomousconnection.protocol.packets.OACPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.AuthPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.INSQueryPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.INSResponsePacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.UnsupportedClassicPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebRequestPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebResponsePacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.stream.WebStreamChunkPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.stream.WebStreamEndPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.stream.WebStreamStartPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_DomainPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_MessagePacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_PingPacket;
import org.openautonomousconnection.protocol.side.client.ProtocolClient;
import org.openautonomousconnection.protocol.side.ins.ProtocolINSServer;
import org.openautonomousconnection.protocol.side.server.ProtocolCustomServer;
import org.openautonomousconnection.protocol.side.web.ProtocolWebServer;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerClient;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerINSServer;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerWebServer;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ClientListener;
import java.io.File;
import java.io.IOException;
@@ -42,7 +34,7 @@ public final class ProtocolBridge {
* The protocol settings for the current connection
*/
@Getter
private final ProtocolSettings protocolSettings;
private final ProtocolValues protocolValues;
/**
* The protocol version for the current connection
@@ -69,27 +61,6 @@ public final class ProtocolBridge {
@Getter
private ProtocolCustomServer protocolServer;
/**
* The classic protocol handlers for INS server side
*/
@Getter
@Setter
private ClassicHandlerINSServer classicHandlerINSServer;
/**
* The classic protocol handlers for web server side
*/
@Getter
@Setter
private ClassicHandlerWebServer classicHandlerWebServer;
/**
* The classic protocol handlers for client side
*/
@Getter
@Setter
private ClassicHandlerClient classicHandlerClient;
/**
* The proxy for client side
*/
@@ -101,16 +72,16 @@ public final class ProtocolBridge {
* Initialize the ProtocolBridge instance for the client side
*
* @param protocolServer The ProtocolCustomServer instance
* @param protocolSettings The ProtocolSettings instance
* @param protocolValues The ProtocolSettings instance
* @param protocolVersion The ProtocolVersion instance
* @param logFolder The folder to store the log files
* @throws Exception if an error occurs while initializing the ProtocolBridge
*/
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.CLIENT)
public ProtocolBridge(ProtocolCustomServer protocolServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception {
public ProtocolBridge(ProtocolCustomServer protocolServer, ProtocolValues protocolValues, ProtocolVersion protocolVersion, File logFolder) throws Exception {
// Assign the parameters to the class fields
this.protocolServer = protocolServer;
this.protocolSettings = protocolSettings;
this.protocolValues = protocolValues;
this.protocolVersion = protocolVersion;
// Initialize the logger and protocol version
@@ -128,16 +99,16 @@ public final class ProtocolBridge {
* Initialize the ProtocolBridge instance for the client side
*
* @param protocolClient The ProtocolClient instance
* @param protocolSettings The ProtocolSettings instance
* @param protocolValues The ProtocolSettings instance
* @param protocolVersion The ProtocolVersion instance
* @param logFolder The folder to store the log files
* @throws Exception if an error occurs while initializing the ProtocolBridge
*/
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.CLIENT)
public ProtocolBridge(ProtocolClient protocolClient, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception {
public ProtocolBridge(ProtocolClient protocolClient, ProtocolValues protocolValues, ProtocolVersion protocolVersion, File logFolder) throws Exception {
// Assign the parameters to the class fields
this.protocolClient = protocolClient;
this.protocolSettings = protocolSettings;
this.protocolValues = protocolValues;
this.protocolVersion = protocolVersion;
// Initialize the logger and protocol version
@@ -155,18 +126,8 @@ public final class ProtocolBridge {
* Register the appropriate packets based on the current protocol version
*/
private void registerPackets() {
// Classic packets
Classic_DomainPacket cDomainPacket = new Classic_DomainPacket();
Classic_MessagePacket cMessagePacket = new Classic_MessagePacket();
Classic_PingPacket cPingPacket = new Classic_PingPacket();
if (isPacketSupported(cDomainPacket)) protocolSettings.packetHandler.registerPacket(cDomainPacket);
if (isPacketSupported(cMessagePacket)) protocolSettings.packetHandler.registerPacket(cMessagePacket);
if (isPacketSupported(cPingPacket)) protocolSettings.packetHandler.registerPacket(cPingPacket);
// 1.0.0-BETA packets
AuthPacket v100bAuthPath = new AuthPacket();
UnsupportedClassicPacket v100bUnsupportedClassicPacket = new UnsupportedClassicPacket();
INSQueryPacket v100BINSQueryPacket = new INSQueryPacket();
INSResponsePacket v100BINSResponsePacket = new INSResponsePacket(this);
WebRequestPacket v100BWebRequestPacket = new WebRequestPacket();
@@ -175,21 +136,19 @@ public final class ProtocolBridge {
WebStreamStartPacket v100BStreamStartPacket = new WebStreamStartPacket();
WebStreamEndPacket v100BStreamEndPacket = new WebStreamEndPacket();
if (isPacketSupported(v100bAuthPath)) protocolSettings.packetHandler.registerPacket(v100bAuthPath);
if (isPacketSupported(v100bUnsupportedClassicPacket))
protocolSettings.packetHandler.registerPacket(v100bUnsupportedClassicPacket);
if (isPacketSupported(v100BINSQueryPacket)) protocolSettings.packetHandler.registerPacket(v100BINSQueryPacket);
if (isPacketSupported(v100bAuthPath)) protocolValues.packetHandler.registerPacket(v100bAuthPath);
if (isPacketSupported(v100BINSQueryPacket)) protocolValues.packetHandler.registerPacket(v100BINSQueryPacket);
if (isPacketSupported(v100BINSResponsePacket))
protocolSettings.packetHandler.registerPacket(v100BINSResponsePacket);
protocolValues.packetHandler.registerPacket(v100BINSResponsePacket);
if (isPacketSupported(v100BWebRequestPacket))
protocolSettings.packetHandler.registerPacket(v100BWebRequestPacket);
if (isPacketSupported(v100BResponsePacket)) protocolSettings.packetHandler.registerPacket(v100BResponsePacket);
protocolValues.packetHandler.registerPacket(v100BWebRequestPacket);
if (isPacketSupported(v100BResponsePacket)) protocolValues.packetHandler.registerPacket(v100BResponsePacket);
if (isPacketSupported(v100BStreamChunkPacket))
protocolSettings.packetHandler.registerPacket(v100BStreamChunkPacket);
protocolValues.packetHandler.registerPacket(v100BStreamChunkPacket);
if (isPacketSupported(v100BStreamStartPacket))
protocolSettings.packetHandler.registerPacket(v100BStreamStartPacket);
protocolValues.packetHandler.registerPacket(v100BStreamStartPacket);
if (isPacketSupported(v100BStreamEndPacket))
protocolSettings.packetHandler.registerPacket(v100BStreamEndPacket);
protocolValues.packetHandler.registerPacket(v100BStreamEndPacket);
}
/**
@@ -198,27 +157,20 @@ public final class ProtocolBridge {
* @throws Exception if an error occurs while registering the listeners
*/
private void registerListeners() throws Exception {
// Classic listeners
if (isClassicSupported()) {
Classic_ClientListener classicListener = new Classic_ClientListener();
classicListener.setProtocolBridge(this);
protocolSettings.eventManager.registerListener(classicListener.getClass());
} else protocolSettings.eventManager.unregisterListener(Classic_ClientListener.class);
// Client Listeners
if (isRunningAsClient()) {
ClientListener clientListener = new ClientListener();
clientListener.setClient(protocolClient);
protocolSettings.eventManager.registerListener(clientListener.getClass());
protocolSettings.eventManager.unregisterListener(CustomServerListener.class);
protocolValues.eventManager.registerListener(clientListener.getClass());
protocolValues.eventManager.unregisterListener(CustomServerListener.class);
}
// Server Listeners
if (isRunningAsServer()) {
CustomServerListener serverListener = new CustomServerListener();
serverListener.setServer(protocolServer);
protocolSettings.eventManager.registerListener(serverListener.getClass());
protocolSettings.eventManager.unregisterListener(ClientListener.class);
protocolValues.eventManager.registerListener(serverListener.getClass());
protocolValues.eventManager.unregisterListener(ClientListener.class);
}
}

View File

@@ -7,18 +7,7 @@ import dev.unlegitdqrk.unlegitlibrary.utils.DefaultMethodsOverrider;
/**
* Settings for the protocol connection.
*/
public final class ProtocolSettings extends DefaultMethodsOverrider {
/**
* The host to connect to.
*/
public String host;
/**
* The port to connect to.
*/
public int port;
public final class ProtocolValues extends DefaultMethodsOverrider {
/**
* The protocol version to use.
*/

View File

@@ -2,8 +2,9 @@ package org.openautonomousconnection.protocol.listeners;
import dev.unlegitdqrk.unlegitlibrary.event.EventListener;
import dev.unlegitdqrk.unlegitlibrary.event.Listener;
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.ClientConnectedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.ClientDisconnectedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.state.connect.ClientConnectedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.state.disconnect.ClientDisconnectedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.Transport;
import lombok.Getter;
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.AuthPacket;
@@ -43,9 +44,10 @@ public final class ClientListener extends EventListener {
@Listener
public void onConnect(ClientConnectedEvent event) {
try {
event.getClient().sendPacket(new AuthPacket(client.getProtocolBridge()));
if (event.getTransport() != Transport.TCP) return;
event.getClient().sendPacket(new AuthPacket(client.getProtocolBridge()), Transport.TCP);
} catch (IOException | ClassNotFoundException exception) {
event.getClient().getLogger().exception("Failed to send auth packet", exception);
client.getProtocolBridge().getLogger().exception("Failed to send auth packet", exception);
event.getClient().disconnect();
}
}

View File

@@ -3,9 +3,11 @@ package org.openautonomousconnection.protocol.listeners;
import dev.unlegitdqrk.unlegitlibrary.event.EventListener;
import dev.unlegitdqrk.unlegitlibrary.event.Listener;
import dev.unlegitdqrk.unlegitlibrary.network.system.server.ConnectionHandler;
import dev.unlegitdqrk.unlegitlibrary.network.system.server.events.ConnectionHandlerConnectedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.server.events.ConnectionHandlerDisconnectedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.server.events.S_PacketReceivedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.server.events.packets.receive.S_PacketReceivedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.server.events.state.connect.ConnectionHandlerConnectedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.server.events.state.disconnect.ConnectionHandlerDisconnectedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.server.events.state.disconnect.ConnectionHandlerFullyDisconnectedEvent;
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.Transport;
import lombok.Getter;
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.INSQueryPacket;
@@ -72,13 +74,8 @@ public final class CustomServerListener extends EventListener {
* @param event The connection handler disconnected event.
*/
@Listener
public void onDisconnect(ConnectionHandlerDisconnectedEvent event) {
for (CustomConnectedClient client : server.getClients()) {
if (client.getPipelineConnection().getClientID() != -1) continue;
server.getProtocolBridge().getProtocolSettings().eventManager.executeEvent(new S_CustomClientDisconnectedEvent(client));
}
server.getClients().removeIf(client -> client.getPipelineConnection().getClientID() == -1);
public void onDisconnect(ConnectionHandlerFullyDisconnectedEvent event) {
server.getClients().removeIf(client -> client.getConnection().getClientId().equals(client.getConnection().getClientId()));
}
@Listener
@@ -86,7 +83,7 @@ public final class CustomServerListener extends EventListener {
if (!server.getProtocolBridge().isRunningAsWebServer()) return;
if (!(event.getPacket() instanceof WebRequestPacket packet)) return;
((ProtocolWebServer) server).onWebRequest(server.getClientByID(event.getConnectionHandler().getClientID()), packet);
((ProtocolWebServer) server).onWebRequest(server.getClientByID(event.getConnectionHandler().getClientId()), packet);
}
/**
@@ -119,11 +116,11 @@ public final class CustomServerListener extends EventListener {
if (q.getSub() == null && q.getTLN().equalsIgnoreCase("oac")) {
if (q.getName().equalsIgnoreCase("info")) {
// Return INS server info site
String[] hostPort = insServer.getINSInfoSite().split(":");
String[] hostPort = insServer.getInsInfoSite().split(":");
resolved = List.of(new INSRecord(q.getType(), hostPort[0], -1, -1, Integer.parseInt(hostPort[1]), 0));
} else if (q.getName().equalsIgnoreCase("register")) {
// Return INS frontend site
String[] hostPort = insServer.getINSFrontendSite().split(":");
String[] hostPort = insServer.getInsFrontendSite().split(":");
resolved = List.of(new INSRecord(q.getType(), hostPort[0], -1, -1, Integer.parseInt(hostPort[1]), 0));
} else {
// Not a special name → use normal resolving
@@ -146,7 +143,7 @@ public final class CustomServerListener extends EventListener {
INSResponsePacket response = new INSResponsePacket(status, resolved, q.getClientId(), insServer.getProtocolBridge());
try {
event.getConnectionHandler().sendPacket(response);
event.getConnectionHandler().sendPacket(response, Transport.TCP);
insServer.onResponseSent(q.getTLN(), q.getName(), q.getSub(), q.getType(), resolved);
} catch (IOException | ClassNotFoundException e) {
insServer.onResponseSentFailed(q.getTLN(), q.getName(), q.getSub(), q.getType(), resolved, e);

View File

@@ -3,6 +3,7 @@ package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
import dev.unlegitdqrk.unlegitlibrary.file.FileUtils;
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
import dev.unlegitdqrk.unlegitlibrary.network.system.server.ConnectionHandler;
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.ClientID;
import dev.unlegitdqrk.unlegitlibrary.network.utils.NetworkUtils;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.packets.OACPacket;
@@ -93,7 +94,7 @@ public final class AuthPacket extends OACPacket {
objectOutputStream.writeUTF(caPem);
objectOutputStream.writeUTF(caSrl);
} else if (protocolBridge.isRunningAsClient()) {
objectOutputStream.writeInt(protocolBridge.getProtocolClient().getClientINSConnection().getClientID());
objectOutputStream.writeObject(protocolBridge.getProtocolClient().getClientINSConnection().getClientId());
objectOutputStream.writeObject(protocolBridge.getProtocolVersion());
}
}
@@ -110,9 +111,9 @@ public final class AuthPacket extends OACPacket {
@Override
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
if (protocolBridge.isRunningAsServer()) {
int clientID = objectInputStream.readInt();
ClientID clientID = (ClientID) objectInputStream.readObject();
ProtocolVersion clientVersion = (ProtocolVersion) objectInputStream.readObject();
ConnectionHandler connectionHandler = protocolBridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID);
ConnectionHandler connectionHandler = protocolBridge.getProtocolServer().getNetwork().getConnectionHandlerByClientId(clientID);
if (!protocolBridge.isVersionSupported(clientVersion)) {
setResponseCode(INSResponseStatus.RESPONSE_AUTH_FAILED);
@@ -123,7 +124,7 @@ public final class AuthPacket extends OACPacket {
CustomConnectedClient client = protocolBridge.getProtocolServer().getClientByID(clientID);
client.setClientVersion(clientVersion);
protocolBridge.getProtocolSettings().eventManager.executeEvent(new S_CustomClientConnectedEvent(client));
protocolBridge.getProtocolValues().eventManager.executeEvent(new S_CustomClientConnectedEvent(client));
} else if (protocolBridge.isRunningAsClient()) {
ProtocolVersion serverVersion = (ProtocolVersion) objectInputStream.readObject();
@@ -163,10 +164,10 @@ public final class AuthPacket extends OACPacket {
}
protocolBridge.getProtocolClient().setInsServerVersion(serverVersion);
protocolBridge.getProtocolSettings().eventManager.executeEvent(new ConnectedToProtocolINSServerEvent(protocolBridge.getProtocolClient()));
protocolBridge.getProtocolValues().eventManager.executeEvent(new ConnectedToProtocolINSServerEvent(protocolBridge.getProtocolClient()));
} catch (Exception ignored) {
protocolBridge.getProtocolClient().setServerVersion(serverVersion);
protocolBridge.getProtocolSettings().eventManager.executeEvent(new ConnectedToProtocolServerEvent(protocolBridge.getProtocolClient()));
protocolBridge.getProtocolValues().eventManager.executeEvent(new ConnectedToProtocolServerEvent(protocolBridge.getProtocolClient()));
}
}
}

View File

@@ -1,6 +1,7 @@
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.ClientID;
import lombok.Getter;
import org.openautonomousconnection.protocol.packets.OACPacket;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
@@ -33,7 +34,7 @@ public final class INSQueryPacket extends OACPacket {
@Getter
private INSRecordType type;
@Getter
private int clientId;
private ClientID clientId;
/**
* Creates a new INS query packet with all required parameters.
@@ -44,7 +45,7 @@ public final class INSQueryPacket extends OACPacket {
* @param type Record type requested.
* @param clientId Sender client ID for routing.
*/
public INSQueryPacket(String tln, String name, String sub, INSRecordType type, int clientId) {
public INSQueryPacket(String tln, String name, String sub, INSRecordType type, ClientID clientId) {
super(5, ProtocolVersion.PV_1_0_0_BETA);
this.TLN = tln;
this.name = name;
@@ -72,7 +73,7 @@ public final class INSQueryPacket extends OACPacket {
if (sub != null) out.writeUTF(sub);
out.writeObject(type);
out.writeInt(clientId);
out.writeObject(clientId);
}
/**
@@ -87,6 +88,6 @@ public final class INSQueryPacket extends OACPacket {
sub = hasSub ? in.readUTF() : null;
type = (INSRecordType) in.readObject();
clientId = in.readInt();
clientId = (ClientID) in.readObject();
}
}

View File

@@ -1,6 +1,7 @@
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.ClientID;
import lombok.Getter;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.packets.OACPacket;
@@ -35,7 +36,7 @@ public final class INSResponsePacket extends OACPacket {
@Getter
private List<INSRecord> records;
@Getter
private int clientId;
private ClientID clientId;
/**
* Creates a populated response packet.
@@ -45,7 +46,7 @@ public final class INSResponsePacket extends OACPacket {
* @param clientId ID of requesting client.
* @param bridge Protocol runtime context.
*/
public INSResponsePacket(INSResponseStatus status, List<INSRecord> records, int clientId, ProtocolBridge bridge) {
public INSResponsePacket(INSResponseStatus status, List<INSRecord> records, ClientID clientId, ProtocolBridge bridge) {
super(6, ProtocolVersion.PV_1_0_0_BETA);
this.status = status;
this.records = records;
@@ -75,7 +76,7 @@ public final class INSResponsePacket extends OACPacket {
out.writeObject(rec);
}
out.writeInt(clientId);
out.writeObject(clientId);
}
/**
@@ -92,7 +93,7 @@ public final class INSResponsePacket extends OACPacket {
records.add((INSRecord) in.readObject());
}
clientId = in.readInt();
clientId = (ClientID) in.readObject();
}
/**

View File

@@ -1,88 +0,0 @@
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.packets.OACPacket;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseStatus;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* Internal compatibility packet used when a classic-protocol packet is received
* but not supported by the current protocol version.
* <p>
* Instead of rejecting the packet entirely, the content and class name are forwarded
* to the appropriate compatibility handler:
* <ul>
* <li>{@link org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerINSServer}</li>
* <li>{@link org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerClient}</li>
* <li>{@link org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerWebServer}</li>
* </ul>
*/
public final class UnsupportedClassicPacket extends OACPacket {
private Class<? extends OACPacket> unsupportedClassicPacket;
private Object[] content;
private ProtocolBridge protocolBridge;
/**
* Constructs a packet describing the unsupported classic packet and its content.
*
* @param unsupportedClassicPacket The packet class that was not understood.
* @param content Serialized field values.
* @param protocolBridge The protocol context.
*/
public UnsupportedClassicPacket(Class<? extends OACPacket> unsupportedClassicPacket, Object[] content, ProtocolBridge protocolBridge) {
this();
this.unsupportedClassicPacket = unsupportedClassicPacket;
this.content = content;
this.protocolBridge = protocolBridge;
}
/**
* Registration Constructor
*/
public UnsupportedClassicPacket() {
super(7, ProtocolVersion.PV_1_0_0_BETA);
}
/**
* Writes the class name and serialized object fields to the stream.
*/
@Override
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
if (protocolBridge.isRunningAsClient())
objectOutputStream.writeInt(protocolBridge.getProtocolClient().getClientINSConnection().getClientID());
objectOutputStream.writeUTF(unsupportedClassicPacket.getName());
objectOutputStream.writeInt(content.length);
for (Object o : content) objectOutputStream.writeObject(o);
setResponseCode(INSResponseStatus.RESPONSE_NOT_REQUIRED);
}
/**
* Reads the unsupported packet data and forwards it to the appropriate compatibility handler.
*/
@Override
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
int clientID = 0;
if (protocolBridge.isRunningAsINSServer()) clientID = objectInputStream.readInt();
String className = objectInputStream.readUTF();
int size = objectInputStream.readInt();
content = new Object[size];
for (int i = 0; i < size; i++) {
content[i] = objectInputStream.readObject();
}
if (protocolBridge.isRunningAsINSServer())
protocolBridge.getClassicHandlerINSServer().unsupportedClassicPacket(className, content, protocolBridge.getProtocolServer().getClientByID(clientID));
else if (protocolBridge.isRunningAsClient())
protocolBridge.getClassicHandlerClient().unsupportedClassicPacket(className, content);
else if (protocolBridge.isRunningAsWebServer())
protocolBridge.getClassicHandlerWebServer().unsupportedClassicPacket(className, content, protocolBridge.getProtocolServer().getClientByID(clientID));
}
}

View File

@@ -1,87 +0,0 @@
package org.openautonomousconnection.protocol.packets.v1_0_0.classic;
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.packets.OACPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.UnsupportedClassicPacket;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.events.Classic_DomainPacketReceivedEvent;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_Domain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_RequestDomain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.SQLException;
public final class Classic_DomainPacket extends OACPacket {
private Classic_RequestDomain requestDomain;
private Classic_Domain domain;
private int clientID;
private ProtocolBridge bridge;
public Classic_DomainPacket(int toClient, Classic_RequestDomain requestDomain, Classic_Domain domain, ProtocolBridge protocolBridge) {
this();
this.clientID = toClient;
this.bridge = protocolBridge;
this.requestDomain = requestDomain;
this.domain = domain;
}
// Registration constructor
public Classic_DomainPacket() {
super(2, ProtocolVersion.PV_1_0_0_CLASSIC);
}
@Override
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
if (bridge.isRunningAsINSServer()) {
objectOutputStream.writeInt(clientID);
objectOutputStream.writeObject(requestDomain);
objectOutputStream.writeObject(domain);
} else if (bridge.isRunningAsClient()) {
clientID = bridge.getProtocolClient().getClientINSConnection().getClientID();
objectOutputStream.writeInt(clientID);
objectOutputStream.writeObject(requestDomain);
}
objectOutputStream.writeObject(Classic_ProtocolVersion.PV_1_0_0);
}
@Override
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
if (bridge.isRunningAsINSServer()) {
clientID = objectInputStream.readInt();
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
try {
domain = bridge.getClassicHandlerINSServer().getDomain(requestDomain);
} catch (SQLException exception) {
exception.printStackTrace();
}
bridge.getProtocolServer().getPipelineServer().getEventManager().executeEvent(new Classic_DomainPacketReceivedEvent(protocolVersion, domain, requestDomain, clientID));
if (bridge.getProtocolServer().getClientByID(clientID).supportClientClassic())
bridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new Classic_DomainPacket(clientID, requestDomain, domain, bridge));
else
bridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{clientID, requestDomain, domain}, bridge));
} else if (bridge.isRunningAsClient()) {
clientID = objectInputStream.readInt();
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
domain = (Classic_Domain) objectInputStream.readObject();
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
bridge.getProtocolClient().getClientINSConnection().getEventManager().executeEvent(new Classic_DomainPacketReceivedEvent(protocolVersion, domain, requestDomain, clientID));
} else if (bridge.isRunningAsWebServer()) {
clientID = objectInputStream.readInt();
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
bridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{clientID, requestDomain, domain}, bridge));
}
}
}

View File

@@ -1,65 +0,0 @@
package org.openautonomousconnection.protocol.packets.v1_0_0.classic;
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.packets.OACPacket;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public final class Classic_MessagePacket extends OACPacket {
private String message;
private int clientID;
private ProtocolBridge bridge;
// Constructor with message and client id
public Classic_MessagePacket(String message, int clientID, ProtocolBridge bridge) {
this();
this.message = message;
this.clientID = clientID;
this.bridge = bridge;
}
public Classic_MessagePacket() {
super(3, ProtocolVersion.PV_1_0_0_CLASSIC);
}
@Override
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
if (bridge.isRunningAsINSServer() || bridge.isRunningAsWebServer())
objectOutputStream.writeInt(clientID);
else if (bridge.isRunningAsClient()) {
clientID = bridge.getProtocolClient().getClientINSConnection().getClientID();
objectOutputStream.writeInt(clientID);
}
objectOutputStream.writeUTF(message);
objectOutputStream.writeObject(Classic_ProtocolVersion.PV_1_0_0);
}
@Override
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
if (bridge.isRunningAsINSServer()) {
clientID = objectInputStream.readInt();
String message = objectInputStream.readUTF();
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
bridge.getClassicHandlerINSServer().handleMessage(bridge.getProtocolServer().getClientByID(clientID), message, protocolVersion);
} else if (bridge.isRunningAsClient()) {
clientID = objectInputStream.readInt();
String message = objectInputStream.readUTF();
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
bridge.getClassicHandlerClient().handleMessage(message, protocolVersion);
} else if (bridge.isRunningAsWebServer()) {
clientID = objectInputStream.readInt();
String message = objectInputStream.readUTF();
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
bridge.getClassicHandlerWebServer().handleMessage(bridge.getProtocolServer().getClientByID(clientID), message, protocolVersion);
}
}
}

View File

@@ -1,93 +0,0 @@
package org.openautonomousconnection.protocol.packets.v1_0_0.classic;
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.packets.OACPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.UnsupportedClassicPacket;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseStatus;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.events.Classic_PingPacketReceivedEvent;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_Domain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_RequestDomain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.SQLException;
public final class Classic_PingPacket extends OACPacket {
private Classic_RequestDomain requestDomain;
private Classic_Domain domain;
private int clientID;
private boolean reachable;
private Classic_ProtocolVersion protocolVersion;
private ProtocolBridge bridge;
public Classic_PingPacket(Classic_RequestDomain requestDomain, Classic_Domain domain, boolean reachable, ProtocolBridge bridge) {
this();
this.bridge = bridge;
this.requestDomain = requestDomain;
this.domain = domain;
this.reachable = reachable;
this.protocolVersion = Classic_ProtocolVersion.PV_1_0_0;
}
public Classic_PingPacket() {
super(1, ProtocolVersion.PV_1_0_0_CLASSIC);
}
@Override
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
if (bridge.isRunningAsINSServer()) {
objectOutputStream.writeInt(clientID);
objectOutputStream.writeObject(requestDomain);
objectOutputStream.writeObject(domain);
objectOutputStream.writeBoolean(reachable);
} else if (bridge.isRunningAsClient()) {
clientID = bridge.getProtocolClient().getClientINSConnection().getClientID();
objectOutputStream.writeInt(clientID);
objectOutputStream.writeObject(requestDomain);
}
objectOutputStream.writeObject(protocolVersion);
}
@Override
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
if (bridge.isRunningAsINSServer()) {
clientID = objectInputStream.readInt();
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
try {
domain = bridge.getClassicHandlerINSServer().ping(requestDomain);
} catch (SQLException exception) {
exception.printStackTrace();
}
reachable = domain != null;
bridge.getProtocolServer().getPipelineServer().getEventManager().executeEvent(new Classic_PingPacketReceivedEvent(protocolVersion, domain, requestDomain, reachable, clientID));
if (bridge.getProtocolServer().getClientByID(clientID).supportClientClassic())
bridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new Classic_PingPacket(requestDomain, domain, reachable, bridge));
else
bridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{requestDomain, domain, reachable}, bridge));
} else if (bridge.isRunningAsClient()) {
clientID = objectInputStream.readInt();
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
domain = (Classic_Domain) objectInputStream.readObject();
boolean reachable = objectInputStream.readBoolean();
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
bridge.getClassicHandlerClient().validationCompleted(domain, reachable ? INSResponseStatus.OK : INSResponseStatus.NOT_FOUND);
bridge.getProtocolClient().getClientINSConnection().getEventManager().executeEvent(new Classic_PingPacketReceivedEvent(protocolVersion, domain, requestDomain, reachable, clientID));
} else if (bridge.isRunningAsWebServer()) {
clientID = objectInputStream.readInt();
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
bridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{requestDomain}, bridge));
}
}
}

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;
}
}
}

View File

@@ -1,4 +1,4 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils;
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
import java.io.Serializable;

View File

@@ -1,51 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.events;
import dev.unlegitdqrk.unlegitlibrary.event.impl.Event;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerINSServer;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_Domain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_RequestDomain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
/**
* This event is fired when a classic domain packet is received.
* This event is deprecated and will be marked for removal in future versions.
*
* @see ClassicHandlerINSServer
* @see org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerClient
* @see org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerWebServer
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public final class Classic_DomainPacketReceivedEvent extends Event {
public final Classic_ProtocolVersion protocolVersion;
public final Classic_Domain domain;
public final Classic_RequestDomain requestDomain;
public final int clientID;
public Classic_DomainPacketReceivedEvent(Classic_ProtocolVersion protocolVersion, Classic_Domain domain, Classic_RequestDomain requestDomain, int clientID) {
this.protocolVersion = protocolVersion;
this.domain = domain;
this.requestDomain = requestDomain;
this.clientID = clientID;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public String toString() {
return super.toString();
}
@Override
public int hashCode() {
return super.hashCode();
}
}

View File

@@ -1,52 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.events;
import dev.unlegitdqrk.unlegitlibrary.event.impl.Event;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerINSServer;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_Domain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_RequestDomain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
/**
* This event is fired when a classic ping packet is received.
* This event is deprecated and will be marked for removal in future versions.
*
* @see ClassicHandlerINSServer
* @see org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerClient
* @see org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerWebServer
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public final class Classic_PingPacketReceivedEvent extends Event {
public final Classic_ProtocolVersion protocolVersion;
public final Classic_Domain domain;
public final Classic_RequestDomain requestDomain;
public final boolean reachable;
public final int clientID;
public Classic_PingPacketReceivedEvent(Classic_ProtocolVersion protocolVersion, Classic_Domain domain, Classic_RequestDomain requestDomain, boolean reachable, int clientID) {
this.protocolVersion = protocolVersion;
this.domain = domain;
this.requestDomain = requestDomain;
this.reachable = reachable;
this.clientID = clientID;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public String toString() {
return super.toString();
}
@Override
public int hashCode() {
return super.hashCode();
}
}

View File

@@ -1,47 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers;
import lombok.Getter;
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_MessagePacket;
import org.openautonomousconnection.protocol.side.client.ProtocolClient;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseStatus;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_Domain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.site.Classic_SiteType;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
import java.io.IOException;
/**
* Abstract class defining the client-side handler for Classic protocol operations.
*/
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.CLIENT)
public abstract class ClassicHandlerClient {
/**
* Reference to the ProtocolClient
*/
@Getter
private final ProtocolClient client;
/**
* Sets the client variable
*
* @param client The ProtocolClient Object
*/
public ClassicHandlerClient(ProtocolClient client) {
this.client = client;
}
public abstract void unsupportedClassicPacket(String classicPacketClassName, Object[] content);
public abstract void handleHTMLContent(Classic_SiteType siteType, Classic_Domain domain, String html);
public abstract void handleMessage(String message, Classic_ProtocolVersion protocolVersion);
public final void sendMessage(String message) throws IOException, ClassNotFoundException {
client.getClientINSConnection().sendPacket(new Classic_MessagePacket(message, client.getClientServerConnection().getClientID(), client.getProtocolBridge()));
}
public abstract void validationCompleted(Classic_Domain domain, INSResponseStatus insResponseStatus);
}

View File

@@ -1,24 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers;
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
import org.openautonomousconnection.protocol.side.ins.ConnectedProtocolClient;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_Domain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_RequestDomain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
import java.sql.SQLException;
/**
* Abstract class defining the INS server-side handler for Classic protocol operations.
*/
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.INS)
public abstract class ClassicHandlerINSServer {
public abstract void handleMessage(ConnectedProtocolClient client, String message, Classic_ProtocolVersion protocolVersion);
public abstract Classic_Domain getDomain(Classic_RequestDomain requestDomain) throws SQLException;
public abstract Classic_Domain ping(Classic_RequestDomain requestDomain) throws SQLException;
public abstract void unsupportedClassicPacket(String className, Object[] content, ConnectedProtocolClient client);
}

View File

@@ -1,16 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers;
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
import org.openautonomousconnection.protocol.side.ins.ConnectedProtocolClient;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
/**
* Abstract class defining the web server-side handler for Classic protocol operations.
*/
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.WEB)
public abstract class ClassicHandlerWebServer {
public abstract void handleMessage(ConnectedProtocolClient client, String message, Classic_ProtocolVersion protocolVersion);
public abstract void unsupportedClassicPacket(String className, Object[] content, ConnectedProtocolClient client);
}

View File

@@ -1,74 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.builtin;
import lombok.Getter;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecordType;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseStatus;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerClient;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.helper.ClassicHelper;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_Domain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.site.Classic_SiteType;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.site.Classic_WebsitesContent;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
import java.io.IOException;
public abstract class ClientClassic extends ClassicHandlerClient {
@Getter
private final ProtocolBridge bridge;
@Getter
private final ClassicHelper helper;
public ClientClassic(ProtocolBridge bridge) {
super(bridge.getProtocolClient());
this.bridge = bridge;
this.helper = new ClassicHelper(bridge);
}
@Override
public void unsupportedClassicPacket(String className, Object[] content) {
bridge.getLogger().warn(
"[Classic UnsupportedPacket] packet=" + className + " content=" + java.util.Arrays.toString(content)
);
onUnsupportedClassicPacket(className, content);
}
/**
* Optional callback
*
* @param className The class name
* @param content The content
*/
public void onUnsupportedClassicPacket(String className, Object[] content) {
}
@Override
public void handleMessage(String message, Classic_ProtocolVersion protocolVersion) {
bridge.getLogger().info("[ClassicHandler] Message received (Classic Version " + protocolVersion.version + "): " + message);
onMessage(message, protocolVersion);
}
/**
* Optional callback
*
* @param message The Message
* @param protocolVersion the Classic version
*/
public void onMessage(String message, Classic_ProtocolVersion protocolVersion) {
}
@Override
public void validationCompleted(Classic_Domain domain, INSResponseStatus insResponseStatus) {
if (insResponseStatus == INSResponseStatus.OK) {
try {
bridge.getProtocolClient().sendINSQuery(domain.topLevelDomain, domain.name, null, INSRecordType.A);
} catch (IOException | ClassNotFoundException e) {
handleHTMLContent(Classic_SiteType.CLIENT, domain, Classic_WebsitesContent.ERROR_OCCURRED(e.getMessage()));
}
} else {
handleHTMLContent(Classic_SiteType.CLIENT, domain, Classic_WebsitesContent.DOMAIN_NOT_REACHABLE);
}
}
}

View File

@@ -1,93 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.builtin;
import lombok.Getter;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.side.ins.ConnectedProtocolClient;
import org.openautonomousconnection.protocol.side.ins.ProtocolINSServer;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecord;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecordType;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerINSServer;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.helper.ClassicHelper;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.helper.ParsedDomain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_Domain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_RequestDomain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
import java.sql.SQLException;
import java.util.List;
public class INSClassic extends ClassicHandlerINSServer {
@Getter
private final ProtocolBridge bridge;
@Getter
private final ClassicHelper helper;
public INSClassic(ProtocolBridge bridge) {
this.bridge = bridge;
this.helper = new ClassicHelper(bridge);
}
@Override
public void handleMessage(ConnectedProtocolClient client, String message, Classic_ProtocolVersion protocolVersion) {
client.getServer().getProtocolBridge().getLogger().info("[ClassicHandler] Message received from ClientID " +
client.getPipelineConnection().getClientID() +
" (Classic Version " + protocolVersion.version + ", Client Version: " +
client.getClientVersion().toString() + "): " + message);
onMessage(client, message, protocolVersion);
}
/**
* Optional callback.
*
* @param client The client sender.
* @param message The message.
* @param protocolVersion The classic version.
*/
public void onMessage(ConnectedProtocolClient client, String message, Classic_ProtocolVersion protocolVersion) {
}
@Override
public Classic_Domain getDomain(Classic_RequestDomain requestDomain) throws SQLException {
if (!bridge.isRunningAsINSServer()) return null;
ParsedDomain pd = helper.parseDomain(requestDomain);
ProtocolINSServer server = (ProtocolINSServer) bridge.getProtocolServer();
// IMPORTANT: resolve() already performs CNAME recursion + sorting.
// Request A directly and pick the first (= best after sorting).
List<INSRecord> aRecords = server.resolve(pd.tln(), pd.name(), pd.sub(), INSRecordType.A);
if (aRecords.isEmpty()) return null;
INSRecord targetA = aRecords.get(0);
return helper.buildClassicDomain(pd, targetA);
}
@Override
public Classic_Domain ping(Classic_RequestDomain req) throws SQLException {
if (!bridge.isRunningAsINSServer()) return null;
return new Classic_Domain(req.name, req.topLevelDomain, null, req.path, bridge);
}
@Override
public void unsupportedClassicPacket(String className, Object[] content, ConnectedProtocolClient client) {
client.getServer().getProtocolBridge().getLogger().warn(
"[Classic UnsupportedPacket] From client " + client.getPipelineConnection().getClientID() +
": packet=" + className + " content=" + java.util.Arrays.toString(content)
);
onUnsupportedClassicPacket(className, content, client);
}
/**
* Optional callback.
*
* @param className The class name.
* @param content The content.
* @param client The client sender.
*/
public void onUnsupportedClassicPacket(String className, Object[] content, ConnectedProtocolClient client) {
}
}

View File

@@ -1,56 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.builtin;
import lombok.Getter;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.side.ins.ConnectedProtocolClient;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerWebServer;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
public class WebClassic extends ClassicHandlerWebServer {
@Getter
private final ProtocolBridge bridge;
public WebClassic(ProtocolBridge bridge) {
this.bridge = bridge;
}
@Override
public void handleMessage(ConnectedProtocolClient client, String message, Classic_ProtocolVersion protocolVersion) {
client.getServer().getProtocolBridge().getLogger().info("[ClassicHandler] Message received from ClientID " +
client.getPipelineConnection().getClientID() +
" (Classic Version " + protocolVersion.version + ", Client Version: " +
client.getClientVersion().toString() + "): " + message);
onMessage(client, message, protocolVersion);
}
/**
* Optional callback
*
* @param client The client sender
* @param message The Message
* @param protocolVersion the Classic version
*/
public void onMessage(ConnectedProtocolClient client, String message, Classic_ProtocolVersion protocolVersion) {
}
@Override
public void unsupportedClassicPacket(String className, Object[] content, ConnectedProtocolClient client) {
client.getServer().getProtocolBridge().getLogger().warn(
"[Classic UnsupportedPacket] From client " + client.getPipelineConnection().getClientID() +
": packet=" + className + " content=" + java.util.Arrays.toString(content)
);
onUnsupportedClassicPacket(className, content, client);
}
/**
* Optional callback
*
* @param className The class name
* @param content The content
* @param client The client sender
*/
public void onUnsupportedClassicPacket(String className, Object[] content, ConnectedProtocolClient client) {
}
}

View File

@@ -1,41 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.helper;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecord;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_Domain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_RequestDomain;
import java.util.Arrays;
public record ClassicHelper(ProtocolBridge bridge) {
public Classic_Domain buildClassicDomain(ParsedDomain req, INSRecord rec) {
Classic_Domain info = new Classic_Domain(req.name(), req.tln(), req.sub(), req.path(), bridge);
String host = rec.value;
int port = rec.port > 0 ? rec.port : 80;
return new Classic_Domain(
req.name(), req.tln(),
host.contains(":") ? host : host + ":" + port,
req.path(), bridge);
}
public ParsedDomain parseDomain(Classic_RequestDomain req) {
String tln = req.topLevelDomain; // example: "net"
String full = req.name; // example: "api.v1.example"
String[] parts = full.split("\\.");
if (parts.length == 1) {
return new ParsedDomain(tln, full, null, req.path);
}
String name = parts[parts.length - 1];
String sub = parts.length > 1
? String.join(".", Arrays.copyOfRange(parts, 0, parts.length - 1))
: null;
return new ParsedDomain(tln, name, sub, req.path);
}
}

View File

@@ -1,4 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.helper;
public record ParsedDomain(String tln, String name, String sub, String path) {
}

View File

@@ -1,7 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.helper;
/**
* Represents a parsed CNAME target in (tln, name, sub) form.
*/
public record TargetName(String tln, String name, String sub) {
}

View File

@@ -1,69 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects;
import org.openautonomousconnection.protocol.ProtocolBridge;
import java.io.Serializable;
/**
* Classic_Domain is an old representation of a InfoName, maintained for backward compatibility.
* It encapsulates the InfoName's name, top-level name, path, and destination.
* This class is deprecated and users are encouraged to use the InfoName class instead.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public class Classic_Domain implements Serializable {
/**
* The name of the domain.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public final String name;
/**
* The top-level domain.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public final String topLevelDomain;
/**
* The path component of the domain.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public final String path;
/**
* The destination of the domain, typically the full URL or address.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public final String destination;
/**
* The ProtocolBridge reference.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public final ProtocolBridge protocolBridge;
public Classic_Domain(String name, String topLevelDomain, String destination, String path, ProtocolBridge bridge) {
this.protocolBridge = bridge;
this.name = name;
this.topLevelDomain = topLevelDomain;
this.destination = destination;
this.path = path;
}
@Override
protected final Object clone() throws CloneNotSupportedException {
return new Classic_Domain(name, topLevelDomain, destination, path, protocolBridge);
}
@Override
public final boolean equals(Object obj) {
if (!(obj instanceof Classic_Domain other)) return false;
return other.name.equalsIgnoreCase(name) && other.topLevelDomain.equalsIgnoreCase(topLevelDomain);
}
@Override
public final int hashCode() {
return super.hashCode();
}
}

View File

@@ -1,14 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects;
import org.openautonomousconnection.protocol.ProtocolBridge;
/**
* Class representing a local domain in the Classic protocol.
* This class extends Classic_Domain and is used for local domain representation.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public final class Classic_LocalDomain extends Classic_Domain {
public Classic_LocalDomain(String name, String endName, String path, ProtocolBridge protocolBridge) {
super(name, endName, null, path, protocolBridge);
}
}

View File

@@ -1,17 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects;
import org.openautonomousconnection.protocol.ProtocolBridge;
import java.io.Serializable;
/**
* Class representing a request for a domain in the Classic protocol.
* This class extends Classic_Domain and is used for requesting domain information.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public final class Classic_RequestDomain extends Classic_Domain implements Serializable {
public Classic_RequestDomain(String name, String topLevelDomain, String path, ProtocolBridge protocolBridge) {
super(name, topLevelDomain, null, path, protocolBridge);
}
}

View File

@@ -1,44 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.site;
import java.io.Serializable;
/**
* Enum representing different types of sites in the Classic protocol.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public enum Classic_SiteType implements Serializable {
/**
* Client site type.
*/
CLIENT("oac-client"),
/**
* Web server site type.
*/
SERVER("oac-server"),
/**
* INS server site type.
*/
PUBLIC("oac"),
/**
* Protocol site type.
*/
PROTOCOL("oac-protocol"),
/**
* Local site type.
*/
LOCAL("oac-local");
/**
* The name of the site type.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public final String name;
Classic_SiteType(String name) {
this.name = name;
}
}

View File

@@ -1,71 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.site;
import dev.unlegitdqrk.unlegitlibrary.utils.DefaultMethodsOverrider;
/**
* This class contains predefined HTML content for various website responses in the Classic protocol.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public final class Classic_WebsitesContent extends DefaultMethodsOverrider {
public static final String DOMAIN_NOT_FOUND = """
<html>
<head>
<title>404 - Domain not found</title>
<meta content="UTF-8" name="charset"/>
<meta content="Open Autonomous Connection" name="author"/>
<meta content="Domain not found" name="description"/>
</head>
<body>
<h1>404 - This infoName was not found</h1>
</body>
</html>
""";
public static final String FILE_NOT_FOUND = """
<html>
<head>
<title>404 - File not found</title>
<meta content="UTF-8" name="charset"/>
<meta content="Open Autonomous Connection" name="author"/>
<meta content="File not found" name="description"/>
</head>
<body>
<h1>404 - This file was not found</h1>
</body>
</html>
""";
public static final String DOMAIN_NOT_REACHABLE = """
<html>
<head>
<title>504 - Site not reachable</title>
<meta content="UTF-8" name="charset"/>
<meta content="Open Autonomous Connection" name="author"/>
<meta content="Site not reached" name="description"/>
</head>
<body>
<h1>504 - This site is currently not reachable</h1>
</body>
</html>
""";
public static String ERROR_OCCURRED = ERROR_OCCURRED("No specified details!");
public static String ERROR_OCCURRED(String errorDetails) {
return """
<html>
<head>
<title>500 - Error occurred</title>
<meta content="UTF-8" name="charset"/>
<meta content="Open Autonomous Connection" name="author"/>
<meta content="Site not reached" name="description"/>
</head>
<body>
<h1>500 - Error occured while resolving infoName!</h1>
<h4>Details:</h2>
<h5>""" + errorDetails + "</h5>" + """
</body>
</html>
""";
}
}

View File

@@ -1,131 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils;
import dev.unlegitdqrk.unlegitlibrary.event.EventListener;
import dev.unlegitdqrk.unlegitlibrary.event.Listener;
import lombok.Getter;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_PingPacket;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.events.Classic_DomainPacketReceivedEvent;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.events.Classic_PingPacketReceivedEvent;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_LocalDomain;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.site.Classic_SiteType;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.site.Classic_WebsitesContent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* This class listens for events related to Classic protocol operations on the client side.
* It handles domain resolution and ping responses, facilitating communication with the INS server
* and web content retrieval.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
public final class Classic_ClientListener extends EventListener {
/**
* Reference to the ProtocolBridge
*/
@Getter
private ProtocolBridge protocolBridge;
/**
* Set protocol bridge
*
* @param protocolBridge The ProtocolBridge object
*/
public void setProtocolBridge(ProtocolBridge protocolBridge) {
if (this.protocolBridge != null) return;
this.protocolBridge = protocolBridge;
}
/**
* Handles the event when a domain packet is received.
* It checks if the domain exists and sends a ping request to the INS server.
* If the domain does not exist, it handles the error accordingly.
*
* @param event The event containing domain information.
*/
@Listener
public void onDomain(Classic_DomainPacketReceivedEvent event) {
// Check if the domain exists
boolean exists = event.domain != null;
if (exists) {
try {
// Send a ping request to the INS server
if (!protocolBridge.getProtocolClient().getClientINSConnection().sendPacket(new Classic_PingPacket(event.requestDomain, event.domain, false, protocolBridge))) {
// If sending the packet fails, handle the error
protocolBridge.getClassicHandlerClient().handleHTMLContent(Classic_SiteType.PROTOCOL, new Classic_LocalDomain("error-occurred", "html", "", protocolBridge),
Classic_WebsitesContent.ERROR_OCCURRED(event.domain + "/" + event.domain.path));
}
} catch (IOException | ClassNotFoundException e) {
// Handle any exceptions that occur during the process
protocolBridge.getClassicHandlerClient().handleHTMLContent(Classic_SiteType.PROTOCOL, new Classic_LocalDomain("error-occurred", "html", "", protocolBridge),
Classic_WebsitesContent.ERROR_OCCURRED(event.domain + "/" + event.domain.path + ":\n" + e.getMessage()));
}
} else
// If the domain does not exist, handle the error
protocolBridge.getClassicHandlerClient().handleHTMLContent(Classic_SiteType.PROTOCOL, new Classic_LocalDomain("domain-not-found", "html", "", protocolBridge), Classic_WebsitesContent.DOMAIN_NOT_FOUND);
}
/**
* Handles the event when a ping packet is received.
* If the domain is reachable, it fetches the HTML content from the domain.
* If not reachable, it handles the error accordingly.
*
* @param event The event containing ping response information.
*/
@Listener
public void onPing(Classic_PingPacketReceivedEvent event) {
// If the domain is reachable, fetch the HTML content
if (event.reachable) {
String destination = event.domain.destination;
try {
// Create a URL object
URL url = new URL(destination);
HttpURLConnection connection2 = (HttpURLConnection) url.openConnection();
connection2.setRequestMethod("GET");
// Read the response
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection2.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) content.append(line);
}
// Handle the HTML content
protocolBridge.getClassicHandlerClient().handleHTMLContent(Classic_SiteType.PUBLIC, event.domain, content.toString());
} catch (IOException exception) {
// Handle any exceptions that occur during the process
protocolBridge.getClassicHandlerClient().handleHTMLContent(Classic_SiteType.PROTOCOL, new Classic_LocalDomain("error-occurred", "html", "", protocolBridge),
Classic_WebsitesContent.ERROR_OCCURRED(exception.getMessage().replace(event.domain.destination, event.domain + "/" + event.domain.path)));
}
} else
// If the domain is not reachable, handle the error
protocolBridge.getClassicHandlerClient().handleHTMLContent(Classic_SiteType.PROTOCOL, new Classic_LocalDomain("error-not-reached", "html", "", protocolBridge), Classic_WebsitesContent.DOMAIN_NOT_REACHABLE);
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public String toString() {
return super.toString();
}
@Override
public int hashCode() {
return super.hashCode();
}
}

View File

@@ -1,100 +0,0 @@
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils;
import dev.unlegitdqrk.unlegitlibrary.utils.DefaultMethodsOverrider;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.site.Classic_SiteType;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
/**
* Utility class for domain-related operations in the Classic protocol.
*/
@Deprecated(forRemoval = false, since = "1.0.0-BETA.3")
class Classic_DomainUtils extends DefaultMethodsOverrider {
/**
* Extracts the top-level domain (TLD) from a given URL.
*
* @param url The URL from which to extract the TLD.
* @return The top-level domain as a string.
* @throws MalformedURLException If the URL is malformed.
*/
public static String getTopLevelDomain(String url) throws MalformedURLException {
URL uri = null;
String tldString = null;
if (url.startsWith(Classic_SiteType.PUBLIC.name + "://"))
url = url.substring((Classic_SiteType.PUBLIC.name + "://").length());
if (url.startsWith(Classic_SiteType.CLIENT.name + "://"))
url = url.substring((Classic_SiteType.CLIENT.name + "://").length());
if (url.startsWith(Classic_SiteType.SERVER.name + "://"))
url = url.substring((Classic_SiteType.SERVER.name + "://").length());
if (url.startsWith(Classic_SiteType.PROTOCOL.name + "://"))
url = url.substring((Classic_SiteType.PROTOCOL.name + "://").length());
if (url.startsWith(Classic_SiteType.LOCAL.name + "://"))
url = url.substring((Classic_SiteType.LOCAL.name + "://").length());
if (!url.startsWith("https://") && !url.startsWith("http://")) url = "https://" + url;
uri = new URL(url);
String[] domainNameParts = uri.getHost().split("\\.");
tldString = domainNameParts[domainNameParts.length - 1];
return tldString;
}
/**
* Extracts the domain name (excluding the TLD) from a given URL.
*
* @param url The URL from which to extract the domain name.
* @return The domain name as a string.
* @throws URISyntaxException If the URL syntax is incorrect.
* @throws MalformedURLException If the URL is malformed.
*/
public static String getDomainName(String url) throws URISyntaxException, MalformedURLException {
if (url.startsWith(Classic_SiteType.PUBLIC.name + "://"))
url = url.substring((Classic_SiteType.PUBLIC.name + "://").length());
if (url.startsWith(Classic_SiteType.CLIENT.name + "://"))
url = url.substring((Classic_SiteType.CLIENT.name + "://").length());
if (url.startsWith(Classic_SiteType.SERVER.name + "://"))
url = url.substring((Classic_SiteType.SERVER.name + "://").length());
if (url.startsWith(Classic_SiteType.PROTOCOL.name + "://"))
url = url.substring((Classic_SiteType.PROTOCOL.name + "://").length());
if (url.startsWith(Classic_SiteType.LOCAL.name + "://"))
url = url.substring((Classic_SiteType.LOCAL.name + "://").length());
if (!url.startsWith("https://") && !url.startsWith("http://")) url = "https://" + url;
URI uri = new URI(url);
return uri.getHost().replace("." + getTopLevelDomain(url), "");
}
/**
* Extracts the path component from a given URL.
*
* @param url The URL from which to extract the path.
* @return The path as a string.
*/
public static String getPath(String url) {
if (!url.startsWith(Classic_SiteType.PUBLIC.name + "://") && !url.startsWith(Classic_SiteType.CLIENT.name + "://") &&
!url.startsWith(Classic_SiteType.SERVER.name + "://") && !url.startsWith(Classic_SiteType.PROTOCOL.name + "://") &&
!url.startsWith(Classic_SiteType.LOCAL.name + "://") && !url.startsWith("http") && !url.startsWith("https")) {
url = Classic_SiteType.PUBLIC.name + "://" + url;
}
String[] split = url.split("/");
if (split.length <= 3) return "";
StringBuilder path = new StringBuilder();
for (int i = 3; i < split.length; i++) path.append(split[i]).append("/");
String pathStr = path.toString();
if (pathStr.startsWith("/")) pathStr = pathStr.substring("/".length());
if (pathStr.endsWith("/")) pathStr = pathStr.substring(0, pathStr.length() - "/".length());
return pathStr;
}
}