From b625887a56adc7158b0f5036b3da7a2afb2cf5dd Mon Sep 17 00:00:00 2001 From: "luca0N\\!" Date: Sun, 21 Mar 2021 18:38:39 -0300 Subject: [PATCH] Finished main Resolve FetchType implementation --- cfg/gen.cfg | 2 +- src/com/luca0n/dual/common/Dual.java | 45 +++++++++++++++---- .../common/UnknownFetchTypeException.java | 32 +++++++++++++ 3 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 src/com/luca0n/dual/common/UnknownFetchTypeException.java diff --git a/cfg/gen.cfg b/cfg/gen.cfg index 646b799..a9f8f5a 100644 --- a/cfg/gen.cfg +++ b/cfg/gen.cfg @@ -39,7 +39,7 @@ # # The following values are allowed for this option: # - Resolve: gets the target IP address by resolving the provided address. -# - RawFetch: gets the target IP address by connecting to the provided +# - PlainText: gets the target IP address by connecting to the provided # address that outputs the desired IP address in plain text. FetchType=Resolve diff --git a/src/com/luca0n/dual/common/Dual.java b/src/com/luca0n/dual/common/Dual.java index ed3c468..ae1d56f 100644 --- a/src/com/luca0n/dual/common/Dual.java +++ b/src/com/luca0n/dual/common/Dual.java @@ -24,11 +24,15 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.FileSystems; import java.nio.file.Path; +import java.net.InetAddress; +import java.util.List; +import java.util.ArrayList; import com.luca0n.dual.common.ConfigSyntaxException; import com.luca0n.dual.common.NoSuchNamespaceException; import com.luca0n.dual.common.NoSuchKeyException; import com.luca0n.dual.common.UnsupportedOperatingSystemException; +import com.luca0n.dual.common.UnknownFetchTypeException; import com.luca0n.dual.config.Config; import com.luca0n.dual.config.ConfigUtil; @@ -57,7 +61,7 @@ public class Dual { } public static void run() throws ConfigSyntaxException, NoSuchNamespaceException, NoSuchKeyException, IOException, - UnsupportedOperatingSystemException { + UnsupportedOperatingSystemException, UnknownFetchTypeException { // Read configuration file Config c = ConfigUtil.fromFile(ConfigUtil.DEFAULT_CONFIG); @@ -122,8 +126,17 @@ public class Dual { System.out.printf(hostsPath == null ? "%s: using %s settings.\n" : "%s: using custom settings for %s.\n", NAME, os); + // Check FetchType. + // TODO: add support for PlainText FetchType. + if (type.equals("PlainText")) + throw new UnsupportedOperationException("The " + type + " FetchType is currently not supported."); + else if (!type.equals("Resolve")) + throw new UnknownFetchTypeException("The specified FetchType in the main configuration file (" + type + ") is unknown."); + + String ip = InetAddress.getByName(source).getHostAddress(); + // Open hosts file. - + // TODO: Use buffered I/O methods. if (hostsPath == null){ boolean isWindows = os.contains(SUPPORTED_OPERATING_SYSTEMS[1]); hostsPath = isWindows ? "C:\\WINDOWS\\System32\\drivers\\etc\\hosts" : "/etc/hosts"; @@ -135,25 +148,39 @@ public class Dual { byte[] fileBytes = Files.readAllBytes(path); String[] lines = new String(fileBytes).split(System.lineSeparator()); + List updatedLines = new ArrayList<>(); - for (String line : lines){ + for (int x = 0; x < lines.length; x++){ + String line = lines[x]; + boolean keepLine = true; // Check if this line is a comment. if (line.startsWith("#")) // Ignore this line. continue; - String[] contents = line.split("#")[0]; + String[] contents = line.split("#"); // Check if this line was auto-generated by this program. if (contents.length == 2){ // Check line comment. - if (contents[1].equals(AUTO_GEN_COMMENT)){ - // Remove this entry if we're going to replace it. - + if (contents[1].substring(1).equals(AUTO_GEN_COMMENT)){ + // This line was auto-generated by this program. + // Check if it matches the current hostname. + if (contents[0].contains(target)) + // Remove this entry since we're going to replace it. + keepLine = false; } } - - + if (keepLine) + updatedLines.add(line); } + // Add a new auto-generated line. + updatedLines.add(ip + " " + target + " # " + AUTO_GEN_COMMENT); + // Write all lines to the file. + StringBuilder contents = new StringBuilder(); + for (String line : updatedLines) + contents.append(line) + .append(System.lineSeparator()); + Files.write(path, contents.toString().getBytes()); } } diff --git a/src/com/luca0n/dual/common/UnknownFetchTypeException.java b/src/com/luca0n/dual/common/UnknownFetchTypeException.java new file mode 100644 index 0000000..801361d --- /dev/null +++ b/src/com/luca0n/dual/common/UnknownFetchTypeException.java @@ -0,0 +1,32 @@ +/* +Dual: a simple manager for custom hostnames + +Copyright (C) 2021 luca0N! + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . + +Contact me by e-mail via . +*/ + +package com.luca0n.dual.common; + +import java.lang.Exception; + +public class UnknownFetchTypeException extends Exception { + public UnknownFetchTypeException(){ + super(); + } + public UnknownFetchTypeException(String message){ + super(message); + } +}