191 lines
8.9 KiB
Java
191 lines
8.9 KiB
Java
package org.openautonomousconnection.protocol;
|
|
|
|
import org.openautonomousconnection.protocol.listeners.ClientListener;
|
|
import org.openautonomousconnection.protocol.listeners.DNSServerListener;
|
|
import org.openautonomousconnection.protocol.listeners.WebServerListener;
|
|
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.dns.ProtocolDNSServer;
|
|
import org.openautonomousconnection.protocol.side.web.ProtocolWebServer;
|
|
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 ProtocolDNSServer protocolDNSServer;
|
|
@Getter
|
|
private ProtocolClient protocolClient;
|
|
@Getter
|
|
private ProtocolWebServer protocolWebServer;
|
|
@Getter
|
|
@Setter
|
|
private ClassicHandlerServer classicHandlerServer;
|
|
@Getter
|
|
@Setter
|
|
private ClassicHandlerClient classicHandlerClient;
|
|
|
|
public ProtocolBridge(ProtocolDNSServer protocolDNSServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
|
this.protocolDNSServer = protocolDNSServer;
|
|
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 DNSServerListener());
|
|
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 DNSServerListener());
|
|
|
|
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 final boolean isPacketSupported(OACPacket packet) {
|
|
return isVersionSupported(packet.getProtocolVersion());
|
|
}
|
|
|
|
public final 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 final boolean isProtocolSupported(ProtocolVersion.Protocol protocol) {
|
|
boolean yes = false;
|
|
for (ProtocolVersion compatibleVersion : protocolVersion.getCompatibleVersions()) {
|
|
yes = compatibleVersion.getSupportedProtocols().contains(protocol);
|
|
if (yes) break;
|
|
}
|
|
|
|
return protocolVersion.getSupportedProtocols().contains(protocol) || yes;
|
|
}
|
|
|
|
public final boolean isRunningAsDNSServer() {
|
|
return protocolDNSServer != null;
|
|
}
|
|
|
|
public final boolean isRunningAsClient() {
|
|
return protocolClient != null;
|
|
}
|
|
|
|
public final boolean isRunningAsWebServer() {
|
|
return protocolWebServer != null;
|
|
}
|
|
|
|
private boolean validateProtocolSide() {
|
|
return
|
|
(isRunningAsClient() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.CLIENT) ||
|
|
(isRunningAsClient() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.CLIENT_WEB) ||
|
|
(isRunningAsClient() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.CLIENT_DNS) ||
|
|
(isRunningAsClient() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.ALL) ||
|
|
|
|
(isRunningAsWebServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.WEB) ||
|
|
(isRunningAsWebServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.CLIENT_WEB) ||
|
|
(isRunningAsWebServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.WEB_DNS) ||
|
|
(isRunningAsWebServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.ALL) ||
|
|
|
|
(isRunningAsDNSServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.DNS) ||
|
|
(isRunningAsDNSServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.WEB_DNS) ||
|
|
(isRunningAsDNSServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.CLIENT_DNS) ||
|
|
(isRunningAsDNSServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.ALL);
|
|
}
|
|
|
|
public final boolean isVersionSupported(ProtocolVersion targetVersion) {
|
|
return protocolVersion == targetVersion || protocolVersion.getCompatibleVersions().contains(targetVersion);
|
|
}
|
|
}
|