Added builtin ClassicHandler
This commit is contained in:
@@ -5,15 +5,23 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.listeners.ClientListener;
|
||||
import org.openautonomousconnection.protocol.listeners.INSServerListener;
|
||||
import org.openautonomousconnection.protocol.listeners.WebServerListener;
|
||||
import org.openautonomousconnection.protocol.listeners.CustomServerListener;
|
||||
import org.openautonomousconnection.protocol.packets.OACPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.*;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.AuthPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.INSQueryPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.INSResponsePacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.UnsupportedClassicPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebRequestPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebResponsePacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.stream.WebStreamChunkPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.stream.WebStreamEndPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.stream.WebStreamStartPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_DomainPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_MessagePacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.classic.Classic_PingPacket;
|
||||
import org.openautonomousconnection.protocol.side.client.ProtocolClient;
|
||||
import org.openautonomousconnection.protocol.side.ins.ProtocolINSServer;
|
||||
import org.openautonomousconnection.protocol.side.server.ProtocolCustomServer;
|
||||
import org.openautonomousconnection.protocol.side.web.ProtocolWebServer;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerClient;
|
||||
@@ -48,23 +56,18 @@ public final class ProtocolBridge {
|
||||
@Getter
|
||||
private Logger logger;
|
||||
|
||||
/**
|
||||
* The protocol side instances
|
||||
*/
|
||||
@Getter
|
||||
private ProtocolINSServer protocolINSServer;
|
||||
|
||||
/**
|
||||
* The protocol side instances
|
||||
*/
|
||||
@Getter
|
||||
private ProtocolClient protocolClient;
|
||||
|
||||
|
||||
/**
|
||||
* The protocol side instances
|
||||
*/
|
||||
@Getter
|
||||
private ProtocolWebServer protocolWebServer;
|
||||
private ProtocolCustomServer protocolServer;
|
||||
|
||||
/**
|
||||
* The classic protocol handlers for INS server side
|
||||
@@ -95,18 +98,18 @@ public final class ProtocolBridge {
|
||||
private Proxy proxy;
|
||||
|
||||
/**
|
||||
* Initialize the ProtocolBridge instance for the INS server side
|
||||
* Initialize the ProtocolBridge instance for the client side
|
||||
*
|
||||
* @param protocolINSServer The ProtocolINSServer instance
|
||||
* @param protocolSettings The ProtocolSettings instance
|
||||
* @param protocolVersion The ProtocolVersion instance
|
||||
* @param logFolder The folder to store the log files
|
||||
* @param protocolServer The ProtocolCustomServer instance
|
||||
* @param protocolSettings The ProtocolSettings instance
|
||||
* @param protocolVersion The ProtocolVersion instance
|
||||
* @param logFolder The folder to store the log files
|
||||
* @throws Exception if an error occurs while initializing the ProtocolBridge
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.INS)
|
||||
public ProtocolBridge(ProtocolINSServer protocolINSServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception {
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.CLIENT)
|
||||
public ProtocolBridge(ProtocolCustomServer protocolServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception {
|
||||
// Assign the parameters to the class fields
|
||||
this.protocolINSServer = protocolINSServer;
|
||||
this.protocolServer = protocolServer;
|
||||
this.protocolSettings = protocolSettings;
|
||||
this.protocolVersion = protocolVersion;
|
||||
|
||||
@@ -118,36 +121,7 @@ public final class ProtocolBridge {
|
||||
registerListeners();
|
||||
registerPackets();
|
||||
|
||||
// Assign Bridge
|
||||
protocolINSServer.attachBridge(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the ProtocolBridge instance for the web server side
|
||||
*
|
||||
* @param protocolWebServer The ProtocolWebServer instance
|
||||
* @param protocolSettings The ProtocolSettings instance
|
||||
* @param protocolVersion The ProtocolVersion instance
|
||||
* @param logFolder The folder to store the log files
|
||||
* @throws Exception if an error occurs while initializing the ProtocolBridge
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.WEB)
|
||||
public ProtocolBridge(ProtocolWebServer protocolWebServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception {
|
||||
// Assign the parameters to the class fields
|
||||
this.protocolWebServer = protocolWebServer;
|
||||
this.protocolSettings = protocolSettings;
|
||||
this.protocolVersion = protocolVersion;
|
||||
|
||||
// Initialize the logger and protocol version
|
||||
initializeLogger(logFolder);
|
||||
initializeProtocolVersion();
|
||||
|
||||
// Register the appropriate listeners and packets
|
||||
registerListeners();
|
||||
registerPackets();
|
||||
|
||||
// Assign Bridge
|
||||
protocolWebServer.attachBridge(this);
|
||||
protocolServer.attachBridge(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,14 +176,20 @@ public final class ProtocolBridge {
|
||||
WebStreamEndPacket v100BStreamEndPacket = new WebStreamEndPacket();
|
||||
|
||||
if (isPacketSupported(v100bAuthPath)) protocolSettings.packetHandler.registerPacket(v100bAuthPath);
|
||||
if (isPacketSupported(v100bUnsupportedClassicPacket)) protocolSettings.packetHandler.registerPacket(v100bUnsupportedClassicPacket);
|
||||
if (isPacketSupported(v100bUnsupportedClassicPacket))
|
||||
protocolSettings.packetHandler.registerPacket(v100bUnsupportedClassicPacket);
|
||||
if (isPacketSupported(v100BINSQueryPacket)) protocolSettings.packetHandler.registerPacket(v100BINSQueryPacket);
|
||||
if (isPacketSupported(v100BINSResponsePacket)) protocolSettings.packetHandler.registerPacket(v100BINSResponsePacket);
|
||||
if (isPacketSupported(v100BWebRequestPacket)) protocolSettings.packetHandler.registerPacket(v100BWebRequestPacket);
|
||||
if (isPacketSupported(v100BINSResponsePacket))
|
||||
protocolSettings.packetHandler.registerPacket(v100BINSResponsePacket);
|
||||
if (isPacketSupported(v100BWebRequestPacket))
|
||||
protocolSettings.packetHandler.registerPacket(v100BWebRequestPacket);
|
||||
if (isPacketSupported(v100BResponsePacket)) protocolSettings.packetHandler.registerPacket(v100BResponsePacket);
|
||||
if (isPacketSupported(v100BStreamChunkPacket)) protocolSettings.packetHandler.registerPacket(v100BStreamChunkPacket);
|
||||
if (isPacketSupported(v100BStreamStartPacket)) protocolSettings.packetHandler.registerPacket(v100BStreamStartPacket);
|
||||
if (isPacketSupported(v100BStreamEndPacket)) protocolSettings.packetHandler.registerPacket(v100BStreamEndPacket);
|
||||
if (isPacketSupported(v100BStreamChunkPacket))
|
||||
protocolSettings.packetHandler.registerPacket(v100BStreamChunkPacket);
|
||||
if (isPacketSupported(v100BStreamStartPacket))
|
||||
protocolSettings.packetHandler.registerPacket(v100BStreamStartPacket);
|
||||
if (isPacketSupported(v100BStreamEndPacket))
|
||||
protocolSettings.packetHandler.registerPacket(v100BStreamEndPacket);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -225,31 +205,20 @@ public final class ProtocolBridge {
|
||||
protocolSettings.eventManager.registerListener(classicListener.getClass());
|
||||
} else protocolSettings.eventManager.unregisterListener(Classic_ClientListener.class);
|
||||
|
||||
// INS Listeners
|
||||
if (isRunningAsINSServer()) {
|
||||
INSServerListener serverListener = new INSServerListener();
|
||||
serverListener.setINSServer(protocolINSServer);
|
||||
protocolSettings.eventManager.registerListener(serverListener.getClass());
|
||||
protocolSettings.eventManager.unregisterListener(WebServerListener.class);
|
||||
protocolSettings.eventManager.unregisterListener(ClientListener.class);
|
||||
}
|
||||
|
||||
// Web Listeners
|
||||
if (isRunningAsWebServer()) {
|
||||
WebServerListener serverListener = new WebServerListener();
|
||||
serverListener.setWebServer(protocolWebServer);
|
||||
protocolSettings.eventManager.registerListener(serverListener.getClass());
|
||||
protocolSettings.eventManager.unregisterListener(INSServerListener.class);
|
||||
protocolSettings.eventManager.unregisterListener(ClientListener.class);
|
||||
}
|
||||
|
||||
// Client Listeners
|
||||
if (isRunningAsClient()) {
|
||||
ClientListener clientListener = new ClientListener();
|
||||
clientListener.setClient(protocolClient);
|
||||
protocolSettings.eventManager.registerListener(clientListener.getClass());
|
||||
protocolSettings.eventManager.unregisterListener(INSServerListener.class);
|
||||
protocolSettings.eventManager.unregisterListener(WebServerListener.class);
|
||||
protocolSettings.eventManager.unregisterListener(CustomServerListener.class);
|
||||
}
|
||||
|
||||
// Server Listeners
|
||||
if (isRunningAsServer()) {
|
||||
CustomServerListener serverListener = new CustomServerListener();
|
||||
serverListener.setServer(protocolServer);
|
||||
protocolSettings.eventManager.registerListener(serverListener.getClass());
|
||||
protocolSettings.eventManager.unregisterListener(ClientListener.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -361,6 +330,8 @@ public final class ProtocolBridge {
|
||||
(isRunningAsWebServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.WEB_INS) ||
|
||||
(isRunningAsWebServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.ALL) ||
|
||||
|
||||
(isRunningAsServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.ALL) ||
|
||||
|
||||
(isRunningAsINSServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.INS) ||
|
||||
(isRunningAsINSServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.WEB_INS) ||
|
||||
(isRunningAsINSServer() && protocolVersion.getProtocolSide() == ProtocolVersion.ProtocolSide.CLIENT_INS) ||
|
||||
@@ -373,7 +344,7 @@ public final class ProtocolBridge {
|
||||
* @return true if the current instance is running as a INS server, false otherwise
|
||||
*/
|
||||
public boolean isRunningAsINSServer() {
|
||||
return protocolINSServer != null;
|
||||
return isRunningAsServer() && protocolServer instanceof ProtocolINSServer;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -391,6 +362,15 @@ public final class ProtocolBridge {
|
||||
* @return true if the current instance is running as a web server, false otherwise
|
||||
*/
|
||||
public boolean isRunningAsWebServer() {
|
||||
return protocolWebServer != null;
|
||||
return isRunningAsServer() && protocolServer instanceof ProtocolWebServer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current instance is running as a custom server
|
||||
*
|
||||
* @return true if the current instance is running as a custom server, false otherwise
|
||||
*/
|
||||
public boolean isRunningAsServer() {
|
||||
return protocolServer != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.openautonomousconnection.protocol.listeners;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.EventListener;
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.Listener;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.events.ConnectionHandlerConnectedEvent;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.events.ConnectionHandlerDisconnectedEvent;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.events.S_PacketReceivedEvent;
|
||||
@@ -9,58 +10,83 @@ import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.INSQueryPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.INSResponsePacket;
|
||||
import org.openautonomousconnection.protocol.side.ins.ConnectedProtocolClient;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebRequestPacket;
|
||||
import org.openautonomousconnection.protocol.side.ins.ProtocolINSServer;
|
||||
import org.openautonomousconnection.protocol.side.server.CustomConnectedClient;
|
||||
import org.openautonomousconnection.protocol.side.server.ProtocolCustomServer;
|
||||
import org.openautonomousconnection.protocol.side.server.events.S_CustomClientDisconnectedEvent;
|
||||
import org.openautonomousconnection.protocol.side.web.ProtocolWebServer;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecord;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseStatus;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Listener for INS server connection events.
|
||||
* Listener for web server connection events.
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.INS)
|
||||
public final class INSServerListener extends EventListener {
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.WEB)
|
||||
public final class CustomServerListener extends EventListener {
|
||||
|
||||
/**
|
||||
* The reference to the INSServer object
|
||||
* The reference to the CustomProtocolServer object
|
||||
*/
|
||||
@Getter
|
||||
private ProtocolINSServer insServer;
|
||||
private ProtocolCustomServer server;
|
||||
|
||||
/**
|
||||
* Injects the INS server instance once.
|
||||
* Sets the webServer variable
|
||||
*
|
||||
* @param insServer The INS server implementation.
|
||||
* @param server The Instance of the CustomProtocolServer
|
||||
*/
|
||||
public void setINSServer(ProtocolINSServer insServer) {
|
||||
if (this.insServer != null) return;
|
||||
this.insServer = insServer;
|
||||
public void setServer(ProtocolCustomServer server) {
|
||||
if (this.server != null) return;
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a new connection handler is attached to the INS network server.
|
||||
* Wraps the connection in a {@link ConnectedProtocolClient} and keeps track of it.
|
||||
* Handles the event when a connection is established.
|
||||
* Adds the connected client to the protocol web server's client list.
|
||||
*
|
||||
* @param event The connection event fired by the network layer.
|
||||
* @param event The connection handler connected event.
|
||||
*/
|
||||
@Listener
|
||||
public void onConnect(ConnectionHandlerConnectedEvent event) {
|
||||
insServer.getClients().add(new ConnectedProtocolClient(event.getConnectionHandler(), insServer));
|
||||
Class<? extends CustomConnectedClient> serverClass = server.getCustomClient();
|
||||
try {
|
||||
CustomConnectedClient client = serverClass.getConstructor(ConnectionHandler.class, ProtocolCustomServer.class).newInstance(event.getConnectionHandler(), server);
|
||||
server.getClients().add(client);
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
|
||||
NoSuchMethodException e) {
|
||||
server.getProtocolBridge().getLogger().exception("Failed to instantiate connected client", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a connection handler disconnects from the INS server.
|
||||
* Removes the associated {@link ConnectedProtocolClient} from the list.
|
||||
* Handles the event when a connection is disconnected.
|
||||
* Removes the disconnected client from the protocol web server's client list.
|
||||
*
|
||||
* @param event The disconnection event fired by the network layer.
|
||||
* @param event The connection handler disconnected event.
|
||||
*/
|
||||
@Listener
|
||||
public void onDisconnect(ConnectionHandlerDisconnectedEvent event) {
|
||||
insServer.getClients().removeIf(client -> client.getConnectionHandler().getClientID() == -1);
|
||||
for (CustomConnectedClient client : server.getClients()) {
|
||||
if (client.getPipelineConnection().getClientID() != -1) continue;
|
||||
server.getProtocolBridge().getProtocolSettings().eventManager.executeEvent(new S_CustomClientDisconnectedEvent(client));
|
||||
}
|
||||
|
||||
server.getClients().removeIf(client -> client.getPipelineConnection().getClientID() == -1);
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onPacketWeb(S_PacketReceivedEvent event) {
|
||||
if (!server.getProtocolBridge().isRunningAsWebServer()) return;
|
||||
if (!(event.getPacket() instanceof WebRequestPacket packet)) return;
|
||||
|
||||
((ProtocolWebServer) server).onWebRequest(server.getClientByID(event.getConnectionHandler().getClientID()), packet);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,8 +106,11 @@ public final class INSServerListener extends EventListener {
|
||||
* @param event The packet event received by the network system.
|
||||
*/
|
||||
@Listener
|
||||
public void onPacket(S_PacketReceivedEvent event) {
|
||||
public void onPacketINS(S_PacketReceivedEvent event) {
|
||||
if (!(event.getPacket() instanceof INSQueryPacket q)) return;
|
||||
if (!server.getProtocolBridge().isRunningAsINSServer()) return;
|
||||
|
||||
ProtocolINSServer insServer = (ProtocolINSServer) server;
|
||||
|
||||
insServer.onQueryReceived(q.getTLN(), q.getName(), q.getSub(), q.getType());
|
||||
List<INSRecord> resolved = new ArrayList<>();
|
||||
@@ -124,4 +153,4 @@ public final class INSServerListener extends EventListener {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.listeners;
|
||||
|
||||
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;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.side.web.ConnectedWebClient;
|
||||
import org.openautonomousconnection.protocol.side.web.ProtocolWebServer;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
|
||||
/**
|
||||
* Listener for web server connection events.
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.WEB)
|
||||
public final class WebServerListener extends EventListener {
|
||||
|
||||
/**
|
||||
* The reference to the ProtocolWebServer object
|
||||
*/
|
||||
@Getter
|
||||
private ProtocolWebServer webServer;
|
||||
|
||||
/**
|
||||
* Sets the webServer variable
|
||||
*
|
||||
* @param webServer The Instance of the ProtocolWebServer
|
||||
*/
|
||||
public void setWebServer(ProtocolWebServer webServer) {
|
||||
if (this.webServer != null) return;
|
||||
this.webServer = webServer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the event when a connection is established.
|
||||
* Adds the connected client to the protocol web server's client list.
|
||||
*
|
||||
* @param event The connection handler connected event.
|
||||
*/
|
||||
@Listener
|
||||
public void onConnect(ConnectionHandlerConnectedEvent event) {
|
||||
webServer.getClients().add(new ConnectedWebClient(event.getConnectionHandler()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the event when a connection is disconnected.
|
||||
* Removes the disconnected client from the protocol web server's client list.
|
||||
*
|
||||
* @param event The connection handler disconnected event.
|
||||
*/
|
||||
@Listener
|
||||
public void onDisconnect(ConnectionHandlerDisconnectedEvent event) {
|
||||
webServer.getClients().removeIf(client -> client.getPipelineConnection().getClientID() == -1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,10 +7,9 @@ import dev.unlegitdqrk.unlegitlibrary.network.utils.NetworkUtils;
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.packets.OACPacket;
|
||||
import org.openautonomousconnection.protocol.side.client.events.ConnectedToProtocolINSServerEvent;
|
||||
import org.openautonomousconnection.protocol.side.client.events.ConnectedToProtocolWebServerEvent;
|
||||
import org.openautonomousconnection.protocol.side.ins.ConnectedProtocolClient;
|
||||
import org.openautonomousconnection.protocol.side.ins.events.ConnectedProtocolClientEvent;
|
||||
import org.openautonomousconnection.protocol.side.web.ConnectedWebClient;
|
||||
import org.openautonomousconnection.protocol.side.client.events.ConnectedToProtocolServerEvent;
|
||||
import org.openautonomousconnection.protocol.side.server.CustomConnectedClient;
|
||||
import org.openautonomousconnection.protocol.side.server.events.S_CustomClientConnectedEvent;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseStatus;
|
||||
|
||||
@@ -71,19 +70,19 @@ public final class AuthPacket extends OACPacket {
|
||||
String caPem = "N/A";
|
||||
String caSrl = "N/A";
|
||||
try {
|
||||
objectOutputStream.writeUTF(protocolBridge.getProtocolINSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress());
|
||||
objectOutputStream.writeUTF(protocolBridge.getProtocolServer().getFolderStructure().getCaPrefix() + NetworkUtils.getPublicIPAddress());
|
||||
|
||||
caKey = FileUtils.readFileFull(new File(
|
||||
protocolBridge.getProtocolINSServer().getFolderStructure().privateCAFolder,
|
||||
protocolBridge.getProtocolINSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".key"));
|
||||
protocolBridge.getProtocolServer().getFolderStructure().privateCAFolder,
|
||||
protocolBridge.getProtocolServer().getFolderStructure().getCaPrefix() + NetworkUtils.getPublicIPAddress() + ".key"));
|
||||
|
||||
caPem = FileUtils.readFileFull(new File(
|
||||
protocolBridge.getProtocolINSServer().getFolderStructure().publicCAFolder,
|
||||
protocolBridge.getProtocolINSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".pem"));
|
||||
protocolBridge.getProtocolServer().getFolderStructure().publicCAFolder,
|
||||
protocolBridge.getProtocolServer().getFolderStructure().getCaPrefix() + NetworkUtils.getPublicIPAddress() + ".pem"));
|
||||
|
||||
caSrl = FileUtils.readFileFull(new File(
|
||||
protocolBridge.getProtocolINSServer().getFolderStructure().publicCAFolder,
|
||||
protocolBridge.getProtocolINSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".srl"));
|
||||
protocolBridge.getProtocolServer().getFolderStructure().publicCAFolder,
|
||||
protocolBridge.getProtocolServer().getFolderStructure().getCaPrefix() + NetworkUtils.getPublicIPAddress() + ".srl"));
|
||||
} catch (Exception exception) {
|
||||
protocolBridge.getLogger().exception("Failed to read ca-files", exception);
|
||||
setResponseCode(INSResponseStatus.RESPONSE_AUTH_FAILED);
|
||||
@@ -110,10 +109,10 @@ public final class AuthPacket extends OACPacket {
|
||||
*/
|
||||
@Override
|
||||
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
if (protocolBridge.isRunningAsINSServer() || protocolBridge.isRunningAsWebServer()) {
|
||||
if (protocolBridge.isRunningAsServer()) {
|
||||
int clientID = objectInputStream.readInt();
|
||||
ProtocolVersion clientVersion = (ProtocolVersion) objectInputStream.readObject();
|
||||
ConnectionHandler connectionHandler = protocolBridge.getProtocolINSServer().getNetworkServer().getConnectionHandlerByID(clientID);
|
||||
ConnectionHandler connectionHandler = protocolBridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID);
|
||||
|
||||
if (!protocolBridge.isVersionSupported(clientVersion)) {
|
||||
setResponseCode(INSResponseStatus.RESPONSE_AUTH_FAILED);
|
||||
@@ -122,14 +121,9 @@ public final class AuthPacket extends OACPacket {
|
||||
} else setResponseCode(INSResponseStatus.RESPONSE_AUTH_SUCCESS);
|
||||
|
||||
|
||||
if (protocolBridge.isRunningAsINSServer()) {
|
||||
ConnectedProtocolClient client = protocolBridge.getProtocolINSServer().getClientByID(clientID);
|
||||
client.setClientVersion(clientVersion);
|
||||
protocolBridge.getProtocolSettings().eventManager.executeEvent(new ConnectedProtocolClientEvent(client));
|
||||
} else {
|
||||
ConnectedWebClient client = protocolBridge.getProtocolWebServer().getClientByID(clientID);
|
||||
client.setClientVersion(clientVersion);
|
||||
}
|
||||
CustomConnectedClient client = protocolBridge.getProtocolServer().getClientByID(clientID);
|
||||
client.setClientVersion(clientVersion);
|
||||
protocolBridge.getProtocolSettings().eventManager.executeEvent(new S_CustomClientConnectedEvent(client));
|
||||
} else if (protocolBridge.isRunningAsClient()) {
|
||||
ProtocolVersion serverVersion = (ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
@@ -171,8 +165,8 @@ public final class AuthPacket extends OACPacket {
|
||||
protocolBridge.getProtocolClient().setInsServerVersion(serverVersion);
|
||||
protocolBridge.getProtocolSettings().eventManager.executeEvent(new ConnectedToProtocolINSServerEvent(protocolBridge.getProtocolClient()));
|
||||
} catch (Exception ignored) {
|
||||
protocolBridge.getProtocolClient().setWebServerVersion(serverVersion);
|
||||
protocolBridge.getProtocolSettings().eventManager.executeEvent(new ConnectedToProtocolWebServerEvent(protocolBridge.getProtocolClient()));
|
||||
protocolBridge.getProtocolClient().setServerVersion(serverVersion);
|
||||
protocolBridge.getProtocolSettings().eventManager.executeEvent(new ConnectedToProtocolServerEvent(protocolBridge.getProtocolClient()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,11 +78,11 @@ public final class UnsupportedClassicPacket extends OACPacket {
|
||||
}
|
||||
|
||||
if (protocolBridge.isRunningAsINSServer())
|
||||
protocolBridge.getClassicHandlerINSServer().unsupportedClassicPacket(className, content, protocolBridge.getProtocolINSServer().getClientByID(clientID));
|
||||
protocolBridge.getClassicHandlerINSServer().unsupportedClassicPacket(className, content, protocolBridge.getProtocolServer().getClientByID(clientID));
|
||||
else if (protocolBridge.isRunningAsClient())
|
||||
protocolBridge.getClassicHandlerClient().unsupportedClassicPacket(className, content);
|
||||
else if (protocolBridge.isRunningAsWebServer())
|
||||
protocolBridge.getClassicHandlerWebServer().unsupportedClassicPacket(className, content, protocolBridge.getProtocolINSServer().getClientByID(clientID));
|
||||
protocolBridge.getClassicHandlerWebServer().unsupportedClassicPacket(className, content, protocolBridge.getProtocolServer().getClientByID(clientID));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta.web;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import lombok.Getter;
|
||||
@@ -20,7 +20,7 @@ public final class WebRequestPacket extends OACPacket {
|
||||
private WebRequestMethod method;
|
||||
|
||||
@Getter
|
||||
private Map<String,String> headers;
|
||||
private Map<String, String> headers;
|
||||
|
||||
@Getter
|
||||
private byte[] body;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta.web;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import lombok.Getter;
|
||||
@@ -19,7 +19,7 @@ public final class WebResponsePacket extends OACPacket {
|
||||
private String contentType; // text/ohtml, text/plain, application/json, text/py
|
||||
|
||||
@Getter
|
||||
private Map<String,String> headers;
|
||||
private Map<String, String> headers;
|
||||
|
||||
@Getter
|
||||
private byte[] body;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.stream;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import lombok.Getter;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.stream;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import lombok.Getter;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta;
|
||||
package org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.stream;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import lombok.Getter;
|
||||
@@ -63,12 +63,12 @@ public final class Classic_DomainPacket extends OACPacket {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
bridge.getProtocolINSServer().getNetworkServer().getEventManager().executeEvent(new Classic_DomainPacketReceivedEvent(protocolVersion, domain, requestDomain, clientID));
|
||||
bridge.getProtocolServer().getPipelineServer().getEventManager().executeEvent(new Classic_DomainPacketReceivedEvent(protocolVersion, domain, requestDomain, clientID));
|
||||
|
||||
if (bridge.getProtocolINSServer().getClientByID(clientID).supportClientClassic())
|
||||
bridge.getProtocolINSServer().getNetworkServer().getConnectionHandlerByID(clientID).sendPacket(new Classic_DomainPacket(clientID, requestDomain, domain, bridge));
|
||||
if (bridge.getProtocolServer().getClientByID(clientID).supportClientClassic())
|
||||
bridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new Classic_DomainPacket(clientID, requestDomain, domain, bridge));
|
||||
else
|
||||
bridge.getProtocolINSServer().getNetworkServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{clientID, requestDomain, domain}, bridge));
|
||||
bridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{clientID, requestDomain, domain}, bridge));
|
||||
} else if (bridge.isRunningAsClient()) {
|
||||
clientID = objectInputStream.readInt();
|
||||
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
|
||||
@@ -81,7 +81,7 @@ public final class Classic_DomainPacket extends OACPacket {
|
||||
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
|
||||
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
bridge.getProtocolWebServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{clientID, requestDomain, domain}, bridge));
|
||||
bridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{clientID, requestDomain, domain}, bridge));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,10 @@ public final class Classic_MessagePacket extends OACPacket {
|
||||
private ProtocolBridge bridge;
|
||||
|
||||
// Constructor with message and client id
|
||||
public Classic_MessagePacket(String message, int toClient, ProtocolBridge bridge) {
|
||||
public Classic_MessagePacket(String message, int clientID, ProtocolBridge bridge) {
|
||||
this();
|
||||
this.message = message;
|
||||
this.clientID = toClient;
|
||||
this.clientID = clientID;
|
||||
this.bridge = bridge;
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public final class Classic_MessagePacket extends OACPacket {
|
||||
String message = objectInputStream.readUTF();
|
||||
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
bridge.getClassicHandlerINSServer().handleMessage(bridge.getProtocolINSServer().getClientByID(clientID), message, protocolVersion);
|
||||
bridge.getClassicHandlerINSServer().handleMessage(bridge.getProtocolServer().getClientByID(clientID), message, protocolVersion);
|
||||
} else if (bridge.isRunningAsClient()) {
|
||||
clientID = objectInputStream.readInt();
|
||||
String message = objectInputStream.readUTF();
|
||||
@@ -59,7 +59,7 @@ public final class Classic_MessagePacket extends OACPacket {
|
||||
String message = objectInputStream.readUTF();
|
||||
Classic_ProtocolVersion protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
bridge.getClassicHandlerWebServer().handleMessage(bridge.getProtocolINSServer().getClientByID(clientID), message, protocolVersion);
|
||||
bridge.getClassicHandlerWebServer().handleMessage(bridge.getProtocolServer().getClientByID(clientID), message, protocolVersion);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,11 +68,11 @@ public final class Classic_PingPacket extends OACPacket {
|
||||
}
|
||||
|
||||
reachable = domain != null;
|
||||
bridge.getProtocolINSServer().getNetworkServer().getEventManager().executeEvent(new Classic_PingPacketReceivedEvent(protocolVersion, domain, requestDomain, reachable, clientID));
|
||||
if (bridge.getProtocolINSServer().getClientByID(clientID).supportClientClassic())
|
||||
bridge.getProtocolINSServer().getNetworkServer().getConnectionHandlerByID(clientID).sendPacket(new Classic_PingPacket(requestDomain, domain, reachable, bridge));
|
||||
bridge.getProtocolServer().getPipelineServer().getEventManager().executeEvent(new Classic_PingPacketReceivedEvent(protocolVersion, domain, requestDomain, reachable, clientID));
|
||||
if (bridge.getProtocolServer().getClientByID(clientID).supportClientClassic())
|
||||
bridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new Classic_PingPacket(requestDomain, domain, reachable, bridge));
|
||||
else
|
||||
bridge.getProtocolINSServer().getNetworkServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{requestDomain, domain, reachable}, bridge));
|
||||
bridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{requestDomain, domain, reachable}, bridge));
|
||||
} else if (bridge.isRunningAsClient()) {
|
||||
clientID = objectInputStream.readInt();
|
||||
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
|
||||
@@ -87,7 +87,7 @@ public final class Classic_PingPacket extends OACPacket {
|
||||
requestDomain = (Classic_RequestDomain) objectInputStream.readObject();
|
||||
protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
bridge.getProtocolWebServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{requestDomain}, bridge));
|
||||
bridge.getProtocolServer().getPipelineServer().getConnectionHandlerByID(clientID).sendPacket(new UnsupportedClassicPacket(Classic_PingPacket.class, new Object[]{requestDomain}, bridge));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,25 +21,23 @@ import java.security.cert.CertificateException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Abstract class defining the client-side protocol operations and interactions with INS and web servers.
|
||||
* Abstract class defining the client-side protocol operations and interactions with INS and servers.
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.INS)
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.CLIENT)
|
||||
public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
/**
|
||||
* Handles everything with INS-Connection.
|
||||
*/
|
||||
private NetworkClient clientToINS;
|
||||
|
||||
/**
|
||||
* Handles everything with WebServer-Connection.
|
||||
*/
|
||||
private NetworkClient clientToWeb;
|
||||
|
||||
/**
|
||||
* Manages the folder structure for client certificates.
|
||||
*/
|
||||
@Getter
|
||||
private final ClientCertificateFolderStructure folderStructure;
|
||||
/**
|
||||
* Handles everything with INS-Connection.
|
||||
*/
|
||||
private NetworkClient clientToINS;
|
||||
/**
|
||||
* Handles everything with Server-Connection.
|
||||
*/
|
||||
private NetworkClient clientToServer;
|
||||
/**
|
||||
* The reference to the ProtocolBridge Object
|
||||
*/
|
||||
@@ -50,9 +48,9 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
*/
|
||||
private ProtocolVersion insServerVersion = null;
|
||||
/**
|
||||
* Stores the protocol version of the connected webserver.
|
||||
* Stores the protocol version of the connected server.
|
||||
*/
|
||||
private ProtocolVersion webServerVersion = null;
|
||||
private ProtocolVersion serverVersion = null;
|
||||
|
||||
/**
|
||||
* Initializes the ProtocolClient, setting up certificate folders and the INS client connection.
|
||||
@@ -74,38 +72,38 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to a WebServer.
|
||||
* Connects to a Server.
|
||||
*
|
||||
* @param host WebServer host
|
||||
* @param port WebServer port
|
||||
* @param host Server host
|
||||
* @param port Server port
|
||||
*/
|
||||
public final void connectToWebServer(String host, int port) {
|
||||
public final void connectToServer(String host, int port) {
|
||||
if (!protocolBridge.isRunningAsClient())
|
||||
throw new IllegalStateException("Not running as client");
|
||||
|
||||
if (clientToWeb != null && clientToWeb.isConnected())
|
||||
if (clientToServer != null && clientToServer.isConnected())
|
||||
return;
|
||||
|
||||
createWebClient(host, port);
|
||||
createServerClient(host, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the WebServer connection client.
|
||||
* Gets the Server connection client.
|
||||
*
|
||||
* @return the NetworkClient handling the WebServer connection.
|
||||
* @return the NetworkClient handling the Server connection.
|
||||
*/
|
||||
public final NetworkClient getClientWebConnection() {
|
||||
return clientToWeb;
|
||||
public final NetworkClient getClientServerConnection() {
|
||||
return clientToServer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize connection to WebServer
|
||||
* Initialize connection to Server
|
||||
*
|
||||
* @param host WebServer host
|
||||
* @param port WebServer port
|
||||
* @param host Server host
|
||||
* @param port Server port
|
||||
*/
|
||||
private final void createWebClient(String host, int port) {
|
||||
clientToWeb = new NetworkClient.ClientBuilder()
|
||||
private final void createServerClient(String host, int port) {
|
||||
clientToServer = new NetworkClient.ClientBuilder()
|
||||
.setLogger(protocolBridge.getLogger())
|
||||
.setProxy(protocolBridge.getProxy())
|
||||
.setHost(host)
|
||||
@@ -122,6 +120,7 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
|
||||
/**
|
||||
* Injects the ProtocolBridge.
|
||||
*
|
||||
* @param bridge the Bridge instance.
|
||||
* @throws IOException when NetworkServer failed to create.
|
||||
*/
|
||||
@@ -181,17 +180,17 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
*
|
||||
* @return the ProtocolVersion of the server, or PV_1_0_0_CLASSIC if not set.
|
||||
*/
|
||||
public final ProtocolVersion getWebServerVersion() {
|
||||
return webServerVersion == null ? ProtocolVersion.PV_1_0_0_CLASSIC : webServerVersion;
|
||||
public final ProtocolVersion getServerVersion() {
|
||||
return serverVersion == null ? ProtocolVersion.PV_1_0_0_CLASSIC : serverVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the protocol version of the connected server.
|
||||
*
|
||||
* @param webServerVersion the ProtocolVersion to set for the server.
|
||||
* @param serverVersion the ProtocolVersion to set for the server.
|
||||
*/
|
||||
public final void setWebServerVersion(ProtocolVersion webServerVersion) {
|
||||
if (webServerVersion == null) this.webServerVersion = insServerVersion;
|
||||
public final void setServerVersion(ProtocolVersion serverVersion) {
|
||||
if (serverVersion == null) this.serverVersion = insServerVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,7 +212,7 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles disconnect events, resetting the server version and closing the web client connection if necessary.
|
||||
* Handles disconnect events, resetting the server version and closing the server client connection if necessary.
|
||||
*
|
||||
* @param event the ClientDisconnectedEvent triggered on INS disconnection.
|
||||
*/
|
||||
@@ -221,10 +220,10 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
// Reset server version on INS disconnect
|
||||
if (!clientToINS.isConnected()) {
|
||||
insServerVersion = null;
|
||||
disconnectFromWebServer();
|
||||
disconnectFromServer();
|
||||
}
|
||||
|
||||
if (!clientToWeb.isConnected()) webServerVersion = null;
|
||||
if (!clientToServer.isConnected()) serverVersion = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -244,7 +243,7 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
*/
|
||||
public final boolean supportINSServerStable() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getWebServerVersion().getCompatibleVersions()) {
|
||||
for (ProtocolVersion compatibleVersion : getServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is stable
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.STABLE;
|
||||
if (yes) break;
|
||||
@@ -349,125 +348,126 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
}
|
||||
|
||||
//fwfwef
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver is a stable server.
|
||||
* Checks if the connected server is a stable server.
|
||||
*
|
||||
* @return true if the server is stable, false otherwise.
|
||||
*/
|
||||
public final boolean isWebStableServer() {
|
||||
public final boolean isStableServer() {
|
||||
// Check if the server version is stable
|
||||
return !isWebBetaServer() && !isWebClassicServer();
|
||||
return !isBetaServer() && !isClassicServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver or its compatible versions support stable protocol.
|
||||
* Checks if the connected server or its compatible versions support stable protocol.
|
||||
*
|
||||
* @return true if stable protocol is supported, false otherwise.
|
||||
*/
|
||||
public final boolean supportWebServerStable() {
|
||||
public final boolean supportServerStable() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getWebServerVersion().getCompatibleVersions()) {
|
||||
for (ProtocolVersion compatibleVersion : getServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is stable
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.STABLE;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the server version is stable
|
||||
return isWebBetaServer() || yes;
|
||||
return isBetaServer() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver is a beta server.
|
||||
* Checks if the connected server is a beta server.
|
||||
*
|
||||
* @return true if the server is beta, false otherwise.
|
||||
*/
|
||||
public final boolean isWebBetaServer() {
|
||||
public final boolean isBetaServer() {
|
||||
// Check if the server version is beta
|
||||
return getWebServerVersion().getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
return getServerVersion().getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver or its compatible versions support beta protocol.
|
||||
* Checks if the connected server or its compatible versions support beta protocol.
|
||||
*
|
||||
* @return true if beta protocol is supported, false otherwise.
|
||||
*/
|
||||
public final boolean supportWebServerBeta() {
|
||||
public final boolean supportServerBeta() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getWebServerVersion().getCompatibleVersions()) {
|
||||
for (ProtocolVersion compatibleVersion : getServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is beta
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the server version is beta
|
||||
return isWebStableServer() || yes;
|
||||
return isStableServer() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver is a classic server.
|
||||
* Checks if the connected server is a classic server.
|
||||
*
|
||||
* @return true if the server is classic, false otherwise.
|
||||
*/
|
||||
public final boolean isWebClassicServer() {
|
||||
public final boolean isClassicServer() {
|
||||
// Check if the server version is classic
|
||||
return getWebServerVersion().getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
return getServerVersion().getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver or its compatible versions support classic protocol.
|
||||
* Checks if the connected server or its compatible versions support classic protocol.
|
||||
*
|
||||
* @return true if classic protocol is supported, false otherwise.
|
||||
*/
|
||||
public final boolean supportWebServerClassic() {
|
||||
public final boolean supportServerClassic() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getWebServerVersion().getCompatibleVersions()) {
|
||||
for (ProtocolVersion compatibleVersion : getServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is classic
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the server version is classic
|
||||
return isWebClassicServer() || yes;
|
||||
return isClassicServer() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver supports the protocol version of the given packet.
|
||||
* Checks if the connected server supports the protocol version of the given packet.
|
||||
*
|
||||
* @param packet the OACPacket to check against the server's supported protocol version.
|
||||
* @return true if the server supports the packet's protocol version, false otherwise.
|
||||
*/
|
||||
public final boolean supportWebServerPacket(OACPacket packet) {
|
||||
public final boolean supportServerPacket(OACPacket packet) {
|
||||
// Check if the server supports the protocol version of the packet
|
||||
return supportWebServerVersion(packet.getProtocolVersion());
|
||||
return supportServerVersion(packet.getProtocolVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver or its compatible versions support the specified protocol version.
|
||||
* Checks if the connected server or its compatible versions support the specified protocol version.
|
||||
*
|
||||
* @param targetVersion the ProtocolVersion to check for support.
|
||||
* @return true if the server or its compatible versions support the target version, false otherwise.
|
||||
*/
|
||||
public final boolean supportWebServerVersion(ProtocolVersion targetVersion) {
|
||||
public final boolean supportServerVersion(ProtocolVersion targetVersion) {
|
||||
// Directly check if the server version matches or is in the list of compatible versions
|
||||
return getWebServerVersion() == targetVersion || getWebServerVersion().getCompatibleVersions().contains(targetVersion);
|
||||
return getServerVersion() == targetVersion || getServerVersion().getCompatibleVersions().contains(targetVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver or its compatible versions support the specified protocol.
|
||||
* Checks if the connected server or its compatible versions support the specified protocol.
|
||||
*
|
||||
* @param protocol the Protocol to check for support.
|
||||
* @return true if the server or its compatible versions support the protocol, false otherwise.
|
||||
*/
|
||||
public final boolean supportWebServerProtocol(ProtocolVersion.Protocol protocol) {
|
||||
public final boolean supportServerProtocol(ProtocolVersion.Protocol protocol) {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getWebServerVersion().getCompatibleVersions()) {
|
||||
for (ProtocolVersion compatibleVersion : getServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version supports the protocol
|
||||
yes = compatibleVersion.getSupportedProtocols().contains(protocol);
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the server version supports the protocol
|
||||
return getWebServerVersion().getSupportedProtocols().contains(protocol) || yes;
|
||||
return getServerVersion().getSupportedProtocols().contains(protocol) || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -491,12 +491,12 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects from the WebServer.
|
||||
* Disconnects from the Server.
|
||||
*/
|
||||
public final void disconnectFromWebServer() {
|
||||
if (clientToWeb != null) {
|
||||
clientToWeb.disconnect();
|
||||
clientToWeb = null;
|
||||
public final void disconnectFromServer() {
|
||||
if (clientToServer != null) {
|
||||
clientToServer.disconnect();
|
||||
clientToServer = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -508,7 +508,8 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
* @param status The response status from the server.
|
||||
* @param records The list of records returned by the server.
|
||||
*/
|
||||
public abstract void onResponse(INSResponseStatus status, List<INSRecord> records);
|
||||
public void onResponse(INSResponseStatus status, List<INSRecord> records) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after a query was sent to the INS server.
|
||||
@@ -525,7 +526,7 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
/**
|
||||
* Manages the folder structure for client certificates.
|
||||
*/
|
||||
public final class ClientCertificateFolderStructure {
|
||||
public static final class ClientCertificateFolderStructure {
|
||||
public final File certificatesFolder;
|
||||
|
||||
public final File publicFolder;
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.openautonomousconnection.protocol.side.client;
|
||||
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
|
||||
/**
|
||||
* Abstract class defining the client-side protocol operations and interactions with INS and servers.
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.CLIENT)
|
||||
public class ProtocolWebClient extends ProtocolClient {
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
* Event triggered when a client successfully connects to a INS protocol server.
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.INS)
|
||||
public final class ConnectedToProtocolWebServerEvent extends Event {
|
||||
public final class ConnectedToProtocolServerEvent extends Event {
|
||||
|
||||
/**
|
||||
* Reference to the ProtocolClient object.
|
||||
@@ -18,7 +18,7 @@ public final class ConnectedToProtocolWebServerEvent extends Event {
|
||||
@Getter
|
||||
private final ProtocolClient client;
|
||||
|
||||
public ConnectedToProtocolWebServerEvent(ProtocolClient client) {
|
||||
public ConnectedToProtocolServerEvent(ProtocolClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@@ -1,176 +1,17 @@
|
||||
package org.openautonomousconnection.protocol.side.ins;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.packets.OACPacket;
|
||||
import org.openautonomousconnection.protocol.side.server.CustomConnectedClient;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
|
||||
/**
|
||||
* Represents a connected protocol client on the INS server side.
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.INS)
|
||||
public final class ConnectedProtocolClient {
|
||||
|
||||
/**
|
||||
* The connection handler associated with this protocol client.
|
||||
*/
|
||||
@Getter
|
||||
private final ConnectionHandler connectionHandler;
|
||||
|
||||
/**
|
||||
* The Protocol Server associated with this protocol client.
|
||||
*/
|
||||
@Getter
|
||||
private final ProtocolINSServer protocolINSServer;
|
||||
|
||||
/**
|
||||
* The protocol version of the connected client.
|
||||
*/
|
||||
private ProtocolVersion clientVersion = null;
|
||||
public final class ConnectedProtocolClient extends CustomConnectedClient {
|
||||
|
||||
public ConnectedProtocolClient(ConnectionHandler connectionHandler, ProtocolINSServer protocolINSServer) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
this.protocolINSServer = protocolINSServer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the protocol version of the connected client.
|
||||
* Defaults to PV_1_0_0_CLASSIC if not set.
|
||||
*
|
||||
* @return The protocol version of the client.
|
||||
*/
|
||||
public ProtocolVersion getClientVersion() {
|
||||
return clientVersion == null ? ProtocolVersion.PV_1_0_0_CLASSIC : clientVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the protocol version of the connected client.
|
||||
*
|
||||
* @param clientVersion The protocol version to set.
|
||||
*/
|
||||
public void setClientVersion(ProtocolVersion clientVersion) {
|
||||
if (clientVersion == null) this.clientVersion = clientVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client is a stable client.
|
||||
*
|
||||
* @return True if the client is stable, false otherwise.
|
||||
*/
|
||||
public boolean isStableClient() {
|
||||
// Check if the server version is stable
|
||||
return !isBetaClient() && !isClassicClient();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports stable protocol versions.
|
||||
*
|
||||
* @return True if the client supports stable versions, false otherwise.
|
||||
*/
|
||||
public boolean supportClientStable() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is stable
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.STABLE;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the client version is stable
|
||||
return isStableClient() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client is a beta client.
|
||||
*
|
||||
* @return True if the client is beta, false otherwise.
|
||||
*/
|
||||
public boolean isBetaClient() {
|
||||
// Check if the server version is beta
|
||||
return getClientVersion().getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports beta protocol versions.
|
||||
*
|
||||
* @return True if the client supports beta versions, false otherwise.
|
||||
*/
|
||||
public boolean supportClientBeta() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is beta
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the client version is beta
|
||||
return isBetaClient() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client is a classic client.
|
||||
*
|
||||
* @return True if the client is classic, false otherwise.
|
||||
*/
|
||||
public boolean isClassicClient() {
|
||||
// Check if the server version is classic
|
||||
return getClientVersion().getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports classic protocol versions.
|
||||
*
|
||||
* @return True if the client supports classic versions, false otherwise.
|
||||
*/
|
||||
public boolean supportClientClassic() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is classic
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the client version is classic
|
||||
return isClassicClient() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports the given packet's protocol version.
|
||||
*
|
||||
* @param packet The packet to check.
|
||||
* @return True if the client supports the packet's protocol version, false otherwise.
|
||||
*/
|
||||
public boolean supportClientPacket(OACPacket packet) {
|
||||
return supportClientVersion(packet.getProtocolVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports the given protocol version.
|
||||
*
|
||||
* @param targetVersion The protocol version to check.
|
||||
* @return True if the client supports the target version, false otherwise.
|
||||
*/
|
||||
public boolean supportClientVersion(ProtocolVersion targetVersion) {
|
||||
// Check if the client version matches or is compatible with the target version
|
||||
return getClientVersion() == targetVersion || getClientVersion().getCompatibleVersions().contains(targetVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports the given protocol.
|
||||
*
|
||||
* @param protocol The protocol to check.
|
||||
* @return True if the client supports the protocol, false otherwise.
|
||||
*/
|
||||
public boolean supportClientProtocol(ProtocolVersion.Protocol protocol) {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version supports the protocol
|
||||
yes = compatibleVersion.getSupportedProtocols().contains(protocol);
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the client version supports the protocol
|
||||
return getClientVersion().getSupportedProtocols().contains(protocol) || yes;
|
||||
super(connectionHandler, protocolINSServer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +1,26 @@
|
||||
package org.openautonomousconnection.protocol.side.ins;
|
||||
|
||||
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 lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.side.server.ProtocolCustomServer;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecord;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecordType;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Abstract class representing a INS server in the protocol.
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.INS)
|
||||
public abstract class ProtocolINSServer extends DefaultMethodsOverrider {
|
||||
/**
|
||||
* The network server instance.
|
||||
*/
|
||||
@Getter
|
||||
private NetworkServer networkServer;
|
||||
|
||||
public abstract class ProtocolINSServer extends ProtocolCustomServer {
|
||||
/**
|
||||
* The configuration manager for handling server configurations.
|
||||
*/
|
||||
private final ConfigurationManager configurationManager;
|
||||
/**
|
||||
* The reference to the ProtocolBridge Object
|
||||
*/
|
||||
@Getter
|
||||
private ProtocolBridge protocolBridge;
|
||||
/**
|
||||
* List of connected protocol clients.
|
||||
*/
|
||||
@Getter
|
||||
private List<ConnectedProtocolClient> clients;
|
||||
/**
|
||||
* The folder structure for server certificates.
|
||||
*/
|
||||
@Getter
|
||||
private ServerCertificateFolderStructure folderStructure;
|
||||
|
||||
/**
|
||||
* Constructs a ProtocolINSServer with the specified configuration file.
|
||||
@@ -56,7 +29,9 @@ public abstract class ProtocolINSServer extends DefaultMethodsOverrider {
|
||||
* @throws IOException If an I/O error occurs.
|
||||
* @throws CertificateException If a certificate error occurs.
|
||||
*/
|
||||
public ProtocolINSServer(File configFile) throws IOException, CertificateException {
|
||||
public ProtocolINSServer(File configFile) throws Exception {
|
||||
super("ca_ins_", "cert_ins_");
|
||||
|
||||
// Ensure the configuration file exists
|
||||
if (!configFile.exists()) configFile.createNewFile();
|
||||
|
||||
@@ -75,99 +50,7 @@ public abstract class ProtocolINSServer extends DefaultMethodsOverrider {
|
||||
configurationManager.saveProperties();
|
||||
}
|
||||
|
||||
// Initialize the folder structure
|
||||
folderStructure = new ServerCertificateFolderStructure();
|
||||
|
||||
// Check for the existence of necessary certificate files
|
||||
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");
|
||||
|
||||
// Initialize the protocol bridge and clients list
|
||||
this.clients = new ArrayList<>();
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects the ProtocolBridge.
|
||||
* @param bridge the Bridge instance.
|
||||
* @throws IOException when NetworkServer failed to create.
|
||||
*/
|
||||
public final void attachBridge(ProtocolBridge bridge) throws IOException {
|
||||
if (this.protocolBridge != null)
|
||||
throw new IllegalStateException("ProtocolBridge already attached!");
|
||||
|
||||
this.protocolBridge = bridge;
|
||||
createNetworkServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the network server with the specified settings
|
||||
*/
|
||||
private void createNetworkServer() throws IOException {
|
||||
// Define the certificate and key files based on the public IP address
|
||||
File certFile = new File(folderStructure.publicServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".crt");
|
||||
File keyFile = new File(folderStructure.privateServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".key");
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the required certificate files exist in the specified folder.
|
||||
*
|
||||
* @param folder The folder to check for certificate files.
|
||||
* @param prefix The prefix of the certificate files.
|
||||
* @param extension The extension of the certificate files.
|
||||
* @throws CertificateException If a certificate error occurs.
|
||||
* @throws IOException If an I/O error occurs.
|
||||
*/
|
||||
private final void checkFileExists(File folder, String prefix, String extension) throws CertificateException, IOException {
|
||||
boolean found = false;
|
||||
|
||||
// Check if the folder exists
|
||||
if (folder == null) throw new FileNotFoundException("Folder does not exist");
|
||||
|
||||
// List all files in the folder
|
||||
File[] files = folder.listFiles();
|
||||
|
||||
// Check if the folder is empty
|
||||
if (files == null || files.length == 0)
|
||||
throw new FileNotFoundException("Folder " + folder.getAbsolutePath() + " is empty");
|
||||
|
||||
// Validate each file in the folder
|
||||
for (File file : files) {
|
||||
if (!file.getName().startsWith(prefix))
|
||||
throw new CertificateException(file.getAbsolutePath() + " is not valid");
|
||||
|
||||
// Check if the file matches the expected naming convention
|
||||
if (!found) found = file.getName().equalsIgnoreCase(prefix + NetworkUtils.getPublicIPAddress() + extension);
|
||||
}
|
||||
|
||||
// If the required file is not found, throw an exception
|
||||
if (!found) throw new CertificateException("Missing " + prefix + NetworkUtils.getPublicIPAddress() + extension);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a connected protocol client by its client ID.
|
||||
*
|
||||
* @param clientID The ID of the client to retrieve.
|
||||
* @return The ConnectedProtocolClient with the specified ID, or null if not found.
|
||||
*/
|
||||
public final ConnectedProtocolClient getClientByID(int clientID) {
|
||||
for (ConnectedProtocolClient client : clients)
|
||||
if (client.getConnectionHandler().getClientID() == clientID) return client;
|
||||
return null;
|
||||
setCustomClient(ConnectedProtocolClient.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -206,7 +89,8 @@ public abstract class ProtocolINSServer extends DefaultMethodsOverrider {
|
||||
* @param sub An optional subname, or {@code null}.
|
||||
* @param type The record type requested.
|
||||
*/
|
||||
public void onQueryReceived(String tln, String name, String sub, INSRecordType type) {}
|
||||
public void onQueryReceived(String tln, String name, String sub, INSRecordType type) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback fired after an INS response was successfully sent to the client.
|
||||
@@ -217,7 +101,8 @@ public abstract class ProtocolINSServer extends DefaultMethodsOverrider {
|
||||
* @param type The requested record type.
|
||||
* @param results The records returned to the client.
|
||||
*/
|
||||
public void onResponseSent(String tln, String name, String sub, INSRecordType type, List<INSRecord> results) {}
|
||||
public void onResponseSent(String tln, String name, String sub, INSRecordType type, List<INSRecord> results) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback fired when an INS response could not be delivered to the client.
|
||||
@@ -229,7 +114,8 @@ public abstract class ProtocolINSServer extends DefaultMethodsOverrider {
|
||||
* @param results The records that would have been sent.
|
||||
* @param exception The exception describing the failure.
|
||||
*/
|
||||
public void onResponseSentFailed(String tln, String name, String sub, INSRecordType type, List<INSRecord> results, Exception exception) {}
|
||||
public void onResponseSentFailed(String tln, String name, String sub, INSRecordType type, List<INSRecord> results, Exception exception) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the information endpoint for a given Top-Level Name (TLN).
|
||||
@@ -257,9 +143,8 @@ public abstract class ProtocolINSServer extends DefaultMethodsOverrider {
|
||||
*
|
||||
* @param tln the top-level name for which the info site should be resolved.
|
||||
* Must not be null. Case-insensitive.
|
||||
*
|
||||
* @return a string in <code>"host:port"</code> format representing the TLN's info endpoint,
|
||||
* or <code>null</code> if the TLN has no registered info site.
|
||||
* or <code>null</code> if the TLN has no registered info site.
|
||||
*/
|
||||
public abstract String resolveTLNInfoSite(String tln);
|
||||
|
||||
@@ -271,46 +156,4 @@ public abstract class ProtocolINSServer extends DefaultMethodsOverrider {
|
||||
public final String getINSFrontendSite() {
|
||||
return configurationManager.getString("server.site.frontend");
|
||||
}
|
||||
|
||||
/**
|
||||
* Class representing the folder structure for server certificates.
|
||||
*/
|
||||
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 final String caPrefix = "ca_ins_";
|
||||
public final String certPrefix = "cert_ins_";
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.side.ins.events;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.impl.Event;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.side.ins.ConnectedProtocolClient;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
|
||||
/**
|
||||
* Event triggered when a protocol client connects to the INS server.
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.INS)
|
||||
public final class ConnectedProtocolClientEvent extends Event {
|
||||
|
||||
@Getter
|
||||
private final ConnectedProtocolClient protocolClient;
|
||||
|
||||
public ConnectedProtocolClientEvent(ConnectedProtocolClient protocolClient) {
|
||||
this.protocolClient = protocolClient;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
package org.openautonomousconnection.protocol.side.server;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.packets.OACPacket;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
|
||||
public abstract class CustomConnectedClient {
|
||||
|
||||
@Getter
|
||||
private final ConnectionHandler pipelineConnection;
|
||||
|
||||
@Getter
|
||||
private final ProtocolCustomServer server;
|
||||
|
||||
private ProtocolVersion clientVersion = null;
|
||||
|
||||
@Getter
|
||||
private boolean clientVersionLoaded = false;
|
||||
|
||||
public CustomConnectedClient(ConnectionHandler pipelineConnection, ProtocolCustomServer protocolServer) {
|
||||
this.pipelineConnection = pipelineConnection;
|
||||
this.server = protocolServer;
|
||||
}
|
||||
|
||||
public synchronized void disconnect() {
|
||||
try {
|
||||
server.clientDisconnected(this);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
try {
|
||||
pipelineConnection.disconnect();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the protocol version of the connected client.
|
||||
*
|
||||
* @return The protocol version of the client, defaults to PV_1_0_0_CLASSIC if not set.
|
||||
*/
|
||||
public ProtocolVersion getClientVersion() {
|
||||
return clientVersion == null ? ProtocolVersion.PV_1_0_0_CLASSIC : clientVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the protocol version of the connected client.
|
||||
*
|
||||
* @param clientVersion The protocol version to set.
|
||||
*/
|
||||
public void setClientVersion(ProtocolVersion clientVersion) {
|
||||
if (clientVersionLoaded) return;
|
||||
if (clientVersion == null) return;
|
||||
|
||||
this.clientVersion = clientVersion;
|
||||
this.clientVersionLoaded = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client is a stable client.
|
||||
*
|
||||
* @return True if the client is stable, false otherwise.
|
||||
*/
|
||||
public boolean isStableClient() {
|
||||
// Check if the server version is stable
|
||||
return !isBetaClient() && !isClassicClient();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports stable protocol versions.
|
||||
*
|
||||
* @return True if the client supports stable versions, false otherwise.
|
||||
*/
|
||||
public boolean supportClientStable() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is stable
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.STABLE;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the client version is stable
|
||||
return isStableClient() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client is a beta client.
|
||||
*
|
||||
* @return True if the client is beta, false otherwise.
|
||||
*/
|
||||
public boolean isBetaClient() {
|
||||
// Check if the server version is beta
|
||||
return getClientVersion().getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports beta protocol versions.
|
||||
*
|
||||
* @return True if the client supports beta versions, false otherwise.
|
||||
*/
|
||||
public boolean supportClientBeta() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is beta
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the client version is beta
|
||||
return isBetaClient() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client is a classic client.
|
||||
*
|
||||
* @return True if the client is classic, false otherwise.
|
||||
*/
|
||||
public boolean isClassicClient() {
|
||||
return getClientVersion().getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports classic protocol versions.
|
||||
*
|
||||
* @return True if the client supports classic versions, false otherwise.
|
||||
*/
|
||||
public boolean supportClientClassic() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is classic
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the client version is classic
|
||||
return isClassicClient() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports the protocol version of the given packet.
|
||||
*
|
||||
* @param packet The packet to check support for.
|
||||
* @return True if the client supports the packet's protocol version, false otherwise.
|
||||
*/
|
||||
public boolean supportClientPacket(OACPacket packet) {
|
||||
return supportClientVersion(packet.getProtocolVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports the given protocol version.
|
||||
*
|
||||
* @param targetVersion The protocol version to check support for.
|
||||
* @return True if the client supports the target version, false otherwise.
|
||||
*/
|
||||
public boolean supportClientVersion(ProtocolVersion targetVersion) {
|
||||
// Check if the client version matches the target version or is compatible
|
||||
return getClientVersion() == targetVersion || getClientVersion().getCompatibleVersions().contains(targetVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports the given protocol.
|
||||
*
|
||||
* @param protocol The protocol to check support for.
|
||||
* @return True if the client supports the protocol, false otherwise.
|
||||
*/
|
||||
public boolean supportClientProtocol(ProtocolVersion.Protocol protocol) {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version supports the protocol
|
||||
yes = compatibleVersion.getSupportedProtocols().contains(protocol);
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the client version supports the protocol
|
||||
return getClientVersion().getSupportedProtocols().contains(protocol) || yes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package org.openautonomousconnection.protocol.side.server;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.NetworkServer;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.utils.NetworkUtils;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
|
||||
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 ProtocolCustomServer {
|
||||
|
||||
/**
|
||||
* Structure for server.
|
||||
*/
|
||||
@Getter
|
||||
private final ServerCertificateFolderStructure folderStructure;
|
||||
|
||||
/**
|
||||
* Certificate files for SSL.
|
||||
*/
|
||||
private final File certFile;
|
||||
/**
|
||||
* Certificate files for SSL.
|
||||
*/
|
||||
private final File keyFile;
|
||||
|
||||
/**
|
||||
* The reference to the ProtocolBridge Object
|
||||
*/
|
||||
@Getter
|
||||
private ProtocolBridge protocolBridge;
|
||||
|
||||
/**
|
||||
* List of connected web clients.
|
||||
*/
|
||||
@Getter
|
||||
private List<CustomConnectedClient> clients;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
private Class<? extends CustomConnectedClient> customClient;
|
||||
|
||||
/**
|
||||
* The network server handling pipeline connections.
|
||||
*/
|
||||
@Getter
|
||||
private NetworkServer pipelineServer;
|
||||
|
||||
/**
|
||||
* Initializes the web server with the given configuration, authentication, and rules files.
|
||||
*
|
||||
* @throws Exception If an error occurs during initialization.
|
||||
*/
|
||||
public ProtocolCustomServer(String caPrefix, String certPrefix) throws Exception {
|
||||
// Initialize the list of connected clients
|
||||
this.clients = new ArrayList<>();
|
||||
|
||||
// Set up folder structure for certificates
|
||||
folderStructure = new ServerCertificateFolderStructure(caPrefix, certPrefix);
|
||||
|
||||
// Check for necessary certificate files
|
||||
checkFileExists(folderStructure.publicServerFolder, folderStructure.certPrefix, ".crt");
|
||||
checkFileExists(folderStructure.privateServerFolder, folderStructure.certPrefix, ".key");
|
||||
|
||||
// Set up certificate files based on public IP address
|
||||
this.certFile = new File(folderStructure.publicServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".crt");
|
||||
this.keyFile = new File(folderStructure.privateServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".key");
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects the ProtocolBridge.
|
||||
*
|
||||
* @param bridge the Bridge instance.
|
||||
* @throws IOException when NetworkServer failed to create.
|
||||
*/
|
||||
public final void attachBridge(ProtocolBridge bridge) throws IOException {
|
||||
if (this.protocolBridge != null)
|
||||
throw new IllegalStateException("ProtocolBridge already attached!");
|
||||
|
||||
this.protocolBridge = bridge;
|
||||
createNetworkServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the pipeline server
|
||||
*/
|
||||
private void createNetworkServer() {
|
||||
pipelineServer = new NetworkServer.ServerBuilder().
|
||||
setPort(protocolBridge.getProtocolSettings().port).setTimeout(0).
|
||||
setPacketHandler(protocolBridge.getProtocolSettings().packetHandler).setEventManager(protocolBridge.getProtocolSettings().eventManager).
|
||||
setLogger(protocolBridge.getLogger()).
|
||||
setServerCertificate(certFile, keyFile).setRootCAFolder(folderStructure.publicCAFolder).
|
||||
build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a connected web client by its client ID.
|
||||
*
|
||||
* @param clientID The client ID to search for.
|
||||
* @return The connected web client with the specified ID, or null if not found.
|
||||
*/
|
||||
public final <T extends CustomConnectedClient> T getClientByID(int clientID) {
|
||||
for (CustomConnectedClient client : clients)
|
||||
if (client.getPipelineConnection().getClientID() == clientID) return (T) client;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the required certificate files exist in the specified folder.
|
||||
*
|
||||
* @param folder The folder to check for certificate files.
|
||||
* @param prefix The prefix of the certificate files.
|
||||
* @param extension The extension of the certificate files.
|
||||
* @throws CertificateException If a required certificate file is missing or invalid.
|
||||
* @throws IOException If an I/O error occurs while checking the files.
|
||||
*/
|
||||
private void checkFileExists(File folder, String prefix, String extension) throws CertificateException, IOException {
|
||||
boolean found = false;
|
||||
|
||||
// Ensure the folder exists
|
||||
if (folder == null) throw new FileNotFoundException("Folder does not exist");
|
||||
|
||||
// List all files in the folder
|
||||
File[] files = folder.listFiles();
|
||||
if (files == null || files.length == 0)
|
||||
throw new FileNotFoundException("Folder " + folder.getAbsolutePath() + " is empty");
|
||||
|
||||
// Check for the required certificate file
|
||||
for (File file : files) {
|
||||
if (!file.getName().startsWith(prefix))
|
||||
throw new CertificateException(file.getAbsolutePath() + " is not valid");
|
||||
|
||||
// Check for file matching the public IP address
|
||||
if (!found) found = file.getName().equalsIgnoreCase(prefix + NetworkUtils.getPublicIPAddress() + extension);
|
||||
}
|
||||
|
||||
// Throw exception if the required file is not found
|
||||
if (!found) throw new CertificateException("Missing " + prefix + NetworkUtils.getPublicIPAddress() + extension);
|
||||
}
|
||||
|
||||
public final void clientDisconnected(CustomConnectedClient client) {
|
||||
clients.remove(client);
|
||||
onDisconnect(client);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional callback when the connection closes.
|
||||
*/
|
||||
public void onDisconnect(CustomConnectedClient client) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the folder structure for server certificates.
|
||||
*/
|
||||
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;
|
||||
|
||||
@Getter
|
||||
private String caPrefix = "ca_server_";
|
||||
@Getter
|
||||
private String certPrefix = "cert_server_";
|
||||
|
||||
public ServerCertificateFolderStructure(String caPrefix, String certPrefix) {
|
||||
this.caPrefix = caPrefix;
|
||||
this.certPrefix = certPrefix;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.openautonomousconnection.protocol.side.server.events;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.impl.Event;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.side.server.CustomConnectedClient;
|
||||
|
||||
/**
|
||||
* Event triggered when a web client connects to the web server.
|
||||
*/
|
||||
public final class S_CustomClientConnectedEvent extends Event {
|
||||
|
||||
/**
|
||||
* The connected web client.
|
||||
*/
|
||||
@Getter
|
||||
private final CustomConnectedClient client;
|
||||
|
||||
public S_CustomClientConnectedEvent(CustomConnectedClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.openautonomousconnection.protocol.side.server.events;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.impl.Event;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.side.server.CustomConnectedClient;
|
||||
|
||||
/**
|
||||
* Event triggered when a web client connects to the web server.
|
||||
*/
|
||||
public final class S_CustomClientDisconnectedEvent extends Event {
|
||||
|
||||
/**
|
||||
* The connected web client.
|
||||
*/
|
||||
@Getter
|
||||
private final CustomConnectedClient client;
|
||||
|
||||
public S_CustomClientDisconnectedEvent(CustomConnectedClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
}
|
||||
@@ -2,258 +2,18 @@ package org.openautonomousconnection.protocol.side.web;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.packets.OACPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebRequestPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebResponsePacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebStreamChunkPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebStreamEndPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebStreamStartPacket;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import org.openautonomousconnection.protocol.side.server.CustomConnectedClient;
|
||||
|
||||
/**
|
||||
* A connected web client using pure protocol packets.
|
||||
*/
|
||||
public final class ConnectedWebClient {
|
||||
public final class ConnectedWebClient extends CustomConnectedClient {
|
||||
|
||||
@Getter
|
||||
private final ConnectionHandler pipelineConnection;
|
||||
private final ProtocolWebServer server;
|
||||
|
||||
@Getter
|
||||
private SSLSocket webSocket;
|
||||
|
||||
@Getter
|
||||
private ProtocolWebServer server;
|
||||
|
||||
private ObjectOutputStream out;
|
||||
private ObjectInputStream in;
|
||||
|
||||
private ProtocolVersion clientVersion = null;
|
||||
|
||||
@Getter
|
||||
private boolean clientVersionLoaded = false;
|
||||
|
||||
private Thread receiveThread;
|
||||
|
||||
public ConnectedWebClient(ConnectionHandler pipelineConnection) {
|
||||
this.pipelineConnection = pipelineConnection;
|
||||
public ConnectedWebClient(ConnectionHandler pipelineConnection, ProtocolWebServer webServer) {
|
||||
super(pipelineConnection, webServer);
|
||||
this.server = webServer;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return webSocket != null &&
|
||||
webSocket.isConnected() &&
|
||||
!webSocket.isClosed() &&
|
||||
pipelineConnection.isConnected();
|
||||
}
|
||||
|
||||
public void attachWebServer(SSLSocket socket, ProtocolWebServer server) throws IOException {
|
||||
if (this.webSocket != null) return;
|
||||
|
||||
this.webSocket = socket;
|
||||
this.server = server;
|
||||
|
||||
// IMPORTANT: ObjectOutputStream first, flush, then ObjectInputStream
|
||||
this.out = new ObjectOutputStream(webSocket.getOutputStream());
|
||||
this.out.flush();
|
||||
this.in = new ObjectInputStream(webSocket.getInputStream());
|
||||
|
||||
this.receiveThread = new Thread(this::receiveLoop, "OAC-WebClient-Receiver");
|
||||
this.receiveThread.start();
|
||||
}
|
||||
|
||||
private void receiveLoop() {
|
||||
try {
|
||||
while (isConnected()) {
|
||||
Object obj = in.readObject();
|
||||
if (!(obj instanceof OACPacket packet)) continue;
|
||||
|
||||
if (packet instanceof WebRequestPacket req) {
|
||||
// server decides whether it returns normal response or does streaming itself
|
||||
WebResponsePacket resp = server.onWebRequest(this, req);
|
||||
if (resp != null) send(resp);
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
} finally {
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void send(Object packet) throws IOException {
|
||||
if (!isConnected()) return;
|
||||
out.writeObject(packet);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
public synchronized void streamStart(WebStreamStartPacket start) throws IOException {
|
||||
send(start);
|
||||
}
|
||||
|
||||
public synchronized void streamChunk(WebStreamChunkPacket chunk) throws IOException {
|
||||
send(chunk);
|
||||
}
|
||||
|
||||
public synchronized void streamEnd(WebStreamEndPacket end) throws IOException {
|
||||
send(end);
|
||||
}
|
||||
|
||||
public synchronized void disconnect() {
|
||||
try { server.onDisconnect(this); } catch (Exception ignored) {}
|
||||
|
||||
try { pipelineConnection.disconnect(); } catch (Exception ignored) {}
|
||||
|
||||
try { if (in != null) in.close(); } catch (Exception ignored) {}
|
||||
try { if (out != null) out.close(); } catch (Exception ignored) {}
|
||||
try { if (webSocket != null) webSocket.close(); } catch (Exception ignored) {}
|
||||
|
||||
in = null;
|
||||
out = null;
|
||||
webSocket = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the protocol version of the connected client.
|
||||
*
|
||||
* @return The protocol version of the client, defaults to PV_1_0_0_CLASSIC if not set.
|
||||
*/
|
||||
public ProtocolVersion getClientVersion() {
|
||||
return clientVersion == null ? ProtocolVersion.PV_1_0_0_CLASSIC : clientVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the protocol version of the connected client.
|
||||
*
|
||||
* @param clientVersion The protocol version to set.
|
||||
*/
|
||||
public void setClientVersion(ProtocolVersion clientVersion) {
|
||||
if (clientVersionLoaded) return;
|
||||
if (clientVersion == null) return;
|
||||
|
||||
this.clientVersion = clientVersion;
|
||||
this.clientVersionLoaded = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client is a stable client.
|
||||
*
|
||||
* @return True if the client is stable, false otherwise.
|
||||
*/
|
||||
public boolean isStableClient() {
|
||||
// Check if the server version is stable
|
||||
return !isBetaClient() && !isClassicClient();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports stable protocol versions.
|
||||
*
|
||||
* @return True if the client supports stable versions, false otherwise.
|
||||
*/
|
||||
public boolean supportClientStable() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is stable
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.STABLE;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the client version is stable
|
||||
return isStableClient() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client is a beta client.
|
||||
*
|
||||
* @return True if the client is beta, false otherwise.
|
||||
*/
|
||||
public boolean isBetaClient() {
|
||||
// Check if the server version is beta
|
||||
return getClientVersion().getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports beta protocol versions.
|
||||
*
|
||||
* @return True if the client supports beta versions, false otherwise.
|
||||
*/
|
||||
public boolean supportClientBeta() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is beta
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the client version is beta
|
||||
return isBetaClient() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client is a classic client.
|
||||
*
|
||||
* @return True if the client is classic, false otherwise.
|
||||
*/
|
||||
public boolean isClassicClient() {
|
||||
return getClientVersion().getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports classic protocol versions.
|
||||
*
|
||||
* @return True if the client supports classic versions, false otherwise.
|
||||
*/
|
||||
public boolean supportClientClassic() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is classic
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the client version is classic
|
||||
return isClassicClient() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports the protocol version of the given packet.
|
||||
*
|
||||
* @param packet The packet to check support for.
|
||||
* @return True if the client supports the packet's protocol version, false otherwise.
|
||||
*/
|
||||
public boolean supportClientPacket(OACPacket packet) {
|
||||
return supportClientVersion(packet.getProtocolVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports the given protocol version.
|
||||
*
|
||||
* @param targetVersion The protocol version to check support for.
|
||||
* @return True if the client supports the target version, false otherwise.
|
||||
*/
|
||||
public boolean supportClientVersion(ProtocolVersion targetVersion) {
|
||||
// Check if the client version matches the target version or is compatible
|
||||
return getClientVersion() == targetVersion || getClientVersion().getCompatibleVersions().contains(targetVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected client supports the given protocol.
|
||||
*
|
||||
* @param protocol The protocol to check support for.
|
||||
* @return True if the client supports the protocol, false otherwise.
|
||||
*/
|
||||
public boolean supportClientProtocol(ProtocolVersion.Protocol protocol) {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getClientVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version supports the protocol
|
||||
yes = compatibleVersion.getSupportedProtocols().contains(protocol);
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the client version supports the protocol
|
||||
return getClientVersion().getSupportedProtocols().contains(protocol) || yes;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,35 +1,24 @@
|
||||
package org.openautonomousconnection.protocol.side.web;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.file.ConfigurationManager;
|
||||
import dev.unlegitdqrk.unlegitlibrary.file.FileUtils;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.NetworkServer;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.utils.NetworkUtils;
|
||||
import dev.unlegitdqrk.unlegitlibrary.string.RandomString;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebRequestPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebResponsePacket;
|
||||
import org.openautonomousconnection.protocol.side.web.events.WebClientConnectedEvent;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebRequestPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebResponsePacket;
|
||||
import org.openautonomousconnection.protocol.side.server.ProtocolCustomServer;
|
||||
import org.openautonomousconnection.protocol.side.web.managers.AuthManager;
|
||||
import org.openautonomousconnection.protocol.side.web.managers.RuleManager;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
|
||||
import javax.net.ssl.SSLServerSocket;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
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;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Represents the web server for the protocol.
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.WEB)
|
||||
public abstract class ProtocolWebServer {
|
||||
public abstract class ProtocolWebServer extends ProtocolCustomServer {
|
||||
/**
|
||||
* Folder for web content.
|
||||
*/
|
||||
@@ -41,46 +30,11 @@ public abstract class ProtocolWebServer {
|
||||
*/
|
||||
@Getter
|
||||
private final File errorsFolder;
|
||||
|
||||
/**
|
||||
* Structure for server.
|
||||
*/
|
||||
@Getter
|
||||
private final ServerCertificateFolderStructure folderStructure;
|
||||
|
||||
/**
|
||||
* Certificate files for SSL.
|
||||
*/
|
||||
private final File certFile;
|
||||
/**
|
||||
* Certificate files for SSL.
|
||||
*/
|
||||
private final File keyFile;
|
||||
/**
|
||||
* The configuration for the web server.
|
||||
*/
|
||||
@Getter
|
||||
private final WebServerConfig serverConfig;
|
||||
/**
|
||||
* The reference to the ProtocolBridge Object
|
||||
*/
|
||||
@Getter
|
||||
private ProtocolBridge protocolBridge;
|
||||
/**
|
||||
* The network server handling pipeline connections.
|
||||
*/
|
||||
@Getter
|
||||
private NetworkServer pipelineServer;
|
||||
/**
|
||||
* The SSL server socket for web connections.
|
||||
*/
|
||||
@Getter
|
||||
private SSLServerSocket webServer;
|
||||
/**
|
||||
* List of connected web clients.
|
||||
*/
|
||||
@Getter
|
||||
private List<ConnectedWebClient> clients;
|
||||
/**
|
||||
* A unique secret for session management.
|
||||
*/
|
||||
@@ -91,25 +45,17 @@ public abstract class ProtocolWebServer {
|
||||
* Initializes the web server with the given configuration, authentication, and rules files.
|
||||
*
|
||||
* @param serverConfig The configuration.
|
||||
* @param authFile The authentication file.
|
||||
* @param rulesFile The rules file.
|
||||
* @param authFile The authentication file.
|
||||
* @param rulesFile The rules file.
|
||||
* @throws Exception If an error occurs during initialization.
|
||||
*/
|
||||
public ProtocolWebServer(WebServerConfig serverConfig, File authFile, File rulesFile) throws Exception {
|
||||
// Initialize the list of connected clients
|
||||
this.clients = new ArrayList<>();
|
||||
super("ca_server_", "cert_server_");
|
||||
|
||||
// Store the configuration file
|
||||
this.serverConfig = serverConfig;
|
||||
this.serverConfig.attachWebServer(this);
|
||||
|
||||
// Set up folder structure for certificates
|
||||
folderStructure = new ServerCertificateFolderStructure();
|
||||
|
||||
// Check for necessary certificate files
|
||||
checkFileExists(folderStructure.publicServerFolder, folderStructure.certPrefix, ".crt");
|
||||
checkFileExists(folderStructure.privateServerFolder, folderStructure.certPrefix, ".key");
|
||||
|
||||
// Set up content and error folders
|
||||
contentFolder = new File("content");
|
||||
errorsFolder = new File("errors");
|
||||
@@ -118,10 +64,6 @@ public abstract class ProtocolWebServer {
|
||||
if (!contentFolder.exists()) contentFolder.mkdir();
|
||||
if (!errorsFolder.exists()) errorsFolder.mkdir();
|
||||
|
||||
// Set up certificate files based on public IP address
|
||||
this.certFile = new File(folderStructure.publicServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".crt");
|
||||
this.keyFile = new File(folderStructure.privateServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".key");
|
||||
|
||||
// Create auth and rules files with default content if they don't exist
|
||||
if (!authFile.exists()) {
|
||||
authFile.createNewFile();
|
||||
@@ -157,202 +99,16 @@ public abstract class ProtocolWebServer {
|
||||
|
||||
AuthManager.loadAuthFile(authFile);
|
||||
RuleManager.loadRules(rulesFile);
|
||||
|
||||
setCustomClient(ConnectedWebClient.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects the ProtocolBridge.
|
||||
* @param bridge the Bridge instance.
|
||||
* @throws IOException when NetworkServer failed to create.
|
||||
*/
|
||||
public final void attachBridge(ProtocolBridge bridge) throws IOException {
|
||||
if (this.protocolBridge != null)
|
||||
throw new IllegalStateException("ProtocolBridge already attached!");
|
||||
|
||||
this.protocolBridge = bridge;
|
||||
createNetworkServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the pipeline server
|
||||
*/
|
||||
private void createNetworkServer() {
|
||||
pipelineServer = new NetworkServer.ServerBuilder().
|
||||
setPort(protocolBridge.getProtocolSettings().port).setTimeout(0).
|
||||
setPacketHandler(protocolBridge.getProtocolSettings().packetHandler).setEventManager(protocolBridge.getProtocolSettings().eventManager).
|
||||
setLogger(protocolBridge.getLogger()).
|
||||
setServerCertificate(certFile, keyFile).setRootCAFolder(folderStructure.publicCAFolder).
|
||||
build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a connected web client by its client ID.
|
||||
*
|
||||
* @param clientID The client ID to search for.
|
||||
* @return The connected web client with the specified ID, or null if not found.
|
||||
*/
|
||||
public final ConnectedWebClient getClientByID(int clientID) {
|
||||
for (ConnectedWebClient client : clients)
|
||||
if (client.getPipelineConnection().getClientID() == clientID) return client;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the web server to accept and handle client connections.
|
||||
*
|
||||
* @throws Exception If an error occurs while starting the server.
|
||||
*/
|
||||
public final void startWebServer() throws Exception {
|
||||
// Start the pipeline server
|
||||
pipelineServer.start();
|
||||
|
||||
// Create the SSL server socket for web connections
|
||||
webServer = (SSLServerSocket) NetworkServer.ServerBuilder.
|
||||
createSSLServerSocketFactory(folderStructure.publicCAFolder, certFile, keyFile).
|
||||
createServerSocket(serverConfig.getPort());
|
||||
webServer.setSoTimeout(0);
|
||||
webServer.setEnabledProtocols(pipelineServer.getServerSocket().getEnabledProtocols());
|
||||
|
||||
// Stop WebServer if pipelineServer dies
|
||||
new Thread(() -> {
|
||||
while (true) {
|
||||
// Check if the pipeline server is still running
|
||||
if (pipelineServer == null || !pipelineServer.getServerSocket().isBound()) {
|
||||
try {
|
||||
// Stop the web server
|
||||
onPipelineStop();
|
||||
} catch (IOException e) {
|
||||
pipelineServer.getLogger().exception("Failed to stop WebServer", e);
|
||||
}
|
||||
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
// Sleep for a while before checking again
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
// Handle every client
|
||||
new Thread(() -> {
|
||||
while (true) {
|
||||
try {
|
||||
// Accept incoming client connections
|
||||
SSLSocket client = (SSLSocket) webServer.accept();
|
||||
|
||||
for (ConnectedWebClient connectedWebClient : clients) {
|
||||
if (connectedWebClient.getPipelineConnection().getClientID() != -1 && connectedWebClient.isClientVersionLoaded()) {
|
||||
// Assign socket to an existing connected client
|
||||
connectedWebClient.attachWebServer(client, this);
|
||||
protocolBridge.getProtocolSettings().eventManager.executeEvent(new WebClientConnectedEvent(connectedWebClient));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
pipelineServer.getLogger().exception("Failed to accept WebClient", e);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional callback when the connection closes.
|
||||
*/
|
||||
public void onDisconnect(ConnectedWebClient client) {}
|
||||
|
||||
/**
|
||||
* Called when the server receives a WebRequestPacket from the client.
|
||||
*
|
||||
* @param client The connected web client (pipeline + web socket).
|
||||
* @param client The connected web client (pipeline + web socket).
|
||||
* @param request The full decoded request packet.
|
||||
* @return The response packet that should be sent back to the client.
|
||||
*/
|
||||
public abstract WebResponsePacket onWebRequest(ConnectedWebClient client, WebRequestPacket request);
|
||||
|
||||
|
||||
/**
|
||||
* Handles the shutdown of the web server when the pipeline server stops.
|
||||
*
|
||||
* @throws IOException If an I/O error occurs while closing the web server.
|
||||
*/
|
||||
private void onPipelineStop() throws IOException {
|
||||
webServer.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the required certificate files exist in the specified folder.
|
||||
*
|
||||
* @param folder The folder to check for certificate files.
|
||||
* @param prefix The prefix of the certificate files.
|
||||
* @param extension The extension of the certificate files.
|
||||
* @throws CertificateException If a required certificate file is missing or invalid.
|
||||
* @throws IOException If an I/O error occurs while checking the files.
|
||||
*/
|
||||
private void checkFileExists(File folder, String prefix, String extension) throws CertificateException, IOException {
|
||||
boolean found = false;
|
||||
|
||||
// Ensure the folder exists
|
||||
if (folder == null) throw new FileNotFoundException("Folder does not exist");
|
||||
|
||||
// List all files in the folder
|
||||
File[] files = folder.listFiles();
|
||||
if (files == null || files.length == 0)
|
||||
throw new FileNotFoundException("Folder " + folder.getAbsolutePath() + " is empty");
|
||||
|
||||
// Check for the required certificate file
|
||||
for (File file : files) {
|
||||
if (!file.getName().startsWith(prefix))
|
||||
throw new CertificateException(file.getAbsolutePath() + " is not valid");
|
||||
|
||||
// Check for file matching the public IP address
|
||||
if (!found) found = file.getName().equalsIgnoreCase(prefix + NetworkUtils.getPublicIPAddress() + extension);
|
||||
}
|
||||
|
||||
// Throw exception if the required file is not found
|
||||
if (!found) throw new CertificateException("Missing " + prefix + NetworkUtils.getPublicIPAddress() + extension);
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the folder structure for server certificates.
|
||||
*/
|
||||
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 final String caPrefix = "ca_server_";
|
||||
public final String certPrefix = "cert_server_";
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import dev.unlegitdqrk.unlegitlibrary.file.ConfigurationManager;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.module.Configuration;
|
||||
|
||||
public class WebServerConfig {
|
||||
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package org.openautonomousconnection.protocol.side.web.events;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.impl.Event;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.side.web.ConnectedWebClient;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
|
||||
/**
|
||||
* Event triggered when a web client connects to the web server.
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.WEB)
|
||||
public final class WebClientConnectedEvent extends Event {
|
||||
|
||||
/**
|
||||
* The connected web client.
|
||||
*/
|
||||
@Getter
|
||||
private final ConnectedWebClient webClient;
|
||||
|
||||
public WebClientConnectedEvent(ConnectedWebClient webClient) {
|
||||
this.webClient = webClient;
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,8 @@ public enum ProtocolVersion implements Serializable {
|
||||
/**
|
||||
* First Beta Version of OAC-Protocol
|
||||
*/
|
||||
PV_1_0_0_BETA("1.0.0", ProtocolType.BETA, ProtocolSide.ALL, List.of(Protocol.OAC), PV_1_0_0_CLASSIC),;
|
||||
PV_1_0_0_BETA("1.0.0", ProtocolType.BETA, ProtocolSide.ALL, List.of(Protocol.OAC), PV_1_0_0_CLASSIC),
|
||||
;
|
||||
|
||||
/**
|
||||
* The version string of the protocol version.
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.beta;
|
||||
|
||||
import org.openautonomousconnection.protocol.side.ins.ProtocolINSServer;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecord;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecordType;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@@ -42,7 +40,6 @@ public final class INSRecordTools {
|
||||
* @param name InfoName.
|
||||
* @param sub Optional sub-name, may be {@code null}.
|
||||
* @param type Desired record type.
|
||||
*
|
||||
* @return A list of resolved records. May be empty if nothing could be resolved.
|
||||
*/
|
||||
public static List<INSRecord> resolveWithCNAME(ProtocolINSServer server,
|
||||
@@ -87,7 +84,7 @@ public final class INSRecordTools {
|
||||
ParsedName target = parseTargetName(cname.value);
|
||||
if (target == null) continue;
|
||||
|
||||
List<INSRecord> resolved = server.resolve(target.tln, target.name,target.sub, targetType);
|
||||
List<INSRecord> resolved = server.resolve(target.tln, target.name, target.sub, targetType);
|
||||
|
||||
if (!resolved.isEmpty()) return resolved;
|
||||
|
||||
@@ -110,7 +107,6 @@ public final class INSRecordTools {
|
||||
* and the one before that is the InfoName.
|
||||
*
|
||||
* @param target FQDN string.
|
||||
*
|
||||
* @return ParsedName or {@code null} if it cannot be parsed.
|
||||
*/
|
||||
private static ParsedName parseTargetName(String target) {
|
||||
@@ -130,12 +126,6 @@ public final class INSRecordTools {
|
||||
return new ParsedName(tln, name, sub);
|
||||
}
|
||||
|
||||
private record ParsedName(String tln, String name, String sub) {}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Validation helpers
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs basic validation on a single {@link INSRecord}.
|
||||
* <p>
|
||||
@@ -148,7 +138,6 @@ public final class INSRecordTools {
|
||||
* </ul>
|
||||
*
|
||||
* @param record The record to validate.
|
||||
*
|
||||
* @return {@code true} if the record passes all checks, {@code false} otherwise.
|
||||
*/
|
||||
public static boolean isValidRecord(INSRecord record) {
|
||||
@@ -157,17 +146,18 @@ public final class INSRecordTools {
|
||||
if (record.value == null || record.value.isEmpty()) return false;
|
||||
|
||||
if (record.port < 0 || record.port > 65535) return false;
|
||||
if (record.ttl < 0) return false;
|
||||
|
||||
return true;
|
||||
return record.ttl >= 0;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Validation helpers
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Validates a collection of records by applying {@link #isValidRecord(INSRecord)}
|
||||
* to each element.
|
||||
*
|
||||
* @param records Records to validate.
|
||||
*
|
||||
* @return A new list containing only valid records.
|
||||
*/
|
||||
public static List<INSRecord> filterValid(List<INSRecord> records) {
|
||||
@@ -178,4 +168,7 @@ public final class INSRecordTools {
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private record ParsedName(String tln, String name, String sub) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public abstract class ClassicHandlerClient {
|
||||
public abstract void handleMessage(String message, Classic_ProtocolVersion protocolVersion);
|
||||
|
||||
public final void sendMessage(String message) throws IOException, ClassNotFoundException {
|
||||
client.getClientINSConnection().sendPacket(new Classic_MessagePacket(message, 0, client.getProtocolBridge()));
|
||||
client.getClientINSConnection().sendPacket(new Classic_MessagePacket(message, client.getClientServerConnection().getClientID(), client.getProtocolBridge()));
|
||||
}
|
||||
|
||||
public abstract void validationCompleted(Classic_Domain domain, INSResponseStatus insResponseStatus);
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.builtin;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecordType;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseStatus;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerClient;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.helper.ClassicHelper;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_Domain;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.site.Classic_SiteType;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.site.Classic_WebsitesContent;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class ClientClassic extends ClassicHandlerClient {
|
||||
@Getter
|
||||
private final ProtocolBridge bridge;
|
||||
|
||||
@Getter
|
||||
private final ClassicHelper helper;
|
||||
|
||||
public ClientClassic(ProtocolBridge bridge) {
|
||||
super(bridge.getProtocolClient());
|
||||
this.bridge = bridge;
|
||||
this.helper = new ClassicHelper(bridge);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsupportedClassicPacket(String className, Object[] content) {
|
||||
bridge.getLogger().warn(
|
||||
"[Classic UnsupportedPacket] packet=" + className + " content=" + java.util.Arrays.toString(content)
|
||||
);
|
||||
|
||||
onUnsupportedClassicPacket(className, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional callback
|
||||
*
|
||||
* @param className The class name
|
||||
* @param content The content
|
||||
*/
|
||||
public void onUnsupportedClassicPacket(String className, Object[] content) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(String message, Classic_ProtocolVersion protocolVersion) {
|
||||
bridge.getLogger().info("[ClassicHandler] Message received (Classic Version " + protocolVersion.version + "): " + message);
|
||||
onMessage(message, protocolVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional callback
|
||||
*
|
||||
* @param message The Message
|
||||
* @param protocolVersion the Classic version
|
||||
*/
|
||||
public void onMessage(String message, Classic_ProtocolVersion protocolVersion) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validationCompleted(Classic_Domain domain, INSResponseStatus insResponseStatus) {
|
||||
if (insResponseStatus == INSResponseStatus.OK) {
|
||||
try {
|
||||
bridge.getProtocolClient().sendINSQuery(domain.topLevelDomain, domain.name, null, INSRecordType.A);
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
handleHTMLContent(Classic_SiteType.CLIENT, domain, Classic_WebsitesContent.ERROR_OCCURRED(e.getMessage()));
|
||||
}
|
||||
} else {
|
||||
handleHTMLContent(Classic_SiteType.CLIENT, domain, Classic_WebsitesContent.DOMAIN_NOT_REACHABLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.builtin;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.side.ins.ConnectedProtocolClient;
|
||||
import org.openautonomousconnection.protocol.side.ins.ProtocolINSServer;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecord;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecordType;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerINSServer;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.helper.ClassicHelper;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.helper.ParsedDomain;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_Domain;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_RequestDomain;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public class INSClassic extends ClassicHandlerINSServer {
|
||||
|
||||
@Getter
|
||||
private final ProtocolBridge bridge;
|
||||
|
||||
@Getter
|
||||
private final ClassicHelper helper;
|
||||
|
||||
public INSClassic(ProtocolBridge bridge) {
|
||||
this.bridge = bridge;
|
||||
this.helper = new ClassicHelper(bridge);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(ConnectedProtocolClient client, String message, Classic_ProtocolVersion protocolVersion) {
|
||||
client.getServer().getProtocolBridge().getLogger().info("[ClassicHandler] Message received from ClientID " +
|
||||
client.getPipelineConnection().getClientID() +
|
||||
" (Classic Version " + protocolVersion.version + ", Client Version: " +
|
||||
client.getClientVersion().toString() + "): " + message);
|
||||
|
||||
onMessage(client, message, protocolVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional callback.
|
||||
*
|
||||
* @param client The client sender.
|
||||
* @param message The message.
|
||||
* @param protocolVersion The classic version.
|
||||
*/
|
||||
public void onMessage(ConnectedProtocolClient client, String message, Classic_ProtocolVersion protocolVersion) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Classic_Domain getDomain(Classic_RequestDomain requestDomain) throws SQLException {
|
||||
if (!bridge.isRunningAsINSServer()) return null;
|
||||
|
||||
ParsedDomain pd = helper.parseDomain(requestDomain);
|
||||
ProtocolINSServer server = (ProtocolINSServer) bridge.getProtocolServer();
|
||||
|
||||
// IMPORTANT: resolve() already performs CNAME recursion + sorting.
|
||||
// Request A directly and pick the first (= best after sorting).
|
||||
List<INSRecord> aRecords = server.resolve(pd.tln(), pd.name(), pd.sub(), INSRecordType.A);
|
||||
if (aRecords.isEmpty()) return null;
|
||||
|
||||
INSRecord targetA = aRecords.get(0);
|
||||
return helper.buildClassicDomain(pd, targetA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Classic_Domain ping(Classic_RequestDomain req) throws SQLException {
|
||||
if (!bridge.isRunningAsINSServer()) return null;
|
||||
return new Classic_Domain(req.name, req.topLevelDomain, null, req.path, bridge);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsupportedClassicPacket(String className, Object[] content, ConnectedProtocolClient client) {
|
||||
client.getServer().getProtocolBridge().getLogger().warn(
|
||||
"[Classic UnsupportedPacket] From client " + client.getPipelineConnection().getClientID() +
|
||||
": packet=" + className + " content=" + java.util.Arrays.toString(content)
|
||||
);
|
||||
|
||||
onUnsupportedClassicPacket(className, content, client);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional callback.
|
||||
*
|
||||
* @param className The class name.
|
||||
* @param content The content.
|
||||
* @param client The client sender.
|
||||
*/
|
||||
public void onUnsupportedClassicPacket(String className, Object[] content, ConnectedProtocolClient client) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.builtin;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.side.ins.ConnectedProtocolClient;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerWebServer;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.utils.Classic_ProtocolVersion;
|
||||
|
||||
public class WebClassic extends ClassicHandlerWebServer {
|
||||
@Getter
|
||||
private final ProtocolBridge bridge;
|
||||
|
||||
public WebClassic(ProtocolBridge bridge) {
|
||||
this.bridge = bridge;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(ConnectedProtocolClient client, String message, Classic_ProtocolVersion protocolVersion) {
|
||||
client.getServer().getProtocolBridge().getLogger().info("[ClassicHandler] Message received from ClientID " +
|
||||
client.getPipelineConnection().getClientID() +
|
||||
" (Classic Version " + protocolVersion.version + ", Client Version: " +
|
||||
client.getClientVersion().toString() + "): " + message);
|
||||
|
||||
onMessage(client, message, protocolVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional callback
|
||||
*
|
||||
* @param client The client sender
|
||||
* @param message The Message
|
||||
* @param protocolVersion the Classic version
|
||||
*/
|
||||
public void onMessage(ConnectedProtocolClient client, String message, Classic_ProtocolVersion protocolVersion) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsupportedClassicPacket(String className, Object[] content, ConnectedProtocolClient client) {
|
||||
client.getServer().getProtocolBridge().getLogger().warn(
|
||||
"[Classic UnsupportedPacket] From client " + client.getPipelineConnection().getClientID() +
|
||||
": packet=" + className + " content=" + java.util.Arrays.toString(content)
|
||||
);
|
||||
|
||||
onUnsupportedClassicPacket(className, content, client);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional callback
|
||||
*
|
||||
* @param className The class name
|
||||
* @param content The content
|
||||
* @param client The client sender
|
||||
*/
|
||||
public void onUnsupportedClassicPacket(String className, Object[] content, ConnectedProtocolClient client) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.helper;
|
||||
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecord;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_Domain;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects.Classic_RequestDomain;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public record ClassicHelper(ProtocolBridge bridge) {
|
||||
|
||||
public Classic_Domain buildClassicDomain(ParsedDomain req, INSRecord rec) {
|
||||
Classic_Domain info = new Classic_Domain(req.name(), req.tln(), req.sub(), req.path(), bridge);
|
||||
|
||||
String host = rec.value;
|
||||
int port = rec.port > 0 ? rec.port : 80;
|
||||
|
||||
return new Classic_Domain(
|
||||
req.name(), req.tln(),
|
||||
host.contains(":") ? host : host + ":" + port,
|
||||
req.path(), bridge);
|
||||
}
|
||||
|
||||
public ParsedDomain parseDomain(Classic_RequestDomain req) {
|
||||
String tln = req.topLevelDomain; // example: "net"
|
||||
String full = req.name; // example: "api.v1.example"
|
||||
|
||||
String[] parts = full.split("\\.");
|
||||
|
||||
if (parts.length == 1) {
|
||||
return new ParsedDomain(tln, full, null, req.path);
|
||||
}
|
||||
|
||||
String name = parts[parts.length - 1];
|
||||
String sub = parts.length > 1
|
||||
? String.join(".", Arrays.copyOfRange(parts, 0, parts.length - 1))
|
||||
: null;
|
||||
|
||||
return new ParsedDomain(tln, name, sub, req.path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.helper;
|
||||
|
||||
public record ParsedDomain(String tln, String name, String sub, String path) {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.helper;
|
||||
|
||||
/**
|
||||
* Represents a parsed CNAME target in (tln, name, sub) form.
|
||||
*/
|
||||
public record TargetName(String tln, String name, String sub) {
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.openautonomousconnection.protocol.versions.v1_0_0.classic.objects;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
Reference in New Issue
Block a user