diff --git a/ins_backend.sql b/ins_backend.sql new file mode 100644 index 0000000..85f6376 --- /dev/null +++ b/ins_backend.sql @@ -0,0 +1,205 @@ +-- phpMyAdmin SQL Dump +-- version 5.2.1deb1+deb12u1 +-- https://www.phpmyadmin.net/ +-- +-- Host: localhost:3306 +-- Generation Time: Dec 11, 2025 at 11:00 AM +-- Server version: 10.11.14-MariaDB-0+deb12u2 +-- PHP Version: 8.2.29 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +START TRANSACTION; +SET time_zone = "+00:00"; + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + +-- +-- Database: `oac_ins` +-- + +-- -------------------------------------------------------- + +-- +-- Table structure for table `infonames` +-- + +CREATE TABLE `infonames` ( + `id` int(11) NOT NULL, + `info` varchar(63) NOT NULL, + `tln_id` int(11) NOT NULL, + `uid` int(11) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `records` +-- + +CREATE TABLE `records` ( + `id` int(11) NOT NULL, + `infoname_id` int(11) NOT NULL, + `subname_id` int(11) DEFAULT NULL, + `type` enum('A','AAAA','TXT','CNAME','MX','SRV','NS') NOT NULL, + `value` varchar(2048) NOT NULL, + `ttl` int(11) DEFAULT 3600, + `priority` int(11) DEFAULT NULL, + `port` int(11) DEFAULT NULL, + `weight` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `subnames` +-- + +CREATE TABLE `subnames` ( + `id` int(11) NOT NULL, + `name` varchar(63) NOT NULL, + `infoname_id` int(11) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `tln` +-- + +CREATE TABLE `tln` ( + `id` int(11) NOT NULL, + `name` varchar(63) NOT NULL, + `info` text DEFAULT NULL, + `owner_id` int(11) DEFAULT NULL, + `is_public` tinyint(1) DEFAULT 1, + `allow_subdomains` tinyint(1) DEFAULT 1 +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `users` +-- + +CREATE TABLE `users` ( + `id` int(11) NOT NULL, + `uid` varchar(36) NOT NULL, + `username` varchar(64) NOT NULL, + `password` varchar(255) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +-- +-- Indexes for dumped tables +-- + +-- +-- Indexes for table `infonames` +-- +ALTER TABLE `infonames` + ADD PRIMARY KEY (`id`), + ADD KEY `tln_id` (`tln_id`), + ADD KEY `uid` (`uid`); + +-- +-- Indexes for table `records` +-- +ALTER TABLE `records` + ADD PRIMARY KEY (`id`), + ADD KEY `infoname_id` (`infoname_id`), + ADD KEY `subname_id` (`subname_id`); + +-- +-- Indexes for table `subnames` +-- +ALTER TABLE `subnames` + ADD PRIMARY KEY (`id`), + ADD KEY `infoname_id` (`infoname_id`); + +-- +-- Indexes for table `tln` +-- +ALTER TABLE `tln` + ADD PRIMARY KEY (`id`), + ADD UNIQUE KEY `name` (`name`), + ADD KEY `owner_id` (`owner_id`); + +-- +-- Indexes for table `users` +-- +ALTER TABLE `users` + ADD PRIMARY KEY (`id`), + ADD UNIQUE KEY `uid` (`uid`), + ADD UNIQUE KEY `username` (`username`); + +-- +-- AUTO_INCREMENT for dumped tables +-- + +-- +-- AUTO_INCREMENT for table `infonames` +-- +ALTER TABLE `infonames` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; + +-- +-- AUTO_INCREMENT for table `records` +-- +ALTER TABLE `records` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; + +-- +-- AUTO_INCREMENT for table `subnames` +-- +ALTER TABLE `subnames` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; + +-- +-- AUTO_INCREMENT for table `tln` +-- +ALTER TABLE `tln` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; + +-- +-- AUTO_INCREMENT for table `users` +-- +ALTER TABLE `users` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; + +-- +-- Constraints for dumped tables +-- + +-- +-- Constraints for table `infonames` +-- +ALTER TABLE `infonames` + ADD CONSTRAINT `infonames_ibfk_1` FOREIGN KEY (`tln_id`) REFERENCES `tln` (`id`) ON DELETE CASCADE, + ADD CONSTRAINT `infonames_ibfk_2` FOREIGN KEY (`uid`) REFERENCES `users` (`id`) ON DELETE CASCADE; + +-- +-- Constraints for table `records` +-- +ALTER TABLE `records` + ADD CONSTRAINT `records_ibfk_1` FOREIGN KEY (`infoname_id`) REFERENCES `infonames` (`id`) ON DELETE CASCADE, + ADD CONSTRAINT `records_ibfk_2` FOREIGN KEY (`subname_id`) REFERENCES `subnames` (`id`) ON DELETE CASCADE; + +-- +-- Constraints for table `subnames` +-- +ALTER TABLE `subnames` + ADD CONSTRAINT `subnames_ibfk_1` FOREIGN KEY (`infoname_id`) REFERENCES `infonames` (`id`) ON DELETE CASCADE; + +-- +-- Constraints for table `tln` +-- +ALTER TABLE `tln` + ADD CONSTRAINT `tln_ibfk_1` FOREIGN KEY (`owner_id`) REFERENCES `users` (`id`) ON DELETE SET NULL; +COMMIT; + +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/pom.xml b/pom.xml index 5047260..6df88c6 100644 --- a/pom.xml +++ b/pom.xml @@ -70,7 +70,13 @@ org.openautonomousconnection protocol - 1.0.0-BETA.7 + 1.0.0-BETA.1 + + + org.projectlombok + lombok + 1.18.38 + provided \ No newline at end of file diff --git a/run/config.properties b/run/config.properties deleted file mode 100644 index 6cc7bee..0000000 --- a/run/config.properties +++ /dev/null @@ -1,3 +0,0 @@ -#Sat Oct 04 01:25:22 CEST 2025 -server.site.info=DNS-SERVER INFO SITE IP -server.site.register=SERVER IP TO DNS-FRONTENT WEBSITE diff --git a/src/main/java/org/openautonomousconnection/dns/ClassicHandler.java b/src/main/java/org/openautonomousconnection/dns/ClassicHandler.java deleted file mode 100644 index ac65573..0000000 --- a/src/main/java/org/openautonomousconnection/dns/ClassicHandler.java +++ /dev/null @@ -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) { - - } -} diff --git a/src/main/java/org/openautonomousconnection/dns/Server.java b/src/main/java/org/openautonomousconnection/dns/Server.java deleted file mode 100644 index 47a20f5..0000000 --- a/src/main/java/org/openautonomousconnection/dns/Server.java +++ /dev/null @@ -1,64 +0,0 @@ -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 java.io.File; -import java.io.IOException; -import java.security.cert.CertificateException; -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")); - } - - @Override - public List getDomains() { - return List.of(); - } - - @Override - public String getDomainDestination(Domain domain) { - return ""; - } - - @Override - public String getSubnameDestination(Domain domain, String subname) { - return ""; - } - - @Override - public String getTLNInfoSite(String topLevelName) { - return ""; - } - - @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); - } -} diff --git a/src/main/java/org/openautonomousconnection/ins/ClassicHandler.java b/src/main/java/org/openautonomousconnection/ins/ClassicHandler.java new file mode 100644 index 0000000..52f98f0 --- /dev/null +++ b/src/main/java/org/openautonomousconnection/ins/ClassicHandler.java @@ -0,0 +1,79 @@ +package org.openautonomousconnection.ins; + +import org.openautonomousconnection.ins.utils.ClassicHelper; +import org.openautonomousconnection.ins.utils.ParsedDomain; +import org.openautonomousconnection.protocol.ProtocolBridge; +import org.openautonomousconnection.protocol.side.ins.ConnectedProtocolClient; +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.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.*; +import java.util.ArrayList; +import java.util.List; + +public class ClassicHandler extends ClassicHandlerINSServer { + + private ProtocolBridge bridge() { + return Main.getProtocolBridge(); + } + + @Override + public void handleMessage(ConnectedProtocolClient client, String message, Classic_ProtocolVersion protocolVersion) { + bridge().getLogger().info("[ClassicHandler] Message received from ClientID " + client.getConnectionHandler().getClientID() + + " (Classic Version " + protocolVersion.version + ", Client Version: " + + client.getClientVersion().toString() + "): " + message); + } + + @Override + public Classic_Domain getDomain(Classic_RequestDomain requestDomain) throws SQLException { + + ParsedDomain pd = ClassicHelper.parseDomain(requestDomain); + + // FIRST try A-record + List a = bridge().getProtocolINSServer().resolve(pd.tln, pd.name, pd.sub, INSRecordType.A); + + if (!a.isEmpty()) { + return ClassicHelper.buildClassicDomain(pd, a.get(0)); + } + + // If no A, try CNAME + List cnameList = bridge().getProtocolINSServer().resolve(pd.tln, pd.name, pd.sub, INSRecordType.CNAME); + + if (cnameList.isEmpty()) return null; // completely not found + + INSRecord cname = cnameList.get(0); + + // CNAME target = "www.example.net" or "app.dev" + String[] cnameParts = cname.value.split("\\."); + if (cnameParts.length < 2) return null; + + String cnameTln = cnameParts[cnameParts.length - 1]; + String cnameName = cnameParts[cnameParts.length - 2]; + String cnameSub = cnameParts.length > 2 + ? String.join(".", java.util.Arrays.copyOfRange(cnameParts, 0, cnameParts.length - 2)) + : null; + + List aAfterCname = bridge().getProtocolINSServer().resolve(cnameTln, cnameName, cnameSub, INSRecordType.A); + + if (aAfterCname.isEmpty()) return null; + + return ClassicHelper.buildClassicDomain(pd, aAfterCname.get(0)); + } + + @Override + public Classic_Domain ping(Classic_RequestDomain req) { + return new Classic_Domain(req.name, req.topLevelDomain, null, req.path, bridge()); + } + + @Override + public void unsupportedClassicPacket(String className, Object[] content, ConnectedProtocolClient client) { + bridge().getLogger().warn( + "[Classic UnsupportedPacket] From client " + client.getConnectionHandler().getClientID() + + ": packet=" + className + " content=" + java.util.Arrays.toString(content) + ); + } +} diff --git a/src/main/java/org/openautonomousconnection/ins/DatabaseINSServer.java b/src/main/java/org/openautonomousconnection/ins/DatabaseINSServer.java new file mode 100644 index 0000000..652bd75 --- /dev/null +++ b/src/main/java/org/openautonomousconnection/ins/DatabaseINSServer.java @@ -0,0 +1,169 @@ +package org.openautonomousconnection.ins; + +import dev.unlegitdqrk.unlegitlibrary.file.ConfigurationManager; +import org.openautonomousconnection.protocol.ProtocolBridge; +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.io.File; +import java.io.IOException; +import java.security.cert.CertificateException; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +/** + * Concrete INS server implementation backed by a SQL database. + *

+ * This implementation expects a table schema similar to: + *

+ * CREATE TABLE ins_records (
+ *   id        BIGINT PRIMARY KEY AUTO_INCREMENT,
+ *   tln       VARCHAR(64)  NOT NULL,
+ *   name      VARCHAR(255) NOT NULL,
+ *   sub       VARCHAR(255) NULL,
+ *   type      VARCHAR(16)  NOT NULL, -- matches {@link INSRecordType#name()}
+ *   value     VARCHAR(1024) NOT NULL,
+ *   priority  INT NOT NULL,
+ *   weight    INT NOT NULL,
+ *   port      INT NOT NULL,
+ *   ttl       INT NOT NULL
+ * );
+ *
+ * CREATE TABLE ins_tln_info (
+ *   tln       VARCHAR(64) PRIMARY KEY,
+ *   host      VARCHAR(255) NOT NULL,
+ *   port      INT NOT NULL
+ * );
+ * 
+ */ +public final class DatabaseINSServer extends ProtocolINSServer { + + private final String jdbcUrl; + private final String jdbcUser; + private final String jdbcPassword; + + private final ConfigurationManager configurationManager; + + /** + * Creates a new database-backed INS server. + * + * @throws IOException If the base server initialization fails. + * @throws CertificateException If required certificate files are missing or invalid. + */ + public DatabaseINSServer() throws IOException, CertificateException { + super(new File("config.properties")); + + configurationManager = new ConfigurationManager(new File("config.properties")); + configurationManager.loadProperties(); + + if (!configurationManager.isSet("db.url")) { + configurationManager.set("db.url", "jdbc:mysql://localhost:3306/DB_NAME?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC\n"); + configurationManager.saveProperties(); + } + + if (!configurationManager.isSet("db.user")) { + configurationManager.set("db.user", "username"); + configurationManager.saveProperties(); + } + + if (!configurationManager.isSet("db.password")) { + configurationManager.set("db.password", "password"); + configurationManager.saveProperties(); + } + + jdbcUrl = configurationManager.getString("db.url"); + jdbcUser = configurationManager.getString("db.user"); + jdbcPassword = configurationManager.getString("db.password"); + } + + private Connection openConnection() throws SQLException { + return DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword); + } + + /** + * Resolves records for the given INS name from the database. + *

+ * This method does a single lookup and returns exactly the records that + * match the given query (no CNAME recursion here – that is handled on a + * higher level, e.g. via {@code INSRecordTools.resolveWithCNAME(...)}). + * + * @param tln Top-level-name. + * @param name InfoName. + * @param sub Optional sub-name. May be {@code null}. + * @param type Desired record type. + * + * @return List of matching {@link INSRecord}s, or an empty list if none exist. + */ + @Override + public List resolve(String tln, String name, String sub, INSRecordType type) { + List result = new ArrayList<>(); + + String sql = + "SELECT type, value, priority, weight, port, ttl " + + "FROM ins_records " + + "WHERE tln = ? AND name = ? AND type = ? " + + "AND ( (sub IS NULL AND ? IS NULL) OR sub = ? )"; + + try (Connection conn = openConnection(); + PreparedStatement ps = conn.prepareStatement(sql)) { + + ps.setString(1, tln); + ps.setString(2, name); + ps.setString(3, type.name()); + ps.setString(4, sub); + ps.setString(5, sub); + + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + INSRecordType dbType = INSRecordType.valueOf(rs.getString("type")); + String value = rs.getString("value"); + int priority = rs.getInt("priority"); + int weight = rs.getInt("weight"); + int port = rs.getInt("port"); + int ttl = rs.getInt("ttl"); + + result.add(new INSRecord(dbType, value, priority, weight, port, ttl)); + } + } + } catch (SQLException ex) { + getProtocolBridge().getLogger().exception("Failed to resolve INS record from database", ex); + } + + return result; + } + + /** + * Resolves the TLN info site, + * which is used when a client queries {@code info.} without any sub-name. + *

+ * The result must be returned as {@code "host:port"}. + * + * @param tln The TLN the client is asking about. + * + * @return A {@code "host:port"} string or {@code null} if no TLN info site is configured. + */ + @Override + public String resolveTLNInfoSite(String tln) { + String sql = "SELECT host, port FROM ins_tln_info WHERE tln = ?"; + + try (Connection conn = openConnection(); + PreparedStatement ps = conn.prepareStatement(sql)) { + + ps.setString(1, tln); + + try (ResultSet rs = ps.executeQuery()) { + if (rs.next()) { + String host = rs.getString("host"); + int port = rs.getInt("port"); + return host + ":" + port; + } + } + } catch (SQLException ex) { + getProtocolBridge().getLogger().exception("Failed to resolve TLN info site for tln=" + tln, ex); + } + + return null; + } +} diff --git a/src/main/java/org/openautonomousconnection/dns/Listener.java b/src/main/java/org/openautonomousconnection/ins/Listener.java similarity index 95% rename from src/main/java/org/openautonomousconnection/dns/Listener.java rename to src/main/java/org/openautonomousconnection/ins/Listener.java index 825f007..69d01e4 100644 --- a/src/main/java/org/openautonomousconnection/dns/Listener.java +++ b/src/main/java/org/openautonomousconnection/ins/Listener.java @@ -1,4 +1,4 @@ -package org.openautonomousconnection.dns; +package org.openautonomousconnection.ins; import dev.unlegitdqrk.unlegitlibrary.command.events.CommandExecutorMissingPermissionEvent; import dev.unlegitdqrk.unlegitlibrary.command.events.CommandNotFoundEvent; diff --git a/src/main/java/org/openautonomousconnection/dns/Main.java b/src/main/java/org/openautonomousconnection/ins/Main.java similarity index 69% rename from src/main/java/org/openautonomousconnection/dns/Main.java rename to src/main/java/org/openautonomousconnection/ins/Main.java index aed2689..35d424d 100644 --- a/src/main/java/org/openautonomousconnection/dns/Main.java +++ b/src/main/java/org/openautonomousconnection/ins/Main.java @@ -1,16 +1,16 @@ -package org.openautonomousconnection.dns; +package org.openautonomousconnection.ins; 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 lombok.Getter; import org.openautonomousconnection.protocol.ProtocolBridge; import org.openautonomousconnection.protocol.ProtocolSettings; +import org.openautonomousconnection.protocol.side.ins.ProtocolINSServer; import org.openautonomousconnection.protocol.versions.ProtocolVersion; +import javax.annotation.processing.Generated; import java.io.File; -import java.io.IOException; -import java.security.cert.CertificateException; import java.util.Scanner; public class Main { @@ -18,11 +18,13 @@ public class Main { private static final CommandExecutor commandExecutor = new CommandExecutor("DNS", PERMISSION_ALL) {}; private static CommandManager commandManager; + @Getter + private static ProtocolBridge protocolBridge; + 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); + protocolBridge = new ProtocolBridge(new DatabaseINSServer(), settings, ProtocolVersion.PV_1_0_0_BETA, new File("logs")); + commandManager = new CommandManager(protocolBridge.getProtocolSettings().eventManager); Scanner scanner = new Scanner(System.in); while (true) { diff --git a/src/main/java/org/openautonomousconnection/ins/utils/ClassicHelper.java b/src/main/java/org/openautonomousconnection/ins/utils/ClassicHelper.java new file mode 100644 index 0000000..4927764 --- /dev/null +++ b/src/main/java/org/openautonomousconnection/ins/utils/ClassicHelper.java @@ -0,0 +1,38 @@ +package org.openautonomousconnection.ins.utils; + +import org.openautonomousconnection.ins.Main; +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; + +public class ClassicHelper { + public static Classic_Domain buildClassicDomain(ParsedDomain req, INSRecord rec) { + Classic_Domain info = new Classic_Domain(req.name, req.tln, req.sub, req.path, Main.getProtocolBridge()); + + 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, Main.getProtocolBridge()); + } + + public static 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(".", java.util.Arrays.copyOfRange(parts, 0, parts.length - 1)) + : null; + + return new ParsedDomain(tln, name, sub, req.path); + } +} diff --git a/src/main/java/org/openautonomousconnection/ins/utils/ParsedDomain.java b/src/main/java/org/openautonomousconnection/ins/utils/ParsedDomain.java new file mode 100644 index 0000000..3089d15 --- /dev/null +++ b/src/main/java/org/openautonomousconnection/ins/utils/ParsedDomain.java @@ -0,0 +1,15 @@ +package org.openautonomousconnection.ins.utils; + +public final class ParsedDomain { + public final String tln; + public final String name; + public final String sub; + public final String path; + + public ParsedDomain(String tln, String name, String sub, String path) { + this.tln = tln; + this.name = name; + this.sub = sub; + this.path = path; + } +} \ No newline at end of file