From fa20f7a67885c46eab0faaad260f07d19d574216 Mon Sep 17 00:00:00 2001 From: Matteo Morena <8678043+xmamo@users.noreply.github.com> Date: Mon, 28 Nov 2016 20:56:14 +0100 Subject: [PATCH] Now missing configuration sections will autogenerate --- .../vanillaVotifier/RconCommandSender.java | 8 +- .../mamo/vanillaVotifier/VanillaVotifier.java | 6 + .../mamo/vanillaVotifier/VotifierServer.java | 6 +- .../java/mamo/vanillaVotifier/YamlConfig.java | 129 +++++++++++++++--- .../utils/SubstitutionUtils.java | 1 - .../mamo/vanillaVotifier/lang/lang.properties | 3 +- .../vanillaVotifier/lang/lang_it.properties | 3 +- 7 files changed, 127 insertions(+), 29 deletions(-) diff --git a/src/main/java/mamo/vanillaVotifier/RconCommandSender.java b/src/main/java/mamo/vanillaVotifier/RconCommandSender.java index 04f11bd..a4f4435 100644 --- a/src/main/java/mamo/vanillaVotifier/RconCommandSender.java +++ b/src/main/java/mamo/vanillaVotifier/RconCommandSender.java @@ -58,10 +58,10 @@ public void logIn() throws IOException, InvalidRconPasswordException { public VotifierPacket sendCommand(@NotNull String command) throws IOException, InvalidRconPasswordException { synchronized (rconConnection) { try { - if (!isLoggedIn()) { - logIn(); - } - return rconConnection.sendCommand(command); + if (!isLoggedIn()) { + logIn(); + } + return rconConnection.sendCommand(command); } catch (BrokenPipeException e) { rconConnection = new RconConnection(rconConnection.getInetSocketAddress(), rconConnection.getPassword()); logIn(); diff --git a/src/main/java/mamo/vanillaVotifier/VanillaVotifier.java b/src/main/java/mamo/vanillaVotifier/VanillaVotifier.java index 2878e42..2f56b98 100644 --- a/src/main/java/mamo/vanillaVotifier/VanillaVotifier.java +++ b/src/main/java/mamo/vanillaVotifier/VanillaVotifier.java @@ -36,6 +36,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.json.JSONException; +import org.yaml.snakeyaml.parser.ParserException; +import org.yaml.snakeyaml.scanner.ScannerException; import java.io.File; import java.io.FileNotFoundException; @@ -236,6 +238,10 @@ protected boolean loadConfig() { return true; } catch (JSONException e) { getLogger().printlnTranslation("s45", new SimpleEntry("exception", e.getMessage().replaceAll("'", "\""))); + } catch (ScannerException e) { + getLogger().printlnTranslation("s66", new SimpleEntry("exception", e.getMessage().replaceAll("'", "\""))); + } catch (ParserException e) { + getLogger().printlnTranslation("s66", new SimpleEntry("exception", e.getMessage().replaceAll("'", "\""))); } catch (PublicKeyFileNotFoundException e) { getLogger().printlnTranslation("s49"); } catch (PrivateKeyFileNotFoundException e) { diff --git a/src/main/java/mamo/vanillaVotifier/VotifierServer.java b/src/main/java/mamo/vanillaVotifier/VotifierServer.java index 1831bd8..32f1199 100644 --- a/src/main/java/mamo/vanillaVotifier/VotifierServer.java +++ b/src/main/java/mamo/vanillaVotifier/VotifierServer.java @@ -25,7 +25,10 @@ import org.jetbrains.annotations.Nullable; import javax.crypto.BadPaddingException; -import java.io.*; +import java.io.BufferedInputStream; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketOptions; @@ -34,7 +37,6 @@ import java.util.AbstractMap.SimpleEntry; import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; diff --git a/src/main/java/mamo/vanillaVotifier/YamlConfig.java b/src/main/java/mamo/vanillaVotifier/YamlConfig.java index e052917..b303696 100644 --- a/src/main/java/mamo/vanillaVotifier/YamlConfig.java +++ b/src/main/java/mamo/vanillaVotifier/YamlConfig.java @@ -67,57 +67,146 @@ protected void loadFromConfigFile() throws IOException, InvalidKeySpecException } BufferedReader reader = new BufferedReader(new FileReader(configFile)); - Map yamlConfig = (Map) new Yaml().load(reader); + Map config = (Map) new Yaml().load(reader); reader.close(); - configVersion = (Integer) yamlConfig.get("config-version"); - logDirectory = new File((String) yamlConfig.get("log-directory")); + boolean save = false; + if (!config.containsKey("config-version")) { + config.put("config-version", 4); + save = true; + } + if (!config.containsKey("log-directory")) { + config.put("log-directory", 4); + save = true; + } + if (!config.containsKey("server")) { + config.put("server", new HashMap()); + save = true; + } + Map serverSubconfig = (Map) config.get("server"); + if (!serverSubconfig.containsKey("ip")) { + serverSubconfig.put("ip", "0.0.0.0"); + save = true; + } + if (!serverSubconfig.containsKey("port")) { + serverSubconfig.put("port", 8192); + save = true; + } + if (!config.containsKey("key-pair-files")) { + config.put("key-pair-files", new HashMap()); + save = true; + } + Map keyPairFilesSubconfig = (Map) config.get("key-pair-files"); + if (!keyPairFilesSubconfig.containsKey("public")) { + keyPairFilesSubconfig.put("public", "public.pem"); + save = true; + } + if (!keyPairFilesSubconfig.containsKey("private")) { + keyPairFilesSubconfig.put("private", "private.pem"); + save = true; + } + if (!config.containsKey("on-vote")) { + config.put("on-vote", new ArrayList()); + save = true; + } + List onVoteSubconfig = (List) config.get("on-vote"); + for (int i = 0; i < onVoteSubconfig.size(); i++) { + Map voteActionSubconfig = (Map) onVoteSubconfig.get(i); + if (voteActionSubconfig.containsKey("action")) { + String voteActionType = (String) voteActionSubconfig.get("action"); + if (voteActionType.equalsIgnoreCase("rcon")) { + if (!voteActionSubconfig.containsKey("server")) { + voteActionSubconfig.put("server", new HashMap()); + save = true; + } + Map voteActionServerSubconfig = (Map) voteActionSubconfig.get("server"); + if (!voteActionServerSubconfig.containsKey("ip")) { + voteActionServerSubconfig.put("ip", "0.0.0.0"); + save = true; + } + if (!voteActionServerSubconfig.containsKey("port")) { + voteActionServerSubconfig.put("port", 25575); + save = true; + } + if (!voteActionServerSubconfig.containsKey("password")) { + voteActionServerSubconfig.put("password", "password"); + save = true; + } + if (!voteActionSubconfig.containsKey("commands")) { + voteActionSubconfig.put("commands", new ArrayList()); + save = true; + } + if (!voteActionSubconfig.containsKey("regex-replace")) { + voteActionSubconfig.put("regex-replace", new HashMap()); + save = true; + } + } + if (voteActionType.equalsIgnoreCase("shell")) { + if (!voteActionSubconfig.containsKey("commands")) { + voteActionSubconfig.put("commands", new ArrayList()); + save = true; + } + if (!voteActionSubconfig.containsKey("regex-replace")) { + voteActionSubconfig.put("regex-replace", new HashMap()); + save = true; + } + } + } + } + + configVersion = (Integer) config.get("config-version"); + logDirectory = new File((String) config.get("log-directory")); if (!logDirectory.exists()) { logDirectory.mkdirs(); } logFile = new File(logDirectory, TimestampUtils.getTimestamp() + ".log"); - inetSocketAddress = new InetSocketAddress((String) ((Map) yamlConfig.get("server")).get("ip"), (Integer) ((Map) yamlConfig.get("server")).get("port")); - publicKeyFile = new File((String) ((Map) yamlConfig.get("key-pair-files")).get("public")); - privateKeyFile = new File((String) ((Map) yamlConfig.get("key-pair-files")).get("private")); + inetSocketAddress = new InetSocketAddress((String) serverSubconfig.get("ip"), (Integer) serverSubconfig.get("port")); + publicKeyFile = new File((String) keyPairFilesSubconfig.get("public")); + privateKeyFile = new File((String) keyPairFilesSubconfig.get("private")); loadKeyPair(); voteActions.clear(); - for (Map commandSenderConfig : (List>) yamlConfig.get("on-vote")) { - if (((String) commandSenderConfig.get("action")).equalsIgnoreCase("rcon")) { - Map server = (Map) commandSenderConfig.get("server"); - voteActions.add(new VoteAction(new RconCommandSender(new RconConnection(new InetSocketAddress((String) server.get("ip"), (Integer) server.get("port")), (String) server.get("password"))), (List) commandSenderConfig.get("commands"), (HashMap) commandSenderConfig.get("regex-replace"))); - } else if (((String) commandSenderConfig.get("action")).equalsIgnoreCase("shell")) { - voteActions.add(new VoteAction(new ShellCommandSender(), (List) commandSenderConfig.get("commands"), (HashMap) commandSenderConfig.get("regex-replace"))); + for (int i = 0; i < onVoteSubconfig.size(); i++) { + Map voteActionSubconfig = (Map) onVoteSubconfig.get(i); + if (((String) voteActionSubconfig.get("action")).equalsIgnoreCase("rcon")) { + Map voteActionServerSubconfig = (Map) voteActionSubconfig.get("server"); + voteActions.add(new VoteAction(new RconCommandSender(new RconConnection(new InetSocketAddress((String) voteActionServerSubconfig.get("ip"), (Integer) voteActionServerSubconfig.get("port")), (String) voteActionServerSubconfig.get("password"))), (List) voteActionSubconfig.get("commands"), (HashMap) voteActionSubconfig.get("regex-replace"))); + } else if (((String) voteActionSubconfig.get("action")).equalsIgnoreCase("shell")) { + voteActions.add(new VoteAction(new ShellCommandSender(), (List) voteActionSubconfig.get("commands"), (HashMap) voteActionSubconfig.get("regex-replace"))); } } + + if (save) { + save(); + } } @Override public void save() throws IOException { - HashMap yamlConfig = new HashMap(); + HashMap yamlConfig = new HashMap(); yamlConfig.put("config-version", getConfigVersion()); yamlConfig.put("log-directory", getLogDirectory().getPath()); - yamlConfig.put("server", new HashMap() {{ + yamlConfig.put("server", new HashMap() {{ put("ip", getInetSocketAddress().getAddress().getHostName()); put("port", getInetSocketAddress().getPort()); }}); - yamlConfig.put("key-pair-files", new HashMap() {{ + yamlConfig.put("key-pair-files", new HashMap() {{ put("public", getPublicKeyFile().getPath()); put("private", getPrivateKeyFile().getPath()); }}); - yamlConfig.put("on-vote", new ArrayList>() {{ + yamlConfig.put("on-vote", new ArrayList() {{ for (final VoteAction voteAction : getVoteActions()) { - add(new HashMap() {{ + add(new HashMap() {{ if (voteAction.getCommandSender() instanceof RconCommandSender) { final RconCommandSender commandSender = (RconCommandSender) voteAction.getCommandSender(); - put("type", "rcon"); - put("server", new HashMap() {{ + put("action", "rcon"); + put("server", new HashMap() {{ put("ip", commandSender.getRconConnection().getInetSocketAddress().getAddress().getHostName()); put("port", commandSender.getRconConnection().getInetSocketAddress().getPort()); put("password", commandSender.getRconConnection().getPassword()); }}); } if (voteAction.getCommandSender() instanceof ShellCommandSender) { - put("type", "shell"); + put("action", "shell"); } put("commands", voteAction.getCommands()); }}); diff --git a/src/main/java/mamo/vanillaVotifier/utils/SubstitutionUtils.java b/src/main/java/mamo/vanillaVotifier/utils/SubstitutionUtils.java index ce527ce..10a985a 100644 --- a/src/main/java/mamo/vanillaVotifier/utils/SubstitutionUtils.java +++ b/src/main/java/mamo/vanillaVotifier/utils/SubstitutionUtils.java @@ -19,7 +19,6 @@ import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.lang3.text.StrSubstitutor; -import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/src/main/resources/mamo/vanillaVotifier/lang/lang.properties b/src/main/resources/mamo/vanillaVotifier/lang/lang.properties index 0ce92ad..46e5ffc 100644 --- a/src/main/resources/mamo/vanillaVotifier/lang/lang.properties +++ b/src/main/resources/mamo/vanillaVotifier/lang/lang.properties @@ -70,4 +70,5 @@ s61=Can't send shell command: invalid command! s62=Unexpected exception while sending shell command: ${exception} s63=Wrong arguments! Correct usage: showkey <(public|private)> s64=${ip}:${port}: connection lost! -s65=Can't apply regex-replace: syntax error in regular-expression pattern! ${exception} \ No newline at end of file +s65=Can't apply regex-replace: syntax error in regular-expression pattern! ${exception} +s66=Can't load config: YAML is malformed! ${exception} \ No newline at end of file diff --git a/src/main/resources/mamo/vanillaVotifier/lang/lang_it.properties b/src/main/resources/mamo/vanillaVotifier/lang/lang_it.properties index 8bbe367..bce4beb 100644 --- a/src/main/resources/mamo/vanillaVotifier/lang/lang_it.properties +++ b/src/main/resources/mamo/vanillaVotifier/lang/lang_it.properties @@ -70,4 +70,5 @@ s61=Impossibile inviare il comando shell: comando non valido! s62=Errore imprevisto nell'invio del comando shell: ${exception} s63=Argomenti invalidi! Uso corretto: showkey <(public|private)> s64=${ip}:${port}: connessione scaduta! -s65=Impossibile applicare regex-replace: errore di sintassi nel pattern dell'espressione regolare! ${exception} \ No newline at end of file +s65=Impossibile applicare regex-replace: errore di sintassi nel pattern dell'espressione regolare! ${exception} +s66=Impossibile caricare la configurazione: il file YAML \u00E8 mal formattato! ${exception} \ No newline at end of file