Initial commit
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package github.openautonomousconnection.protocol;
|
||||
|
||||
import github.openautonomousconnection.protocol.handle.ClassicHandler;
|
||||
import github.openautonomousconnection.protocol.side.ProtocolClient;
|
||||
import github.openautonomousconnection.protocol.side.ProtocolServer;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import me.finn.unlegitlibrary.utils.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ProtocolBridge {
|
||||
|
||||
@Getter
|
||||
private final ProtocolSettings protocolSettings;
|
||||
|
||||
@Getter
|
||||
private final ProtocolVersion protocolVersion;
|
||||
|
||||
@Getter
|
||||
private ProtocolServer protocolServer;
|
||||
|
||||
@Getter
|
||||
private ProtocolClient protocolClient;
|
||||
|
||||
@Getter
|
||||
private final Logger logger;
|
||||
|
||||
@Getter @Setter
|
||||
private ClassicHandler classicHandler;
|
||||
|
||||
public ProtocolBridge(ProtocolServer protocolServer, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) {
|
||||
this.protocolServer = protocolServer;
|
||||
this.protocolSettings = protocolSettings;
|
||||
this.protocolVersion = protocolVersion;
|
||||
|
||||
Logger tmpLogger = null;
|
||||
try {
|
||||
tmpLogger = new Logger(logFolder, false, true);
|
||||
} catch (IOException | NoSuchFieldException | IllegalAccessException exception) {
|
||||
exception.printStackTrace();
|
||||
tmpLogger = null;
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
this.logger = tmpLogger;
|
||||
|
||||
if (!validateProtocolVersion()) {
|
||||
this.logger.error("Invalid protocol version '" + protocolVersion.toString() + "'!");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public ProtocolBridge(ProtocolClient protocolClient, ProtocolSettings protocolSettings, ProtocolVersion protocolVersion, File logFolder) {
|
||||
this.protocolClient = protocolClient;
|
||||
this.protocolSettings = protocolSettings;
|
||||
this.protocolVersion = protocolVersion;
|
||||
|
||||
Logger tmpLogger = null;
|
||||
try {
|
||||
tmpLogger = new Logger(logFolder, false, true);
|
||||
} catch (IOException | NoSuchFieldException | IllegalAccessException exception) {
|
||||
exception.printStackTrace();
|
||||
tmpLogger = null;
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
this.logger = tmpLogger;
|
||||
|
||||
if (!validateProtocolVersion()) {
|
||||
this.logger.error("Invalid protocol version '" + protocolVersion.toString() + "'!");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isRunningAsServer() {
|
||||
return protocolServer != null;
|
||||
}
|
||||
|
||||
public boolean isRunningAsClient() {
|
||||
return protocolClient != null;
|
||||
}
|
||||
|
||||
private boolean validateProtocolVersion() {
|
||||
return (isRunningAsServer() && protocolVersion.getProtocolSide() != ProtocolVersion.ProtocolSide.CLIENT) ||
|
||||
(isRunningAsClient() && protocolVersion.getProtocolSide() != ProtocolVersion.ProtocolSide.SERVER);
|
||||
}
|
||||
|
||||
public boolean validateProtocolVersion(ProtocolVersion targetVersion) {
|
||||
return protocolVersion == targetVersion || protocolVersion.getCompatibleVersions().contains(targetVersion);
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package github.openautonomousconnection.protocol;
|
||||
|
||||
import me.finn.unlegitlibrary.event.EventManager;
|
||||
import me.finn.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
public class ProtocolSettings extends DefaultMethodsOverrider {
|
||||
|
||||
public String host;
|
||||
public int port;
|
||||
public PacketHandler packetHandler;
|
||||
public EventManager eventManager;
|
||||
|
||||
}
|
@@ -0,0 +1,70 @@
|
||||
package github.openautonomousconnection.protocol;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public enum ProtocolVersion implements Serializable {
|
||||
PV_1_0_0_CLASSIC("1.0.0", ProtocolType.CLASSIC, ProtocolSide.SERVER),
|
||||
PV_1_0_0_BETA("1.0.0", ProtocolType.BETA, ProtocolSide.BOTH, PV_1_0_0_CLASSIC)
|
||||
;
|
||||
|
||||
@Getter
|
||||
private final String version;
|
||||
@Getter
|
||||
private final ProtocolType protocolType;
|
||||
@Getter
|
||||
private final ProtocolSide protocolSide;
|
||||
@Getter
|
||||
private final List<ProtocolVersion> compatibleVersions;
|
||||
|
||||
ProtocolVersion(String version, ProtocolType protocolType, ProtocolSide protocolSide, ProtocolVersion... compatibleVersions) {
|
||||
this.version = version;
|
||||
this.protocolType = protocolType;
|
||||
this.protocolSide = protocolSide;
|
||||
this.compatibleVersions = Arrays.stream(compatibleVersions).toList();
|
||||
if (!this.compatibleVersions.contains(this)) this.compatibleVersions.add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
StringBuilder compatible = new StringBuilder("[");
|
||||
for (ProtocolVersion compatibleVersion : compatibleVersions) compatible.append(compatibleVersion.buildName());
|
||||
compatible.append("]");
|
||||
|
||||
return "{version=" + version + ";type=" + protocolType.toString() + ";side=" + protocolSide.toString() + ";compatible=" + compatible.toString() + "}";
|
||||
}
|
||||
|
||||
public final String buildName() {
|
||||
return version + "-" + protocolType.toString();
|
||||
}
|
||||
|
||||
public enum ProtocolType {
|
||||
CLASSIC, // -> See "_old" Projects on GitHub Organisation https://github.com/Open-Autonomous-Connection/
|
||||
BETA,
|
||||
ALPHA,
|
||||
STABLE
|
||||
|
||||
;
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return name().toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
public enum ProtocolSide {
|
||||
CLIENT, // Protocol version can only used on Client
|
||||
SERVER, // Protocol version can only used on Server
|
||||
BOTH // Protocol version can only used on Server and Client
|
||||
|
||||
;
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return name().toUpperCase();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
package github.openautonomousconnection.protocol.classic;
|
||||
|
||||
public class ClassicConverter {
|
||||
}
|
@@ -0,0 +1,77 @@
|
||||
package github.openautonomousconnection.protocol.classic;
|
||||
|
||||
import me.finn.unlegitlibrary.string.StringUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Classic_Domain implements Serializable {
|
||||
public final String name;
|
||||
public final String topLevelDomain;
|
||||
private final String destination;
|
||||
private final String path;
|
||||
|
||||
public Classic_Domain(String name, String topLevelDomain, String destination, String path) {
|
||||
if (path == null) path = "";
|
||||
|
||||
this.name = name;
|
||||
this.topLevelDomain = topLevelDomain;
|
||||
this.destination = destination;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public final String realDestination() {
|
||||
String tmpDestination = destination.endsWith("/") ? destination : destination + "/";
|
||||
String tmpPath = getPath();
|
||||
|
||||
if (tmpPath == null) tmpPath = "";
|
||||
if (tmpPath.startsWith("/")) tmpPath = tmpPath.substring("/".length());
|
||||
if (tmpPath.endsWith("/")) tmpPath = tmpPath.substring(0, tmpPath.length() - "/".length());
|
||||
|
||||
return tmpDestination + tmpPath;
|
||||
}
|
||||
|
||||
public final String getPath() {
|
||||
if (path.endsWith("/")) return path.substring(0, path.length() - "/".length());
|
||||
if (path.startsWith("/")) return path.substring("/".length());
|
||||
return path;
|
||||
}
|
||||
|
||||
public final String parsedDestination() {
|
||||
if (destination.toLowerCase().startsWith("https://github.com/")) {
|
||||
String base = "https://raw.githubusercontent.com/";
|
||||
String username = Classic_DomainUtils.getPath(destination).split("/")[0];
|
||||
String site = Classic_DomainUtils.getPath(destination).split("/")[1];
|
||||
|
||||
String tmpPath = getPath();
|
||||
if (tmpPath == null || StringUtils.isEmptyString(tmpPath)) tmpPath = "index.html";
|
||||
if (tmpPath.startsWith("/")) tmpPath = tmpPath.substring("/".length());
|
||||
if (tmpPath.endsWith("/")) tmpPath = tmpPath.substring(0, tmpPath.length() - "/".length());
|
||||
|
||||
base = base + username + "/" + site + "/main/" + tmpPath;
|
||||
return base;
|
||||
}
|
||||
|
||||
return realDestination();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return new Classic_Domain(name, topLevelDomain, destination, path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
if (!(obj instanceof Classic_Domain other)) return false;
|
||||
return other.name.equalsIgnoreCase(name) && other.topLevelDomain.equalsIgnoreCase(topLevelDomain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return "{parsed='" + parsedDestination() + "';real='" + realDestination() + "'}";
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
package github.openautonomousconnection.protocol.classic;
|
||||
|
||||
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
|
||||
class Classic_DomainUtils extends DefaultMethodsOverrider {
|
||||
|
||||
public static String getTopLevelDomain(String url) throws MalformedURLException {
|
||||
URL uri = null;
|
||||
String tldString = null;
|
||||
|
||||
if (url.startsWith(Classic_SiteType.PUBLIC.name + "://")) url = url.substring((Classic_SiteType.PUBLIC.name + "://").length());
|
||||
if (url.startsWith(Classic_SiteType.CLIENT.name + "://")) url = url.substring((Classic_SiteType.CLIENT.name + "://").length());
|
||||
if (url.startsWith(Classic_SiteType.SERVER.name + "://")) url = url.substring((Classic_SiteType.SERVER.name + "://").length());
|
||||
if (url.startsWith(Classic_SiteType.PROTOCOL.name + "://")) url = url.substring((Classic_SiteType.PROTOCOL.name + "://").length());
|
||||
if (url.startsWith(Classic_SiteType.LOCAL.name + "://")) url = url.substring((Classic_SiteType.LOCAL.name + "://").length());
|
||||
|
||||
if (!url.startsWith("https://") && !url.startsWith("http://")) url = "https://" + url;
|
||||
|
||||
uri = new URL(url);
|
||||
String[] domainNameParts = uri.getHost().split("\\.");
|
||||
tldString = domainNameParts[domainNameParts.length - 1];
|
||||
|
||||
return tldString;
|
||||
}
|
||||
|
||||
public static String getDomainName(String url) throws URISyntaxException, MalformedURLException {
|
||||
if (url.startsWith(Classic_SiteType.PUBLIC.name + "://")) url = url.substring((Classic_SiteType.PUBLIC.name + "://").length());
|
||||
if (url.startsWith(Classic_SiteType.CLIENT.name + "://")) url = url.substring((Classic_SiteType.CLIENT.name + "://").length());
|
||||
if (url.startsWith(Classic_SiteType.SERVER.name + "://")) url = url.substring((Classic_SiteType.SERVER.name + "://").length());
|
||||
if (url.startsWith(Classic_SiteType.PROTOCOL.name + "://")) url = url.substring((Classic_SiteType.PROTOCOL.name + "://").length());
|
||||
if (url.startsWith(Classic_SiteType.LOCAL.name + "://")) url = url.substring((Classic_SiteType.LOCAL.name + "://").length());
|
||||
|
||||
if (!url.startsWith("https://") && !url.startsWith("http://")) url = "https://" + url;
|
||||
|
||||
URI uri = new URI(url);
|
||||
String domain = uri.getHost().replace("." + getTopLevelDomain(url), "");
|
||||
return domain;
|
||||
}
|
||||
|
||||
|
||||
public static String getPath(String url) {
|
||||
if (!url.startsWith(Classic_SiteType.PUBLIC.name + "://") && !url.startsWith(Classic_SiteType.CLIENT.name + "://") &&
|
||||
!url.startsWith(Classic_SiteType.SERVER.name + "://") && !url.startsWith(Classic_SiteType.PROTOCOL.name + "://") &&
|
||||
!url.startsWith(Classic_SiteType.LOCAL.name + "://") && !url.startsWith("http") && !url.startsWith("https")) {
|
||||
url = Classic_SiteType.PUBLIC.name + "://" + url;
|
||||
}
|
||||
|
||||
String[] split = url.split("/");
|
||||
if (split.length <= 3) return "";
|
||||
|
||||
StringBuilder path = new StringBuilder();
|
||||
|
||||
for (int i = 3; i < split.length; i++) path.append(split[i]).append("/");
|
||||
|
||||
String pathStr = path.toString();
|
||||
if (pathStr.startsWith("/")) pathStr = pathStr.substring("/".length());
|
||||
if (pathStr.endsWith("/")) pathStr = pathStr.substring(0, pathStr.length() - "/".length());
|
||||
|
||||
return pathStr;
|
||||
}
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
package github.openautonomousconnection.protocol.classic;
|
||||
|
||||
public class Classic_LocalDomain extends Classic_Domain {
|
||||
public Classic_LocalDomain(String name, String endName, String path) {
|
||||
super(name, endName, null, path);
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package github.openautonomousconnection.protocol.classic;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public enum Classic_ProtocolVersion implements Serializable {
|
||||
PV_1_0_0("1.0.0");
|
||||
;
|
||||
public final String version;
|
||||
|
||||
Classic_ProtocolVersion(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
package github.openautonomousconnection.protocol.classic;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Classic_RequestDomain extends Classic_Domain implements Serializable {
|
||||
|
||||
public Classic_RequestDomain(String name, String topLevelDomain, String path) {
|
||||
super(name, topLevelDomain, null, path);
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package github.openautonomousconnection.protocol.classic;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
enum Classic_SiteType implements Serializable {
|
||||
CLIENT("oac-client"), SERVER("oac-server"),
|
||||
PUBLIC("oac"), PROTOCOL("oac-protocol"), LOCAL("oac-local");
|
||||
;
|
||||
|
||||
public final String name;
|
||||
|
||||
Classic_SiteType(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
package github.openautonomousconnection.protocol.handle;
|
||||
|
||||
import me.finn.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
|
||||
public abstract class ClassicHandler {
|
||||
|
||||
public abstract void handleMessage(ConnectionHandler connectionHandler, String message);
|
||||
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package github.openautonomousconnection.protocol.packets;
|
||||
|
||||
import github.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import github.openautonomousconnection.protocol.ProtocolVersion;
|
||||
import lombok.Getter;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
|
||||
public abstract class OACPacket extends Packet {
|
||||
@Getter
|
||||
private final ProtocolVersion.ProtocolType packetType;
|
||||
@Getter
|
||||
private final ProtocolBridge protocolBridge;
|
||||
|
||||
public OACPacket(int id, ProtocolVersion.ProtocolType packetType, ProtocolBridge protocolBridge) {
|
||||
super(id);
|
||||
this.packetType = packetType;
|
||||
this.protocolBridge = protocolBridge;
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
package github.openautonomousconnection.protocol.packets.v1_0_0.classic;
|
||||
|
||||
import github.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import github.openautonomousconnection.protocol.ProtocolVersion;
|
||||
import github.openautonomousconnection.protocol.classic.Classic_Domain;
|
||||
import github.openautonomousconnection.protocol.classic.Classic_ProtocolVersion;
|
||||
import github.openautonomousconnection.protocol.classic.Classic_RequestDomain;
|
||||
import github.openautonomousconnection.protocol.packets.OACPacket;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
// ProtocolVersion 1.0.0-CLASSIC is ProtocolSide Server only
|
||||
public class DomainPacket extends OACPacket {
|
||||
private final Classic_RequestDomain requestDomain;
|
||||
private final Classic_Domain domain;
|
||||
private final int clientID;
|
||||
|
||||
public DomainPacket(ProtocolBridge protocolBridge, int toClient, Classic_RequestDomain requestDomain, Classic_Domain domain) {
|
||||
super(2, ProtocolVersion.ProtocolType.CLASSIC, protocolBridge);
|
||||
this.clientID = toClient;
|
||||
this.requestDomain = requestDomain;
|
||||
this.domain = domain;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
|
||||
objectOutputStream.writeInt(clientID);
|
||||
objectOutputStream.writeObject(requestDomain);
|
||||
objectOutputStream.writeObject(domain);
|
||||
|
||||
objectOutputStream.writeObject(Classic_ProtocolVersion.PV_1_0_0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
package github.openautonomousconnection.protocol.packets.v1_0_0.classic;
|
||||
|
||||
import github.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import github.openautonomousconnection.protocol.ProtocolVersion;
|
||||
import github.openautonomousconnection.protocol.classic.Classic_ProtocolVersion;
|
||||
import github.openautonomousconnection.protocol.packets.OACPacket;
|
||||
import github.openautonomousconnection.protocol.side.ProtocolClient;
|
||||
import me.finn.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
// ProtocolVersion 1.0.0-CLASSIC is ProtocolSide Server only
|
||||
public class MessagePacket extends OACPacket {
|
||||
private final String message;
|
||||
private Classic_ProtocolVersion protocolVersion;
|
||||
private final int clientID;
|
||||
|
||||
public MessagePacket(String message, int toClient, ProtocolBridge protocolBridge) {
|
||||
super(3, ProtocolVersion.ProtocolType.CLASSIC, protocolBridge);
|
||||
this.message = message;
|
||||
this.clientID = toClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
|
||||
objectOutputStream.writeInt(clientID);
|
||||
|
||||
objectOutputStream.writeUTF(message);
|
||||
objectOutputStream.writeObject(Classic_ProtocolVersion.PV_1_0_0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
int clientID = objectInputStream.readInt();
|
||||
String message = objectInputStream.readUTF();
|
||||
protocolVersion = (Classic_ProtocolVersion) objectInputStream.readObject();
|
||||
|
||||
getProtocolBridge().getClassicHandler().handleMessage(getProtocolBridge().getProtocolServer().getNetworkServer().getConnectionHandlerByID(clientID), message);
|
||||
}
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
package github.openautonomousconnection.protocol.packets.v1_0_0.classic;
|
||||
|
||||
public class PingPacket {
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
package github.openautonomousconnection.protocol.side;
|
||||
|
||||
public class ProtocolClient {
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package github.openautonomousconnection.protocol.side;
|
||||
|
||||
import github.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import github.openautonomousconnection.protocol.handle.ClassicHandler;
|
||||
import lombok.Getter;
|
||||
import me.finn.unlegitlibrary.network.system.server.NetworkServer;
|
||||
|
||||
public abstract class ProtocolServer {
|
||||
@Getter
|
||||
private final ProtocolBridge protocolBridge;
|
||||
|
||||
@Getter
|
||||
private NetworkServer networkServer;
|
||||
|
||||
public ProtocolServer(ProtocolBridge protocolBridge) {
|
||||
this.protocolBridge = protocolBridge;
|
||||
|
||||
this.networkServer = new NetworkServer.ServerBuilder().
|
||||
setEventManager(protocolBridge.getProtocolSettings().eventManager).
|
||||
setPacketHandler(protocolBridge.getProtocolSettings().packetHandler).
|
||||
setPort(protocolBridge.getProtocolSettings().port).
|
||||
build();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user