Finished main Resolve FetchType implementation

This commit is contained in:
luca0N! 2021-03-21 18:38:39 -03:00
parent be75e15cda
commit b625887a56
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
3 changed files with 69 additions and 10 deletions

View File

@ -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

View File

@ -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);
// Open hosts file.
// 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<String> 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());
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
Contact me by e-mail via <luca0n [at] luca0n [dot] com>.
*/
package com.luca0n.dual.common;
import java.lang.Exception;
public class UnknownFetchTypeException extends Exception {
public UnknownFetchTypeException(){
super();
}
public UnknownFetchTypeException(String message){
super(message);
}
}