master #1
@@ -31,13 +31,6 @@ import java.net.Proxy;
|
||||
* It manages the protocol settings, version, and side instances.
|
||||
*/
|
||||
public final class ProtocolBridge {
|
||||
|
||||
/**
|
||||
* The singleton instance of the ProtocolBridge class
|
||||
*/
|
||||
@Getter
|
||||
private static ProtocolBridge instance;
|
||||
|
||||
/**
|
||||
* The protocol settings for the current connection
|
||||
*/
|
||||
@@ -115,6 +108,7 @@ public final class ProtocolBridge {
|
||||
public ProtocolBridge(ProtocolDNSServer protocolDNSServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception {
|
||||
// Assign the parameters to the class fields
|
||||
this.protocolDNSServer = protocolDNSServer;
|
||||
this.protocolDNSServer.setProtocolBridge(this);
|
||||
this.protocolSettings = protocolSettings;
|
||||
this.protocolVersion = protocolVersion;
|
||||
|
||||
@@ -125,9 +119,6 @@ public final class ProtocolBridge {
|
||||
// Register the appropriate listeners and packets
|
||||
registerListeners();
|
||||
registerPackets();
|
||||
|
||||
// Set the static instance to this instance
|
||||
instance = this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,6 +134,7 @@ public final class ProtocolBridge {
|
||||
public ProtocolBridge(ProtocolWebServer protocolWebServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception {
|
||||
// Assign the parameters to the class fields
|
||||
this.protocolWebServer = protocolWebServer;
|
||||
this.protocolWebServer.setProtocolBridge(this);
|
||||
this.protocolSettings = protocolSettings;
|
||||
this.protocolVersion = protocolVersion;
|
||||
|
||||
@@ -153,9 +145,6 @@ public final class ProtocolBridge {
|
||||
// Register the appropriate listeners and packets
|
||||
registerListeners();
|
||||
registerPackets();
|
||||
|
||||
// Set the static instance to this instance
|
||||
instance = this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -171,6 +160,7 @@ public final class ProtocolBridge {
|
||||
public ProtocolBridge(ProtocolClient protocolClient, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) throws Exception {
|
||||
// Assign the parameters to the class fields
|
||||
this.protocolClient = protocolClient;
|
||||
this.protocolClient.setProtocolBridge(this);
|
||||
this.protocolSettings = protocolSettings;
|
||||
this.protocolVersion = protocolVersion;
|
||||
|
||||
@@ -181,9 +171,6 @@ public final class ProtocolBridge {
|
||||
// Register the appropriate listeners and packets
|
||||
registerListeners();
|
||||
registerPackets();
|
||||
|
||||
// Set the static instance to this instance
|
||||
instance = this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,9 +26,9 @@ public final class ClientListener extends EventListener {
|
||||
@Listener
|
||||
public void onConnect(ClientConnectedEvent event) {
|
||||
try {
|
||||
event.getClient().sendPacket(new AuthPacket());
|
||||
event.getClient().sendPacket(new AuthPacket(protocolBridge));
|
||||
} catch (IOException | ClassNotFoundException exception) {
|
||||
ProtocolBridge.getInstance().getLogger().exception("Failed to send auth packet", exception);
|
||||
event.getClient().getLogger().exception("Failed to send auth packet", exception);
|
||||
event.getClient().disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,13 +20,12 @@ import java.io.ObjectOutputStream;
|
||||
|
||||
public final class AuthPacket extends OACPacket {
|
||||
|
||||
File certificatesFolder = new File("certificates");
|
||||
File publicFolder = new File(certificatesFolder, "public");
|
||||
File publicCAFolder = new File(publicFolder, "ca");
|
||||
File publicServerFolder = new File(publicFolder, "server");
|
||||
File privateFolder = new File(certificatesFolder, "private");
|
||||
File privateCAFolder = new File(privateFolder, "ca");
|
||||
File privateServerFolder = new File(privateFolder, "server");
|
||||
private ProtocolBridge protocolBridge;
|
||||
|
||||
public AuthPacket(ProtocolBridge protocolBridge) {
|
||||
this();
|
||||
this.protocolBridge = protocolBridge;
|
||||
}
|
||||
|
||||
public AuthPacket() {
|
||||
super(4, ProtocolVersion.PV_1_0_0_BETA);
|
||||
@@ -34,29 +33,29 @@ public final class AuthPacket extends OACPacket {
|
||||
|
||||
@Override
|
||||
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsDNSServer()) {
|
||||
objectOutputStream.writeObject(ProtocolBridge.getInstance().getProtocolVersion());
|
||||
if (protocolBridge.isRunningAsDNSServer()) {
|
||||
objectOutputStream.writeObject(protocolBridge.getProtocolVersion());
|
||||
|
||||
// Read ca files
|
||||
String caKey = "N/A";
|
||||
String caPem = "N/A";
|
||||
String caSrl = "N/A";
|
||||
try {
|
||||
objectOutputStream.writeUTF(ProtocolBridge.getInstance().getProtocolDNSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress());
|
||||
objectOutputStream.writeUTF(protocolBridge.getProtocolDNSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress());
|
||||
|
||||
caKey = FileUtils.readFileFull(new File(
|
||||
ProtocolBridge.getInstance().getProtocolDNSServer().getFolderStructure().privateCAFolder,
|
||||
ProtocolBridge.getInstance().getProtocolDNSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".key"));
|
||||
protocolBridge.getProtocolDNSServer().getFolderStructure().privateCAFolder,
|
||||
protocolBridge.getProtocolDNSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".key"));
|
||||
|
||||
caPem = FileUtils.readFileFull(new File(
|
||||
ProtocolBridge.getInstance().getProtocolDNSServer().getFolderStructure().publicCAFolder,
|
||||
ProtocolBridge.getInstance().getProtocolDNSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".pem"));
|
||||
protocolBridge.getProtocolDNSServer().getFolderStructure().publicCAFolder,
|
||||
protocolBridge.getProtocolDNSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".pem"));
|
||||
|
||||
caSrl = FileUtils.readFileFull(new File(
|
||||
ProtocolBridge.getInstance().getProtocolDNSServer().getFolderStructure().publicCAFolder,
|
||||
ProtocolBridge.getInstance().getProtocolDNSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".srl"));
|
||||
protocolBridge.getProtocolDNSServer().getFolderStructure().publicCAFolder,
|
||||
protocolBridge.getProtocolDNSServer().getFolderStructure().caPrefix + NetworkUtils.getPublicIPAddress() + ".srl"));
|
||||
} catch (Exception exception) {
|
||||
ProtocolBridge.getInstance().getLogger().exception("Failed to read ca-files", exception);
|
||||
protocolBridge.getLogger().exception("Failed to read ca-files", exception);
|
||||
setResponseCode(DNSResponseCode.RESPONSE_AUTH_FAILED);
|
||||
}
|
||||
|
||||
@@ -64,40 +63,40 @@ public final class AuthPacket extends OACPacket {
|
||||
objectOutputStream.writeUTF(caKey);
|
||||
objectOutputStream.writeUTF(caPem);
|
||||
objectOutputStream.writeUTF(caSrl);
|
||||
} else if (ProtocolBridge.getInstance().isRunningAsClient()) {
|
||||
objectOutputStream.writeInt(ProtocolBridge.getInstance().getProtocolClient().getClientDNSConnection().getClientID());
|
||||
objectOutputStream.writeObject(ProtocolBridge.getInstance().getProtocolVersion());
|
||||
} else if (protocolBridge.isRunningAsClient()) {
|
||||
objectOutputStream.writeInt(protocolBridge.getProtocolClient().getClientDNSConnection().getClientID());
|
||||
objectOutputStream.writeObject(protocolBridge.getProtocolVersion());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsDNSServer() || ProtocolBridge.getInstance().isRunningAsWebServer()) {
|
||||
if (protocolBridge.isRunningAsDNSServer() || protocolBridge.isRunningAsWebServer()) {
|
||||
int clientID = objectInputStream.readInt();
|
||||
ProtocolVersion clientVersion = (ProtocolVersion) objectInputStream.readObject();
|
||||
ConnectionHandler connectionHandler = ProtocolBridge.getInstance().getProtocolDNSServer().getNetworkServer().getConnectionHandlerByID(clientID);
|
||||
ConnectionHandler connectionHandler = protocolBridge.getProtocolDNSServer().getNetworkServer().getConnectionHandlerByID(clientID);
|
||||
|
||||
if (!ProtocolBridge.getInstance().isVersionSupported(clientVersion)) {
|
||||
if (!protocolBridge.isVersionSupported(clientVersion)) {
|
||||
setResponseCode(DNSResponseCode.RESPONSE_AUTH_FAILED);
|
||||
connectionHandler.disconnect();
|
||||
return;
|
||||
} else setResponseCode(DNSResponseCode.RESPONSE_AUTH_SUCCESS);
|
||||
|
||||
|
||||
if (ProtocolBridge.getInstance().isRunningAsDNSServer()) {
|
||||
ConnectedProtocolClient client = ProtocolBridge.getInstance().getProtocolDNSServer().getClientByID(clientID);
|
||||
if (protocolBridge.isRunningAsDNSServer()) {
|
||||
ConnectedProtocolClient client = protocolBridge.getProtocolDNSServer().getClientByID(clientID);
|
||||
client.setClientVersion(clientVersion);
|
||||
ProtocolBridge.getInstance().getProtocolSettings().eventManager.executeEvent(new ConnectedProtocolClientEvent(client));
|
||||
protocolBridge.getProtocolSettings().eventManager.executeEvent(new ConnectedProtocolClientEvent(client));
|
||||
} else {
|
||||
ConnectedWebClient client = ProtocolBridge.getInstance().getProtocolWebServer().getClientByID(clientID);
|
||||
ConnectedWebClient client = protocolBridge.getProtocolWebServer().getClientByID(clientID);
|
||||
client.setClientVersion(clientVersion);
|
||||
}
|
||||
} else if (ProtocolBridge.getInstance().isRunningAsClient()) {
|
||||
} else if (protocolBridge.isRunningAsClient()) {
|
||||
ProtocolVersion serverVersion = (ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
if (!ProtocolBridge.getInstance().isVersionSupported(serverVersion)) {
|
||||
if (!protocolBridge.isVersionSupported(serverVersion)) {
|
||||
setResponseCode(DNSResponseCode.RESPONSE_AUTH_FAILED);
|
||||
ProtocolBridge.getInstance().getProtocolClient().getClientDNSConnection().disconnect();
|
||||
protocolBridge.getProtocolClient().getClientDNSConnection().disconnect();
|
||||
return;
|
||||
} else setResponseCode(DNSResponseCode.RESPONSE_AUTH_SUCCESS);
|
||||
|
||||
@@ -111,9 +110,9 @@ public final class AuthPacket extends OACPacket {
|
||||
setResponseCode(DNSResponseCode.RESPONSE_AUTH_FAILED);
|
||||
else {
|
||||
|
||||
File caPemFile = new File(ProtocolBridge.getInstance().getProtocolClient().getFolderStructure().publicCAFolder, caPrefix + ".pem");
|
||||
File caSrlFile = new File(ProtocolBridge.getInstance().getProtocolClient().getFolderStructure().publicCAFolder, caPrefix + ".srl");
|
||||
File caKeyFile = new File(ProtocolBridge.getInstance().getProtocolClient().getFolderStructure().privateCAFolder, caPrefix + ".key");
|
||||
File caPemFile = new File(protocolBridge.getProtocolClient().getFolderStructure().publicCAFolder, caPrefix + ".pem");
|
||||
File caSrlFile = new File(protocolBridge.getProtocolClient().getFolderStructure().publicCAFolder, caPrefix + ".srl");
|
||||
File caKeyFile = new File(protocolBridge.getProtocolClient().getFolderStructure().privateCAFolder, caPrefix + ".key");
|
||||
|
||||
try {
|
||||
if (!caPemFile.exists()) caPemFile.createNewFile();
|
||||
@@ -124,13 +123,13 @@ public final class AuthPacket extends OACPacket {
|
||||
FileUtils.writeFile(caSrlFile, caKey);
|
||||
FileUtils.writeFile(caKeyFile, caSrl);
|
||||
} catch (Exception exception) {
|
||||
ProtocolBridge.getInstance().getLogger().exception("Failed to create/save ca-files", exception);
|
||||
protocolBridge.getLogger().exception("Failed to create/save ca-files", exception);
|
||||
setResponseCode(DNSResponseCode.RESPONSE_AUTH_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
ProtocolBridge.getInstance().getProtocolClient().setServerVersion(serverVersion);
|
||||
ProtocolBridge.getInstance().getProtocolSettings().eventManager.executeEvent(new ConnectedToProtocolDNSServerEvent());
|
||||
protocolBridge.getProtocolClient().setServerVersion(serverVersion);
|
||||
protocolBridge.getProtocolSettings().eventManager.executeEvent(new ConnectedToProtocolDNSServerEvent());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,10 +13,12 @@ import java.io.ObjectOutputStream;
|
||||
public final class ValidateDomainPacket extends OACPacket {
|
||||
private Domain domain;
|
||||
private int clientID;
|
||||
private ProtocolBridge protocolBridge;
|
||||
|
||||
public ValidateDomainPacket(Domain domain) {
|
||||
public ValidateDomainPacket(Domain domain, ProtocolBridge protocolBridge) {
|
||||
this();
|
||||
this.domain = domain;
|
||||
this.protocolBridge = protocolBridge;
|
||||
}
|
||||
|
||||
public ValidateDomainPacket() {
|
||||
@@ -25,17 +27,17 @@ public final class ValidateDomainPacket extends OACPacket {
|
||||
|
||||
@Override
|
||||
public void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsClient())
|
||||
objectOutputStream.writeInt(ProtocolBridge.getInstance().getProtocolClient().getClientDNSConnection().getClientID());
|
||||
else if (ProtocolBridge.getInstance().isRunningAsDNSServer())
|
||||
setResponseCode(ProtocolBridge.getInstance().getProtocolDNSServer().validateDomain(domain));
|
||||
if (protocolBridge.isRunningAsClient())
|
||||
objectOutputStream.writeInt(protocolBridge.getProtocolClient().getClientDNSConnection().getClientID());
|
||||
else if (protocolBridge.isRunningAsDNSServer())
|
||||
setResponseCode(protocolBridge.getProtocolDNSServer().validateDomain(domain));
|
||||
|
||||
objectOutputStream.writeObject(domain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
if (ProtocolBridge.getInstance().isRunningAsDNSServer()) clientID = objectInputStream.readInt();
|
||||
if (protocolBridge.isRunningAsDNSServer()) clientID = objectInputStream.readInt();
|
||||
domain = (Domain) objectInputStream.readObject();
|
||||
}
|
||||
|
||||
@@ -43,14 +45,14 @@ public final class ValidateDomainPacket extends OACPacket {
|
||||
protected void onResponseCodeRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) {
|
||||
super.onResponseCodeRead(packetHandler, objectInputStream);
|
||||
|
||||
if (ProtocolBridge.getInstance().isRunningAsDNSServer()) {
|
||||
if (protocolBridge.isRunningAsDNSServer()) {
|
||||
try {
|
||||
ProtocolBridge.getInstance().getProtocolDNSServer().getClientByID(clientID).getConnectionHandler().sendPacket(new ValidateDomainPacket(domain));
|
||||
protocolBridge.getProtocolDNSServer().getClientByID(clientID).getConnectionHandler().sendPacket(new ValidateDomainPacket(domain, protocolBridge));
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
ProtocolBridge.getInstance().getProtocolDNSServer().validationPacketSendFailed(domain, ProtocolBridge.getInstance().getProtocolDNSServer().getClientByID(clientID), e);
|
||||
protocolBridge.getProtocolDNSServer().validationPacketSendFailed(domain, protocolBridge.getProtocolDNSServer().getClientByID(clientID), e);
|
||||
}
|
||||
|
||||
} else if (ProtocolBridge.getInstance().isRunningAsClient())
|
||||
ProtocolBridge.getInstance().getProtocolClient().validationCompleted(domain, getResponseCode());
|
||||
} else if (protocolBridge.isRunningAsClient())
|
||||
protocolBridge.getProtocolClient().validationCompleted(domain, getResponseCode());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,12 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
*/
|
||||
private ProtocolVersion serverVersion = null;
|
||||
|
||||
/**
|
||||
* The reference to the ProtocolBridge Object
|
||||
*/
|
||||
@Getter
|
||||
private ProtocolBridge protocolBridge;
|
||||
|
||||
/**
|
||||
* Initializes the ProtocolClient, setting up certificate folders and the DNS client connection.
|
||||
*
|
||||
@@ -61,9 +67,9 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
folderStructure = new ClientCertificateFolderStructure();
|
||||
|
||||
// Initialize connection to DNS server
|
||||
clientToDNS = new NetworkClient.ClientBuilder().setLogger(ProtocolBridge.getInstance().getLogger()).setProxy(ProtocolBridge.getInstance().getProxy()).
|
||||
setHost(ProtocolBridge.getInstance().getProtocolSettings().host).setPort(ProtocolBridge.getInstance().getProtocolSettings().port).
|
||||
setPacketHandler(ProtocolBridge.getInstance().getProtocolSettings().packetHandler).setEventManager(ProtocolBridge.getInstance().getProtocolSettings().eventManager).
|
||||
clientToDNS = new NetworkClient.ClientBuilder().setLogger(protocolBridge.getLogger()).setProxy(protocolBridge.getProxy()).
|
||||
setHost(protocolBridge.getProtocolSettings().host).setPort(protocolBridge.getProtocolSettings().port).
|
||||
setPacketHandler(protocolBridge.getProtocolSettings().packetHandler).setEventManager(protocolBridge.getProtocolSettings().eventManager).
|
||||
setRootCAFolder(folderStructure.publicCAFolder).setClientCertificatesFolder(folderStructure.publicClientFolder, folderStructure.privateClientFolder).
|
||||
build();
|
||||
}
|
||||
@@ -87,7 +93,7 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
*/
|
||||
public final void createWebConnection(Domain domain, int pipelinePort, int webPort) throws Exception {
|
||||
// Ensure the protocol supports web connections
|
||||
if (!ProtocolBridge.getInstance().isProtocolSupported(ProtocolVersion.Protocol.OAC))
|
||||
if (!protocolBridge.isProtocolSupported(ProtocolVersion.Protocol.OAC))
|
||||
throw new UnsupportedProtocolException();
|
||||
|
||||
// Check if web client is already connected and close it
|
||||
@@ -95,13 +101,13 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
try {
|
||||
webClient.closeConnection();
|
||||
} catch (IOException e) {
|
||||
ProtocolBridge.getInstance().getLogger().exception("Failed to close connection to web server", e);
|
||||
protocolBridge.getLogger().exception("Failed to close connection to web server", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Verify necessary certificate files exist
|
||||
webClient = new WebClient(domain, pipelinePort, webPort);
|
||||
webClient = new WebClient(domain, pipelinePort, webPort, this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,7 +175,7 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
try {
|
||||
webClient.closeConnection();
|
||||
} catch (IOException e) {
|
||||
ProtocolBridge.getInstance().getLogger().exception("Failed to close connection to web server", e);
|
||||
protocolBridge.getLogger().exception("Failed to close connection to web server", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -305,10 +311,10 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
public final void validateDomain(Domain domain) throws IOException, ClassNotFoundException {
|
||||
// Send Classic_PingPacket if classic protocol is supported
|
||||
Classic_PingPacket cPingPacket = new Classic_PingPacket(new Classic_RequestDomain(domain.getName(), domain.getTopLevelName(), domain.getPath()), null, false);
|
||||
if (ProtocolBridge.getInstance().isClassicSupported()) clientToDNS.sendPacket(cPingPacket);
|
||||
if (protocolBridge.isClassicSupported()) clientToDNS.sendPacket(cPingPacket);
|
||||
|
||||
// Send ValidateDomainPacket
|
||||
clientToDNS.sendPacket(new ValidateDomainPacket(domain));
|
||||
clientToDNS.sendPacket(new ValidateDomainPacket(domain, protocolBridge));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -322,12 +328,20 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
public final void requestDestination(Domain domain, DNSResponseCode responseCode) throws IOException, ClassNotFoundException {
|
||||
// Send Classic_DomainPacket if classic protocol is supported
|
||||
Classic_DomainPacket cDomainPacket = new Classic_DomainPacket(0, new Classic_RequestDomain(domain.getName(), domain.getTopLevelName(), domain.getPath()), null);
|
||||
if (ProtocolBridge.getInstance().isClassicSupported()) clientToDNS.sendPacket(cDomainPacket);
|
||||
if (protocolBridge.isClassicSupported()) clientToDNS.sendPacket(cDomainPacket);
|
||||
|
||||
// Send GetDestinationPacket
|
||||
clientToDNS.sendPacket(new GetDestinationPacket(domain, responseCode));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set protocol bridge
|
||||
* @param protocolBridge The ProtocolBridge object
|
||||
*/
|
||||
public void setProtocolBridge(ProtocolBridge protocolBridge) {
|
||||
if (this.protocolBridge == null) this.protocolBridge = protocolBridge;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback method invoked when domain validation is completed.
|
||||
*
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.openautonomousconnection.protocol.side.client;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.client.NetworkClient;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
@@ -42,34 +44,43 @@ public final class WebClient {
|
||||
*/
|
||||
private ObjectInputStream inputStream;
|
||||
|
||||
/**
|
||||
* The Protocol Client associated with this protocol client.
|
||||
*/
|
||||
@Getter
|
||||
private final ProtocolClient protocolClient;
|
||||
|
||||
/**
|
||||
* Constructs a WebClient instance and establishes a secure connection to the web server.
|
||||
*
|
||||
* @param domain The domain information for the web server.
|
||||
* @param pipelinePort The port for the pipeline connection.
|
||||
* @param webPort The port for the web server connection.
|
||||
* @param protocolClient The Protocol Client associated with this protocol client.
|
||||
* @throws Exception If an error occurs during connection setup.
|
||||
*/
|
||||
public WebClient(Domain domain, int pipelinePort, int webPort) throws Exception {
|
||||
public WebClient(Domain domain, int pipelinePort, int webPort, ProtocolClient protocolClient) throws Exception {
|
||||
this.protocolClient = protocolClient;
|
||||
|
||||
// Initialize and connect the pipeline client
|
||||
clientToWebPipeline = new NetworkClient.ClientBuilder().
|
||||
// Set logger from ProtocolBridge
|
||||
setLogger(ProtocolBridge.getInstance().getLogger()).
|
||||
setLogger(protocolClient.getProtocolBridge().getLogger()).
|
||||
// Set the destination and port for the pipeline connection
|
||||
setHost(domain.getDestination()).setPort(pipelinePort).
|
||||
|
||||
// Configure packet handler and event manager
|
||||
setPacketHandler(ProtocolBridge.getInstance().getProtocolSettings().packetHandler).
|
||||
setEventManager(ProtocolBridge.getInstance().getProtocolSettings().eventManager).
|
||||
setPacketHandler(protocolClient.getProtocolBridge().getProtocolSettings().packetHandler).
|
||||
setEventManager(protocolClient.getProtocolBridge().getProtocolSettings().eventManager).
|
||||
|
||||
// Set proxy and ssl parameters from DNS connection settings
|
||||
setProxy(ProtocolBridge.getInstance().getProtocolClient().getClientDNSConnection().getProxy()).
|
||||
setSSLParameters(ProtocolBridge.getInstance().getProtocolClient().getClientDNSConnection().getSocket().getSSLParameters()).
|
||||
setProxy(protocolClient.getProtocolBridge().getProtocolClient().getClientDNSConnection().getProxy()).
|
||||
setSSLParameters(protocolClient.getProtocolBridge().getProtocolClient().getClientDNSConnection().getSocket().getSSLParameters()).
|
||||
|
||||
// Set certificates and folders for SSL
|
||||
setRootCAFolder(ProtocolBridge.getInstance().getProtocolClient().getFolderStructure().publicCAFolder).
|
||||
setClientCertificatesFolder(ProtocolBridge.getInstance().getProtocolClient().getFolderStructure().publicClientFolder,
|
||||
ProtocolBridge.getInstance().getProtocolClient().getFolderStructure().privateClientFolder).
|
||||
setRootCAFolder(protocolClient.getProtocolBridge().getProtocolClient().getFolderStructure().publicCAFolder).
|
||||
setClientCertificatesFolder(protocolClient.getProtocolBridge().getProtocolClient().getFolderStructure().publicClientFolder,
|
||||
protocolClient.getProtocolBridge().getProtocolClient().getFolderStructure().privateClientFolder).
|
||||
|
||||
|
||||
// Finalize the client setup
|
||||
@@ -83,9 +94,9 @@ public final class WebClient {
|
||||
|
||||
// Create SSL socket factory using client certificates
|
||||
SSLSocketFactory sslSocketFactory = NetworkClient.ClientBuilder.
|
||||
createSSLSocketFactory(ProtocolBridge.getInstance().getProtocolClient().getFolderStructure().publicCAFolder,
|
||||
ProtocolBridge.getInstance().getProtocolClient().getFolderStructure().publicClientFolder,
|
||||
ProtocolBridge.getInstance().getProtocolClient().getFolderStructure().privateClientFolder);
|
||||
createSSLSocketFactory(protocolClient.getProtocolBridge().getProtocolClient().getFolderStructure().publicCAFolder,
|
||||
protocolClient.getProtocolBridge().getProtocolClient().getFolderStructure().publicClientFolder,
|
||||
protocolClient.getProtocolBridge().getProtocolClient().getFolderStructure().privateClientFolder);
|
||||
|
||||
// Get proxy settings from the pipeline client
|
||||
Proxy proxy = clientToWebPipeline.getProxy();
|
||||
@@ -166,7 +177,7 @@ public final class WebClient {
|
||||
public boolean isConnected() {
|
||||
return this.clientToWebServer != null && this.clientToWebServer.isConnected() && !this.clientToWebServer.isClosed()
|
||||
&& this.receiveThread.isAlive() && !this.receiveThread.isInterrupted() &&
|
||||
ProtocolBridge.getInstance().getProtocolClient().getClientDNSConnection().isConnected() && clientToWebPipeline.isConnected();
|
||||
protocolClient.getProtocolBridge().getProtocolClient().getClientDNSConnection().isConnected() && clientToWebPipeline.isConnected();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,7 +194,7 @@ public final class WebClient {
|
||||
try {
|
||||
this.closeConnection();
|
||||
} catch (IOException exception) {
|
||||
ProtocolBridge.getInstance().getLogger().exception("Failed to close connection to web server", var2);
|
||||
protocolClient.getProtocolBridge().getLogger().exception("Failed to close connection to web server", var2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ 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.web.ProtocolWebServer;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
|
||||
/**
|
||||
@@ -18,13 +19,20 @@ public final class ConnectedProtocolClient {
|
||||
@Getter
|
||||
private final ConnectionHandler connectionHandler;
|
||||
|
||||
/**
|
||||
* The Protocol Server associated with this protocol client.
|
||||
*/
|
||||
@Getter
|
||||
private final ProtocolDNSServer protocolDNSServer;
|
||||
|
||||
/**
|
||||
* The protocol version of the connected client.
|
||||
*/
|
||||
private ProtocolVersion clientVersion = null;
|
||||
|
||||
public ConnectedProtocolClient(ConnectionHandler connectionHandler) {
|
||||
public ConnectedProtocolClient(ConnectionHandler connectionHandler, ProtocolDNSServer protocolDNSServer) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
this.protocolDNSServer = protocolDNSServer;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -46,6 +46,12 @@ public abstract class ProtocolDNSServer extends DefaultMethodsOverrider {
|
||||
@Getter
|
||||
private ServerCertificateFolderStructure folderStructure;
|
||||
|
||||
/**
|
||||
* The reference to the ProtocolBridge Object
|
||||
*/
|
||||
@Getter
|
||||
private ProtocolBridge protocolBridge;
|
||||
|
||||
/**
|
||||
* Constructs a ProtocolDNSServer with the specified configuration file.
|
||||
*
|
||||
@@ -88,7 +94,6 @@ public abstract class ProtocolDNSServer extends DefaultMethodsOverrider {
|
||||
File keyFile = new File(folderStructure.privateServerFolder, folderStructure.certPrefix + NetworkUtils.getPublicIPAddress() + ".key");
|
||||
|
||||
// Initialize the protocol bridge and clients list
|
||||
ProtocolBridge protocolBridge = ProtocolBridge.getInstance();
|
||||
this.clients = new ArrayList<>();
|
||||
|
||||
// Build the network server with the specified settings
|
||||
@@ -148,6 +153,14 @@ public abstract class ProtocolDNSServer extends DefaultMethodsOverrider {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set protocol bridge
|
||||
* @param protocolBridge The ProtocolBridge object
|
||||
*/
|
||||
public void setProtocolBridge(ProtocolBridge protocolBridge) {
|
||||
if (this.protocolBridge == null) this.protocolBridge = protocolBridge;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the DNS information site URL from the configuration.
|
||||
*
|
||||
@@ -208,7 +221,6 @@ public abstract class ProtocolDNSServer extends DefaultMethodsOverrider {
|
||||
|
||||
/**
|
||||
* Abstract method called when a validation packet fails to send.
|
||||
*
|
||||
* @param domain The domain associated with the validation.
|
||||
* @param client The connected protocol client.
|
||||
* @param exception The exception that occurred during sending.
|
||||
@@ -217,7 +229,6 @@ public abstract class ProtocolDNSServer extends DefaultMethodsOverrider {
|
||||
|
||||
/**
|
||||
* Abstract method called when a domain destination packet fails to send.
|
||||
*
|
||||
* @param client The connected protocol client.
|
||||
* @param domain The domain associated with the packet.
|
||||
* @param validationResponse The DNS response code from validation.
|
||||
|
||||
@@ -68,6 +68,12 @@ public final class ConnectedWebClient {
|
||||
*/
|
||||
private final Thread receiveThread = new Thread(this::receive);
|
||||
|
||||
/**
|
||||
* The reference to the ProtocolWebServer Object
|
||||
*/
|
||||
@Getter
|
||||
private ProtocolWebServer protocolWebServer;
|
||||
|
||||
/**
|
||||
* Sends an HTTP redirect response to the client.
|
||||
*
|
||||
@@ -629,7 +635,7 @@ public final class ConnectedWebClient {
|
||||
path = URLDecoder.decode(path, StandardCharsets.UTF_8);
|
||||
path = normalizePath(path);
|
||||
|
||||
File file = new File(ProtocolBridge.getInstance().getProtocolWebServer().getContentFolder(), path);
|
||||
File file = new File(protocolWebServer.getProtocolBridge().getProtocolWebServer().getContentFolder(), path);
|
||||
|
||||
String sessionId = null;
|
||||
if (headers.containsKey("cookie")) {
|
||||
@@ -642,14 +648,14 @@ public final class ConnectedWebClient {
|
||||
}
|
||||
|
||||
if (!file.exists() || !file.isFile()) {
|
||||
sendResponse(out, 404, new File(ProtocolBridge.getInstance().getProtocolWebServer().getErrorsFolder(), "404.html"));
|
||||
sendResponse(out, 404, new File(protocolWebServer.getProtocolBridge().getProtocolWebServer().getErrorsFolder(), "404.html"));
|
||||
return;
|
||||
}
|
||||
|
||||
String clientIp = webSocket.getInetAddress().getHostAddress();
|
||||
String userAgent = headers.getOrDefault("user-agent", null);
|
||||
|
||||
boolean loggedIn = sessionId != null && SessionManager.isValid(sessionId, clientIp, userAgent);
|
||||
boolean loggedIn = sessionId != null && SessionManager.isValid(sessionId, clientIp, userAgent, protocolWebServer);
|
||||
|
||||
if (path.equals("/403-login") && headers.getOrDefault("content-type", "").startsWith("application/x-www-form-urlencoded")) {
|
||||
Map<String, String> postParams = parsePostParams(in);
|
||||
@@ -657,7 +663,7 @@ public final class ConnectedWebClient {
|
||||
String password = postParams.get("password");
|
||||
|
||||
if (AuthManager.checkAuth(login, password)) {
|
||||
String newSessionId = SessionManager.create(login, clientIp, userAgent);
|
||||
String newSessionId = SessionManager.create(login, clientIp, userAgent, protocolWebServer);
|
||||
Map<String, String> cookies = Map.of("Set-Cookie", "SESSIONID=" + newSessionId + "; HttpOnly; Path=/");
|
||||
sendRedirect(out, "/main.html", cookies);
|
||||
return;
|
||||
@@ -668,18 +674,18 @@ public final class ConnectedWebClient {
|
||||
}
|
||||
|
||||
if (isMultipart(headers)) {
|
||||
handleMultipart(in, headers, new File(ProtocolBridge.getInstance().getProtocolWebServer().getContentFolder(), "uploads"));
|
||||
handleMultipart(in, headers, new File(protocolWebServer.getProtocolBridge().getProtocolWebServer().getContentFolder(), "uploads"));
|
||||
}
|
||||
|
||||
if (RuleManager.requiresAuth(path) && !loggedIn) {
|
||||
PHPResponse phpResp = renderPHPWithCookies(new File(ProtocolBridge.getInstance().getProtocolWebServer().getContentFolder(), "403.php"));
|
||||
PHPResponse phpResp = renderPHPWithCookies(new File(protocolWebServer.getProtocolBridge().getProtocolWebServer().getContentFolder(), "403.php"));
|
||||
sendResponse(out, 200, phpResp.body.getBytes(StandardCharsets.UTF_8), "text/html", phpResp.cookies);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (RuleManager.isDenied(path) && !RuleManager.isAllowed(path)) {
|
||||
sendResponse(out, 403, new File(ProtocolBridge.getInstance().getProtocolWebServer().getErrorsFolder(), "403.php"));
|
||||
sendResponse(out, 403, new File(protocolWebServer.getProtocolBridge().getProtocolWebServer().getErrorsFolder(), "403.php"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -701,6 +707,14 @@ public final class ConnectedWebClient {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set protocol bridge
|
||||
* @param protocolWebServer The ProtocolWebServer object
|
||||
*/
|
||||
public void setProtocolWebServer(ProtocolWebServer protocolWebServer) {
|
||||
if (this.protocolWebServer == null) this.protocolWebServer = protocolWebServer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the response from a PHP script, including body and cookies.
|
||||
*/
|
||||
|
||||
@@ -83,6 +83,12 @@ public final class ProtocolWebServer {
|
||||
@Getter
|
||||
private String uniqueSessionString;
|
||||
|
||||
/**
|
||||
* The reference to the ProtocolBridge Object
|
||||
*/
|
||||
@Getter
|
||||
private ProtocolBridge protocolBridge;
|
||||
|
||||
/**
|
||||
* Initializes the web server with the given configuration, authentication, and rules files.
|
||||
*
|
||||
@@ -159,8 +165,8 @@ public final class ProtocolWebServer {
|
||||
// Initialize the pipeline server
|
||||
pipelineServer = new NetworkServer.ServerBuilder().
|
||||
setPort(configurationManager.getInt("port.pipeline")).setTimeout(0).
|
||||
setPacketHandler(ProtocolBridge.getInstance().getProtocolSettings().packetHandler).setEventManager(ProtocolBridge.getInstance().getProtocolSettings().eventManager).
|
||||
setLogger(ProtocolBridge.getInstance().getLogger()).
|
||||
setPacketHandler(protocolBridge.getProtocolSettings().packetHandler).setEventManager(protocolBridge.getProtocolSettings().eventManager).
|
||||
setLogger(protocolBridge.getLogger()).
|
||||
setServerCertificate(certFile, keyFile).setRootCAFolder(folderStructure.publicCAFolder).
|
||||
build();
|
||||
}
|
||||
@@ -177,6 +183,14 @@ public final class ProtocolWebServer {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set protocol bridge
|
||||
* @param protocolBridge The ProtocolBridge object
|
||||
*/
|
||||
public void setProtocolBridge(ProtocolBridge protocolBridge) {
|
||||
if (this.protocolBridge == null) this.protocolBridge = protocolBridge;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the web server to accept and handle client connections.
|
||||
*
|
||||
@@ -228,6 +242,7 @@ public final class ProtocolWebServer {
|
||||
if (connectedWebClient.getPipelineConnection().getClientID() != -1 && connectedWebClient.isClientVersionLoaded()) {
|
||||
// Assign socket to an existing connected client
|
||||
connectedWebClient.setWebSocket(client);
|
||||
connectedWebClient.setProtocolWebServer(this);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.openautonomousconnection.protocol.side.web.managers;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.side.web.ProtocolWebServer;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -34,19 +35,20 @@ public final class SessionManager {
|
||||
* @param login The username associated with the session.
|
||||
* @param ip The IP address of the client.
|
||||
* @param userAgent The User-Agent string of the client.
|
||||
* @param protocolWebServer The Protocol WebServer for the unique Session
|
||||
* @return The generated session ID.
|
||||
* @throws IOException If an I/O error occurs.
|
||||
*/
|
||||
public static String create(String login, String ip, String userAgent) throws IOException {
|
||||
public static String create(String login, String ip, String userAgent, ProtocolWebServer protocolWebServer) throws IOException {
|
||||
// Generate a secure random session ID
|
||||
byte[] bytes = new byte[32];
|
||||
secureRandom.nextBytes(bytes);
|
||||
|
||||
// Encode the bytes to a URL-safe Base64 string
|
||||
String sessionId = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes) + ProtocolBridge.getInstance().getProtocolWebServer().getUniqueSessionString();
|
||||
String sessionId = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes) + protocolWebServer.getUniqueSessionString();
|
||||
|
||||
// Create and store the new session
|
||||
sessions.put(sessionId, new Session(login, ip, userAgent));
|
||||
sessions.put(sessionId, new Session(login, ip, userAgent, protocolWebServer));
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
@@ -56,10 +58,11 @@ public final class SessionManager {
|
||||
* @param sessionId The session ID to validate.
|
||||
* @param ip The IP address of the client.
|
||||
* @param userAgent The User-Agent string of the client.
|
||||
* @param protocolWebServer The Protocol WebServer to get the config for refreshing
|
||||
* @return True if the session is valid, false otherwise.
|
||||
* @throws IOException If an I/O error occurs.
|
||||
*/
|
||||
public static boolean isValid(String sessionId, String ip, String userAgent) throws IOException {
|
||||
public static boolean isValid(String sessionId, String ip, String userAgent, ProtocolWebServer protocolWebServer) throws IOException {
|
||||
// Retrieve the session associated with the session ID
|
||||
Session session = sessions.get(sessionId);
|
||||
|
||||
@@ -70,7 +73,7 @@ public final class SessionManager {
|
||||
}
|
||||
|
||||
// Refresh the session expiration time
|
||||
session.refresh();
|
||||
session.refresh(protocolWebServer);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -122,11 +125,11 @@ public final class SessionManager {
|
||||
String userAgent;
|
||||
long expiresAt;
|
||||
|
||||
Session(String login, String ip, String userAgent) throws IOException {
|
||||
Session(String login, String ip, String userAgent, ProtocolWebServer protocolWebServer) throws IOException {
|
||||
this.login = login;
|
||||
this.ip = ip;
|
||||
this.userAgent = userAgent;
|
||||
this.expiresAt = System.currentTimeMillis() + (long) ProtocolBridge.getInstance().getProtocolWebServer().getConfigurationManager().getInt("sessionexpireminutes") * 60 * 1000;
|
||||
this.expiresAt = System.currentTimeMillis() + (long) protocolWebServer.getConfigurationManager().getInt("sessionexpireminutes") * 60 * 1000;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,11 +154,11 @@ public final class SessionManager {
|
||||
|
||||
/**
|
||||
* Refreshes the session's expiration time.
|
||||
*
|
||||
* @param protocolWebServer The Protocol WebServer to get the Config setting
|
||||
* @throws IOException If an I/O error occurs.
|
||||
*/
|
||||
void refresh() throws IOException {
|
||||
this.expiresAt = System.currentTimeMillis() + (long) ProtocolBridge.getInstance().getProtocolWebServer().getConfigurationManager().getInt("sessionexpireminutes") * 60 * 1000;
|
||||
void refresh(ProtocolWebServer protocolWebServer) throws IOException {
|
||||
this.expiresAt = System.currentTimeMillis() + (long) protocolWebServer.getConfigurationManager().getInt("sessionexpireminutes") * 60 * 1000;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user