159 lines
4.8 KiB
Java
159 lines
4.8 KiB
Java
/*
|
|
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.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.FileSystems;
|
|
import java.nio.file.Path;
|
|
|
|
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.config.Config;
|
|
import com.luca0n.dual.config.ConfigUtil;
|
|
|
|
/**
|
|
* @version 1.0
|
|
* @since 2021-03-21
|
|
*/
|
|
public class Dual {
|
|
public static final String NAME = "Dual";
|
|
public static final String VERSION = "1.0";
|
|
private static final String AUTO_GEN_COMMENT = "This entry was auto-generated by " + NAME + ".";
|
|
public static final int VERSION_CODE = 1;
|
|
|
|
private static final String[] SUPPORTED_OPERATING_SYSTEMS = {
|
|
"Linux",
|
|
"Windows"
|
|
};
|
|
|
|
public static void main(String[] args){
|
|
try {
|
|
run();
|
|
} catch (Exception e){
|
|
e.printStackTrace();
|
|
System.exit(1);
|
|
}
|
|
}
|
|
public static void run() throws ConfigSyntaxException,
|
|
NoSuchNamespaceException, NoSuchKeyException, IOException,
|
|
UnsupportedOperatingSystemException {
|
|
// Read configuration file
|
|
Config c = ConfigUtil.fromFile(ConfigUtil.DEFAULT_CONFIG);
|
|
|
|
Config.Namespace n = c.getNamespace("Dual");
|
|
|
|
if (n == null)
|
|
throw new NoSuchNamespaceException("Could not find required namespace Dual in the default configuration file.");
|
|
|
|
String type = n.getValue("FetchType"),
|
|
source = n.getValue("Source"),
|
|
target = n.getValue("Target"),
|
|
hostsPath = n.getValue("hostsPath");
|
|
|
|
// Check for null values.
|
|
if (source == null)
|
|
throw new NoSuchKeyException("Could not find required key Dual.Source in the default configuration file.");
|
|
else if (target == null)
|
|
throw new NoSuchKeyException("Could not find required key Dual.Target in the default configuration file.");
|
|
|
|
System.out.printf("%s: All tests passed.\n", NAME);
|
|
|
|
if (type == null){
|
|
type = "Resolve";
|
|
System.out.printf("%s: WARNING: no FetchType specified. Assuming Resolve FetchType...\n", NAME);
|
|
}
|
|
|
|
System.out.printf("%s: Using %s FetchType and %s Source.\n", NAME, type, source);
|
|
System.out.printf("%s: Using %s hostname.\n", NAME, target);
|
|
|
|
// Check OS.
|
|
String os = System.getProperty("os.name");
|
|
boolean supportedOs = false;
|
|
for (String sos : SUPPORTED_OPERATING_SYSTEMS)
|
|
if (os.contains(sos)){
|
|
supportedOs = true;
|
|
break;
|
|
}
|
|
|
|
if (!supportedOs && hostsPath == null){
|
|
StringBuilder msg = new StringBuilder();
|
|
msg.append("Unsupported operating system: ")
|
|
.append(os)
|
|
.append(". ")
|
|
.append("This program must be run under ");
|
|
|
|
for (int x = 0; x < SUPPORTED_OPERATING_SYSTEMS.length; x++){
|
|
if (x == SUPPORTED_OPERATING_SYSTEMS.length - 1)
|
|
msg.append(" or ")
|
|
.append(SUPPORTED_OPERATING_SYSTEMS[x])
|
|
.append(". ");
|
|
else if (x == 0)
|
|
msg.append(SUPPORTED_OPERATING_SYSTEMS[x]);
|
|
else
|
|
msg.append(", ")
|
|
.append(SUPPORTED_OPERATING_SYSTEMS[x]);
|
|
}
|
|
|
|
msg.append("If you want to override this system requirement, please provide a path to the hosts file on your operating system.");
|
|
|
|
throw new UnsupportedOperatingSystemException(msg.toString());
|
|
}
|
|
|
|
System.out.printf(hostsPath == null ? "%s: using %s settings.\n" : "%s: using custom settings for %s.\n", NAME, os);
|
|
|
|
// Open hosts file.
|
|
|
|
if (hostsPath == null){
|
|
boolean isWindows = os.contains(SUPPORTED_OPERATING_SYSTEMS[1]);
|
|
hostsPath = isWindows ? "C:\\WINDOWS\\System32\\drivers\\etc\\hosts" : "/etc/hosts";
|
|
}
|
|
|
|
System.out.printf("%s: opening %s...\n", NAME, hostsPath);
|
|
|
|
Path path = FileSystems.getDefault().getPath(hostsPath);
|
|
byte[] fileBytes = Files.readAllBytes(path);
|
|
|
|
String[] lines = new String(fileBytes).split(System.lineSeparator());
|
|
|
|
for (String line : lines){
|
|
// Check if this line is a comment.
|
|
if (line.startsWith("#"))
|
|
// Ignore this line.
|
|
continue;
|
|
|
|
String[] contents = line.split("#")[0];
|
|
// 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.
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
}
|