Moved classic code to classic branch

This commit is contained in:
Finn
2025-12-11 11:09:04 +01:00
parent fd0c062aed
commit 7c02af118a
30 changed files with 1604 additions and 305 deletions

View File

@@ -1,31 +0,0 @@
package org.openautonomousconnection.dns;
import org.openautonomousconnection.protocol.side.dns.ConnectedProtocolClient;
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.ClassicHandlerDNSServer;
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;
public class ClassicHandler extends ClassicHandlerDNSServer {
@Override
public void handleMessage(ConnectedProtocolClient client, String message, Classic_ProtocolVersion protocolVersion) {
}
@Override
public Classic_Domain getDomain(Classic_RequestDomain requestDomain) throws SQLException {
return null;
}
@Override
public Classic_Domain ping(Classic_RequestDomain requestDomain) throws SQLException {
return null;
}
@Override
public void unsupportedClassicPacket(String className, Object[] content, ConnectedProtocolClient client) {
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) 2024 Open Autonomous Connection - All Rights Reserved
*
* You are unauthorized to remove this copyright.
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/Open-Autonomous-Connection
* See LICENSE-File if exists
*/
/*
* Copyright (C) 2024 Open Autonomous Connection - All Rights Reserved
*
* You are unauthorized to remove this copyright.
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/Open-Autonomous-Connection
* See LICENSE-File if exists
*/
package org.openautonomousconnection.dns;
import org.openautonomousconnection.dns.utils.Database;
import org.openautonomousconnection.protocol.domain.Domain;
import org.openautonomousconnection.protocol.domain.RequestDomain;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DomainManager {
public static Domain getDomain(String name, String topLevelDomain) throws SQLException {
for (Domain domain : getDomains())
if (domain.name.equalsIgnoreCase(name) && domain.topLevelDomain.equalsIgnoreCase(topLevelDomain))
return domain;
return null;
}
public static Domain getDomain(RequestDomain requestDomain) throws SQLException {
return getDomain(requestDomain.name, requestDomain.topLevelDomain);
}
public static List<Domain> getDomains() throws SQLException {
List<Domain> domains = new ArrayList<>();
ResultSet result = Database.getConnection().prepareStatement("SELECT name, topleveldomain, destination FROM domains").executeQuery();
while (result.next()) {
String name = result.getString("name");
String topLevelDomain = result.getString("topleveldomain");
String destination = result.getString("destination");
domains.add(new Domain(name, topLevelDomain, destination, ""));
}
return domains;
}
public static List<String> getTopLevelDomains() throws SQLException {
List<String> topLevelDomains = new ArrayList<>();
ResultSet result = Database.getConnection().prepareStatement("SELECT name FROM topleveldomains").executeQuery();
while (result.next()) topLevelDomains.add(result.getString("name"));
return topLevelDomains;
}
}

View File

@@ -1,21 +0,0 @@
package org.openautonomousconnection.dns;
import dev.unlegitdqrk.unlegitlibrary.command.events.CommandExecutorMissingPermissionEvent;
import dev.unlegitdqrk.unlegitlibrary.command.events.CommandNotFoundEvent;
import dev.unlegitdqrk.unlegitlibrary.event.EventListener;
import org.openautonomousconnection.protocol.ProtocolBridge;
public class Listener extends EventListener {
@dev.unlegitdqrk.unlegitlibrary.event.Listener
public void onCommandNotFound(CommandNotFoundEvent event) {
StringBuilder argsBuilder = new StringBuilder();
for (String arg : event.getArgs()) argsBuilder.append(arg).append(" ");
ProtocolBridge.getInstance().getLogger().error("Command '" + event.getName() + argsBuilder.toString() + "' not found!");
}
@dev.unlegitdqrk.unlegitlibrary.event.Listener
public void onMissingCommandPermission(CommandExecutorMissingPermissionEvent event) {
ProtocolBridge.getInstance().getLogger().error("You do not have enough permissions to execute this command!");
}
}

View File

@@ -1,35 +1,109 @@
/*
* Copyright (C) 2024 Open Autonomous Connection - All Rights Reserved
*
* You are unauthorized to remove this copyright.
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/Open-Autonomous-Connection
* See LICENSE-File if exists
*/
package org.openautonomousconnection.dns;
import dev.unlegitdqrk.unlegitlibrary.command.CommandExecutor;
import dev.unlegitdqrk.unlegitlibrary.command.CommandManager;
import dev.unlegitdqrk.unlegitlibrary.command.CommandPermission;
import dev.unlegitdqrk.unlegitlibrary.utils.Logger;
import dev.unlegitdqrk.unlegitlibrary.addon.AddonLoader;
import dev.unlegitdqrk.unlegitlibrary.addon.impl.AddonInfo;
import org.openautonomousconnection.dns.utils.Config;
import org.openautonomousconnection.dns.utils.Database;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.ProtocolSettings;
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
import org.openautonomousconnection.protocol.ProtocolVersion;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.util.Scanner;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.SQLException;
import java.util.Objects;
public class Main {
private static final CommandPermission PERMISSION_ALL = new CommandPermission("all", 1);
private static final CommandExecutor commandExecutor = new CommandExecutor("DNS", PERMISSION_ALL) {};
private static CommandManager commandManager;
public static ProtocolBridge protocolBridge;
public static AddonLoader addonLoader;
public static void main(String[] args) throws Exception {
ProtocolSettings settings = new ProtocolSettings();
new ProtocolBridge(new Server(), settings, ProtocolVersion.PV_1_0_0_BETA, new File("logs"));
ProtocolBridge.getInstance().setClassicHandlerDNSServer(new ClassicHandler());
commandManager = new CommandManager(ProtocolBridge.getInstance().getProtocolSettings().eventManager);
Scanner scanner = new Scanner(System.in);
public static final File modulesFolder = new File("modules");
while (true) {
System.out.println(commandExecutor.getName() + "> ");
String line = scanner.nextLine();
commandManager.execute(commandExecutor, line);
public static void main(String[] args) {
try {
URL oracle = new URI("https://raw.githubusercontent.com/Open-Autonomous-Connection/dns/master/src/resources/version.txt").toURL();
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
StringBuilder version = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) version.append(inputLine);
if (!version.toString().equalsIgnoreCase(Files.readString(Path.of(Objects.requireNonNull(Main.class.getResource("../../../version.txt")).toURI())))) {
System.out.println();
System.out.println("======================================================");
System.out.println("IMPORTANT: A NEW SERVER VERSION IS PUBLISHED ON GITHUB");
System.out.println("======================================================");
System.out.println();
}
} catch (IOException | URISyntaxException exception) {
System.out.println();
System.out.println("=====================================================================");
System.out.println("IMPORTANT: SERVER VERSION CHECK COULD NOT COMPLETED! VISIT OUR GITHUB");
System.out.println(" https://github.com/Open-Autonomous-Connection ");
System.out.println("=====================================================================");
System.out.println();
}
try {
Config.init();
Database.connect();
} catch (SQLException | InstantiationException | ClassNotFoundException | IllegalAccessException | IOException exception) {
exception.printStackTrace();
return;
}
final ProtocolSettings protocolSettings = new ProtocolSettings();
protocolSettings.port = Config.getPort();
try {
protocolBridge = new ProtocolBridge(ProtocolVersion.PV_1_0_0, protocolSettings, new Server());
protocolBridge.getProtocolServer().setProtocolBridge(protocolBridge);
protocolBridge.getProtocolServer().getServer().getEventManager().registerListener(ServerEventListener.class);
protocolBridge.getProtocolServer().startServer();
System.out.println();
} catch (Exception exception) {
exception.printStackTrace();
return;
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
protocolBridge.getProtocolServer().stopServer();
Database.close();
} catch (SQLException | IOException exception) {
exception.printStackTrace();
}
}));
addonLoader = new AddonLoader(protocolSettings.eventManager, null);
try {
addonLoader.loadAddonsFromDirectory(modulesFolder);
addonLoader.getLoadedAddons().forEach(addon -> {
if (addon.isEnabled()) return;
AddonInfo info = addon.getAddonInfo();
System.out.println("Enabling Addon '" + info.name() + " v" + info.version() + "' by " + info.author() + "...");
addon.enable();
System.out.println("Addon '" + info.name() + " v" + info.version() + "' enabled.");
});
} catch (IOException exception) {
exception.printStackTrace();
}
}
}

View File

@@ -1,64 +1,56 @@
/*
* Copyright (C) 2024 Open Autonomous Connection - All Rights Reserved
*
* You are unauthorized to remove this copyright.
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/Open-Autonomous-Connection
* See LICENSE-File if exists
*/
package org.openautonomousconnection.dns;
import org.openautonomousconnection.protocol.ProtocolBridge;
import org.openautonomousconnection.protocol.side.dns.ConnectedProtocolClient;
import org.openautonomousconnection.protocol.side.dns.ProtocolDNSServer;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.DNSResponseCode;
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.Domain;
import dev.unlegitdqrk.unlegitlibrary.network.system.server.ConnectionHandler;
import org.openautonomousconnection.dns.utils.Config;
import org.openautonomousconnection.dns.utils.Database;
import org.openautonomousconnection.protocol.domain.Domain;
import org.openautonomousconnection.protocol.side.ProtocolServer;
import java.io.File;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class Server extends ProtocolDNSServer {
/**
* Constructs a ProtocolDNSServer with the specified configuration file.
*
* @throws IOException If an I/O error occurs.
* @throws CertificateException If a certificate error occurs.
*/
public Server() throws IOException, CertificateException {
super(new File("config.properties"));
public class Server extends ProtocolServer {
@Override
public List<Domain> getDomains() throws SQLException {
return DomainManager.getDomains();
}
@Override
public List<Domain> getDomains() {
return List.of();
public List<String> getTopLevelDomains() throws SQLException {
return DomainManager.getTopLevelDomains();
}
@Override
public String getDomainDestination(Domain domain) {
return "";
public void handleMessage(ConnectionHandler connectionHandler, String message) {
System.out.println("[MESSAGE] " + connectionHandler.getClientID() + ": " + message);
}
@Override
public String getSubnameDestination(Domain domain, String subname) {
return "";
public String getInfoSite(String topLevelDomain) throws SQLException {
if (!topLevelDomainExists(topLevelDomain)) return null;
ResultSet resultSet = Database.getConnection().prepareStatement("SELECT name, info FROM topleveldomains").executeQuery();
while (resultSet.next()) if (resultSet.getString("name").equals(topLevelDomain)) return resultSet.getString("info");
return null;
}
@Override
public String getTLNInfoSite(String topLevelName) {
return "";
public String getInterfaceSite() {
return Config.getInterfaceSite();
}
@Override
public DNSResponseCode validateDomain(Domain requestedDomain) {
if (!requestedDomain.getProtocol().equalsIgnoreCase("oac")) return DNSResponseCode.RESPONSE_INVALID_REQUEST;
return DNSResponseCode.RESPONSE_DOMAIN_FULLY_NOT_EXIST;
}
@Override
public void validationPacketSendFailed(Domain domain, ConnectedProtocolClient client, Exception exception) {
ProtocolBridge.getInstance().getLogger().exception("Failed to send ValidationPacket. (" +
"Domain: " + domain.getProtocol() + "." + (domain.hasSubname() ? domain.getSubname() : "") + "." + domain.getTopLevelName() + "/" + domain.getPath() + "?" + domain.getQuery() + "#" + domain.getFragment() + ";" +
";Client: " + client.getConnectionHandler().getClientID() + ")", exception);
}
@Override
public void domainDestinationPacketFailedSend(ConnectedProtocolClient client, Domain domain, DNSResponseCode validationResponse, Exception exception) {
ProtocolBridge.getInstance().getLogger().exception("Failed to send DomainDestinationPacket. (" +
"Domain: " + domain.getProtocol() + "." + (domain.hasSubname() ? domain.getSubname() : "") + "." + domain.getTopLevelName() + "/" + domain.getPath() + "?" + domain.getQuery() + "#" + domain.getFragment() + ";" +
"Validation response: " + validationResponse + ";Client: " + client.getConnectionHandler().getClientID() + ")", exception);
public String getDNSServerInfoSite() {
return Config.getInfoSite();
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (C) 2024 Open Autonomous Connection - All Rights Reserved
*
* You are unauthorized to remove this copyright.
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/Open-Autonomous-Connection
* See LICENSE-File if exists
*/
package org.openautonomousconnection.dns;
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 org.openautonomousconnection.protocol.events.v1_0_0.DomainPacketReceivedEvent;
import org.openautonomousconnection.protocol.events.v1_0_0.PingPacketReceivedEvent;
public class ServerEventListener extends EventListener {
@Listener
public void onConnect(ConnectionHandlerConnectedEvent event) {
System.out.println("New client connected. ID: " + event.getConnectionHandler().getClientID());
System.out.println();
}
@Listener
public void onDisconnect(ConnectionHandlerDisconnectedEvent event) {
System.out.println("Client disconnected. ID: " + event.getConnectionHandler().getClientID());
System.out.println();
}
@Listener
public void onPing(PingPacketReceivedEvent event) {
System.out.println("New Ping request:");
System.out.println(" » From client id: " + event.clientID);
System.out.println(" » Request domain: " + event.requestDomain.toString());
System.out.println(" » Path: " + event.requestDomain.getPath());
System.out.println(" » Reachable: " + (event.reachable ? "Yes" : "No"));
System.out.println(" » Destination: " + (event.domain == null ? "N/A" : event.domain.parsedDestination()));
System.out.println();
}
@Listener
public void onExistCheck(DomainPacketReceivedEvent event) {
System.out.println("New Domain packet request:");
System.out.println(" » From client id: " + event.clientID);
System.out.println(" » Request domain: " + event.requestDomain.toString());
System.out.println(" » Path: " + event.requestDomain.getPath());
System.out.println(" » Exists: " + (event.domain == null ? "No" : "Yes"));
System.out.println(" » Destination: " + (event.domain == null ? "N/A" : event.domain.parsedDestination()));
System.out.println(" ");
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2024 Open Autonomous Connection - All Rights Reserved
*
* You are unauthorized to remove this copyright.
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/Open-Autonomous-Connection
* See LICENSE-File if exists
*/
package org.openautonomousconnection.dns.utils;
import org.openautonomousconnection.protocol.utils.APIInformation;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class APIManager {
public static boolean hasKey(String username, String application) throws SQLException {
PreparedStatement statement = Database.getConnection().prepareStatement("SELECT application, username FROM apikeys WHERE application = ? AND username = ?");
statement.setString(1, application.toLowerCase());
statement.setString(2, username);
ResultSet result = statement.executeQuery();
return result.next() && result.getString("application").equalsIgnoreCase(application) && result.getString("username").equalsIgnoreCase(username);
}
public static boolean validateKey(String username, String application, String apiKey) throws SQLException {
if (!hasKey(username, application)) return false;
PreparedStatement statement = Database.getConnection().prepareStatement("SELECT application, keyapi, username FROM apikeys WHERE application = ? AND keyapi = ? AND username = ?");
statement.setString(1, application.toLowerCase());
statement.setString(2, apiKey);
statement.setString(3, username);
ResultSet result = statement.executeQuery();
return result.next() && result.getString("application").equalsIgnoreCase(application) && result.getString("keyapi").equals(apiKey) && result.getString("username").equalsIgnoreCase(username);
}
public static boolean validateKey(APIInformation apiInformation) throws SQLException {
return validateKey(apiInformation.username, apiInformation.apiApplication, apiInformation.apiKey);
}
}

View File

@@ -0,0 +1,116 @@
/*
* Copyright (C) 2024 Open Autonomous Connection - All Rights Reserved
*
* You are unauthorized to remove this copyright.
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/Open-Autonomous-Connection
* See LICENSE-File if exists
*/
package org.openautonomousconnection.dns.utils;
import dev.unlegitdqrk.unlegitlibrary.file.ConfigurationManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Config {
public static boolean parseBoolean(int i) {
return i != 0;
}
public static int booleanToInt(boolean bool) {
return bool ? 1 : 0;
}
public static boolean topLevelDomainRegisteringAllowed() throws SQLException {
PreparedStatement statement = Database.getConnection().prepareStatement("SELECT value FROM config WHERE name = ?");
statement.setString(1, "allow_register_tld");
ResultSet result = statement.executeQuery();
return result.next() && parseBoolean(Integer.parseInt(result.getString("value")));
}
public static boolean domainRegisteringAllowed() throws SQLException {
PreparedStatement statement = Database.getConnection().prepareStatement("SELECT value FROM config WHERE name = ?");
statement.setString(1, "allow_register_domain");
ResultSet result = statement.executeQuery();
return result.next() && parseBoolean(Integer.parseInt(result.getString("value")));
}
public static boolean accountRegisteringAllowed() throws SQLException {
PreparedStatement statement = Database.getConnection().prepareStatement("SELECT value FROM config WHERE name = ?");
statement.setString(1, "allow_register_account");
ResultSet result = statement.executeQuery();
return result.next() && parseBoolean(Integer.parseInt(result.getString("value")));
}
public static int maxApiKeys() throws SQLException {
PreparedStatement statement = Database.getConnection().prepareStatement("SELECT value FROM config WHERE name = ?");
statement.setString(1, "max_apikeys");
ResultSet result = statement.executeQuery();
if (!result.next()) return 0;
return Integer.parseInt(result.getString("value")); // -1 = Endless
}
private static final File configFile = new File("./config.properties");
private static ConfigurationManager config;
public static void init() throws IOException {
URL whatIsMyIp = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new InputStreamReader(whatIsMyIp.openStream()));
String ip = in.readLine();
if (!configFile.exists()) configFile.createNewFile();
config = new ConfigurationManager(configFile);
config.loadProperties();
if (!config.isSet("port")) config.set("port", 9382);
if (!config.isSet("sites.info")) config.set("sites.info", "DNS SERVER NEED A INFO SITE!");
if (!config.isSet("sites.interface")) config.set("sites.interface", ip);
if (!config.isSet("database.host")) config.set("database.host", "127.0.0.1");
if (!config.isSet("database.port")) config.set("database.port", 3306);
if (!config.isSet("database.name")) config.set("database.name", "my_db");
if (!config.isSet("database.username")) config.set("database.username", "my_username");
if (!config.isSet("database.password")) config.set("database.password", "my_password");
config.saveProperties();
}
public static String getInfoSite() {
return config.getString("sites.info");
}
public static String getInterfaceSite() {
return config.getString("sites.interface");
}
public static int getPort() {
return config.getInt("port");
}
public static String getDatabaseHost() {
return config.getString("database.host");
}
public static int getDatabasePort() {
return config.getInt("database.port");
}
public static String getDatabaseName() {
return config.getString("database.name");
}
public static String getDatabaseUsername() {
return config.getString("database.username");
}
public static String getDatabasePassword() {
return config.getString("database.password");
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright (C) 2024 Open Autonomous Connection - All Rights Reserved
*
* You are unauthorized to remove this copyright.
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/Open-Autonomous-Connection
* See LICENSE-File if exists
*/
package org.openautonomousconnection.dns.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Database {
private static Connection connection;
public static Connection getConnection() {
return connection;
}
public static void connect() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
if (isConnected()) return;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://" + Config.getDatabaseHost() + ":" + Config.getDatabasePort() + "/" +
Config.getDatabaseName() + "?autoReconnect=true", Config.getDatabaseUsername(), Config.getDatabasePassword());
}
public static void close() throws SQLException {
if (!isConnected()) return;
connection.close();
connection = null;
}
public static boolean isConnected() {
return connection != null;
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2024 Open Autonomous Connection - All Rights Reserved
*
* You are unauthorized to remove this copyright.
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/Open-Autonomous-Connection
* See LICENSE-File if exists
*/
package org.openautonomousconnection.dns.utils;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Utils {
public static String createAccessKey(String input) {
return sha256(shuffleString(sha256(input) + getAlphaNumericString(5) +
sha256(getAlphaNumericString(5)) +
sha256(getAlphaNumericString(5)) +
sha256(getAlphaNumericString(5))));
}
public static String sha256(final String base) {
try {
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
final byte[] hash = digest.digest(base.getBytes(StandardCharsets.UTF_8));
final StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
final String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static String getAlphaNumericString(int length) {
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvxyz";
StringBuilder builder = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = (int) (chars.length() * Math.random());
builder.append(chars.charAt(index));
}
return builder.toString();
}
public static String shuffleString(String input) {
List<Character> characters = new ArrayList<>();
for (char c : input.toCharArray()) characters.add(c);
Collections.shuffle(characters);
StringBuilder shuffledString = new StringBuilder();
for (char c : characters) shuffledString.append(c);
return shuffledString.toString();
}
}