Skip to content

Commit

Permalink
Now missing configuration sections will autogenerate
Browse files Browse the repository at this point in the history
  • Loading branch information
xmamo committed Nov 28, 2016
1 parent 0349cb7 commit fa20f7a
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/main/java/mamo/vanillaVotifier/RconCommandSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/mamo/vanillaVotifier/VanillaVotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -236,6 +238,10 @@ protected boolean loadConfig() {
return true;
} catch (JSONException e) {
getLogger().printlnTranslation("s45", new SimpleEntry<String, Object>("exception", e.getMessage().replaceAll("'", "\"")));
} catch (ScannerException e) {
getLogger().printlnTranslation("s66", new SimpleEntry<String, Object>("exception", e.getMessage().replaceAll("'", "\"")));
} catch (ParserException e) {
getLogger().printlnTranslation("s66", new SimpleEntry<String, Object>("exception", e.getMessage().replaceAll("'", "\"")));
} catch (PublicKeyFileNotFoundException e) {
getLogger().printlnTranslation("s49");
} catch (PrivateKeyFileNotFoundException e) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/mamo/vanillaVotifier/VotifierServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
129 changes: 109 additions & 20 deletions src/main/java/mamo/vanillaVotifier/YamlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,57 +67,146 @@ protected void loadFromConfigFile() throws IOException, InvalidKeySpecException
}

BufferedReader reader = new BufferedReader(new FileReader(configFile));
Map<String, Object> yamlConfig = (Map<String, Object>) 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<Map>());
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<String, Object> commandSenderConfig : (List<Map<String, Object>>) yamlConfig.get("on-vote")) {
if (((String) commandSenderConfig.get("action")).equalsIgnoreCase("rcon")) {
Map<String, Object> server = (Map<String, Object>) 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<String>) commandSenderConfig.get("commands"), (HashMap<String, String>) commandSenderConfig.get("regex-replace")));
} else if (((String) commandSenderConfig.get("action")).equalsIgnoreCase("shell")) {
voteActions.add(new VoteAction(new ShellCommandSender(), (List<String>) commandSenderConfig.get("commands"), (HashMap<String, String>) 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<String, Object> yamlConfig = new HashMap<String, Object>();
HashMap yamlConfig = new HashMap();
yamlConfig.put("config-version", getConfigVersion());
yamlConfig.put("log-directory", getLogDirectory().getPath());
yamlConfig.put("server", new HashMap<String, Object>() {{
yamlConfig.put("server", new HashMap() {{
put("ip", getInetSocketAddress().getAddress().getHostName());
put("port", getInetSocketAddress().getPort());
}});
yamlConfig.put("key-pair-files", new HashMap<String, Object>() {{
yamlConfig.put("key-pair-files", new HashMap() {{
put("public", getPublicKeyFile().getPath());
put("private", getPrivateKeyFile().getPath());
}});
yamlConfig.put("on-vote", new ArrayList<HashMap<String, Object>>() {{
yamlConfig.put("on-vote", new ArrayList<Map>() {{
for (final VoteAction voteAction : getVoteActions()) {
add(new HashMap<String, Object>() {{
add(new HashMap() {{
if (voteAction.getCommandSender() instanceof RconCommandSender) {
final RconCommandSender commandSender = (RconCommandSender) voteAction.getCommandSender();
put("type", "rcon");
put("server", new HashMap<String, Object>() {{
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());
}});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/mamo/vanillaVotifier/lang/lang.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}
s65=Can't apply regex-replace: syntax error in regular-expression pattern! ${exception}
s66=Can't load config: YAML is malformed! ${exception}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
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}

0 comments on commit fa20f7a

Please sign in to comment.