Finished main Resolve FetchType implementation
This commit is contained in:
parent
be75e15cda
commit
b625887a56
|
@ -39,7 +39,7 @@
|
||||||
#
|
#
|
||||||
# The following values are allowed for this option:
|
# The following values are allowed for this option:
|
||||||
# - Resolve: gets the target IP address by resolving the provided address.
|
# - 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.
|
# address that outputs the desired IP address in plain text.
|
||||||
FetchType=Resolve
|
FetchType=Resolve
|
||||||
|
|
||||||
|
|
|
@ -24,11 +24,15 @@ import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.FileSystems;
|
import java.nio.file.FileSystems;
|
||||||
import java.nio.file.Path;
|
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.ConfigSyntaxException;
|
||||||
import com.luca0n.dual.common.NoSuchNamespaceException;
|
import com.luca0n.dual.common.NoSuchNamespaceException;
|
||||||
import com.luca0n.dual.common.NoSuchKeyException;
|
import com.luca0n.dual.common.NoSuchKeyException;
|
||||||
import com.luca0n.dual.common.UnsupportedOperatingSystemException;
|
import com.luca0n.dual.common.UnsupportedOperatingSystemException;
|
||||||
|
import com.luca0n.dual.common.UnknownFetchTypeException;
|
||||||
import com.luca0n.dual.config.Config;
|
import com.luca0n.dual.config.Config;
|
||||||
import com.luca0n.dual.config.ConfigUtil;
|
import com.luca0n.dual.config.ConfigUtil;
|
||||||
|
|
||||||
|
@ -57,7 +61,7 @@ public class Dual {
|
||||||
}
|
}
|
||||||
public static void run() throws ConfigSyntaxException,
|
public static void run() throws ConfigSyntaxException,
|
||||||
NoSuchNamespaceException, NoSuchKeyException, IOException,
|
NoSuchNamespaceException, NoSuchKeyException, IOException,
|
||||||
UnsupportedOperatingSystemException {
|
UnsupportedOperatingSystemException, UnknownFetchTypeException {
|
||||||
// Read configuration file
|
// Read configuration file
|
||||||
Config c = ConfigUtil.fromFile(ConfigUtil.DEFAULT_CONFIG);
|
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);
|
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.
|
// Open hosts file.
|
||||||
|
// TODO: Use buffered I/O methods.
|
||||||
if (hostsPath == null){
|
if (hostsPath == null){
|
||||||
boolean isWindows = os.contains(SUPPORTED_OPERATING_SYSTEMS[1]);
|
boolean isWindows = os.contains(SUPPORTED_OPERATING_SYSTEMS[1]);
|
||||||
hostsPath = isWindows ? "C:\\WINDOWS\\System32\\drivers\\etc\\hosts" : "/etc/hosts";
|
hostsPath = isWindows ? "C:\\WINDOWS\\System32\\drivers\\etc\\hosts" : "/etc/hosts";
|
||||||
|
@ -135,25 +148,39 @@ public class Dual {
|
||||||
byte[] fileBytes = Files.readAllBytes(path);
|
byte[] fileBytes = Files.readAllBytes(path);
|
||||||
|
|
||||||
String[] lines = new String(fileBytes).split(System.lineSeparator());
|
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.
|
// Check if this line is a comment.
|
||||||
if (line.startsWith("#"))
|
if (line.startsWith("#"))
|
||||||
// Ignore this line.
|
// Ignore this line.
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
String[] contents = line.split("#")[0];
|
String[] contents = line.split("#");
|
||||||
// Check if this line was auto-generated by this program.
|
// Check if this line was auto-generated by this program.
|
||||||
if (contents.length == 2){
|
if (contents.length == 2){
|
||||||
// Check line comment.
|
// Check line comment.
|
||||||
if (contents[1].equals(AUTO_GEN_COMMENT)){
|
if (contents[1].substring(1).equals(AUTO_GEN_COMMENT)){
|
||||||
// Remove this entry if we're going to replace it.
|
// 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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue