@@ -1,159 +0,0 @@
|
||||
package org.openautonomousconnection.protocol;
|
||||
|
||||
import org.openautonomousconnection.protocol.listeners.ClientListener;
|
||||
import org.openautonomousconnection.protocol.listeners.ServerListener;
|
||||
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.GetDestinationPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.UnsupportedClassicPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.ValidateDomainPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_DomainPacket;
|
||||
import org.openautonomousconnection.protocol.side.client.ProtocolClient;
|
||||
import org.openautonomousconnection.protocol.side.server.ProtocolServer;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.ClassicHandlerClient;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.ClassicHandlerServer;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.Classic_ClientListener;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import dev.unlegitdqrk.unlegitlibrary.utils.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class ProtocolBridge {
|
||||
|
||||
@Getter
|
||||
private static ProtocolBridge instance;
|
||||
@Getter
|
||||
private final ProtocolSettings protocolSettings;
|
||||
@Getter
|
||||
private final ProtocolVersion protocolVersion;
|
||||
@Getter
|
||||
private final Logger logger;
|
||||
@Getter
|
||||
private ProtocolServer protocolServer;
|
||||
@Getter
|
||||
private ProtocolClient protocolClient;
|
||||
@Getter
|
||||
@Setter
|
||||
private ClassicHandlerServer classicHandlerServer;
|
||||
@Getter
|
||||
@Setter
|
||||
private ClassicHandlerClient classicHandlerClient;
|
||||
|
||||
public ProtocolBridge(ProtocolServer protocolServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
||||
this.protocolServer = protocolServer;
|
||||
this.protocolSettings = protocolSettings;
|
||||
this.protocolVersion = protocolVersion;
|
||||
|
||||
Logger tmpLogger = null;
|
||||
try {
|
||||
tmpLogger = new Logger(logFolder, false, true);
|
||||
} catch (IOException | NoSuchFieldException | IllegalAccessException exception) {
|
||||
exception.printStackTrace();
|
||||
tmpLogger = null;
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
this.logger = tmpLogger;
|
||||
protocolSettings.eventManager.registerListener(new ServerListener());
|
||||
protocolSettings.eventManager.unregisterListener(new ClientListener());
|
||||
|
||||
if (!validateProtocolSide()) {
|
||||
this.logger.error("Invalid protocol version '" + protocolVersion.toString() + "'!");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
if (isClassicSupported()) protocolSettings.eventManager.unregisterListener(new Classic_ClientListener());
|
||||
registerPackets();
|
||||
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public ProtocolBridge(ProtocolClient protocolClient, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
||||
this.protocolClient = protocolClient;
|
||||
this.protocolSettings = protocolSettings;
|
||||
this.protocolVersion = protocolVersion;
|
||||
|
||||
Logger tmpLogger = null;
|
||||
try {
|
||||
tmpLogger = new Logger(logFolder, false, true);
|
||||
} catch (IOException | NoSuchFieldException | IllegalAccessException exception) {
|
||||
exception.printStackTrace();
|
||||
tmpLogger = null;
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
this.logger = tmpLogger;
|
||||
protocolSettings.eventManager.registerListener(new ClientListener());
|
||||
protocolSettings.eventManager.unregisterListener(new ServerListener());
|
||||
|
||||
if (!validateProtocolSide()) {
|
||||
this.logger.error("Invalid protocol version '" + protocolVersion.toString() + "'!");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
if (isClassicSupported()) protocolSettings.eventManager.registerListener(new Classic_ClientListener());
|
||||
registerPackets();
|
||||
|
||||
instance = this;
|
||||
}
|
||||
|
||||
private void registerPackets() {
|
||||
// Classic packets
|
||||
Classic_DomainPacket cDomainPacket = new Classic_DomainPacket();
|
||||
Classic_DomainPacket cMessagePacket = new Classic_DomainPacket();
|
||||
Classic_DomainPacket cPingPacket = new Classic_DomainPacket();
|
||||
|
||||
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();
|
||||
ValidateDomainPacket v100bValidateDomainPacket = new ValidateDomainPacket();
|
||||
GetDestinationPacket v100bGetDestinationPacket = new GetDestinationPacket();
|
||||
|
||||
if (isPacketSupported(v100bAuthPath)) protocolSettings.packetHandler.registerPacket(v100bAuthPath);
|
||||
if (isPacketSupported(v100bUnsupportedClassicPacket))
|
||||
protocolSettings.packetHandler.registerPacket(v100bUnsupportedClassicPacket);
|
||||
if (isPacketSupported(v100bValidateDomainPacket))
|
||||
protocolSettings.packetHandler.registerPacket(v100bValidateDomainPacket);
|
||||
if (isPacketSupported(v100bGetDestinationPacket))
|
||||
protocolSettings.packetHandler.registerPacket(v100bGetDestinationPacket);
|
||||
}
|
||||
|
||||
public boolean isPacketSupported(OACPacket packet) {
|
||||
return isVersionSupported(packet.getProtocolVersion());
|
||||
}
|
||||
|
||||
public boolean isClassicSupported() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : protocolVersion.getCompatibleVersions()) {
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
return protocolVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC || yes;
|
||||
}
|
||||
|
||||
public boolean isRunningAsServer() {
|
||||
return protocolServer != null;
|
||||
}
|
||||
|
||||
public boolean isRunningAsClient() {
|
||||
return protocolClient != null;
|
||||
}
|
||||
|
||||
private boolean validateProtocolSide() {
|
||||
return (isRunningAsServer() && protocolVersion.getProtocolSide() != ProtocolVersion.ProtocolSide.CLIENT) ||
|
||||
(isRunningAsClient() && protocolVersion.getProtocolSide() != ProtocolVersion.ProtocolSide.SERVER);
|
||||
}
|
||||
|
||||
public boolean isVersionSupported(ProtocolVersion targetVersion) {
|
||||
return protocolVersion == targetVersion || protocolVersion.getCompatibleVersions().contains(targetVersion);
|
||||
}
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
package org.openautonomousconnection.protocol;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.EventManager;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import dev.unlegitdqrk.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
public class ProtocolSettings extends DefaultMethodsOverrider {
|
||||
|
||||
public String host;
|
||||
public int port;
|
||||
public PacketHandler packetHandler;
|
||||
public EventManager eventManager;
|
||||
|
||||
}
|
@@ -1,29 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.listeners;
|
||||
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.AuthPacket;
|
||||
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 java.io.IOException;
|
||||
|
||||
public class ClientListener extends EventListener {
|
||||
|
||||
@Listener
|
||||
public void onConnect(ClientConnectedEvent event) {
|
||||
try {
|
||||
event.client.sendPacket(new AuthPacket());
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
ProtocolBridge.getInstance().getLogger().exception("Failed to send auth packet", exception);
|
||||
event.client.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onDisconnect(ClientDisconnectedEvent event) {
|
||||
ProtocolBridge.getInstance().getProtocolClient().onDisconnect(event);
|
||||
}
|
||||
|
||||
}
|
@@ -1,22 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.listeners;
|
||||
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.side.server.ConnectedProtocolClient;
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.EventListener;
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.Listener;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.events.ConnectionHandlerConnectedEvent;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.events.ConnectionHandlerDisconnectedEvent;
|
||||
|
||||
public class ServerListener extends EventListener {
|
||||
|
||||
@Listener
|
||||
public void onConnect(ConnectionHandlerConnectedEvent event) {
|
||||
ProtocolBridge.getInstance().getProtocolServer().getClients().add(new ConnectedProtocolClient(event.connectionHandler));
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onDisconnect(ConnectionHandlerDisconnectedEvent event) {
|
||||
ProtocolBridge.getInstance().getProtocolServer().getClients().removeIf(client -> client.getConnectionHandler().getClientID() == -1);
|
||||
}
|
||||
|
||||
}
|
@@ -1,54 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.packets;
|
||||
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.DNSResponseCode;
|
||||
import lombok.Getter;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.Packet;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public abstract class OACPacket extends Packet {
|
||||
|
||||
@Getter
|
||||
private final ProtocolVersion protocolVersion;
|
||||
|
||||
private DNSResponseCode responseCode = DNSResponseCode.RESPONSE_NOT_REQUIRED;
|
||||
|
||||
public OACPacket(int id, ProtocolVersion protocolVersion) {
|
||||
super(id);
|
||||
this.protocolVersion = protocolVersion;
|
||||
}
|
||||
|
||||
protected final DNSResponseCode getResponseCode() {
|
||||
return responseCode;
|
||||
}
|
||||
|
||||
protected final void setResponseCode(DNSResponseCode responseCode) {
|
||||
this.responseCode = responseCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void write(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
|
||||
onWrite(packetHandler, objectOutputStream);
|
||||
if (protocolVersion != ProtocolVersion.PV_1_0_0_CLASSIC) objectOutputStream.writeObject(responseCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void read(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
onRead(packetHandler, objectInputStream);
|
||||
if (protocolVersion != ProtocolVersion.PV_1_0_0_CLASSIC) {
|
||||
responseCode = (DNSResponseCode) objectInputStream.readObject();
|
||||
onResponseCodeRead(packetHandler, objectInputStream);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException;
|
||||
|
||||
public abstract void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException;
|
||||
|
||||
protected void onResponseCodeRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) {
|
||||
}
|
||||
}
|
@@ -1,133 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
|
||||
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.packets.OACPacket;
|
||||
import org.openautonomousconnection.protocol.side.client.events.ConnectedToProtocolServer;
|
||||
import org.openautonomousconnection.protocol.side.server.ConnectedProtocolClient;
|
||||
import org.openautonomousconnection.protocol.side.server.events.ProtocolClientConnected;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.DNSResponseCode;
|
||||
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.utils.NetworkUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class AuthPacket extends OACPacket {
|
||||
|
||||
public AuthPacket() {
|
||||
super(4, ProtocolVersion.PV_1_0_0_BETA);
|
||||
}
|
||||
|
||||
File certificatesFolder = new File("certificates");
|
||||
|
||||
File publicFolder = new File(certificatesFolder, "public");
|
||||
File privateFolder = new File(certificatesFolder, "private");
|
||||
|
||||
File privateCAFolder = new File(privateFolder, "ca");
|
||||
File privateServerFolder = new File(privateFolder, "server");
|
||||
|
||||
File publicCAFolder = new File(publicFolder, "ca");
|
||||
File publicServerFolder = new File(publicFolder, "server");
|
||||
|
||||
@Override
|
||||
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsServer()) {
|
||||
objectOutputStream.writeObject(ProtocolBridge.getInstance().getProtocolVersion());
|
||||
|
||||
// Read ca files
|
||||
String caKey = "N/A";
|
||||
String caPem = "N/A";
|
||||
String caSrl = "N/A";
|
||||
try {
|
||||
objectOutputStream.writeUTF(ProtocolBridge.getInstance().getProtocolServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress());
|
||||
|
||||
caKey = FileUtils.readFileFull(new File(
|
||||
ProtocolBridge.getInstance().getProtocolServer().getFolderStructure().privateCAFolder,
|
||||
ProtocolBridge.getInstance().getProtocolServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".key"));
|
||||
|
||||
caPem = FileUtils.readFileFull(new File(
|
||||
ProtocolBridge.getInstance().getProtocolServer().getFolderStructure().publicCAFolder,
|
||||
ProtocolBridge.getInstance().getProtocolServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".pem"));
|
||||
|
||||
caSrl = FileUtils.readFileFull(new File(
|
||||
ProtocolBridge.getInstance().getProtocolServer().getFolderStructure().publicCAFolder,
|
||||
ProtocolBridge.getInstance().getProtocolServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".srl"));
|
||||
} catch (Exception exception) {
|
||||
ProtocolBridge.getInstance().getLogger().exception("Failed to read ca-files", exception);
|
||||
setResponseCode(DNSResponseCode.RESPONSE_AUTH_FAILED);
|
||||
}
|
||||
|
||||
// Send ca data
|
||||
objectOutputStream.writeUTF(caKey);
|
||||
objectOutputStream.writeUTF(caPem);
|
||||
objectOutputStream.writeUTF(caSrl);
|
||||
} else {
|
||||
objectOutputStream.writeInt(ProtocolBridge.getInstance().getProtocolClient().getNetworkClient().getClientID());
|
||||
objectOutputStream.writeObject(ProtocolBridge.getInstance().getProtocolVersion());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsServer()) {
|
||||
int clientID = objectInputStream.readInt();
|
||||
ProtocolVersion clientVersion = (ProtocolVersion) objectInputStream.readObject();
|
||||
ConnectionHandler connectionHandler = ProtocolBridge.getInstance().getProtocolServer().getNetworkServer().getConnectionHandlerByID(clientID);
|
||||
|
||||
if (!ProtocolBridge.getInstance().isVersionSupported(clientVersion)) {
|
||||
setResponseCode(DNSResponseCode.RESPONSE_AUTH_FAILED);
|
||||
connectionHandler.disconnect();
|
||||
return;
|
||||
} else setResponseCode(DNSResponseCode.RESPONSE_AUTH_SUCCESS);
|
||||
|
||||
|
||||
ConnectedProtocolClient client = ProtocolBridge.getInstance().getProtocolServer().getClientByID(clientID);
|
||||
client.setClientVersion(clientVersion);
|
||||
ProtocolBridge.getInstance().getProtocolSettings().eventManager.executeEvent(new ProtocolClientConnected(client));
|
||||
} else {
|
||||
ProtocolVersion serverVersion = (ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
if (!ProtocolBridge.getInstance().isVersionSupported(serverVersion)) {
|
||||
setResponseCode(DNSResponseCode.RESPONSE_AUTH_FAILED);
|
||||
ProtocolBridge.getInstance().getProtocolClient().getNetworkClient().disconnect();
|
||||
return;
|
||||
} else setResponseCode(DNSResponseCode.RESPONSE_AUTH_SUCCESS);
|
||||
|
||||
String caPrefix = objectInputStream.readUTF();
|
||||
|
||||
String caKey = objectInputStream.readUTF();
|
||||
String caPem = objectInputStream.readUTF();
|
||||
String caSrl = objectInputStream.readUTF();
|
||||
|
||||
if (caKey.equalsIgnoreCase("N/A") || caPem.equalsIgnoreCase("N/A") || caSrl.equalsIgnoreCase("N/A"))
|
||||
setResponseCode(DNSResponseCode.RESPONSE_AUTH_FAILED);
|
||||
else {
|
||||
|
||||
File caPemFile = new File(ProtocolBridge.getInstance().getProtocolClient().getFolderStructure().publicCAFolder, caPrefix + ".pem");
|
||||
File caSrlFile = new File(ProtocolBridge.getInstance().getProtocolClient().getFolderStructure().publicCAFolder, caPrefix + ".srl");
|
||||
File caKeyFile = new File(ProtocolBridge.getInstance().getProtocolClient().getFolderStructure().privateCAFolder, caPrefix + ".key");
|
||||
|
||||
try {
|
||||
if (!caPemFile.exists()) caPemFile.createNewFile();
|
||||
if (!caSrlFile.exists()) caSrlFile.createNewFile();
|
||||
if (!caKeyFile.exists()) caKeyFile.createNewFile();
|
||||
|
||||
FileUtils.writeFile(caPemFile, caPem);
|
||||
FileUtils.writeFile(caSrlFile, caKey);
|
||||
FileUtils.writeFile(caKeyFile, caSrl);
|
||||
} catch (Exception exception) {
|
||||
ProtocolBridge.getInstance().getLogger().exception("Failed to create/save ca-files", exception);
|
||||
setResponseCode(DNSResponseCode.RESPONSE_AUTH_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
ProtocolBridge.getInstance().getProtocolClient().setServerVersion(serverVersion);
|
||||
ProtocolBridge.getInstance().getProtocolSettings().eventManager.executeEvent(new ConnectedToProtocolServer());
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,75 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
|
||||
|
||||
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.DNSResponseCode;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.Domain;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class GetDestinationPacket extends OACPacket {
|
||||
private Domain domain;
|
||||
private int clientID;
|
||||
private DNSResponseCode validationResponse;
|
||||
private String destination;
|
||||
|
||||
public GetDestinationPacket(Domain domain, DNSResponseCode validationResponse, String destination) {
|
||||
this();
|
||||
this.domain = domain;
|
||||
this.validationResponse = validationResponse;
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
public GetDestinationPacket() {
|
||||
super(6, ProtocolVersion.PV_1_0_0_BETA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsClient()) {
|
||||
if (validationResponse != DNSResponseCode.RESPONSE_DOMAIN_FULLY_EXIST) return;
|
||||
|
||||
objectOutputStream.writeInt(ProtocolBridge.getInstance().getProtocolClient().getNetworkClient().getClientID());
|
||||
objectOutputStream.writeObject(domain);
|
||||
objectOutputStream.writeObject(validationResponse);
|
||||
} else {
|
||||
objectOutputStream.writeObject(domain);
|
||||
objectOutputStream.writeObject(validationResponse);
|
||||
objectOutputStream.writeUTF(destination);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsServer()) {
|
||||
clientID = objectInputStream.readInt();
|
||||
domain = (Domain) objectInputStream.readObject();
|
||||
validationResponse = (DNSResponseCode) objectInputStream.readObject();
|
||||
} else {
|
||||
domain = (Domain) objectInputStream.readObject();
|
||||
validationResponse = (DNSResponseCode) objectInputStream.readObject();
|
||||
destination = objectInputStream.readUTF();
|
||||
|
||||
ProtocolBridge.getInstance().getProtocolClient().getDestinationCompleted(domain, destination, validationResponse);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResponseCodeRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) {
|
||||
super.onResponseCodeRead(packetHandler, objectInputStream);
|
||||
|
||||
if (ProtocolBridge.getInstance().isRunningAsServer()) {
|
||||
if (validationResponse != DNSResponseCode.RESPONSE_DOMAIN_FULLY_EXIST) return;
|
||||
destination = domain.getDestination();
|
||||
try {
|
||||
ProtocolBridge.getInstance().getProtocolServer().getClientByID(clientID).getConnectionHandler().sendPacket(new GetDestinationPacket(domain, validationResponse, destination));
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
ProtocolBridge.getInstance().getProtocolServer().getDomainDestinationFailed(ProtocolBridge.getInstance().getProtocolServer().getClientByID(clientID), domain, validationResponse, exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,53 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
|
||||
|
||||
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.DNSResponseCode;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class UnsupportedClassicPacket extends OACPacket {
|
||||
private Class<? extends OACPacket> unsupportedClassicPacket;
|
||||
private Object[] content;
|
||||
|
||||
public UnsupportedClassicPacket(Class<? extends OACPacket> unsupportedClassicPacket, Object[] content) {
|
||||
this();
|
||||
this.unsupportedClassicPacket = unsupportedClassicPacket;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public UnsupportedClassicPacket() {
|
||||
super(5, ProtocolVersion.PV_1_0_0_BETA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsClient())
|
||||
objectOutputStream.writeInt(ProtocolBridge.getInstance().getProtocolClient().getNetworkClient().getClientID());
|
||||
objectOutputStream.writeUTF(unsupportedClassicPacket.getName());
|
||||
objectOutputStream.writeInt(content.length);
|
||||
for (Object o : content) objectOutputStream.writeObject(o);
|
||||
setResponseCode(DNSResponseCode.RESPONSE_NOT_REQUIRED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
int clientID = 0;
|
||||
if (ProtocolBridge.getInstance().isRunningAsServer()) 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.getInstance().isRunningAsServer())
|
||||
ProtocolBridge.getInstance().getClassicHandlerServer().unsupportedClassicPacket(className, content, ProtocolBridge.getInstance().getProtocolServer().getClientByID(clientID));
|
||||
else ProtocolBridge.getInstance().getClassicHandlerClient().unsupportedClassicPacket(className, content);
|
||||
}
|
||||
}
|
@@ -1,57 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
|
||||
|
||||
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.Domain;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class ValidateDomainPacket extends OACPacket {
|
||||
private Domain domain;
|
||||
private int clientID;
|
||||
|
||||
public ValidateDomainPacket(Domain domain) {
|
||||
this();
|
||||
this.domain = domain;
|
||||
}
|
||||
|
||||
public ValidateDomainPacket() {
|
||||
super(6, ProtocolVersion.PV_1_0_0_BETA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsClient())
|
||||
objectOutputStream.writeInt(ProtocolBridge.getInstance().getProtocolClient().getNetworkClient().getClientID());
|
||||
else setResponseCode(ProtocolBridge.getInstance().getProtocolServer().validateDomain(domain));
|
||||
|
||||
objectOutputStream.writeObject(domain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsServer()) clientID = objectInputStream.readInt();
|
||||
domain = (Domain) objectInputStream.readObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResponseCodeRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) {
|
||||
super.onResponseCodeRead(packetHandler, objectInputStream);
|
||||
|
||||
if (ProtocolBridge.getInstance().isRunningAsServer()) {
|
||||
try {
|
||||
ProtocolBridge.getInstance().getProtocolServer().getClientByID(clientID).getConnectionHandler().sendPacket(new ValidateDomainPacket(domain));
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
ProtocolBridge.getInstance().getProtocolServer().validationFailed(domain, ProtocolBridge.getInstance().getProtocolServer().getClientByID(clientID), e);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ProtocolBridge.getInstance().getProtocolClient().validationCompleted(domain, getResponseCode());
|
||||
}
|
||||
}
|
@@ -1,78 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.classic;
|
||||
|
||||
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.Classic_Domain;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.Classic_DomainPacketReceivedEvent;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.Classic_ProtocolVersion;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.Classic_RequestDomain;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class Classic_DomainPacket extends OACPacket {
|
||||
private Classic_RequestDomain requestDomain;
|
||||
private Classic_Domain domain;
|
||||
private int clientID;
|
||||
|
||||
public Classic_DomainPacket(int toClient, Classic_RequestDomain requestDomain, Classic_Domain domain) {
|
||||
this();
|
||||
this.clientID = toClient;
|
||||
|
||||
this.requestDomain = requestDomain;
|
||||
this.domain = domain;
|
||||
}
|
||||
|
||||
public Classic_DomainPacket() {
|
||||
super(2, ProtocolVersion.PV_1_0_0_CLASSIC);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsServer()) {
|
||||
objectOutputStream.writeInt(clientID);
|
||||
objectOutputStream.writeObject(requestDomain);
|
||||
objectOutputStream.writeObject(domain);
|
||||
} else {
|
||||
clientID = ProtocolBridge.getInstance().getProtocolClient().getNetworkClient().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 (ProtocolBridge.getInstance().isRunningAsServer()) {
|
||||
clientID = objectInputStream.readInt();
|
||||
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
|
||||
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
try {
|
||||
domain = ProtocolBridge.getInstance().getClassicHandlerServer().getDomain(requestDomain);
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
ProtocolBridge.getInstance().getProtocolServer().getNetworkServer().getEventManager().executeEvent(new Classic_DomainPacketReceivedEvent(protocolVersion, domain, requestDomain, clientID));
|
||||
|
||||
if (ProtocolBridge.getInstance().getProtocolServer().getClientByID(clientID).clientSupportClassic())
|
||||
ProtocolBridge.getInstance().getProtocolServer().getNetworkServer().getConnectionHandlerByID(clientID).sendPacket(new Classic_DomainPacket(clientID, requestDomain, domain));
|
||||
else
|
||||
ProtocolBridge.getInstance().getProtocolServer().getNetworkServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{clientID, requestDomain, domain}));
|
||||
} else {
|
||||
clientID = objectInputStream.readInt();
|
||||
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
|
||||
domain = (Classic_Domain) objectInputStream.readObject();
|
||||
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
ProtocolBridge.getInstance().getProtocolClient().getNetworkClient().getEventManager().executeEvent(new Classic_DomainPacketReceivedEvent(protocolVersion, domain, requestDomain, clientID));
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,55 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.classic;
|
||||
|
||||
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.Classic_ProtocolVersion;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class Classic_MessagePacket extends OACPacket {
|
||||
private String message;
|
||||
private int clientID;
|
||||
|
||||
public Classic_MessagePacket(String message, int toClient) {
|
||||
this();
|
||||
this.message = message;
|
||||
this.clientID = toClient;
|
||||
}
|
||||
|
||||
public Classic_MessagePacket() {
|
||||
super(3, ProtocolVersion.PV_1_0_0_CLASSIC);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsServer()) objectOutputStream.writeInt(clientID);
|
||||
else {
|
||||
clientID = ProtocolBridge.getInstance().getProtocolClient().getNetworkClient().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 (ProtocolBridge.getInstance().isRunningAsServer()) {
|
||||
clientID = objectInputStream.readInt();
|
||||
String message = objectInputStream.readUTF();
|
||||
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
ProtocolBridge.getInstance().getClassicHandlerServer().handleMessage(ProtocolBridge.getInstance().getProtocolServer().getClientByID(clientID), message, protocolVersion);
|
||||
} else {
|
||||
clientID = objectInputStream.readInt();
|
||||
String message = objectInputStream.readUTF();
|
||||
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
ProtocolBridge.getInstance().getClassicHandlerClient().handleMessage(message);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,83 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.classic;
|
||||
|
||||
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.Classic_Domain;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.Classic_PingPacketReceivedEvent;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.Classic_ProtocolVersion;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.Classic_RequestDomain;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class Classic_PingPacket extends OACPacket {
|
||||
private Classic_RequestDomain requestDomain;
|
||||
private Classic_Domain domain;
|
||||
private int clientID;
|
||||
private boolean reachable;
|
||||
private Classic_ProtocolVersion protocolVersion;
|
||||
|
||||
public Classic_PingPacket(Classic_RequestDomain requestDomain, Classic_Domain domain, boolean reachable) {
|
||||
this();
|
||||
|
||||
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 (ProtocolBridge.getInstance().isRunningAsServer()) {
|
||||
objectOutputStream.writeInt(clientID);
|
||||
objectOutputStream.writeObject(requestDomain);
|
||||
objectOutputStream.writeObject(domain);
|
||||
objectOutputStream.writeBoolean(reachable);
|
||||
} else {
|
||||
clientID = ProtocolBridge.getInstance().getProtocolClient().getNetworkClient().getClientID();
|
||||
objectOutputStream.writeInt(clientID);
|
||||
objectOutputStream.writeObject(requestDomain);
|
||||
}
|
||||
|
||||
objectOutputStream.writeObject(protocolVersion);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsServer()) {
|
||||
clientID = objectInputStream.readInt();
|
||||
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
|
||||
protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
try {
|
||||
domain = ProtocolBridge.getInstance().getClassicHandlerServer().ping(requestDomain);
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
reachable = domain != null;
|
||||
ProtocolBridge.getInstance().getProtocolServer().getNetworkServer().getEventManager().executeEvent(new Classic_PingPacketReceivedEvent(protocolVersion, domain, requestDomain, reachable, clientID));
|
||||
if (ProtocolBridge.getInstance().getProtocolServer().getClientByID(clientID).clientSupportClassic())
|
||||
ProtocolBridge.getInstance().getProtocolServer().getNetworkServer().getConnectionHandlerByID(clientID).sendPacket(new Classic_PingPacket(requestDomain, domain, reachable));
|
||||
else
|
||||
ProtocolBridge.getInstance().getProtocolServer().getNetworkServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{requestDomain, domain, reachable}));
|
||||
} else {
|
||||
clientID = objectInputStream.readInt();
|
||||
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
|
||||
domain = (Classic_Domain) objectInputStream.readObject();
|
||||
boolean reachable = objectInputStream.readBoolean();
|
||||
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
ProtocolBridge.getInstance().getProtocolClient().getNetworkClient().getEventManager().executeEvent(new Classic_PingPacketReceivedEvent(protocolVersion, domain, requestDomain, reachable, clientID));
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,158 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.side.client;
|
||||
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.packets.OACPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.ValidateDomainPacket;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.DNSResponseCode;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.Domain;
|
||||
import lombok.Getter;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.ClientDisconnectedEvent;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.utils.NetworkUtils;
|
||||
import dev.unlegitdqrk.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.security.cert.CertificateException;
|
||||
|
||||
public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
public final class ClientCertificateFolderStructure {
|
||||
public final File certificatesFolder;
|
||||
|
||||
public final File publicFolder;
|
||||
public final File privateFolder;
|
||||
|
||||
public final File privateCAFolder;
|
||||
public final File privateClientFolder;
|
||||
|
||||
public final File publicCAFolder;
|
||||
public final File publicClientFolder;
|
||||
|
||||
public ClientCertificateFolderStructure() {
|
||||
certificatesFolder = new File("certificates");
|
||||
|
||||
publicFolder = new File(certificatesFolder, "public");
|
||||
privateFolder = new File(certificatesFolder, "private");
|
||||
|
||||
privateCAFolder = new File(privateFolder, "ca");
|
||||
privateClientFolder = new File(privateFolder, "client");
|
||||
|
||||
publicCAFolder = new File(publicFolder, "ca");
|
||||
publicClientFolder = new File(publicFolder, "client");
|
||||
|
||||
if (!certificatesFolder.exists()) certificatesFolder.mkdirs();
|
||||
|
||||
if (!publicFolder.exists()) publicFolder.mkdirs();
|
||||
if (!privateFolder.exists()) privateFolder.mkdirs();
|
||||
|
||||
if (!privateCAFolder.exists()) privateCAFolder.mkdirs();
|
||||
if (!privateClientFolder.exists()) privateClientFolder.mkdirs();
|
||||
|
||||
if (!publicCAFolder.exists()) publicCAFolder.mkdirs();
|
||||
if (!publicClientFolder.exists()) publicClientFolder.mkdirs();
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
private final NetworkClient networkClient;
|
||||
private ProtocolVersion serverVersion = null;
|
||||
@Getter
|
||||
private final ClientCertificateFolderStructure folderStructure;
|
||||
|
||||
public ProtocolClient() throws CertificateException, IOException {
|
||||
folderStructure = new ClientCertificateFolderStructure();
|
||||
|
||||
networkClient = new NetworkClient.ClientBuilder().setLogger(ProtocolBridge.getInstance().getLogger()).
|
||||
setHost(ProtocolBridge.getInstance().getProtocolSettings().host).setPort(ProtocolBridge.getInstance().getProtocolSettings().port).
|
||||
setPacketHandler(ProtocolBridge.getInstance().getProtocolSettings().packetHandler).setEventManager(ProtocolBridge.getInstance().getProtocolSettings().eventManager).
|
||||
setRootCAFolder(folderStructure.publicCAFolder).setClientCertificatesFolder(folderStructure.publicClientFolder, folderStructure.privateClientFolder).
|
||||
build();
|
||||
}
|
||||
|
||||
private final void checkFileExists(File folder, String prefix, String extension) throws CertificateException, IOException {
|
||||
boolean found = false;
|
||||
if (folder == null) throw new FileNotFoundException("Folder does not exist");
|
||||
|
||||
File[] files = folder.listFiles();
|
||||
if (files == null || files.length == 0) throw new FileNotFoundException("Folder " + folder.getAbsolutePath() + " is empty");
|
||||
|
||||
for (File file : files) {
|
||||
if (!file.getName().startsWith(prefix) || !file.getName().endsWith(extension)) throw new CertificateException(file.getAbsolutePath() + " is not valid");
|
||||
if (!found) found = file.getName().equalsIgnoreCase(prefix + NetworkUtils.getPublicIPAddress() + extension);
|
||||
}
|
||||
|
||||
if (!found) throw new CertificateException("Missing " + prefix + NetworkUtils.getPublicIPAddress() + extension);
|
||||
}
|
||||
|
||||
public final ProtocolVersion getServerVersion() {
|
||||
return serverVersion == null ? ProtocolVersion.PV_1_0_0_CLASSIC : serverVersion;
|
||||
}
|
||||
|
||||
public final void setServerVersion(ProtocolVersion serverVersion) {
|
||||
if (serverVersion == null) this.serverVersion = serverVersion;
|
||||
}
|
||||
|
||||
public final void onDisconnect(ClientDisconnectedEvent event) {
|
||||
serverVersion = null;
|
||||
}
|
||||
|
||||
public final boolean isStableServer() {
|
||||
return !isBetaServer() && !isClassicServer();
|
||||
}
|
||||
|
||||
public final boolean serverSupportStable() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getServerVersion().getCompatibleVersions()) {
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.STABLE;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
return isStableServer() || yes;
|
||||
}
|
||||
|
||||
public final boolean isBetaServer() {
|
||||
return getServerVersion().getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
}
|
||||
|
||||
public final boolean serverSupportBeta() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getServerVersion().getCompatibleVersions()) {
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
return isBetaServer() || yes;
|
||||
}
|
||||
|
||||
public final boolean isClassicServer() {
|
||||
return getServerVersion().getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
}
|
||||
|
||||
public final boolean serverSupportClassic() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getServerVersion().getCompatibleVersions()) {
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
return isClassicServer() || yes;
|
||||
}
|
||||
|
||||
public final boolean isPacketSupported(OACPacket packet) {
|
||||
return isVersionSupported(packet.getProtocolVersion());
|
||||
}
|
||||
|
||||
public final boolean isVersionSupported(ProtocolVersion targetVersion) {
|
||||
return getServerVersion() == targetVersion || getServerVersion().getCompatibleVersions().contains(targetVersion);
|
||||
}
|
||||
|
||||
public final void validateDomain(Domain domain) throws IOException, ClassNotFoundException {
|
||||
networkClient.sendPacket(new ValidateDomainPacket(domain));
|
||||
}
|
||||
|
||||
public abstract void validationCompleted(Domain domain, DNSResponseCode responseCode);
|
||||
|
||||
public abstract void getDestinationCompleted(Domain domain, String destination, DNSResponseCode validationResponse);
|
||||
}
|
@@ -1,6 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.side.client.events;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.impl.Event;
|
||||
|
||||
public class ConnectedToProtocolServer extends Event {
|
||||
}
|
@@ -1,76 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.side.server;
|
||||
|
||||
import org.openautonomousconnection.protocol.packets.OACPacket;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
import lombok.Getter;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
|
||||
public class ConnectedProtocolClient {
|
||||
|
||||
@Getter
|
||||
private final ConnectionHandler connectionHandler;
|
||||
|
||||
private ProtocolVersion clientVersion = null;
|
||||
|
||||
public ConnectedProtocolClient(ConnectionHandler connectionHandler) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
}
|
||||
|
||||
public ProtocolVersion getClientVersion() {
|
||||
return clientVersion == null ? ProtocolVersion.PV_1_0_0_CLASSIC : clientVersion;
|
||||
}
|
||||
|
||||
public void setClientVersion(ProtocolVersion clientVersion) {
|
||||
if (clientVersion == null) this.clientVersion = clientVersion;
|
||||
}
|
||||
|
||||
public boolean isStableClient() {
|
||||
return !isBetaClient() && !isClassicClient();
|
||||
}
|
||||
|
||||
public boolean clientSupportStable() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.STABLE;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
return isStableClient() || yes;
|
||||
}
|
||||
|
||||
public boolean isBetaClient() {
|
||||
return getClientVersion().getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
}
|
||||
|
||||
public boolean clientSupportBeta() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
return isBetaClient() || yes;
|
||||
}
|
||||
|
||||
public boolean isClassicClient() {
|
||||
return getClientVersion().getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
}
|
||||
|
||||
public boolean clientSupportClassic() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
return isClassicClient() || yes;
|
||||
}
|
||||
|
||||
public boolean isPacketSupported(OACPacket packet) {
|
||||
return isVersionSupported(packet.getProtocolVersion());
|
||||
}
|
||||
|
||||
public boolean isVersionSupported(ProtocolVersion targetVersion) {
|
||||
return getClientVersion() == targetVersion || getClientVersion().getCompatibleVersions().contains(targetVersion);
|
||||
}
|
||||
}
|
@@ -1,152 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.side.server;
|
||||
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.DNSResponseCode;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.Domain;
|
||||
import lombok.Getter;
|
||||
import dev.unlegitdqrk.unlegitlibrary.file.ConfigurationManager;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.NetworkServer;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.utils.NetworkUtils;
|
||||
import dev.unlegitdqrk.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class ProtocolServer extends DefaultMethodsOverrider {
|
||||
public final class ServerCertificateFolderStructure {
|
||||
public final File certificatesFolder;
|
||||
|
||||
public final File publicFolder;
|
||||
public final File privateFolder;
|
||||
|
||||
public final File privateCAFolder;
|
||||
public final File privateServerFolder;
|
||||
|
||||
public final File publicCAFolder;
|
||||
public final File publicServerFolder;
|
||||
|
||||
public ServerCertificateFolderStructure() {
|
||||
certificatesFolder = new File("certificates");
|
||||
|
||||
publicFolder = new File(certificatesFolder, "public");
|
||||
privateFolder = new File(certificatesFolder, "private");
|
||||
|
||||
privateCAFolder = new File(privateFolder, "ca");
|
||||
privateServerFolder = new File(privateFolder, "server");
|
||||
|
||||
publicCAFolder = new File(publicFolder, "ca");
|
||||
publicServerFolder = new File(publicFolder, "server");
|
||||
|
||||
if (!certificatesFolder.exists()) certificatesFolder.mkdirs();
|
||||
|
||||
if (!publicFolder.exists()) publicFolder.mkdirs();
|
||||
if (!privateFolder.exists()) privateFolder.mkdirs();
|
||||
|
||||
if (!privateCAFolder.exists()) privateCAFolder.mkdirs();
|
||||
if (!privateServerFolder.exists()) privateServerFolder.mkdirs();
|
||||
|
||||
if (!publicCAFolder.exists()) publicCAFolder.mkdirs();
|
||||
if (!publicServerFolder.exists()) publicServerFolder.mkdirs();
|
||||
}
|
||||
|
||||
public final String caPrefix = "ca_dns_";
|
||||
public final String certPrefix = "cert_dns_";
|
||||
}
|
||||
|
||||
@Getter
|
||||
private final NetworkServer networkServer;
|
||||
|
||||
@Getter
|
||||
private List<ConnectedProtocolClient> clients;
|
||||
|
||||
@Getter
|
||||
private ServerCertificateFolderStructure folderStructure;
|
||||
|
||||
private final ConfigurationManager configurationManager;
|
||||
|
||||
public ProtocolServer(File configFile) throws IOException, CertificateException {
|
||||
if (!configFile.exists()) configFile.createNewFile();
|
||||
|
||||
configurationManager = new ConfigurationManager(configFile);
|
||||
configurationManager.loadProperties();
|
||||
|
||||
if (!configurationManager.isSet("server.site.info")) {
|
||||
configurationManager.set("server.site.info", "DNS-SERVER INFO SITE IP");
|
||||
configurationManager.saveProperties();
|
||||
}
|
||||
|
||||
if (!configurationManager.isSet("server.site.register")) {
|
||||
configurationManager.set("server.site.register", "SERVER IP TO DNS-FRONTENT WEBSITE");
|
||||
configurationManager.saveProperties();
|
||||
}
|
||||
|
||||
folderStructure = new ServerCertificateFolderStructure();
|
||||
|
||||
checkFileExists(folderStructure.publicCAFolder, folderStructure.caPrefix, ".pem");
|
||||
checkFileExists(folderStructure.publicCAFolder, folderStructure.caPrefix, ".srl");
|
||||
checkFileExists(folderStructure.privateCAFolder, folderStructure.caPrefix, ".key");
|
||||
|
||||
checkFileExists(folderStructure.publicServerFolder, folderStructure.certPrefix, ".crt");
|
||||
checkFileExists(folderStructure.privateServerFolder, folderStructure.certPrefix, ".key");
|
||||
|
||||
File certFile = new File(folderStructure.publicServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".crt");
|
||||
File keyFile = new File(folderStructure.privateServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".key");
|
||||
|
||||
ProtocolBridge protocolBridge = ProtocolBridge.getInstance();
|
||||
this.clients = new ArrayList<>();
|
||||
|
||||
this.networkServer = new NetworkServer.ServerBuilder().setLogger(protocolBridge.getLogger()).
|
||||
setEventManager(protocolBridge.getProtocolSettings().eventManager).
|
||||
setPacketHandler(protocolBridge.getProtocolSettings().packetHandler).
|
||||
setPort(protocolBridge.getProtocolSettings().port).
|
||||
setRequireClientCertificate(false).setRootCAFolder(folderStructure.publicCAFolder).setServerCertificate(certFile, keyFile).
|
||||
build();
|
||||
}
|
||||
|
||||
private final void checkFileExists(File folder, String prefix, String extension) throws CertificateException, IOException {
|
||||
boolean found = false;
|
||||
if (folder == null) throw new FileNotFoundException("Folder does not exist");
|
||||
|
||||
File[] files = folder.listFiles();
|
||||
if (files == null || files.length == 0) throw new FileNotFoundException("Folder " + folder.getAbsolutePath() + " is empty");
|
||||
|
||||
for (File file : files) {
|
||||
if (!file.getName().startsWith(prefix) || !file.getName().endsWith(extension)) throw new CertificateException(file.getAbsolutePath() + " is not valid");
|
||||
if (!found) found = file.getName().equalsIgnoreCase(prefix + NetworkUtils.getPublicIPAddress() + extension);
|
||||
}
|
||||
|
||||
if (!found) throw new CertificateException("Missing " + prefix + NetworkUtils.getPublicIPAddress() + extension);
|
||||
}
|
||||
|
||||
public final ConnectedProtocolClient getClientByID(int clientID) {
|
||||
for (ConnectedProtocolClient client : clients)
|
||||
if (client.getConnectionHandler().getClientID() == clientID) return client;
|
||||
return null;
|
||||
}
|
||||
|
||||
public final String getDNSInfoSite() {
|
||||
return configurationManager.getString("server.site.info");
|
||||
}
|
||||
|
||||
public final String getDNSRegisterSite() {
|
||||
return configurationManager.getString("server.site.register");
|
||||
}
|
||||
|
||||
public abstract List<Domain> getDomains();
|
||||
|
||||
public abstract String getDomainDestination(Domain domain);
|
||||
|
||||
public abstract String getSubnameDestination(Domain domain, String subname);
|
||||
|
||||
public abstract String getTLNInfoSite(String topLevelName);
|
||||
|
||||
public abstract DNSResponseCode validateDomain(Domain requestedDomain);
|
||||
|
||||
public abstract void validationFailed(Domain domain, ConnectedProtocolClient client, Exception exception);
|
||||
|
||||
public abstract void getDomainDestinationFailed(ConnectedProtocolClient client, Domain domain, DNSResponseCode validationResponse, Exception exception);
|
||||
}
|
@@ -1,15 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.side.server.events;
|
||||
|
||||
import org.openautonomousconnection.protocol.side.server.ConnectedProtocolClient;
|
||||
import lombok.Getter;
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.impl.Event;
|
||||
|
||||
public class ProtocolClientConnected extends Event {
|
||||
|
||||
@Getter
|
||||
private final ConnectedProtocolClient protocolClient;
|
||||
|
||||
public ProtocolClientConnected(ConnectedProtocolClient protocolClient) {
|
||||
this.protocolClient = protocolClient;
|
||||
}
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public enum ProtocolVersion implements Serializable {
|
||||
PV_1_0_0_CLASSIC("1.0.0", ProtocolType.CLASSIC, ProtocolSide.BOTH),
|
||||
PV_1_0_0_BETA("1.0.0", ProtocolType.BETA, ProtocolSide.BOTH, PV_1_0_0_CLASSIC);
|
||||
|
||||
@Getter
|
||||
private final String version;
|
||||
@Getter
|
||||
private final ProtocolType protocolType;
|
||||
@Getter
|
||||
private final ProtocolSide protocolSide;
|
||||
@Getter
|
||||
private final List<ProtocolVersion> compatibleVersions;
|
||||
|
||||
ProtocolVersion(String version, ProtocolType protocolType, ProtocolSide protocolSide, ProtocolVersion... compatibleVersions) {
|
||||
this.version = version;
|
||||
this.protocolType = protocolType;
|
||||
this.protocolSide = protocolSide;
|
||||
this.compatibleVersions = new ArrayList<>(Arrays.stream(compatibleVersions).toList());
|
||||
if (!this.compatibleVersions.contains(this)) this.compatibleVersions.add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
StringBuilder compatible = new StringBuilder("[");
|
||||
for (ProtocolVersion compatibleVersion : compatibleVersions) compatible.append(compatibleVersion.buildName());
|
||||
compatible.append("]");
|
||||
|
||||
return "{version=" + version + ";type=" + protocolType.toString() + ";side=" + protocolSide.toString() + ";compatible=" + compatible + "}";
|
||||
}
|
||||
|
||||
public final String buildName() {
|
||||
return version + "-" + protocolType.toString();
|
||||
}
|
||||
|
||||
public enum ProtocolType implements Serializable {
|
||||
CLASSIC, // -> See "_old" Projects on GitHub Organisation https://github.com/Open-Autonomous-Connection/
|
||||
BETA,
|
||||
STABLE;
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return name().toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
public enum ProtocolSide implements Serializable {
|
||||
CLIENT, // Protocol version can only used on Client
|
||||
SERVER, // Protocol version can only used on Server
|
||||
BOTH // Protocol version can only used on Server and Client
|
||||
|
||||
;
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return name().toUpperCase();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,47 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.beta;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public enum DNSResponseCode implements Serializable {
|
||||
RESPONSE_NOT_REQUIRED(0, "Response code not required"),
|
||||
RESPONSE_INVALID_REQUEST(1, "Invalid request"),
|
||||
|
||||
RESPONSE_AUTH_SUCCESS(4, "Auth success"),
|
||||
RESPONSE_AUTH_FAILED(5, "Auth failed"),
|
||||
|
||||
RESPONSE_DOMAIN_NAME_EXIST(100, "Domainname exist"),
|
||||
RESPONSE_DOMAIN_NAME_NOT_EXIST(101, "Domainname does not exist"),
|
||||
RESPONSE_DOMAIN_NAME_CREATED(105, "Domainname created"),
|
||||
RESPONSE_DOMAIN_NAME_DELETED(106, "Domainname deleted"),
|
||||
|
||||
RESPONSE_DOMAIN_TLN_EXIST(110, "TopLevelName exist"),
|
||||
RESPONSE_DOMAIN_TLN_NOT_EXIST(111, "TopLevelName does not exist"),
|
||||
RESPONSE_DOMAIN_TLN_CREATED(115, "TopLevelName created"),
|
||||
RESPONSE_DOMAIN_TLN_DELETED(116, "TopLevelName deleted"),
|
||||
|
||||
RESPONSE_DOMAIN_SUBNAME_EXIST(120, "Subname exist"),
|
||||
RESPONSE_DOMAIN_SUBNAME_NOT_EXIST(121, "Subname does not exist"),
|
||||
RESPONSE_DOMAIN_SUBNAME_CREATED(125, "Subname created"),
|
||||
RESPONSE_DOMAIN_SUBNAME_DELETED(126, "Subname deleted"),
|
||||
|
||||
RESPONSE_DOMAIN_FULLY_EXIST(130, "Full domain exist"),
|
||||
RESPONSE_DOMAIN_FULLY_NOT_EXIST(131, "Full domain does not exist");
|
||||
|
||||
@Getter
|
||||
private final int code;
|
||||
|
||||
@Getter
|
||||
private final String description;
|
||||
|
||||
DNSResponseCode(int code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{code=" + code + ";description=" + description + "}";
|
||||
}
|
||||
}
|
@@ -1,102 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.beta;
|
||||
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Domain implements Serializable {
|
||||
@Getter
|
||||
private final String subname;
|
||||
@Getter
|
||||
private final String name;
|
||||
@Getter
|
||||
private final String topLevelName;
|
||||
@Getter
|
||||
private String path;
|
||||
@Getter
|
||||
private String query;
|
||||
@Getter
|
||||
private String fragment;
|
||||
@Getter
|
||||
private String protocol;
|
||||
public Domain(String fullDomain) {
|
||||
// Remove protocol
|
||||
String domainWithPath = fullDomain.contains("://") ? fullDomain.split("://", 2)[1] : fullDomain;
|
||||
this.protocol = fullDomain.contains("://") ? fullDomain.split("://", 2)[0] : "";
|
||||
if (this.protocol.endsWith("://"))
|
||||
this.protocol = this.protocol.substring(0, this.protocol.length() - "://".length());
|
||||
|
||||
// Cut path
|
||||
String[] domainPartsAndPath = domainWithPath.split("/", 2);
|
||||
String host = domainPartsAndPath[0]; // z.B. hello.world.com
|
||||
String fullPath = domainPartsAndPath.length > 1 ? "/" + domainPartsAndPath[1] : "";
|
||||
|
||||
// Split domain in labels
|
||||
List<String> labels = Arrays.asList(host.split("\\."));
|
||||
if (labels.size() < 2) throw new IllegalArgumentException("Invalid domain: " + host);
|
||||
|
||||
this.topLevelName = labels.getLast();
|
||||
this.name = labels.get(labels.size() - 2);
|
||||
this.subname = labels.size() > 2 ? String.join(".", labels.subList(0, labels.size() - 2)) : null;
|
||||
|
||||
if (fullPath.contains("#")) {
|
||||
this.fragment = "#" + Arrays.stream(fullPath.split("#")).toList().getLast();
|
||||
fullPath = fullPath.substring(0, fullPath.length() - ("#" + fragment).length());
|
||||
} else this.fragment = "";
|
||||
|
||||
// Split path and query
|
||||
if (fullPath.contains("?")) {
|
||||
String[] parts = fullPath.split("\\?", 2);
|
||||
this.path = parts[0];
|
||||
this.query = parts[1];
|
||||
} else {
|
||||
this.path = fullPath;
|
||||
this.query = "";
|
||||
}
|
||||
|
||||
if (this.path.startsWith("/")) this.path = this.path.substring(1);
|
||||
if (this.path.endsWith("/")) this.path = this.path.substring(0, this.path.length() - 1);
|
||||
|
||||
if (this.query.startsWith("?")) this.query = this.query.substring(1);
|
||||
if (this.fragment.startsWith("#")) this.fragment = this.fragment.substring(1);
|
||||
}
|
||||
|
||||
public final boolean hasSubname() {
|
||||
return subname != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
if (!(obj instanceof Domain domain)) return false;
|
||||
|
||||
return domain.getSubname().equalsIgnoreCase(this.subname) && domain.getName().equalsIgnoreCase(this.name) &&
|
||||
domain.getTopLevelName().equalsIgnoreCase(this.topLevelName) && domain.getProtocol().equalsIgnoreCase(this.protocol);
|
||||
}
|
||||
|
||||
public final String getDestination() {
|
||||
if (ProtocolBridge.getInstance().isRunningAsClient())
|
||||
return DNSResponseCode.RESPONSE_INVALID_REQUEST.toString();
|
||||
|
||||
if (this.equals(DefaultDomains.DNS_INFO_SITE))
|
||||
return ProtocolBridge.getInstance().getProtocolServer().getDNSInfoSite();
|
||||
if (this.equals(DefaultDomains.DNS_REGISTER_SITE))
|
||||
return ProtocolBridge.getInstance().getProtocolServer().getDNSRegisterSite();
|
||||
if (this.name.equalsIgnoreCase("about") && this.protocol.equalsIgnoreCase("oac"))
|
||||
return ProtocolBridge.getInstance().getProtocolServer().getTLNInfoSite(topLevelName);
|
||||
|
||||
return !hasSubname() ? ProtocolBridge.getInstance().getProtocolServer().getDomainDestination(this) : ProtocolBridge.getInstance().getProtocolServer().getSubnameDestination(this, subname);
|
||||
}
|
||||
|
||||
public static class DefaultDomains {
|
||||
public static final Domain DNS_INFO_SITE = new Domain("oac://about.oac/");
|
||||
public static final Domain DNS_REGISTER_SITE = new Domain("oac://register.oac/");
|
||||
|
||||
public static Domain TLN_INFO_SITE(String topLevelName) {
|
||||
return new Domain("oac://about." + topLevelName + "/");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,25 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.Domain;
|
||||
|
||||
public class ClassicConverter {
|
||||
|
||||
public static Domain classicDomainToNewDomain(Classic_Domain classicDomain) {
|
||||
return new Domain(classicDomain.name + "." + classicDomain.topLevelDomain + (classicDomain.path.startsWith("/") ? classicDomain.path : "/" + classicDomain.path));
|
||||
}
|
||||
|
||||
public static Classic_Domain newDomainToClassicDomain(Domain newDomain) {
|
||||
return new Classic_Domain(newDomain.getName(), newDomain.getTopLevelName(), newDomain.getDestination(), newDomain.getPath());
|
||||
}
|
||||
|
||||
public static ProtocolVersion classicProtocolVersionToNewProtocolVersion(Classic_ProtocolVersion classicProtocolVersion) {
|
||||
if (classicProtocolVersion == Classic_ProtocolVersion.PV_1_0_0) return ProtocolVersion.PV_1_0_0_CLASSIC;
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Classic_ProtocolVersion newProtocolVersionToClassicProtocolVersion(ProtocolVersion newProtocolVersion) {
|
||||
return Classic_ProtocolVersion.PV_1_0_0;
|
||||
}
|
||||
|
||||
}
|
@@ -1,10 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
public abstract class ClassicHandlerClient {
|
||||
|
||||
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);
|
||||
}
|
@@ -1,15 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
import org.openautonomousconnection.protocol.side.server.ConnectedProtocolClient;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public abstract class ClassicHandlerServer {
|
||||
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);
|
||||
}
|
@@ -1,78 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_PingPacket;
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.EventListener;
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.Listener;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class Classic_ClientListener extends EventListener {
|
||||
|
||||
@Listener
|
||||
public void onDomain(Classic_DomainPacketReceivedEvent event) {
|
||||
boolean exists = event.domain != null;
|
||||
|
||||
if (exists) {
|
||||
try {
|
||||
if (!ProtocolBridge.getInstance().getProtocolClient().getNetworkClient().sendPacket(new Classic_PingPacket(event.requestDomain, event.domain, false))) {
|
||||
ProtocolBridge.getInstance().getClassicHandlerClient().handleHTMLContent(Classic_SiteType.PROTOCOL, new Classic_LocalDomain("error-occurred", "html", ""),
|
||||
Classic_WebsitesContent.ERROR_OCCURRED(event.domain + "/" + event.domain.path));
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
ProtocolBridge.getInstance().getClassicHandlerClient().handleHTMLContent(Classic_SiteType.PROTOCOL, new Classic_LocalDomain("error-occurred", "html", ""),
|
||||
Classic_WebsitesContent.ERROR_OCCURRED(event.domain + "/" + event.domain.path + ":\n" + e.getMessage()));
|
||||
}
|
||||
} else
|
||||
ProtocolBridge.getInstance().getClassicHandlerClient().handleHTMLContent(Classic_SiteType.PROTOCOL, new Classic_LocalDomain("domain-not-found", "html", ""), Classic_WebsitesContent.DOMAIN_NOT_FOUND);
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onPing(Classic_PingPacketReceivedEvent event) {
|
||||
if (event.reachable) {
|
||||
String destination = event.domain.getDomain().getDestination();
|
||||
|
||||
try {
|
||||
URL url = new URL(destination);
|
||||
HttpURLConnection connection2 = (HttpURLConnection) url.openConnection();
|
||||
connection2.setRequestMethod("GET");
|
||||
|
||||
StringBuilder content = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection2.getInputStream()))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) content.append(line);
|
||||
}
|
||||
|
||||
ProtocolBridge.getInstance().getClassicHandlerClient().handleHTMLContent(Classic_SiteType.PUBLIC, event.domain, content.toString());
|
||||
} catch (IOException exception) {
|
||||
ProtocolBridge.getInstance().getClassicHandlerClient().handleHTMLContent(Classic_SiteType.PROTOCOL, new Classic_LocalDomain("error-occurred", "html", ""),
|
||||
Classic_WebsitesContent.ERROR_OCCURRED(exception.getMessage().replace(event.domain.getDomain().getDestination(), event.domain + "/" + event.domain.path)));
|
||||
}
|
||||
} else
|
||||
ProtocolBridge.getInstance().getClassicHandlerClient().handleHTMLContent(Classic_SiteType.PROTOCOL, new Classic_LocalDomain("error-not-reached", "html", ""), Classic_WebsitesContent.DOMAIN_NOT_REACHABLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -1,39 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.Domain;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Classic_Domain implements Serializable {
|
||||
public final String name;
|
||||
public final String topLevelDomain;
|
||||
public final String path;
|
||||
private final String destination;
|
||||
@Getter
|
||||
private final Domain domain;
|
||||
|
||||
public Classic_Domain(String name, String topLevelDomain, String destination, String path) {
|
||||
this.domain = new Domain(name + "." + topLevelDomain + "/" + (path.startsWith("/") ? path : "/" + path));
|
||||
this.name = domain.getName();
|
||||
this.topLevelDomain = domain.getTopLevelName();
|
||||
this.destination = domain.getDestination();
|
||||
this.path = domain.getPath();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return new Classic_Domain(name, topLevelDomain, destination, path);
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
@@ -1,38 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.impl.Event;
|
||||
|
||||
public 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 final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -1,76 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
|
||||
class Classic_DomainUtils extends DefaultMethodsOverrider {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
String domain = uri.getHost().replace("." + getTopLevelDomain(url), "");
|
||||
return domain;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@@ -1,7 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
public class Classic_LocalDomain extends Classic_Domain {
|
||||
public Classic_LocalDomain(String name, String endName, String path) {
|
||||
super(name, endName, null, path);
|
||||
}
|
||||
}
|
@@ -1,40 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.impl.Event;
|
||||
|
||||
public 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 final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -1,12 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public enum Classic_ProtocolVersion implements Serializable {
|
||||
PV_1_0_0("1.0.0");
|
||||
public final String version;
|
||||
|
||||
Classic_ProtocolVersion(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
@@ -1,10 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Classic_RequestDomain extends Classic_Domain implements Serializable {
|
||||
|
||||
public Classic_RequestDomain(String name, String topLevelDomain, String path) {
|
||||
super(name, topLevelDomain, null, path);
|
||||
}
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
enum Classic_SiteType implements Serializable {
|
||||
CLIENT("oac-client"), SERVER("oac-server"),
|
||||
PUBLIC("oac"), PROTOCOL("oac-protocol"), LOCAL("oac-local");
|
||||
|
||||
public final String name;
|
||||
|
||||
Classic_SiteType(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
public 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 domain 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 domain!</h1>
|
||||
<h4>Details:</h2>
|
||||
<h5>""" + errorDetails + "</h5>" + """
|
||||
</body>
|
||||
</html>
|
||||
""";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user