Advanced InfoNameSystem

This commit is contained in:
Finn
2025-12-11 01:22:58 +01:00
parent fecee6043c
commit acacff60ff
24 changed files with 562 additions and 856 deletions

View File

@@ -4,7 +4,7 @@ import dev.unlegitdqrk.unlegitlibrary.network.system.packets.Packet;
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
import lombok.Getter;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseCode;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseStatus;
import java.io.IOException;
import java.io.ObjectInputStream;
@@ -25,7 +25,7 @@ public abstract class OACPacket extends Packet {
/**
* The response code for the packet, defaulting to RESPONSE_NOT_REQUIRED.
*/
private INSResponseCode responseCode = INSResponseCode.RESPONSE_NOT_REQUIRED;
private INSResponseStatus responseCode = INSResponseStatus.RESPONSE_NOT_REQUIRED;
/**
* Constructor for OACPacket.
@@ -43,7 +43,7 @@ public abstract class OACPacket extends Packet {
*
* @return The INSResponseCode associated with the packet.
*/
protected final INSResponseCode getResponseCode() {
protected final INSResponseStatus getResponseCode() {
return responseCode;
}
@@ -52,7 +52,7 @@ public abstract class OACPacket extends Packet {
*
* @param responseCode The INSResponseCode to set for the packet.
*/
protected final void setResponseCode(INSResponseCode responseCode) {
protected final void setResponseCode(INSResponseStatus responseCode) {
this.responseCode = responseCode;
}
@@ -80,8 +80,8 @@ public abstract class OACPacket extends Packet {
// Read the response code if the protocol version is not classic
if (protocolVersion != ProtocolVersion.PV_1_0_0_CLASSIC)
responseCode = (INSResponseCode) objectInputStream.readObject();
else responseCode = INSResponseCode.RESPONSE_NOT_REQUIRED;
responseCode = (INSResponseStatus) objectInputStream.readObject();
else responseCode = INSResponseStatus.RESPONSE_NOT_REQUIRED;
// Call the response code read handler
onResponseCodeRead(packetHandler, objectInputStream);

View File

@@ -11,26 +11,54 @@ import org.openautonomousconnection.protocol.side.ins.ConnectedProtocolClient;
import org.openautonomousconnection.protocol.side.ins.events.ConnectedProtocolClientEvent;
import org.openautonomousconnection.protocol.side.web.ConnectedWebClient;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseCode;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseStatus;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* Authentication packet used between client and INS/Web servers.
* <p>
* Responsibilities:
* <ul>
* <li>Client → Server: Sends client ID and protocol version</li>
* <li>Server → Client: Sends CA key, CA certificate and CA serial files</li>
* <li>Performs version compatibility validation</li>
* <li>Triggers authentication callbacks on both sides</li>
* </ul>
*/
public final class AuthPacket extends OACPacket {
private ProtocolBridge protocolBridge;
/**
* Creates a new authentication packet for sending CA data or client identity.
*
* @param protocolBridge The protocol context of the current instance.
*/
public AuthPacket(ProtocolBridge protocolBridge) {
this();
this.protocolBridge = protocolBridge;
}
/**
* Registration constructor
*/
public AuthPacket() {
super(4, ProtocolVersion.PV_1_0_0_BETA);
}
/**
* Writes authentication data to the output stream.
* <p>
* Behavior differs based on the running side:
* <ul>
* <li>INS Server → sends CA bundle to the client</li>
* <li>Client → sends client ID + protocol version</li>
* </ul>
*/
@Override
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
if (protocolBridge.isRunningAsINSServer()) {
@@ -56,7 +84,7 @@ public final class AuthPacket extends OACPacket {
protocolBridge.getProtocolINSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".srl"));
} catch (Exception exception) {
protocolBridge.getLogger().exception("Failed to read ca-files", exception);
setResponseCode(INSResponseCode.RESPONSE_AUTH_FAILED);
setResponseCode(INSResponseStatus.RESPONSE_AUTH_FAILED);
}
// Send ca data
@@ -69,6 +97,15 @@ public final class AuthPacket extends OACPacket {
}
}
/**
* Reads authentication data and updates protocol state.
* <p>
* Behavior:
* <ul>
* <li>Server validates version and registers new connected client</li>
* <li>Client saves received CA files and completes TLS initialization</li>
* </ul>
*/
@Override
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
if (protocolBridge.isRunningAsINSServer() || protocolBridge.isRunningAsWebServer()) {
@@ -77,10 +114,10 @@ public final class AuthPacket extends OACPacket {
ConnectionHandler connectionHandler = protocolBridge.getProtocolINSServer().getNetworkServer().getConnectionHandlerByID(clientID);
if (!protocolBridge.isVersionSupported(clientVersion)) {
setResponseCode(INSResponseCode.RESPONSE_AUTH_FAILED);
setResponseCode(INSResponseStatus.RESPONSE_AUTH_FAILED);
connectionHandler.disconnect();
return;
} else setResponseCode(INSResponseCode.RESPONSE_AUTH_SUCCESS);
} else setResponseCode(INSResponseStatus.RESPONSE_AUTH_SUCCESS);
if (protocolBridge.isRunningAsINSServer()) {
@@ -95,10 +132,10 @@ public final class AuthPacket extends OACPacket {
ProtocolVersion serverVersion = (ProtocolVersion) objectInputStream.readObject();
if (!protocolBridge.isVersionSupported(serverVersion)) {
setResponseCode(INSResponseCode.RESPONSE_AUTH_FAILED);
setResponseCode(INSResponseStatus.RESPONSE_AUTH_FAILED);
protocolBridge.getProtocolClient().getClientINSConnection().disconnect();
return;
} else setResponseCode(INSResponseCode.RESPONSE_AUTH_SUCCESS);
} else setResponseCode(INSResponseStatus.RESPONSE_AUTH_SUCCESS);
String caPrefix = objectInputStream.readUTF();
@@ -107,7 +144,7 @@ public final class AuthPacket extends OACPacket {
String caSrl = objectInputStream.readUTF();
if (caKey.equalsIgnoreCase("N/A") || caPem.equalsIgnoreCase("N/A") || caSrl.equalsIgnoreCase("N/A"))
setResponseCode(INSResponseCode.RESPONSE_AUTH_FAILED);
setResponseCode(INSResponseStatus.RESPONSE_AUTH_FAILED);
else {
File caPemFile = new File(protocolBridge.getProtocolClient().getFolderStructure().publicCAFolder, caPrefix + ".pem");
@@ -124,7 +161,7 @@ public final class AuthPacket extends OACPacket {
FileUtils.writeFile(caKeyFile, caSrl);
} catch (Exception exception) {
protocolBridge.getLogger().exception("Failed to create/save ca-files", exception);
setResponseCode(INSResponseCode.RESPONSE_AUTH_FAILED);
setResponseCode(INSResponseStatus.RESPONSE_AUTH_FAILED);
}
}

View File

@@ -1,89 +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.INSResponseCode;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.InfoName;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public final class GetDestinationPacket extends OACPacket {
private InfoName infoName;
private int clientID;
private INSResponseCode validationResponse;
private String destination;
private ProtocolBridge protocolBridge;
// INS-Server Constructor
public GetDestinationPacket(InfoName infoName, INSResponseCode validationResponse, String destination, ProtocolBridge protocolBridge) {
this();
this.infoName = infoName;
this.validationResponse = validationResponse;
this.destination = destination;
this.protocolBridge = protocolBridge;
}
// Client Constructor
public GetDestinationPacket(InfoName infoName, INSResponseCode validationResponse, ProtocolBridge protocolBridge) {
this();
this.infoName = infoName;
this.validationResponse = validationResponse;
this.destination = destination;
this.protocolBridge = protocolBridge;
}
// Registration Constructor
public GetDestinationPacket() {
super(6, ProtocolVersion.PV_1_0_0_BETA);
}
@Override
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
if (protocolBridge.isRunningAsClient()) {
if (validationResponse != INSResponseCode.RESPONSE_INFONAME_FULLY_EXIST) return;
objectOutputStream.writeInt(protocolBridge.getProtocolClient().getClientINSConnection().getClientID());
objectOutputStream.writeObject(infoName);
objectOutputStream.writeObject(validationResponse);
} else if (protocolBridge.isRunningAsINSServer()) {
objectOutputStream.writeObject(infoName);
objectOutputStream.writeObject(validationResponse);
objectOutputStream.writeUTF(destination);
}
}
@Override
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
if (protocolBridge.isRunningAsINSServer()) {
clientID = objectInputStream.readInt();
infoName = (InfoName) objectInputStream.readObject();
validationResponse = (INSResponseCode) objectInputStream.readObject();
} else if (protocolBridge.isRunningAsClient()) {
infoName = (InfoName) objectInputStream.readObject();
validationResponse = (INSResponseCode) objectInputStream.readObject();
destination = objectInputStream.readUTF();
protocolBridge.getProtocolClient().getDestinationCompleted(infoName, destination, validationResponse);
}
}
@Override
protected void onResponseCodeRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) {
super.onResponseCodeRead(packetHandler, objectInputStream);
if (protocolBridge.isRunningAsINSServer()) {
if (validationResponse != INSResponseCode.RESPONSE_INFONAME_FULLY_EXIST) return;
destination = infoName.getDestination(protocolBridge);
try {
protocolBridge.getProtocolINSServer().getClientByID(clientID).getConnectionHandler().sendPacket(new GetDestinationPacket(infoName, validationResponse, destination, protocolBridge));
} catch (IOException | ClassNotFoundException exception) {
protocolBridge.getProtocolINSServer().infoNameDestinationPacketFailedSend(protocolBridge.getProtocolINSServer().getClientByID(clientID), infoName, validationResponse, exception);
}
}
}
}

View File

@@ -0,0 +1,86 @@
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
import org.openautonomousconnection.protocol.packets.OACPacket;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecordType;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* Packet used by clients to query INS records from an INS server.
* <p>
* Contains all information required for resolving an InfoName:
* <ul>
* <li>TLN</li>
* <li>Name</li>
* <li>Optional subname </li>
* <li>Record type</li>
* <li>Client ID for routing responses</li>
* </ul>
*/
public final class INSQueryPacket extends OACPacket {
public String tln;
public String name;
public String sub;
public INSRecordType type;
public int clientId;
/**
* Creates a new INS query packet with all required parameters.
*
* @param tln The top-level namespace.
* @param name The InfoName.
* @param sub Optional subname ("www") or null.
* @param type Record type requested.
* @param clientId Sender client ID for routing.
*/
public INSQueryPacket(String tln, String name, String sub, INSRecordType type, int clientId) {
super(6, ProtocolVersion.PV_1_0_0_BETA);
this.tln = tln;
this.name = name;
this.sub = sub;
this.type = type;
this.clientId = clientId;
}
/**
* Registration constructor
*/
public INSQueryPacket() {
super(6, ProtocolVersion.PV_1_0_0_BETA);
}
/**
* Serializes the INS query into the stream.
*/
@Override
public void onWrite(PacketHandler handler, ObjectOutputStream out) throws IOException {
out.writeUTF(tln);
out.writeUTF(name);
out.writeBoolean(sub != null);
if (sub != null) out.writeUTF(sub);
out.writeObject(type);
out.writeInt(clientId);
}
/**
* Deserializes the INS query from the stream.
*/
@Override
public void onRead(PacketHandler handler, ObjectInputStream in) throws IOException, ClassNotFoundException {
tln = in.readUTF();
name = in.readUTF();
boolean hasSub = in.readBoolean();
sub = hasSub ? in.readUTF() : null;
type = (INSRecordType) in.readObject();
clientId = in.readInt();
}
}

View File

@@ -0,0 +1,104 @@
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.INSRecord;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseStatus;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Response packet returned by an INS server after resolving a query.
* <p>
* Contains:
* <ul>
* <li>Status code ({@link INSResponseStatus})</li>
* <li>List of resolved {@link INSRecord} entries</li>
* <li>The ID of the requesting client</li>
* </ul>
* On the client side, {@link org.openautonomousconnection.protocol.side.client.ProtocolClient#onResponse(INSResponseStatus, List)}
* is automatically invoked through {@link #onResponseCodeRead}.
*/
public final class INSResponsePacket extends OACPacket {
private final ProtocolBridge bridge;
public INSResponseStatus status;
public List<INSRecord> records;
public int clientId;
/**
* Creates a populated response packet.
*
* @param status The resolution status.
* @param records List of resolved records.
* @param clientId ID of requesting client.
* @param bridge Protocol runtime context.
*/
public INSResponsePacket(INSResponseStatus status, List<INSRecord> records, int clientId, ProtocolBridge bridge) {
super(7, ProtocolVersion.PV_1_0_0_BETA);
this.status = status;
this.records = records;
this.clientId = clientId;
this.bridge = bridge;
}
/**
* Registration Constructor
*
* @param bridge Protocol runtime context.
*/
public INSResponsePacket(ProtocolBridge bridge) {
super(7, ProtocolVersion.PV_1_0_0_BETA);
this.bridge = bridge;
}
/**
* Serializes the response status, records and client ID.
*/
@Override
public void onWrite(PacketHandler handler, ObjectOutputStream out) throws IOException {
out.writeObject(status);
out.writeInt(records.size());
for (INSRecord rec : records) {
out.writeObject(rec);
}
out.writeInt(clientId);
}
/**
* Deserializes the response, reconstructing the record list.
*/
@Override
public void onRead(PacketHandler handler, ObjectInputStream in) throws IOException, ClassNotFoundException {
status = (INSResponseStatus) in.readObject();
int size = in.readInt();
records = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
records.add((INSRecord) in.readObject());
}
clientId = in.readInt();
}
/**
* Called after reading the server's response code.
* <p>
* If running on a client, forwards the result to the client-side API.
*/
@Override
protected void onResponseCodeRead(PacketHandler handler, ObjectInputStream in) {
if (bridge.isRunningAsClient()) {
bridge.getProtocolClient().onResponse(status, records);
}
}
}

View File

@@ -4,18 +4,36 @@ 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.INSResponseCode;
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;
// Constructor with more information
/**
* 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;
@@ -23,11 +41,16 @@ public final class UnsupportedClassicPacket extends OACPacket {
this.protocolBridge = protocolBridge;
}
// Registration Constructor
/**
* Registration Constructor
*/
public UnsupportedClassicPacket() {
super(5, 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())
@@ -36,9 +59,12 @@ public final class UnsupportedClassicPacket extends OACPacket {
objectOutputStream.writeUTF(unsupportedClassicPacket.getName());
objectOutputStream.writeInt(content.length);
for (Object o : content) objectOutputStream.writeObject(o);
setResponseCode(INSResponseCode.RESPONSE_NOT_REQUIRED);
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;

View File

@@ -1,58 +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.InfoName;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public final class ValidateInfoNamePacket extends OACPacket {
private InfoName infoName;
private int clientID;
private ProtocolBridge protocolBridge;
public ValidateInfoNamePacket(InfoName infoName, ProtocolBridge protocolBridge) {
this();
this.infoName = infoName;
this.protocolBridge = protocolBridge;
}
public ValidateInfoNamePacket() {
super(6, ProtocolVersion.PV_1_0_0_BETA);
}
@Override
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
if (protocolBridge.isRunningAsClient())
objectOutputStream.writeInt(protocolBridge.getProtocolClient().getClientINSConnection().getClientID());
else if (protocolBridge.isRunningAsINSServer())
setResponseCode(protocolBridge.getProtocolINSServer().validateInfoName(infoName));
objectOutputStream.writeObject(infoName);
}
@Override
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
if (protocolBridge.isRunningAsINSServer()) clientID = objectInputStream.readInt();
infoName = (InfoName) objectInputStream.readObject();
}
@Override
protected void onResponseCodeRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) {
super.onResponseCodeRead(packetHandler, objectInputStream);
if (protocolBridge.isRunningAsINSServer()) {
try {
protocolBridge.getProtocolINSServer().getClientByID(clientID).getConnectionHandler().sendPacket(new ValidateInfoNamePacket(infoName, protocolBridge));
} catch (IOException | ClassNotFoundException e) {
protocolBridge.getProtocolINSServer().validationPacketSendFailed(infoName, protocolBridge.getProtocolINSServer().getClientByID(clientID), e);
}
} else if (protocolBridge.isRunningAsClient())
protocolBridge.getProtocolClient().validationCompleted(infoName, getResponseCode());
}
}

View File

@@ -5,7 +5,6 @@ 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.INSResponseCode;
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;