diff options
author | SoniEx2 <endermoneymod@gmail.com> | 2022-07-25 21:38:13 -0300 |
---|---|---|
committer | SoniEx2 <endermoneymod@gmail.com> | 2022-07-25 21:56:20 -0300 |
commit | 97c48aa55abca34478f470fa99c3a150c6629f16 (patch) | |
tree | 4cb27f57335c19820f8c55047b9df13a275bea5d /src/main/java/ganarchy/friendcode/util | |
parent | 354df6d333ffb7b69e92117406c8ce8d61ea09e0 (diff) |
Fix SAM bridge security
Diffstat (limited to 'src/main/java/ganarchy/friendcode/util')
-rw-r--r-- | src/main/java/ganarchy/friendcode/util/ConfigUtil.java | 106 | ||||
-rw-r--r-- | src/main/java/ganarchy/friendcode/util/KeyUtil.java | 8 |
2 files changed, 114 insertions, 0 deletions
diff --git a/src/main/java/ganarchy/friendcode/util/ConfigUtil.java b/src/main/java/ganarchy/friendcode/util/ConfigUtil.java new file mode 100644 index 0000000..b2d6af2 --- /dev/null +++ b/src/main/java/ganarchy/friendcode/util/ConfigUtil.java @@ -0,0 +1,106 @@ +package ganarchy.friendcode.util; + +import ganarchy.friendcode.FriendCode; + +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +import static org.apache.commons.lang3.SystemUtils.USER_HOME; +import static org.apache.commons.lang3.SystemUtils.getEnvironmentVariable; + +/** + * Config utils. + */ +public class ConfigUtil { + /** + * The message stored in the config. Could probably use some improvement. + */ + private static final String CONFIG_MESSAGE = + "This is the friendcode config file." + + " It's created when you open your world to a friend code."; + /** + * The (cached) global friendcode config dir. + */ + private static File confdir; + + /** + * Creates and returns the global friendcode config dir. + */ + public static File getGlobalConfig() { + if (confdir != null) { + return confdir; + } + var configHome = getEnvironmentVariable("XDG_CONFIG_HOME", ""); + if (!configHome.isEmpty()) { + confdir = Path.of(configHome, "mc_friendcode").toFile(); + } else { + confdir = Path.of(USER_HOME, ".config", "mc_friendcode").toFile(); + } + confdir.mkdirs(); + return confdir; + } + + /** + * Returns the path to the global friendcode config file. + */ + public static Path getGlobalConfigFilePath() { + return getGlobalConfig().toPath().resolve("config.properties"); + } + + /** + * Retrieves the settings from the global config file and stores them in + * the given object. + * + * @param properties Where to store the read config. + * @return Whether reading was successful. + */ + public static boolean getSettings(Properties properties) { + try { + var inputStream = Files.newInputStream( + getGlobalConfigFilePath() + ); + var reader = new InputStreamReader( + inputStream, StandardCharsets.UTF_8 + ); + properties.load(reader); + return true; + } catch (IOException e) { + return false; + } + } + + /** + * Updates the global config file with the settings in the given object. + * + * @param properties The settings to add to the config. + * @return Whether writing was successful. + */ + public static boolean updateSettings(Properties properties) { + var prop = new Properties(); + if (!getSettings(prop)) { + FriendCode.LOGGER.warn( + "Couldn't read global config." + + " If it doesn't exist, it will be created." + ); + } + prop.putAll(properties); + try { + var outputStream = Files.newOutputStream( + getGlobalConfigFilePath() + ); + var writer = new OutputStreamWriter( + outputStream, StandardCharsets.UTF_8 + ); + prop.store(writer, CONFIG_MESSAGE); + return true; + } catch (IOException e) { + return false; + } + } +} diff --git a/src/main/java/ganarchy/friendcode/util/KeyUtil.java b/src/main/java/ganarchy/friendcode/util/KeyUtil.java index cc6396c..e8e07e5 100644 --- a/src/main/java/ganarchy/friendcode/util/KeyUtil.java +++ b/src/main/java/ganarchy/friendcode/util/KeyUtil.java @@ -1,7 +1,15 @@ package ganarchy.friendcode.util; +import java.nio.file.Path; + /** * Helper to deal with private keys. */ public class KeyUtil { + public static String readKeyFile(Path keyFile) { + return null; + } + public static boolean writeKeyFile(Path keyFile, String key) { + return false; + } } |