From c41bb8c74cf10542980ea7d7664624f7858c02a0 Mon Sep 17 00:00:00 2001 From: Matteo Morena <8678043+xmamo@users.noreply.github.com> Date: Wed, 24 Feb 2016 22:59:25 +0100 Subject: [PATCH] Add Italian translations, fix #9 and #10 --- .gitignore | 3 +- pom.xml | 222 +++--- .../vanillaVotifier/impl/JsonConfig.java | 635 +++++++++--------- .../vanillaVotifier/impl/RconConnection.java | 237 +++---- .../vanillaVotifier/impl/VanillaVotifier.java | 578 ++++++++-------- .../impl/VanillaVotifierLogger.java | 212 +++--- .../impl/VanillaVotifierServer.java | 389 ++++++----- .../impl/VanillaVotifierServerListener.java | 248 +++---- .../vanillaVotifier/impl/lang/help.txt | 26 +- .../vanillaVotifier/impl/lang/help_it.txt | 17 + .../vanillaVotifier/impl/lang/lang.properties | 140 ++-- .../impl/lang/lang_it.properties | 67 ++ .../impl/lang/license-notice_it.txt | 7 + .../vanillaVotifier/impl/lang/manual.txt | 132 ++-- 14 files changed, 1513 insertions(+), 1400 deletions(-) create mode 100644 src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/help_it.txt create mode 100644 src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/lang_it.properties create mode 100644 src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/license-notice_it.txt diff --git a/.gitignore b/.gitignore index 4e438ca..a6f89c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -/target/ -/src/test/ \ No newline at end of file +/target/ \ No newline at end of file diff --git a/pom.xml b/pom.xml index e7826e4..476f22d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,112 +1,112 @@ - - - - 4.0.0 - co.virtualdragon - VanillaVotifier - 3.2 - VanillaVotifier - jar - - - org.apache.commons - commons-lang3 - 3.4 - - - org.json - json - 20140107 - - - org.bouncycastle - bcprov-ext-jdk15on - 1.52 - - - - UTF-8 - 1.6 - 1.6 - - - - - src/main/resources - - ** - - - - src/main/resources - - co/virtualdragon/vanillaVotifier/impl/lang/info.txt - - true - - - - - org.apache.maven.plugins - maven-jar-plugin - 2.6 - - - true - - co.virtualdragon.vanillaVotifier.impl.VanillaVotifier - - - VirtualDragon - - - - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.1 - - - package - - shade - - - false - true - - - *:* - - META-INF/** - - - - - - co.virtualdragon.vanillaVotifier.impl.VanillaVotifier - - - - - - - - + + + + 4.0.0 + co.virtualdragon + VanillaVotifier + 3.3 + VanillaVotifier + jar + + + org.apache.commons + commons-lang3 + 3.4 + + + org.json + json + 20140107 + + + org.bouncycastle + bcprov-ext-jdk15on + 1.52 + + + + UTF-8 + 1.6 + 1.6 + + + + + src/main/resources + + ** + + + + src/main/resources + + co/virtualdragon/vanillaVotifier/impl/lang/info.txt + + true + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + + + true + + co.virtualdragon.vanillaVotifier.impl.VanillaVotifier + + + VirtualDragon + + + + + + org.apache.maven.plugins + maven-shade-plugin + 2.4.1 + + + package + + shade + + + false + true + + + *:* + + META-INF/** + + + + + + co.virtualdragon.vanillaVotifier.impl.VanillaVotifier + + + + + + + + \ No newline at end of file diff --git a/src/main/java/co/virtualdragon/vanillaVotifier/impl/JsonConfig.java b/src/main/java/co/virtualdragon/vanillaVotifier/impl/JsonConfig.java index 87a1138..1bb850a 100644 --- a/src/main/java/co/virtualdragon/vanillaVotifier/impl/JsonConfig.java +++ b/src/main/java/co/virtualdragon/vanillaVotifier/impl/JsonConfig.java @@ -1,309 +1,326 @@ -/* - * Copyright (C) 2015 VirtualDragon - * - * 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 . - */ -package co.virtualdragon.vanillaVotifier.impl; - -import co.virtualdragon.vanillaVotifier.Config; -import co.virtualdragon.vanillaVotifier.exception.InvalidPrivateKeyFileException; -import co.virtualdragon.vanillaVotifier.exception.InvalidPublicKeyFileException; -import co.virtualdragon.vanillaVotifier.exception.PrivateKeyFileNotFoundException; -import co.virtualdragon.vanillaVotifier.exception.PublicKeyFileNotFoundException; -import co.virtualdragon.vanillaVotifier.util.JsonUtils; -import co.virtualdragon.vanillaVotifier.util.RsaUtils; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.security.KeyPair; -import java.security.spec.InvalidKeySpecException; -import java.util.ArrayList; -import java.util.List; -import org.bouncycastle.util.io.pem.PemObject; -import org.bouncycastle.util.io.pem.PemReader; -import org.bouncycastle.util.io.pem.PemWriter; -import org.json.JSONArray; -import org.json.JSONObject; -import org.json.JSONTokener; - -public class JsonConfig implements Config { - - private final File configFile; - - private boolean loaded; - private int configVersion; - private File logFile; - private InetSocketAddress inetSocketAddress; - private File publicKeyFile; - private File privateKeyFile; - private KeyPair keyPair; - private ArrayList rconConfigs; - - public JsonConfig(File configFile) { - this.configFile = configFile; - } - - @Override - public synchronized void load() throws IOException, InvalidKeySpecException { - if (!configFile.exists()) { - BufferedInputStream in = new BufferedInputStream(JsonConfig.class.getResourceAsStream("config.json")); - StringBuilder stringBuilder = new StringBuilder(); - int i; - while ((i = in.read()) != -1) { - stringBuilder.append((char) i); - } - BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(configFile)); - for (char c : stringBuilder.toString().replaceAll("\\R", System.lineSeparator()).toCharArray()) { - out.write((int) c); - } - out.flush(); - out.close(); - } - BufferedInputStream in = new BufferedInputStream(JsonConfig.class.getResourceAsStream("config.json")); - JSONObject defaultConfig = new JSONObject(new JSONTokener(in)); - in.close(); - JSONObject config = new JSONObject(new JSONTokener(new BufferedInputStream(new FileInputStream(configFile)))); - boolean save = JsonUtils.merge(defaultConfig, config); - configVersion = config.getInt("config-version"); - if (configVersion == 2) { - v2ToV3(config); - configVersion = 3; - save = true; - } - logFile = new File(config.getString("log-file")); - inetSocketAddress = new InetSocketAddress(config.getString("ip"), config.getInt("port")); - publicKeyFile = new File(config.getJSONObject("key-pair-files").getString("public")); - privateKeyFile = new File(config.getJSONObject("key-pair-files").getString("private")); - if (!publicKeyFile.exists() && !privateKeyFile.exists()) { - KeyPair keyPair = RsaUtils.genKeyPair(); - PemWriter publicPemWriter = new PemWriter(new BufferedWriter(new FileWriter(publicKeyFile))); - publicPemWriter.writeObject(new PemObject("PUBLIC KEY", keyPair.getPublic().getEncoded())); - publicPemWriter.flush(); - publicPemWriter.close(); - PemWriter privatePemWriter = new PemWriter(new BufferedWriter(new FileWriter(privateKeyFile))); - privatePemWriter.writeObject(new PemObject("RSA PRIVATE KEY", keyPair.getPrivate().getEncoded())); - privatePemWriter.flush(); - privatePemWriter.close(); - } - if (!publicKeyFile.exists()) { - throw new PublicKeyFileNotFoundException(); - } - if (!privateKeyFile.exists()) { - throw new PrivateKeyFileNotFoundException(); - } - PemReader publicKeyPemReader = new PemReader(new BufferedReader(new FileReader(publicKeyFile))); - PemReader privateKeyPemReader = new PemReader(new BufferedReader(new FileReader(privateKeyFile))); - PemObject publicPemObject = publicKeyPemReader.readPemObject(); - if (publicPemObject == null) { - throw new InvalidPublicKeyFileException(); - } - PemObject privatePemObject = privateKeyPemReader.readPemObject(); - if (privatePemObject == null) { - throw new InvalidPrivateKeyFileException(); - } - keyPair = new KeyPair(RsaUtils.bytesToPublicKey(publicPemObject.getContent()), RsaUtils.bytesToPrivateKey(privatePemObject.getContent())); - publicKeyPemReader.close(); - privateKeyPemReader.close(); - rconConfigs = new ArrayList(); - for (int i = 0; i < config.getJSONArray("rcon-list").length(); i++) { - JSONObject jsonObject = config.getJSONArray("rcon-list").getJSONObject(i); - VanillaVotifierRconConfig rconConfig = new VanillaVotifierRconConfig(new InetSocketAddress(jsonObject.getString("ip"), jsonObject.getInt("port")), jsonObject.getString("password")); - for (int j = 0; j < jsonObject.getJSONArray("commands").length(); j++) { - rconConfig.getCommands().add(jsonObject.getJSONArray("commands").getString(j)); - } - rconConfigs.add(rconConfig); - } - loaded = true; - if (save) { - save(); - } - } - - private void v2ToV3(JSONObject jsonObject) { - if (!jsonObject.has("commands")) { - jsonObject.put("commands", new JSONArray()); - } - if (!jsonObject.has("rcon-list")) { - jsonObject.put("rcon-list", new JSONArray() { - { - put(new JSONObject()); - } - }); - } - jsonObject.getJSONArray("rcon-list").put(jsonObject.get("rcon-list")); - jsonObject.getJSONArray("rcon-list").getJSONObject(0).put("commands", jsonObject.get("commands")); - jsonObject.remove("commands"); - jsonObject.put("config-version", 3); - } - - @Override - public synchronized boolean isLoaded() { - return loaded; - } - - @Override - public synchronized int getConfigVersion() { - checkState(); - return configVersion; - } - - @Override - public File getLogFile() { - checkState(); - return logFile; - } - - @Override - public void setLogFile(File location) { - checkState(); - if (location == null) { - location = new File("log.log"); - } - logFile = location; - } - - @Override - public synchronized InetSocketAddress getInetSocketAddress() { - checkState(); - return inetSocketAddress; - } - - @Override - public synchronized void setInetSocketAddress(InetSocketAddress inetSocketAddress) { - checkState(); - if (inetSocketAddress == null) { - inetSocketAddress = new InetSocketAddress("127.0.0.1", 8192); - } - this.inetSocketAddress = inetSocketAddress; - } - - @Override - public synchronized File getPublicKeyFile() { - checkState(); - return publicKeyFile; - } - - @Override - public synchronized void setPublicKeyFile(File location) { - checkState(); - if (location == null) { - location = new File("public.pem"); - } - publicKeyFile = location; - } - - @Override - public synchronized File getPrivateKeyFile() { - checkState(); - return privateKeyFile; - } - - @Override - public synchronized void setPrivateKeyFile(File location) { - checkState(); - if (location == null) { - location = new File("private.pem"); - } - privateKeyFile = location; - } - - @Override - public synchronized KeyPair getKeyPair() { - checkState(); - return keyPair; - } - - @Override - public synchronized void setKeyPair(KeyPair keyPair) { - checkState(); - if (keyPair == null) { - keyPair = RsaUtils.genKeyPair(2048); - } - this.keyPair = keyPair; - } - - @Override - public synchronized void genKeyPair() { - genKeyPair(2048); - } - - @Override - public synchronized void genKeyPair(int keySize) { - setKeyPair(RsaUtils.genKeyPair(keySize)); - } - - @Override - public synchronized List getRconConfigs() { - return rconConfigs; - } - - @Override - public synchronized void save() throws IOException { - checkState(); - JSONObject config = new JSONObject(); - config.put("config-version", getConfigVersion()); - config.put("log-file", getLogFile().getPath()); - config.put("ip", getInetSocketAddress().getHostString()); - config.put("port", getInetSocketAddress().getPort()); - config.put("key-pair-files", new JSONObject() { - { - put("public", getPublicKeyFile().getPath()); - put("private", getPrivateKeyFile().getPath()); - } - }); - config.put("rcon-list", new JSONArray() { - { - for (final RconConfig rconConfig : rconConfigs) { - put(new JSONObject() { - { - put("ip", rconConfig.getInetSocketAddress().getHostString()); - put("port", rconConfig.getInetSocketAddress().getPort()); - put("password", rconConfig.getPassword()); - put("commands", rconConfig.getCommands()); - } - }); - } - } - }); - String configString = JsonUtils.jsonToPrettyString(config); - BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(configFile)); - for (char c : configString.toCharArray()) { - out.write(c); - } - out.flush(); - out.close(); - PemWriter publicPemWriter = new PemWriter(new BufferedWriter(new FileWriter(getPublicKeyFile()))); - publicPemWriter.writeObject(new PemObject("PUBLIC KEY", getKeyPair().getPublic().getEncoded())); - publicPemWriter.flush(); - publicPemWriter.close(); - PemWriter privatePemWriter = new PemWriter(new BufferedWriter(new FileWriter(getPrivateKeyFile()))); - privatePemWriter.writeObject(new PemObject("RSA PRIVATE KEY", getKeyPair().getPrivate().getEncoded())); - privatePemWriter.flush(); - privatePemWriter.close(); - } - - private void checkState() { - if (!isLoaded()) { - throw new IllegalStateException("Config isn't loaded yet!"); - } - } -} +/* + * Copyright (C) 2015 VirtualDragon + * + * 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 . + */ +package co.virtualdragon.vanillaVotifier.impl; + +import co.virtualdragon.vanillaVotifier.Config; +import co.virtualdragon.vanillaVotifier.exception.InvalidPrivateKeyFileException; +import co.virtualdragon.vanillaVotifier.exception.InvalidPublicKeyFileException; +import co.virtualdragon.vanillaVotifier.exception.PrivateKeyFileNotFoundException; +import co.virtualdragon.vanillaVotifier.exception.PublicKeyFileNotFoundException; +import co.virtualdragon.vanillaVotifier.util.JsonUtils; +import co.virtualdragon.vanillaVotifier.util.RsaUtils; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.net.InetSocketAddress; +import java.security.KeyPair; +import java.security.spec.InvalidKeySpecException; +import java.util.ArrayList; +import java.util.List; +import org.bouncycastle.util.io.pem.PemObject; +import org.bouncycastle.util.io.pem.PemReader; +import org.bouncycastle.util.io.pem.PemWriter; +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONTokener; + +public class JsonConfig implements Config { + + private final File configFile; + + private boolean loaded; + private int configVersion; + private File logFile; + private InetSocketAddress inetSocketAddress; + private File publicKeyFile; + private File privateKeyFile; + private KeyPair keyPair; + private ArrayList rconConfigs; + + public JsonConfig(File configFile) { + this.configFile = configFile; + } + + @Override + public synchronized void load() throws IOException, InvalidKeySpecException { + if (!configFile.exists()) { + BufferedInputStream in = new BufferedInputStream(JsonConfig.class.getResourceAsStream("config.json")); + StringBuilder stringBuilder = new StringBuilder(); + int i; + while ((i = in.read()) != -1) { + stringBuilder.append((char) i); + } + BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(configFile)); + for (char c : stringBuilder.toString().replaceAll("\\R", System.lineSeparator()).toCharArray()) { + out.write((int) c); + } + out.flush(); + out.close(); + } + BufferedInputStream in = new BufferedInputStream(JsonConfig.class.getResourceAsStream("config.json")); + JSONObject defaultConfig = new JSONObject(new JSONTokener(in)); + in.close(); + JSONObject config = new JSONObject(new JSONTokener(new BufferedInputStream(new FileInputStream(configFile)))); + boolean save = JsonUtils.merge(defaultConfig, config); + configVersion = config.getInt("config-version"); + if (configVersion == 2) { + v2ToV3(config); + configVersion = 3; + save = true; + } + logFile = new File(config.getString("log-file")); + inetSocketAddress = new InetSocketAddress(config.getString("ip"), config.getInt("port")); + publicKeyFile = new File(config.getJSONObject("key-pair-files").getString("public")); + privateKeyFile = new File(config.getJSONObject("key-pair-files").getString("private")); + if (!publicKeyFile.exists() && !privateKeyFile.exists()) { + KeyPair keyPair = RsaUtils.genKeyPair(); + PemWriter publicPemWriter = new PemWriter(new BufferedWriter(new FileWriter(publicKeyFile))); + publicPemWriter.writeObject(new PemObject("PUBLIC KEY", keyPair.getPublic().getEncoded())); + publicPemWriter.flush(); + publicPemWriter.close(); + PemWriter privatePemWriter = new PemWriter(new BufferedWriter(new FileWriter(privateKeyFile))); + privatePemWriter.writeObject(new PemObject("RSA PRIVATE KEY", keyPair.getPrivate().getEncoded())); + privatePemWriter.flush(); + privatePemWriter.close(); + } + if (!publicKeyFile.exists()) { + throw new PublicKeyFileNotFoundException(); + } + if (!privateKeyFile.exists()) { + throw new PrivateKeyFileNotFoundException(); + } + PemReader publicKeyPemReader = new PemReader(new BufferedReader(new FileReader(publicKeyFile))); + PemReader privateKeyPemReader = new PemReader(new BufferedReader(new FileReader(privateKeyFile))); + PemObject publicPemObject = publicKeyPemReader.readPemObject(); + if (publicPemObject == null) { + throw new InvalidPublicKeyFileException(); + } + PemObject privatePemObject = privateKeyPemReader.readPemObject(); + if (privatePemObject == null) { + throw new InvalidPrivateKeyFileException(); + } + keyPair = new KeyPair(RsaUtils.bytesToPublicKey(publicPemObject.getContent()), RsaUtils.bytesToPrivateKey(privatePemObject.getContent())); + publicKeyPemReader.close(); + privateKeyPemReader.close(); + rconConfigs = new ArrayList(); + for (int i = 0; i < config.getJSONArray("rcon-list").length(); i++) { + JSONObject jsonObject = config.getJSONArray("rcon-list").getJSONObject(i); + VanillaVotifierRconConfig rconConfig = new VanillaVotifierRconConfig(new InetSocketAddress(jsonObject.getString("ip"), jsonObject.getInt("port")), jsonObject.getString("password")); + for (int j = 0; j < jsonObject.getJSONArray("commands").length(); j++) { + rconConfig.getCommands().add(jsonObject.getJSONArray("commands").getString(j)); + } + rconConfigs.add(rconConfig); + } + loaded = true; + if (save) { + save(); + } + } + + private void v2ToV3(JSONObject jsonObject) { + if (!jsonObject.has("commands")) { + jsonObject.put("commands", new JSONArray()); + } + if (!jsonObject.has("rcon-list")) { + jsonObject.put("rcon-list", new JSONArray() { + { + put(new JSONObject()); + } + }); + } + jsonObject.getJSONArray("rcon-list").put(jsonObject.get("rcon-list")); + jsonObject.getJSONArray("rcon-list").getJSONObject(0).put("commands", jsonObject.get("commands")); + jsonObject.remove("commands"); + jsonObject.put("config-version", 3); + } + + @Override + public synchronized boolean isLoaded() { + return loaded; + } + + @Override + public synchronized int getConfigVersion() { + checkState(); + return configVersion; + } + + @Override + public synchronized File getLogFile() { + checkState(); + return logFile; + } + + @Override + public synchronized void setLogFile(File location) { + checkState(); + if (location == null) { + location = new File("log.log"); + } + logFile = location; + } + + @Override + public synchronized InetSocketAddress getInetSocketAddress() { + checkState(); + return inetSocketAddress; + } + + @Override + public synchronized void setInetSocketAddress(InetSocketAddress inetSocketAddress) { + checkState(); + if (inetSocketAddress == null) { + inetSocketAddress = new InetSocketAddress("127.0.0.1", 8192); + } + this.inetSocketAddress = inetSocketAddress; + } + + @Override + public synchronized File getPublicKeyFile() { + checkState(); + return publicKeyFile; + } + + @Override + public synchronized void setPublicKeyFile(File location) { + checkState(); + if (location == null) { + location = new File("public.pem"); + } + publicKeyFile = location; + } + + @Override + public synchronized File getPrivateKeyFile() { + checkState(); + return privateKeyFile; + } + + @Override + public synchronized void setPrivateKeyFile(File location) { + checkState(); + if (location == null) { + location = new File("private.pem"); + } + privateKeyFile = location; + } + + @Override + public synchronized KeyPair getKeyPair() { + checkState(); + return keyPair; + } + + @Override + public synchronized void setKeyPair(KeyPair keyPair) { + checkState(); + if (keyPair == null) { + keyPair = RsaUtils.genKeyPair(2048); + } + this.keyPair = keyPair; + } + + @Override + public synchronized void genKeyPair() { + genKeyPair(2048); + } + + @Override + public synchronized void genKeyPair(int keySize) { + setKeyPair(RsaUtils.genKeyPair(keySize)); + } + + @Override + public synchronized List getRconConfigs() { + return rconConfigs; + } + + @Override + public synchronized void save() throws IOException { + checkState(); + JSONObject config = new JSONObject(); + config.put("config-version", getConfigVersion()); + config.put("log-file", getLogFile().getPath()); + config.put("ip", getInetSocketAddress().getHostString()); + config.put("port", getInetSocketAddress().getPort()); + config.put("key-pair-files", new JSONObject() { + { + put("public", getPublicKeyFile().getPath()); + put("private", getPrivateKeyFile().getPath()); + } + }); + config.put("rcon-list", new JSONArray() { + { + for (final RconConfig rconConfig : rconConfigs) { + put(new JSONObject() { + { + put("ip", rconConfig.getInetSocketAddress().getHostString()); + put("port", rconConfig.getInetSocketAddress().getPort()); + put("password", rconConfig.getPassword()); + put("commands", rconConfig.getCommands()); + } + }); + } + } + }); + String configString; + try { + configString = new String(JsonUtils.jsonToPrettyString(config).getBytes(), "UTF-8"); + } catch (UnsupportedEncodingException e) { + configString = JsonUtils.jsonToPrettyString(config); + } + BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(configFile)); + for (char c : configString.toCharArray()) { + out.write(c); + } + out.flush(); + out.close(); + PemWriter publicPemWriter; + try { + publicPemWriter = new PemWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(getPublicKeyFile()), "UTF-8"))); + } catch (UnsupportedEncodingException e) { + publicPemWriter = new PemWriter(new BufferedWriter(new FileWriter(getPublicKeyFile()))); + } + publicPemWriter.writeObject(new PemObject("PUBLIC KEY", getKeyPair().getPublic().getEncoded())); + publicPemWriter.flush(); + publicPemWriter.close(); + PemWriter privatePemWriter; + try { + privatePemWriter = new PemWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(getPrivateKeyFile()), "UTF-8"))); + } catch (UnsupportedEncodingException e) { + privatePemWriter = new PemWriter(new BufferedWriter(new FileWriter(getPrivateKeyFile()))); + } + privatePemWriter.writeObject(new PemObject("RSA PRIVATE KEY", getKeyPair().getPrivate().getEncoded())); + privatePemWriter.flush(); + privatePemWriter.close(); + } + + private void checkState() { + if (!isLoaded()) { + throw new IllegalStateException("Config isn't loaded yet!"); + } + } +} diff --git a/src/main/java/co/virtualdragon/vanillaVotifier/impl/RconConnection.java b/src/main/java/co/virtualdragon/vanillaVotifier/impl/RconConnection.java index 275fa08..3df1bca 100644 --- a/src/main/java/co/virtualdragon/vanillaVotifier/impl/RconConnection.java +++ b/src/main/java/co/virtualdragon/vanillaVotifier/impl/RconConnection.java @@ -1,118 +1,119 @@ -/* - * Copyright (C) 2015 VirtualDragon - * - * 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 . - */ -package co.virtualdragon.vanillaVotifier.impl; - -import co.virtualdragon.vanillaVotifier.Config.RconConfig; -import co.virtualdragon.vanillaVotifier.Rcon; -import co.virtualdragon.vanillaVotifier.Rcon.Packet.Type; -import co.virtualdragon.vanillaVotifier.Rcon.VanillaVotifierPacket; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.Socket; -import java.net.SocketOptions; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.security.SecureRandom; - -public class RconConnection implements Rcon { - - private final RconConfig rconConfig; - - private Socket socket; - private int requestId; - - public RconConnection(RconConfig rconConfig) { - if (rconConfig == null) { - throw new IllegalArgumentException("rconConfig can't be null!"); - } - this.rconConfig = rconConfig; - SecureRandom random = new SecureRandom(); - while (true) { - requestId = random.nextInt(); - if (requestId != -1) { - break; - } - } - } - - @Override - public RconConfig getRconConfig() { - return rconConfig; - } - - @Override - public int getRequestId() { - return requestId; - } - - @Override - public synchronized void connect() throws IOException { - socket = new Socket(rconConfig.getInetSocketAddress().getAddress(), rconConfig.getInetSocketAddress().getPort()); - socket.setSoTimeout(SocketOptions.SO_TIMEOUT); - } - - @Override - public synchronized boolean isConnected() { - if (socket != null) { - try { - System.out.println(sendRequest(new VanillaVotifierPacket(requestId, Type.COMMAND, null))); - return true; - } catch (Exception e) { - } - } - return false; - } - - @Override - public Packet logIn() throws UnsupportedEncodingException, IOException { - return sendRequest(new VanillaVotifierPacket(requestId, Type.LOG_IN, rconConfig.getPassword())); - } - - @Override - public synchronized Packet sendRequest(Packet request) throws UnsupportedEncodingException, IOException { - if (socket == null) { - throw new IllegalStateException("RCon has yet to be connected!"); - } - byte[] requestBytes = new byte[request.getLength() + Integer.SIZE / 8]; - ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); - requestBuffer.order(ByteOrder.LITTLE_ENDIAN); - requestBuffer.putInt(request.getLength()); - requestBuffer.putInt(requestId); - requestBuffer.putInt(request.getType().toInt()); - requestBuffer.put(request.getPayload().getBytes("UTF-8")); - requestBuffer.put((byte) 0); - requestBuffer.put((byte) 0); - socket.getOutputStream().write(requestBytes); - socket.getOutputStream().flush(); - byte[] responseBytes = new byte[Integer.SIZE / 8]; - socket.getInputStream().read(responseBytes); - ByteBuffer responseBuffer = ByteBuffer.wrap(responseBytes); - responseBuffer.order(ByteOrder.LITTLE_ENDIAN); - int responseLength = responseBuffer.getInt(); - responseBytes = new byte[responseLength]; - socket.getInputStream().read(responseBytes); - responseBuffer = ByteBuffer.wrap(responseBytes); - responseBuffer.order(ByteOrder.LITTLE_ENDIAN); - int responseRequestId = responseBuffer.getInt(); - Type responseType = Type.fromInt(responseBuffer.getInt()); - byte[] responsePayload = new byte[responseLength - Integer.SIZE / 8 - Integer.SIZE / 8 - Byte.SIZE / 8 * 2]; - responseBuffer.get(responsePayload); - responseBuffer.get(); - responseBuffer.get(); - return new VanillaVotifierPacket(responseLength, responseRequestId, responseType, new String(responsePayload, "UTF-8")); - } -} +/* + * Copyright (C) 2015 VirtualDragon + * + * 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 . + */ +package co.virtualdragon.vanillaVotifier.impl; + +import co.virtualdragon.vanillaVotifier.Config.RconConfig; +import co.virtualdragon.vanillaVotifier.Rcon; +import co.virtualdragon.vanillaVotifier.Rcon.Packet.Type; +import co.virtualdragon.vanillaVotifier.Rcon.VanillaVotifierPacket; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.Socket; +import java.net.SocketOptions; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.security.SecureRandom; + +public class RconConnection implements Rcon { + + private final RconConfig rconConfig; + + private Socket socket; + private int requestId; + + public RconConnection(RconConfig rconConfig) { + if (rconConfig == null) { + throw new IllegalArgumentException("rconConfig can't be null!"); + } + this.rconConfig = rconConfig; + SecureRandom random = new SecureRandom(); + while (true) { + requestId = random.nextInt(); + if (requestId != -1) { + break; + } + } + } + + @Override + public RconConfig getRconConfig() { + return rconConfig; + } + + @Override + public int getRequestId() { + return requestId; + } + + @Override + public synchronized void connect() throws IOException { + socket = new Socket(rconConfig.getInetSocketAddress().getAddress(), rconConfig.getInetSocketAddress().getPort()); + socket.setSoTimeout(SocketOptions.SO_TIMEOUT); + } + + @Override + public synchronized boolean isConnected() { + if (socket != null) { + try { + sendRequest(new VanillaVotifierPacket(requestId, Type.COMMAND, null)); + return true; + } catch (Exception e) { + // UnsupportedEncodingException, IOException + } + } + return false; + } + + @Override + public Packet logIn() throws UnsupportedEncodingException, IOException { + return sendRequest(new VanillaVotifierPacket(requestId, Type.LOG_IN, rconConfig.getPassword())); + } + + @Override + public synchronized Packet sendRequest(Packet request) throws UnsupportedEncodingException, IOException { + if (socket == null) { + throw new IllegalStateException("RCon has yet to be connected!"); + } + byte[] requestBytes = new byte[request.getLength() + Integer.SIZE / 8]; + ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); + requestBuffer.order(ByteOrder.LITTLE_ENDIAN); + requestBuffer.putInt(request.getLength()); + requestBuffer.putInt(requestId); + requestBuffer.putInt(request.getType().toInt()); + requestBuffer.put(request.getPayload().getBytes("UTF-8")); + requestBuffer.put((byte) 0); + requestBuffer.put((byte) 0); + socket.getOutputStream().write(requestBytes); + socket.getOutputStream().flush(); + byte[] responseBytes = new byte[Integer.SIZE / 8]; + socket.getInputStream().read(responseBytes); + ByteBuffer responseBuffer = ByteBuffer.wrap(responseBytes); + responseBuffer.order(ByteOrder.LITTLE_ENDIAN); + int responseLength = responseBuffer.getInt(); + responseBytes = new byte[responseLength]; + socket.getInputStream().read(responseBytes); + responseBuffer = ByteBuffer.wrap(responseBytes); + responseBuffer.order(ByteOrder.LITTLE_ENDIAN); + int responseRequestId = responseBuffer.getInt(); + Type responseType = Type.fromInt(responseBuffer.getInt()); + byte[] responsePayload = new byte[responseLength - Integer.SIZE / 8 - Integer.SIZE / 8 - Byte.SIZE / 8 * 2]; + responseBuffer.get(responsePayload); + responseBuffer.get(); + responseBuffer.get(); + return new VanillaVotifierPacket(responseLength, responseRequestId, responseType, new String(responsePayload, "UTF-8")); + } +} diff --git a/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifier.java b/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifier.java index d601cac..e13a3e7 100644 --- a/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifier.java +++ b/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifier.java @@ -1,288 +1,290 @@ -/* - * Copyright (C) 2015 VirtualDragon - * - * 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 . - */ -package co.virtualdragon.vanillaVotifier.impl; - -import co.virtualdragon.vanillaVotifier.CommandSender; -import co.virtualdragon.vanillaVotifier.Config; -import co.virtualdragon.vanillaVotifier.Config.RconConfig; -import co.virtualdragon.vanillaVotifier.LanguagePack; -import co.virtualdragon.vanillaVotifier.Listener; -import co.virtualdragon.vanillaVotifier.Rcon; -import co.virtualdragon.vanillaVotifier.Server; -import co.virtualdragon.vanillaVotifier.Tester; -import co.virtualdragon.vanillaVotifier.Votifier; -import co.virtualdragon.vanillaVotifier.event.Event; -import co.virtualdragon.vanillaVotifier.event.server.ServerStoppedEvent; -import co.virtualdragon.vanillaVotifier.exception.InvalidPrivateKeyFileException; -import co.virtualdragon.vanillaVotifier.exception.InvalidPublicKeyFileException; -import co.virtualdragon.vanillaVotifier.exception.PrivateKeyFileNotFoundException; -import co.virtualdragon.vanillaVotifier.exception.PublicKeyFileNotFoundException; -import java.io.File; -import java.net.BindException; -import java.util.AbstractMap.SimpleEntry; -import java.util.ArrayList; -import java.util.List; -import java.util.NoSuchElementException; -import java.util.Scanner; -import org.json.JSONException; -import co.virtualdragon.vanillaVotifier.Logger; -import java.io.FileNotFoundException; - -public class VanillaVotifier implements Votifier { - - private final PropertiesLanguagePack languagePack; - private final VanillaVotifierLogger logger; - private final File configFile; - private final JsonConfig config; - private final RconCommandSender commandSender; - private final VanillaVotifierServer server; - private final ArrayList rcons; - private final VanillaVotifierTester tester; - - { - languagePack = new PropertiesLanguagePack("co/virtualdragon/vanillaVotifier/impl/lang/lang"); - configFile = new File("config.json"); - config = new JsonConfig(configFile); - logger = new VanillaVotifierLogger(this); - commandSender = new RconCommandSender(); - server = new VanillaVotifierServer(this); - rcons = new ArrayList(); - tester = new VanillaVotifierTester(this); - } - - public static void main(String[] args) { - String[] javaVersion = System.getProperty("java.version").split("\\."); - if (!(javaVersion.length >= 1 && Integer.parseInt(javaVersion[0]) >= 1 && javaVersion.length >= 2 && Integer.parseInt(javaVersion[1]) >= 6)) { - System.out.println("You need at least Java 1.6 to run this program! Current version: " + System.getProperty("java.version") + "."); - return; - } - final VanillaVotifier votifier = new VanillaVotifier(); - votifier.getLogger().printlnTranslation("s42"); - if (!(loadConfig(votifier) && startServer(votifier))) { - return; - } - Scanner in = new Scanner(System.in); - while (true) { - String command; - try { - command = in.nextLine(); - } catch (NoSuchElementException e) { - // Can only happen at unexpected program interruption (i. e. CTRL-C). Ignoring. - continue; - } - if (command.equalsIgnoreCase("stop") || command.toLowerCase().startsWith("stop ")) { - if (command.split(" ").length == 1) { - stopServer(votifier); - break; - } else { - votifier.getLogger().printlnTranslation("s17"); - } - } else if (command.equalsIgnoreCase("restart") || command.toLowerCase().startsWith("restart ")) { - if (command.split(" ").length == 1) { - Listener listener = new Listener() { - @Override - public void onEvent(Event event, Votifier votifier) { - if (event instanceof ServerStoppedEvent) { - if (loadConfig((VanillaVotifier) votifier) && startServer((VanillaVotifier) votifier)) { - votifier.getServer().getListeners().remove(this); - } else { - System.exit(0); - } - } - } - }; - votifier.getServer().getListeners().add(listener); - if (!stopServer(votifier)) { - System.exit(0); // "return" somehow isn't enough. - return; - } - } else { - votifier.getLogger().printlnTranslation("s32"); - } - } else if (command.equalsIgnoreCase("gen-key-pair") || command.startsWith("gen-key-pair ")) { - String[] commandArgs = command.split(" "); - int keySize; - if (commandArgs.length == 1) { - keySize = 2048; - } else if (commandArgs.length == 2) { - try { - keySize = Integer.parseInt(commandArgs[1]); - } catch (Exception e) { // NumberFormatException - votifier.getLogger().printlnTranslation("s19"); - continue; - } - if (keySize < 512) { - votifier.getLogger().printlnTranslation("s51"); - continue; - } - if (keySize > 16384) { - votifier.getLogger().printlnTranslation("s52"); - continue; - } - } else { - votifier.getLogger().printlnTranslation("s20"); - continue; - } - votifier.getLogger().printlnTranslation("s16"); - votifier.getConfig().genKeyPair(keySize); - try { - votifier.getConfig().save(); - } catch (Exception e) { - votifier.getLogger().printlnTranslation("s21", new SimpleEntry("exception", e)); - } - votifier.getLogger().printlnTranslation("s23"); - } else if (command.equalsIgnoreCase("test-vote") || command.toLowerCase().startsWith("test-vote ")) { - String[] commandArgs = command.split(" "); - if (commandArgs.length == 2) { - try { - votifier.getTester().testVote(new VanillaVotifierVote("TesterService", commandArgs[1], votifier.getConfig().getInetSocketAddress().getAddress().getHostName())); - } catch (Exception e) { // GeneralSecurityException, IOException - votifier.getLogger().printlnTranslation("s27", new SimpleEntry("exception", e)); - } - } else { - votifier.getLogger().printlnTranslation("s26"); - } - } else if (command.equalsIgnoreCase("test-query") || command.toLowerCase().startsWith("test-query ")) { - if (command.split(" ").length >= 2) { - try { - votifier.getTester().testQuery(command.replaceFirst("test-query ", "").replaceAll("\t", "\n")); - } catch (Exception e) { // GeneralSecurityException, IOException - votifier.getLogger().printlnTranslation("s35", new SimpleEntry("exception", e)); - } - } else { - votifier.getLogger().printlnTranslation("s34"); - } - } else if (command.equalsIgnoreCase("help") || command.toLowerCase().startsWith("help ")) { - if (command.split(" ").length == 1) { - votifier.getLogger().printlnTranslation("s31"); - } else { - votifier.getLogger().printlnTranslation("s32"); - } - } else if (command.equalsIgnoreCase("manual") || command.toLowerCase().startsWith("manual ")) { - if (command.split(" ").length == 1) { - votifier.getLogger().printlnTranslation("s36"); - } else { - votifier.getLogger().printlnTranslation("s37"); - } - } else if (command.equalsIgnoreCase("info") || command.toLowerCase().startsWith("info ")) { - if (command.split(" ").length == 1) { - votifier.getLogger().printlnTranslation("s40"); - } else { - votifier.getLogger().printlnTranslation("s41"); - } - } else if (command.equalsIgnoreCase("license") || command.toLowerCase().startsWith("license ")) { - if (command.split(" ").length == 1) { - votifier.getLogger().printlnTranslation("s43"); - } else { - votifier.getLogger().printlnTranslation("s44"); - } - } else { - votifier.getLogger().printlnTranslation("s33"); - } - - } - } - - private static boolean loadConfig(VanillaVotifier votifier) { - votifier.getLogger().printlnTranslation("s24"); - try { - votifier.getConfig().load(); - votifier.getRcons().clear(); - for (RconConfig rconConfig : votifier.getConfig().getRconConfigs()) { - votifier.getRcons().add(new RconConnection(rconConfig)); - } - votifier.getLogger().printlnTranslation("s25"); - return true; - } catch (JSONException e) { - votifier.getLogger().printlnTranslation("s45", new SimpleEntry("exception", e.getMessage().replaceAll("'", "\""))); - } catch (PublicKeyFileNotFoundException e) { - votifier.getLogger().printlnTranslation("s49"); - } catch (PrivateKeyFileNotFoundException e) { - votifier.getLogger().printlnTranslation("s50"); - } catch (InvalidPublicKeyFileException e) { - votifier.getLogger().printlnTranslation("s47"); - } catch (InvalidPrivateKeyFileException e) { - votifier.getLogger().printlnTranslation("s48"); - } catch (FileNotFoundException e) { - if (votifier.configFile.exists()) { - votifier.getLogger().printlnTranslation("s18"); - } else { - votifier.getLogger().printlnTranslation("s15", new SimpleEntry("exception", e)); - } - } catch (Exception e) { - votifier.getLogger().printlnTranslation("s15", new SimpleEntry("exception", e)); - } - return false; - } - - private static boolean startServer(VanillaVotifier votifier) { - try { - votifier.getServer().start(); - return true; - } catch (BindException e) { - votifier.getLogger().printlnTranslation("s38", new SimpleEntry("port", votifier.getConfig().getInetSocketAddress().getPort())); - } catch (Exception e) { - votifier.getLogger().printlnTranslation("s13", new SimpleEntry("exception", e)); - } - return false; - } - - private static boolean stopServer(VanillaVotifier votifier) { - try { - votifier.getServer().stop(); - return true; - } catch (Exception e) { - votifier.getLogger().printlnTranslation("s12", new SimpleEntry("exception", e)); - } - return false; - } - - @Override - public LanguagePack getLanguagePack() { - return languagePack; - } - - @Override - public Logger getLogger() { - return logger; - } - - @Override - public Config getConfig() { - return config; - } - - @Override - public CommandSender getCommandSender() { - return commandSender; - } - - @Override - public Server getServer() { - return server; - } - - @Override - public List getRcons() { - return rcons; - } - - @Override - public Tester getTester() { - return tester; - } -} +/* + * Copyright (C) 2015 VirtualDragon + * + * 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 . + */ +package co.virtualdragon.vanillaVotifier.impl; + +import co.virtualdragon.vanillaVotifier.CommandSender; +import co.virtualdragon.vanillaVotifier.Config; +import co.virtualdragon.vanillaVotifier.Config.RconConfig; +import co.virtualdragon.vanillaVotifier.LanguagePack; +import co.virtualdragon.vanillaVotifier.Listener; +import co.virtualdragon.vanillaVotifier.Rcon; +import co.virtualdragon.vanillaVotifier.Server; +import co.virtualdragon.vanillaVotifier.Tester; +import co.virtualdragon.vanillaVotifier.Votifier; +import co.virtualdragon.vanillaVotifier.event.Event; +import co.virtualdragon.vanillaVotifier.event.server.ServerStoppedEvent; +import co.virtualdragon.vanillaVotifier.exception.InvalidPrivateKeyFileException; +import co.virtualdragon.vanillaVotifier.exception.InvalidPublicKeyFileException; +import co.virtualdragon.vanillaVotifier.exception.PrivateKeyFileNotFoundException; +import co.virtualdragon.vanillaVotifier.exception.PublicKeyFileNotFoundException; +import java.io.File; +import java.net.BindException; +import java.util.AbstractMap.SimpleEntry; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import org.json.JSONException; +import co.virtualdragon.vanillaVotifier.Logger; +import java.io.FileNotFoundException; + +public class VanillaVotifier implements Votifier { + + private final PropertiesLanguagePack languagePack; + private final VanillaVotifierLogger logger; + private final File configFile; + private final JsonConfig config; + private final RconCommandSender commandSender; + private final VanillaVotifierServer server; + private final ArrayList rcons; + private final VanillaVotifierTester tester; + + { + languagePack = new PropertiesLanguagePack("co/virtualdragon/vanillaVotifier/impl/lang/lang"); + configFile = new File("config.json"); + config = new JsonConfig(configFile); + logger = new VanillaVotifierLogger(this); + commandSender = new RconCommandSender(); + server = new VanillaVotifierServer(this); + rcons = new ArrayList(); + tester = new VanillaVotifierTester(this); + } + + public static void main(String[] args) { + String[] javaVersion = System.getProperty("java.version").split("\\."); + if (!(javaVersion.length >= 1 && Integer.parseInt(javaVersion[0]) >= 1 && javaVersion.length >= 2 && Integer.parseInt(javaVersion[1]) >= 6)) { + try { + System.out.println(new String(("You need at least Java 1.6 to run this program! Current version: " + System.getProperty("java.version") + ".").getBytes(), "UTF-8")); + } catch (Exception e) { // UnsupportedEncodingException + System.out.println("You need at least Java 1.6 to run this program! Current version: " + System.getProperty("java.version") + "."); + } + return; + } + final VanillaVotifier votifier = new VanillaVotifier(); + votifier.getLogger().printlnTranslation("s42"); + if (!(loadConfig(votifier) && startServer(votifier))) { + return; + } + Scanner in = new Scanner(System.in); + while (true) { + String command; + try { + command = in.nextLine(); + } catch (Exception e) { + // NoSuchElementException: Can only happen at unexpected program interruption (i. e. CTRL-C). Ignoring. + continue; + } + if (command.equalsIgnoreCase("stop") || command.toLowerCase().startsWith("stop ")) { + if (command.split(" ").length == 1) { + stopServer(votifier); + break; + } else { + votifier.getLogger().printlnTranslation("s17"); + } + } else if (command.equalsIgnoreCase("restart") || command.toLowerCase().startsWith("restart ")) { + if (command.split(" ").length == 1) { + Listener listener = new Listener() { + @Override + public void onEvent(Event event, Votifier votifier) { + if (event instanceof ServerStoppedEvent) { + if (loadConfig((VanillaVotifier) votifier) && startServer((VanillaVotifier) votifier)) { + votifier.getServer().getListeners().remove(this); + } else { + System.exit(0); + } + } + } + }; + votifier.getServer().getListeners().add(listener); + if (!stopServer(votifier)) { + System.exit(0); // "return" somehow isn't enough. + return; + } + } else { + votifier.getLogger().printlnTranslation("s32"); + } + } else if (command.equalsIgnoreCase("gen-key-pair") || command.startsWith("gen-key-pair ")) { + String[] commandArgs = command.split(" "); + int keySize; + if (commandArgs.length == 1) { + keySize = 2048; + } else if (commandArgs.length == 2) { + try { + keySize = Integer.parseInt(commandArgs[1]); + } catch (Exception e) { // NumberFormatException + votifier.getLogger().printlnTranslation("s19"); + continue; + } + if (keySize < 512) { + votifier.getLogger().printlnTranslation("s51"); + continue; + } + if (keySize > 16384) { + votifier.getLogger().printlnTranslation("s52"); + continue; + } + } else { + votifier.getLogger().printlnTranslation("s20"); + continue; + } + votifier.getLogger().printlnTranslation("s16"); + votifier.getConfig().genKeyPair(keySize); + try { + votifier.getConfig().save(); + } catch (Exception e) { + votifier.getLogger().printlnTranslation("s21", new SimpleEntry("exception", e)); + } + votifier.getLogger().printlnTranslation("s23"); + } else if (command.equalsIgnoreCase("test-vote") || command.toLowerCase().startsWith("test-vote ")) { + String[] commandArgs = command.split(" "); + if (commandArgs.length == 2) { + try { + votifier.getTester().testVote(new VanillaVotifierVote("TesterService", commandArgs[1], votifier.getConfig().getInetSocketAddress().getAddress().getHostName())); + } catch (Exception e) { // GeneralSecurityException, IOException + votifier.getLogger().printlnTranslation("s27", new SimpleEntry("exception", e)); + } + } else { + votifier.getLogger().printlnTranslation("s26"); + } + } else if (command.equalsIgnoreCase("test-query") || command.toLowerCase().startsWith("test-query ")) { + if (command.split(" ").length >= 2) { + try { + votifier.getTester().testQuery(command.replaceFirst("test-query ", "").replaceAll("\t", "\n")); + } catch (Exception e) { // GeneralSecurityException, IOException + votifier.getLogger().printlnTranslation("s35", new SimpleEntry("exception", e)); + } + } else { + votifier.getLogger().printlnTranslation("s34"); + } + } else if (command.equalsIgnoreCase("help") || command.toLowerCase().startsWith("help ")) { + if (command.split(" ").length == 1) { + votifier.getLogger().printlnTranslation("s31"); + } else { + votifier.getLogger().printlnTranslation("s32"); + } + } else if (command.equalsIgnoreCase("manual") || command.toLowerCase().startsWith("manual ")) { + if (command.split(" ").length == 1) { + votifier.getLogger().printlnTranslation("s36"); + } else { + votifier.getLogger().printlnTranslation("s37"); + } + } else if (command.equalsIgnoreCase("info") || command.toLowerCase().startsWith("info ")) { + if (command.split(" ").length == 1) { + votifier.getLogger().printlnTranslation("s40"); + } else { + votifier.getLogger().printlnTranslation("s41"); + } + } else if (command.equalsIgnoreCase("license") || command.toLowerCase().startsWith("license ")) { + if (command.split(" ").length == 1) { + votifier.getLogger().printlnTranslation("s43"); + } else { + votifier.getLogger().printlnTranslation("s44"); + } + } else { + votifier.getLogger().printlnTranslation("s33"); + } + } + } + + private static boolean loadConfig(VanillaVotifier votifier) { + votifier.getLogger().printlnTranslation("s24"); + try { + votifier.getConfig().load(); + votifier.getRcons().clear(); + for (RconConfig rconConfig : votifier.getConfig().getRconConfigs()) { + votifier.getRcons().add(new RconConnection(rconConfig)); + } + votifier.getLogger().printlnTranslation("s25"); + return true; + } catch (JSONException e) { + votifier.getLogger().printlnTranslation("s45", new SimpleEntry("exception", e.getMessage().replaceAll("'", "\""))); + } catch (PublicKeyFileNotFoundException e) { + votifier.getLogger().printlnTranslation("s49"); + } catch (PrivateKeyFileNotFoundException e) { + votifier.getLogger().printlnTranslation("s50"); + } catch (InvalidPublicKeyFileException e) { + votifier.getLogger().printlnTranslation("s47"); + } catch (InvalidPrivateKeyFileException e) { + votifier.getLogger().printlnTranslation("s48"); + } catch (FileNotFoundException e) { + if (votifier.configFile.exists()) { + votifier.getLogger().printlnTranslation("s18"); + } else { + votifier.getLogger().printlnTranslation("s15", new SimpleEntry("exception", e)); + } + } catch (Exception e) { + votifier.getLogger().printlnTranslation("s15", new SimpleEntry("exception", e)); + } + return false; + } + + private static boolean startServer(VanillaVotifier votifier) { + try { + votifier.getServer().start(); + return true; + } catch (BindException e) { + votifier.getLogger().printlnTranslation("s38", new SimpleEntry("port", votifier.getConfig().getInetSocketAddress().getPort())); + } catch (Exception e) { + votifier.getLogger().printlnTranslation("s13", new SimpleEntry("exception", e)); + } + return false; + } + + private static boolean stopServer(VanillaVotifier votifier) { + try { + votifier.getServer().stop(); + return true; + } catch (Exception e) { + votifier.getLogger().printlnTranslation("s12", new SimpleEntry("exception", e)); + } + return false; + } + + @Override + public LanguagePack getLanguagePack() { + return languagePack; + } + + @Override + public Logger getLogger() { + return logger; + } + + @Override + public Config getConfig() { + return config; + } + + @Override + public CommandSender getCommandSender() { + return commandSender; + } + + @Override + public Server getServer() { + return server; + } + + @Override + public List getRcons() { + return rcons; + } + + @Override + public Tester getTester() { + return tester; + } +} diff --git a/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifierLogger.java b/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifierLogger.java index 6ad86c8..d156ef1 100644 --- a/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifierLogger.java +++ b/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifierLogger.java @@ -1,104 +1,108 @@ -/* - * Copyright (C) 2015 VirtualDragon - * - * 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 . - */ -package co.virtualdragon.vanillaVotifier.impl; - -import co.virtualdragon.vanillaVotifier.Votifier; -import java.util.Map.Entry; -import org.apache.commons.lang3.exception.ExceptionUtils; -import co.virtualdragon.vanillaVotifier.Logger; -import java.io.BufferedWriter; -import java.io.FileNotFoundException; -import java.io.FileWriter; - -public class VanillaVotifierLogger implements Logger { - - private final Votifier votifier; - private final StringBuffer buffer; - - private BufferedWriter logWriter; - - { - buffer = new StringBuffer(); - } - - public VanillaVotifierLogger(Votifier votifier) { - this.votifier = votifier; - } - - @Override - public void print(Object object) { - String string; - if (!(object instanceof Throwable)) { - string = object.toString(); - } else { - string = ExceptionUtils.getStackTrace((Throwable) object); - } - synchronized (System.out) { - System.out.print(string); - } - initWriterIfInitialized(); - if (logWriter != null) { - synchronized (logWriter) { - buffer.append(string); - try { - logWriter.write(buffer.toString()); - logWriter.flush(); - } catch (Exception e) { - // Ignoring. - } - buffer.setLength(0); - } - } else { - buffer.append(string); - } - } - - @Override - public void println(Object object) { - print(object + System.lineSeparator()); - } - - @Override - public void printTranslation(String key) { - printTranslation(key, new Entry[]{}); - } - - @Override - public void printTranslation(String key, Entry... substitutions) { - print(votifier.getLanguagePack().getString(key, substitutions)); - } - - @Override - public void printlnTranslation(String key) { - printlnTranslation(key, new Entry[]{}); - } - - @Override - public void printlnTranslation(String key, Entry... substitutions) { - println(votifier.getLanguagePack().getString(key, substitutions)); - } - - private void initWriterIfInitialized() { - if (logWriter == null && votifier.getConfig().isLoaded()) { - try { - logWriter = new BufferedWriter(new FileWriter(votifier.getConfig().getLogFile())); - } catch (Exception e) { - // Ignoring. - } - } - } -} +/* + * Copyright (C) 2015 VirtualDragon + * + * 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 . + */ +package co.virtualdragon.vanillaVotifier.impl; + +import co.virtualdragon.vanillaVotifier.Votifier; +import java.util.Map.Entry; +import org.apache.commons.lang3.exception.ExceptionUtils; +import co.virtualdragon.vanillaVotifier.Logger; +import java.io.BufferedWriter; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; + +public class VanillaVotifierLogger implements Logger { + + private final Votifier votifier; + private final StringBuffer buffer; + + private BufferedWriter logWriter; + + { + buffer = new StringBuffer(); + } + + public VanillaVotifierLogger(Votifier votifier) { + this.votifier = votifier; + } + + @Override + public void print(Object object) { + String string; + if (!(object instanceof Throwable)) { + string = object.toString(); + } else { + string = ExceptionUtils.getStackTrace((Throwable) object); + } + synchronized (System.out) { + try { + System.out.print(new String(string.getBytes(), "UTF-8")); + } catch (Exception e) { // UnsupportedEncodingException + System.out.println(string); + } + } + initWriterIfInitialized(); + if (logWriter != null) { + synchronized (logWriter) { + buffer.append(string); + try { + logWriter.write(buffer.toString()); + logWriter.flush(); + } catch (Exception e) { + // Ignoring. + } + buffer.setLength(0); + } + } else { + buffer.append(string); + } + } + + @Override + public void println(Object object) { + print(object + System.lineSeparator()); + } + + @Override + public void printTranslation(String key) { + printTranslation(key, new Entry[]{}); + } + + @Override + public void printTranslation(String key, Entry... substitutions) { + print(votifier.getLanguagePack().getString(key, substitutions)); + } + + @Override + public void printlnTranslation(String key) { + printlnTranslation(key, new Entry[]{}); + } + + @Override + public void printlnTranslation(String key, Entry... substitutions) { + println(votifier.getLanguagePack().getString(key, substitutions)); + } + + private void initWriterIfInitialized() { + if (logWriter == null && votifier.getConfig().isLoaded()) { + try { + logWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(votifier.getConfig().getLogFile()), "UTF-8")); + } catch (Exception e) { + // FileNotFoundException, UnsupportedEncodingException: ignoring. + } + } + } +} diff --git a/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifierServer.java b/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifierServer.java index 3d3058b..624f308 100644 --- a/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifierServer.java +++ b/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifierServer.java @@ -1,195 +1,194 @@ -/* - * Copyright (C) 2015 VirtualDragon - * - * 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 . - */ -package co.virtualdragon.vanillaVotifier.impl; - -import co.virtualdragon.vanillaVotifier.Listener; -import co.virtualdragon.vanillaVotifier.Rcon; -import co.virtualdragon.vanillaVotifier.Server; -import co.virtualdragon.vanillaVotifier.Votifier; -import co.virtualdragon.vanillaVotifier.event.Event; -import co.virtualdragon.vanillaVotifier.event.server.RconCommandResponseEvent; -import co.virtualdragon.vanillaVotifier.event.server.ComunicationExceptionEvent; -import co.virtualdragon.vanillaVotifier.event.server.ConnectionCloseExceptionEvent; -import co.virtualdragon.vanillaVotifier.event.server.ConnectionClosedEvent; -import co.virtualdragon.vanillaVotifier.event.server.ConnectionEstablishExceptionEvent; -import co.virtualdragon.vanillaVotifier.event.server.ConnectionEstablishedEvent; -import co.virtualdragon.vanillaVotifier.event.server.ConnectionInputStreamCloseExceptionEvent; -import co.virtualdragon.vanillaVotifier.event.server.DecryptInputExceptionEvent; -import co.virtualdragon.vanillaVotifier.event.server.DecryptedInputReceivedEvent; -import co.virtualdragon.vanillaVotifier.event.server.EncryptedInputReceivedEvent; -import co.virtualdragon.vanillaVotifier.event.server.InvalidRequestEvent; -import co.virtualdragon.vanillaVotifier.event.server.RconExceptionEvent; -import co.virtualdragon.vanillaVotifier.event.server.SendingRconCommandEvent; -import co.virtualdragon.vanillaVotifier.event.server.ServerAwaitingTaskCompletionEvent; -import co.virtualdragon.vanillaVotifier.event.server.ServerStartedEvent; -import co.virtualdragon.vanillaVotifier.event.server.ServerStartingEvent; -import co.virtualdragon.vanillaVotifier.event.server.ServerStoppedEvent; -import co.virtualdragon.vanillaVotifier.event.server.ServerStoppingEvent; -import co.virtualdragon.vanillaVotifier.event.server.VoteEvent; -import co.virtualdragon.vanillaVotifier.util.RsaUtils; -import java.io.BufferedInputStream; -import java.io.IOException; -import java.net.ServerSocket; -import java.net.Socket; -import java.net.SocketOptions; -import java.nio.charset.StandardCharsets; -import java.security.GeneralSecurityException; -import java.security.interfaces.RSAPublicKey; -import java.util.HashMap; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import javax.crypto.BadPaddingException; -import org.apache.commons.lang3.text.StrSubstitutor; - -public class VanillaVotifierServer implements Server { - - private final Votifier votifier; - private final CopyOnWriteArrayList listeners; - - private boolean running; - private ServerSocket serverSocket; - - { - listeners = new CopyOnWriteArrayList(); - getListeners().add(new VanillaVotifierServerListener()); - } - - public VanillaVotifierServer(Votifier votifier) { - this.votifier = votifier; - } - - @Override - public synchronized void start() throws IOException, GeneralSecurityException { - if (isRunning()) { - throw new IllegalStateException("Server is already running!"); - } - notifyListeners(new ServerStartingEvent()); - serverSocket = new ServerSocket(); - serverSocket.bind(votifier.getConfig().getInetSocketAddress()); - running = true; - notifyListeners(new ServerStartedEvent()); - new Thread(new Runnable() { - @Override - public void run() { - ExecutorService executorService = Executors.newSingleThreadExecutor(); - while (isRunning()) { - try { - final Socket socket = serverSocket.accept(); - executorService.execute(new Runnable() { - @Override - public void run() { - try { - notifyListeners(new ConnectionEstablishedEvent(socket)); - socket.setSoTimeout(SocketOptions.SO_TIMEOUT); // SocketException: handled by try/catch. - BufferedInputStream in = new BufferedInputStream(socket.getInputStream()); // IOException: handled by try/catch. - byte[] request = new byte[((RSAPublicKey) votifier.getConfig().getKeyPair().getPublic()).getModulus().bitLength() / Byte.SIZE]; - in.read(request); // IOException: handled by try/catch. - notifyListeners(new EncryptedInputReceivedEvent(socket, new String(request, StandardCharsets.UTF_8))); // UnsupportedEncodingException: can't happen. - request = RsaUtils.getDecryptCipher(votifier.getConfig().getKeyPair().getPrivate()).doFinal(request); // IllegalBlockSizeException: can't happen. - String requestString = new String(request, StandardCharsets.UTF_8); // UnsupportedEncodingException: can't happen. - notifyListeners(new DecryptedInputReceivedEvent(socket, requestString)); - String[] requestArray = requestString.split("\n"); - if ((requestArray.length == 5 || requestArray.length == 6) && requestArray[0].equals("VOTE")) { - notifyListeners(new VoteEvent(socket, new VanillaVotifierVote(requestArray[1], requestArray[2], requestArray[3], requestArray[4]))); - HashMap substitutions = new HashMap(); - substitutions.put("service-name", requestArray[1]); - substitutions.put("user-name", requestArray[2]); - substitutions.put("address", requestArray[3]); - substitutions.put("time-stamp", requestArray[4]); - StrSubstitutor substitutor = new StrSubstitutor(substitutions); - for (Rcon rcon : votifier.getRcons()) { - for (String command : rcon.getRconConfig().getCommands()) { - command = substitutor.replace(command); - notifyListeners(new SendingRconCommandEvent(rcon, command)); - try { - notifyListeners(new RconCommandResponseEvent(rcon, votifier.getCommandSender().sendCommand(rcon, command))); - } catch (Exception e) { - notifyListeners(new RconExceptionEvent(rcon, e)); - } - } - } - } else { - notifyListeners(new InvalidRequestEvent(socket, requestString)); - } - try { - in.close(); - } catch (Exception e) { // IOException: catching just in case. Continue even if stream doesn't close. - notifyListeners(new ConnectionInputStreamCloseExceptionEvent(socket, e)); - } - } catch (BadPaddingException e) { - notifyListeners(new DecryptInputExceptionEvent(socket, e)); - } catch (Exception e) { - notifyListeners(new ComunicationExceptionEvent(socket, e)); - } - try { - socket.close(); - notifyListeners(new ConnectionClosedEvent(socket)); - } catch (Exception e) { // IOException: catching just in case. Continue even if socket doesn't close. - notifyListeners(new ConnectionCloseExceptionEvent(socket, e)); - } - } - }); - } catch (Exception e) { - if (running) { // Show errors only while running, to hide error while stopping. - notifyListeners(new ConnectionEstablishExceptionEvent(e)); - } - } - } - executorService.shutdown(); - if (!executorService.isTerminated()) { - notifyListeners(new ServerAwaitingTaskCompletionEvent()); - try { - executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); - } catch (Exception e) { - // InterruptedException: can't happen. - } - } - notifyListeners(new ServerStoppedEvent()); - } - }).start(); - } - - @Override - public synchronized void stop() throws IOException { - if (!isRunning()) { - throw new IllegalStateException("Server isn't running!"); - } - notifyListeners(new ServerStoppingEvent()); - running = false; - serverSocket.close(); - } - - @Override - public synchronized boolean isRunning() { - return running; - } - - @Override - public List getListeners() { - return listeners; - } - - @Override - public void notifyListeners(Event event) { - for (Listener listener : listeners) { - listener.onEvent(event, votifier); - } - } -} +/* + * Copyright (C) 2015 VirtualDragon + * + * 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 . + */ +package co.virtualdragon.vanillaVotifier.impl; + +import co.virtualdragon.vanillaVotifier.Listener; +import co.virtualdragon.vanillaVotifier.Rcon; +import co.virtualdragon.vanillaVotifier.Server; +import co.virtualdragon.vanillaVotifier.Votifier; +import co.virtualdragon.vanillaVotifier.event.Event; +import co.virtualdragon.vanillaVotifier.event.server.RconCommandResponseEvent; +import co.virtualdragon.vanillaVotifier.event.server.ComunicationExceptionEvent; +import co.virtualdragon.vanillaVotifier.event.server.ConnectionCloseExceptionEvent; +import co.virtualdragon.vanillaVotifier.event.server.ConnectionClosedEvent; +import co.virtualdragon.vanillaVotifier.event.server.ConnectionEstablishExceptionEvent; +import co.virtualdragon.vanillaVotifier.event.server.ConnectionEstablishedEvent; +import co.virtualdragon.vanillaVotifier.event.server.ConnectionInputStreamCloseExceptionEvent; +import co.virtualdragon.vanillaVotifier.event.server.DecryptInputExceptionEvent; +import co.virtualdragon.vanillaVotifier.event.server.DecryptedInputReceivedEvent; +import co.virtualdragon.vanillaVotifier.event.server.EncryptedInputReceivedEvent; +import co.virtualdragon.vanillaVotifier.event.server.InvalidRequestEvent; +import co.virtualdragon.vanillaVotifier.event.server.RconExceptionEvent; +import co.virtualdragon.vanillaVotifier.event.server.SendingRconCommandEvent; +import co.virtualdragon.vanillaVotifier.event.server.ServerAwaitingTaskCompletionEvent; +import co.virtualdragon.vanillaVotifier.event.server.ServerStartedEvent; +import co.virtualdragon.vanillaVotifier.event.server.ServerStartingEvent; +import co.virtualdragon.vanillaVotifier.event.server.ServerStoppedEvent; +import co.virtualdragon.vanillaVotifier.event.server.ServerStoppingEvent; +import co.virtualdragon.vanillaVotifier.event.server.VoteEvent; +import co.virtualdragon.vanillaVotifier.util.RsaUtils; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketOptions; +import java.security.GeneralSecurityException; +import java.security.interfaces.RSAPublicKey; +import java.util.HashMap; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import javax.crypto.BadPaddingException; +import org.apache.commons.lang3.text.StrSubstitutor; + +public class VanillaVotifierServer implements Server { + + private final Votifier votifier; + private final CopyOnWriteArrayList listeners; + + private boolean running; + private ServerSocket serverSocket; + + { + listeners = new CopyOnWriteArrayList(); + getListeners().add(new VanillaVotifierServerListener()); + } + + public VanillaVotifierServer(Votifier votifier) { + this.votifier = votifier; + } + + @Override + public synchronized void start() throws IOException, GeneralSecurityException { + if (isRunning()) { + throw new IllegalStateException("Server is already running!"); + } + notifyListeners(new ServerStartingEvent()); + serverSocket = new ServerSocket(); + serverSocket.bind(votifier.getConfig().getInetSocketAddress()); + running = true; + notifyListeners(new ServerStartedEvent()); + new Thread(new Runnable() { + @Override + public void run() { + ExecutorService executorService = Executors.newSingleThreadExecutor(); + while (isRunning()) { + try { + final Socket socket = serverSocket.accept(); + executorService.execute(new Runnable() { + @Override + public void run() { + try { + notifyListeners(new ConnectionEstablishedEvent(socket)); + socket.setSoTimeout(SocketOptions.SO_TIMEOUT); // SocketException: handled by try/catch. + BufferedInputStream in = new BufferedInputStream(socket.getInputStream()); // IOException: handled by try/catch. + byte[] request = new byte[((RSAPublicKey) votifier.getConfig().getKeyPair().getPublic()).getModulus().bitLength() / Byte.SIZE]; + in.read(request); // IOException: handled by try/catch. + notifyListeners(new EncryptedInputReceivedEvent(socket, new String(request, "UTF-8"))); // UnsupportedEncodingException: can't happen. + request = RsaUtils.getDecryptCipher(votifier.getConfig().getKeyPair().getPrivate()).doFinal(request); // IllegalBlockSizeException: can't happen. + String requestString = new String(request, "UTF-8"); // UnsupportedEncodingException: can't happen. + notifyListeners(new DecryptedInputReceivedEvent(socket, requestString)); + String[] requestArray = requestString.split("\n"); + if ((requestArray.length == 5 || requestArray.length == 6) && requestArray[0].equals("VOTE")) { + notifyListeners(new VoteEvent(socket, new VanillaVotifierVote(requestArray[1], requestArray[2], requestArray[3], requestArray[4]))); + HashMap substitutions = new HashMap(); + substitutions.put("service-name", requestArray[1]); + substitutions.put("user-name", requestArray[2]); + substitutions.put("address", requestArray[3]); + substitutions.put("time-stamp", requestArray[4]); + StrSubstitutor substitutor = new StrSubstitutor(substitutions); + for (Rcon rcon : votifier.getRcons()) { + for (String command : rcon.getRconConfig().getCommands()) { + command = substitutor.replace(command); + notifyListeners(new SendingRconCommandEvent(rcon, command)); + try { + notifyListeners(new RconCommandResponseEvent(rcon, votifier.getCommandSender().sendCommand(rcon, command))); + } catch (Exception e) { + notifyListeners(new RconExceptionEvent(rcon, e)); + } + } + } + } else { + notifyListeners(new InvalidRequestEvent(socket, requestString)); + } + try { + in.close(); + } catch (Exception e) { // IOException: catching just in case. Continue even if stream doesn't close. + notifyListeners(new ConnectionInputStreamCloseExceptionEvent(socket, e)); + } + } catch (BadPaddingException e) { + notifyListeners(new DecryptInputExceptionEvent(socket, e)); + } catch (Exception e) { + notifyListeners(new ComunicationExceptionEvent(socket, e)); + } + try { + socket.close(); + notifyListeners(new ConnectionClosedEvent(socket)); + } catch (Exception e) { // IOException: catching just in case. Continue even if socket doesn't close. + notifyListeners(new ConnectionCloseExceptionEvent(socket, e)); + } + } + }); + } catch (Exception e) { + if (running) { // Show errors only while running, to hide error while stopping. + notifyListeners(new ConnectionEstablishExceptionEvent(e)); + } + } + } + executorService.shutdown(); + if (!executorService.isTerminated()) { + notifyListeners(new ServerAwaitingTaskCompletionEvent()); + try { + executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); + } catch (Exception e) { + // InterruptedException: can't happen. + } + } + notifyListeners(new ServerStoppedEvent()); + } + }).start(); + } + + @Override + public synchronized void stop() throws IOException { + if (!isRunning()) { + throw new IllegalStateException("Server isn't running!"); + } + notifyListeners(new ServerStoppingEvent()); + running = false; + serverSocket.close(); + } + + @Override + public synchronized boolean isRunning() { + return running; + } + + @Override + public List getListeners() { + return listeners; + } + + @Override + public void notifyListeners(Event event) { + for (Listener listener : listeners) { + listener.onEvent(event, votifier); + } + } +} diff --git a/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifierServerListener.java b/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifierServerListener.java index afdc08d..27d12c1 100644 --- a/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifierServerListener.java +++ b/src/main/java/co/virtualdragon/vanillaVotifier/impl/VanillaVotifierServerListener.java @@ -1,124 +1,124 @@ -/* - * Copyright (C) 2015 VirtualDragon - * - * 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 . - */ -package co.virtualdragon.vanillaVotifier.impl; - -import co.virtualdragon.vanillaVotifier.Listener; -import co.virtualdragon.vanillaVotifier.Votifier; -import co.virtualdragon.vanillaVotifier.event.Event; -import co.virtualdragon.vanillaVotifier.event.server.RconCommandResponseEvent; -import co.virtualdragon.vanillaVotifier.event.server.ComunicationExceptionEvent; -import co.virtualdragon.vanillaVotifier.event.server.ConnectionCloseExceptionEvent; -import co.virtualdragon.vanillaVotifier.event.server.ConnectionClosedEvent; -import co.virtualdragon.vanillaVotifier.event.server.ConnectionEstablishExceptionEvent; -import co.virtualdragon.vanillaVotifier.event.server.ConnectionEstablishedEvent; -import co.virtualdragon.vanillaVotifier.event.server.ConnectionInputStreamCloseExceptionEvent; -import co.virtualdragon.vanillaVotifier.event.server.DecryptInputExceptionEvent; -import co.virtualdragon.vanillaVotifier.event.server.InvalidRequestEvent; -import co.virtualdragon.vanillaVotifier.event.server.RconExceptionEvent; -import co.virtualdragon.vanillaVotifier.event.server.SendingRconCommandEvent; -import co.virtualdragon.vanillaVotifier.event.server.ServerAwaitingTaskCompletionEvent; -import co.virtualdragon.vanillaVotifier.event.server.ServerStartedEvent; -import co.virtualdragon.vanillaVotifier.event.server.ServerStartingEvent; -import co.virtualdragon.vanillaVotifier.event.server.ServerStoppedEvent; -import co.virtualdragon.vanillaVotifier.event.server.ServerStoppingEvent; -import co.virtualdragon.vanillaVotifier.event.server.VoteEvent; -import co.virtualdragon.vanillaVotifier.exception.InvalidRconPasswordException; -import java.net.ConnectException; -import java.util.AbstractMap.SimpleEntry; - -public class VanillaVotifierServerListener implements Listener { - - @Override - public void onEvent(Event event, Votifier votifier) { - if (event instanceof ServerStartingEvent) { - votifier.getLogger().printlnTranslation("s1"); - } else if (event instanceof ServerStartedEvent) { - votifier.getLogger().printlnTranslation("s2"); - } else if (event instanceof ConnectionEstablishedEvent) { - ConnectionEstablishedEvent connectionEstablishedEvent = (ConnectionEstablishedEvent) event; - votifier.getLogger().printlnTranslation("s3", - new SimpleEntry("ip", connectionEstablishedEvent.getSocket().getInetAddress().getHostAddress()), - new SimpleEntry("port", connectionEstablishedEvent.getSocket().getPort())); - } else if (event instanceof VoteEvent) { - VoteEvent voteEvent = (VoteEvent) event; - votifier.getLogger().printlnTranslation("s4", - new SimpleEntry("ip", voteEvent.getSocket().getInetAddress().getHostAddress()), - new SimpleEntry("port", voteEvent.getSocket().getPort()), - new SimpleEntry("service-name", voteEvent.getVote().getServiceName()), - new SimpleEntry("user-name", voteEvent.getVote().getUserName()), - new SimpleEntry("address", voteEvent.getVote().getAddress()), - new SimpleEntry("time-stamp", voteEvent.getVote().getTimeStamp())); - } else if (event instanceof SendingRconCommandEvent) { - SendingRconCommandEvent sendingRconCommandEvent = (SendingRconCommandEvent) event; - votifier.getLogger().printlnTranslation("s5", - new SimpleEntry("ip", sendingRconCommandEvent.getRcon().getRconConfig().getInetSocketAddress().getHostString()), - new SimpleEntry("port", sendingRconCommandEvent.getRcon().getRconConfig().getInetSocketAddress().getPort()), - new SimpleEntry("command", sendingRconCommandEvent.getCommand())); - } else if (event instanceof RconCommandResponseEvent) { - RconCommandResponseEvent commandResponseEvent = ((RconCommandResponseEvent) event); - if (commandResponseEvent.getMessage() != null && !commandResponseEvent.getMessage().isEmpty()) { - votifier.getLogger().printlnTranslation("s6", new SimpleEntry("response", commandResponseEvent.getMessage())); - } else { - votifier.getLogger().printlnTranslation("s53"); - } - } else if (event instanceof RconExceptionEvent) { - Exception exception = ((RconExceptionEvent) event).getException(); - if (exception instanceof InvalidRconPasswordException) { - votifier.getLogger().printlnTranslation("s7"); - } else if (exception instanceof ConnectException) { - votifier.getLogger().printlnTranslation("s39"); - } else { - votifier.getLogger().printlnTranslation("s28", new SimpleEntry("exception", exception)); - } - } else if (event instanceof InvalidRequestEvent) { - InvalidRequestEvent invalidRequestEvent = (InvalidRequestEvent) event; - votifier.getLogger().printlnTranslation("s8", - new SimpleEntry("ip", invalidRequestEvent.getSocket().getInetAddress().getHostAddress()), - new SimpleEntry("port", invalidRequestEvent.getSocket().getPort()), - new SimpleEntry("message", invalidRequestEvent.getMessage().replaceAll("\n", "\t"))); - } else if (event instanceof ConnectionInputStreamCloseExceptionEvent) { - ConnectionInputStreamCloseExceptionEvent socketInputStreamCloseException = (ConnectionInputStreamCloseExceptionEvent) event; - votifier.getLogger().printlnTranslation("s9", - new SimpleEntry("ip", socketInputStreamCloseException.getSocket().getInetAddress().getHostAddress()), - new SimpleEntry("port", socketInputStreamCloseException.getSocket().getPort()), - new SimpleEntry("exception", socketInputStreamCloseException.getException())); - } else if (event instanceof ConnectionClosedEvent) { - ConnectionClosedEvent connectionClosedEvent = (ConnectionClosedEvent) event; - votifier.getLogger().printlnTranslation("s10", - new SimpleEntry("ip", connectionClosedEvent.getSocket().getInetAddress().getHostAddress()), - new SimpleEntry("port", connectionClosedEvent.getSocket().getPort())); - } else if (event instanceof ConnectionCloseExceptionEvent) { - ConnectionCloseExceptionEvent connectionCloseException = (ConnectionCloseExceptionEvent) event; - votifier.getLogger().printlnTranslation("s11", - new SimpleEntry("ip", connectionCloseException.getSocket().getInetAddress().getHostAddress()), - new SimpleEntry("port", connectionCloseException.getSocket().getPort()), - new SimpleEntry("exception", connectionCloseException.getException())); - } else if (event instanceof DecryptInputExceptionEvent) { - votifier.getLogger().printlnTranslation("s46"); - } else if (event instanceof ComunicationExceptionEvent) { - votifier.getLogger().printlnTranslation("s29", new SimpleEntry("exception", ((ComunicationExceptionEvent) event).getException())); - } else if (event instanceof ConnectionEstablishExceptionEvent) { - votifier.getLogger().printlnTranslation("s30", new SimpleEntry("exception", ((ConnectionEstablishExceptionEvent) event).getException())); - } else if (event instanceof ServerStoppingEvent) { - votifier.getLogger().printlnTranslation("s22"); - } else if (event instanceof ServerAwaitingTaskCompletionEvent) { - votifier.getLogger().printlnTranslation("s54"); - } else if (event instanceof ServerStoppedEvent) { - votifier.getLogger().printlnTranslation("s14"); - } - } -} +/* + * Copyright (C) 2015 VirtualDragon + * + * 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 . + */ +package co.virtualdragon.vanillaVotifier.impl; + +import co.virtualdragon.vanillaVotifier.Listener; +import co.virtualdragon.vanillaVotifier.Votifier; +import co.virtualdragon.vanillaVotifier.event.Event; +import co.virtualdragon.vanillaVotifier.event.server.RconCommandResponseEvent; +import co.virtualdragon.vanillaVotifier.event.server.ComunicationExceptionEvent; +import co.virtualdragon.vanillaVotifier.event.server.ConnectionCloseExceptionEvent; +import co.virtualdragon.vanillaVotifier.event.server.ConnectionClosedEvent; +import co.virtualdragon.vanillaVotifier.event.server.ConnectionEstablishExceptionEvent; +import co.virtualdragon.vanillaVotifier.event.server.ConnectionEstablishedEvent; +import co.virtualdragon.vanillaVotifier.event.server.ConnectionInputStreamCloseExceptionEvent; +import co.virtualdragon.vanillaVotifier.event.server.DecryptInputExceptionEvent; +import co.virtualdragon.vanillaVotifier.event.server.InvalidRequestEvent; +import co.virtualdragon.vanillaVotifier.event.server.RconExceptionEvent; +import co.virtualdragon.vanillaVotifier.event.server.SendingRconCommandEvent; +import co.virtualdragon.vanillaVotifier.event.server.ServerAwaitingTaskCompletionEvent; +import co.virtualdragon.vanillaVotifier.event.server.ServerStartedEvent; +import co.virtualdragon.vanillaVotifier.event.server.ServerStartingEvent; +import co.virtualdragon.vanillaVotifier.event.server.ServerStoppedEvent; +import co.virtualdragon.vanillaVotifier.event.server.ServerStoppingEvent; +import co.virtualdragon.vanillaVotifier.event.server.VoteEvent; +import co.virtualdragon.vanillaVotifier.exception.InvalidRconPasswordException; +import java.net.ConnectException; +import java.util.AbstractMap.SimpleEntry; + +public class VanillaVotifierServerListener implements Listener { + + @Override + public void onEvent(Event event, Votifier votifier) { + if (event instanceof ServerStartingEvent) { + votifier.getLogger().printlnTranslation("s1"); + } else if (event instanceof ServerStartedEvent) { + votifier.getLogger().printlnTranslation("s2"); + } else if (event instanceof ConnectionEstablishedEvent) { + ConnectionEstablishedEvent connectionEstablishedEvent = (ConnectionEstablishedEvent) event; + votifier.getLogger().printlnTranslation("s3", + new SimpleEntry("ip", connectionEstablishedEvent.getSocket().getInetAddress().getHostAddress()), + new SimpleEntry("port", connectionEstablishedEvent.getSocket().getPort())); + } else if (event instanceof VoteEvent) { + VoteEvent voteEvent = (VoteEvent) event; + votifier.getLogger().printlnTranslation("s4", + new SimpleEntry("ip", voteEvent.getSocket().getInetAddress().getHostAddress()), + new SimpleEntry("port", voteEvent.getSocket().getPort()), + new SimpleEntry("service-name", voteEvent.getVote().getServiceName()), + new SimpleEntry("user-name", voteEvent.getVote().getUserName()), + new SimpleEntry("address", voteEvent.getVote().getAddress()), + new SimpleEntry("time-stamp", voteEvent.getVote().getTimeStamp())); + } else if (event instanceof SendingRconCommandEvent) { + SendingRconCommandEvent sendingRconCommandEvent = (SendingRconCommandEvent) event; + votifier.getLogger().printlnTranslation("s5", + new SimpleEntry("ip", sendingRconCommandEvent.getRcon().getRconConfig().getInetSocketAddress().getHostString()), + new SimpleEntry("port", sendingRconCommandEvent.getRcon().getRconConfig().getInetSocketAddress().getPort()), + new SimpleEntry("command", sendingRconCommandEvent.getCommand())); + } else if (event instanceof RconCommandResponseEvent) { + RconCommandResponseEvent commandResponseEvent = ((RconCommandResponseEvent) event); + if (commandResponseEvent.getMessage() != null && !commandResponseEvent.getMessage().isEmpty()) { + votifier.getLogger().printlnTranslation("s6", new SimpleEntry("response", commandResponseEvent.getMessage())); + } else { + votifier.getLogger().printlnTranslation("s53"); + } + } else if (event instanceof RconExceptionEvent) { + Exception exception = ((RconExceptionEvent) event).getException(); + if (exception instanceof InvalidRconPasswordException) { + votifier.getLogger().printlnTranslation("s7"); + } else if (exception instanceof ConnectException) { + votifier.getLogger().printlnTranslation("s39"); + } else { + votifier.getLogger().printlnTranslation("s28", new SimpleEntry("exception", exception)); + } + } else if (event instanceof InvalidRequestEvent) { + InvalidRequestEvent invalidRequestEvent = (InvalidRequestEvent) event; + votifier.getLogger().printlnTranslation("s8", + new SimpleEntry("ip", invalidRequestEvent.getSocket().getInetAddress().getHostAddress()), + new SimpleEntry("port", invalidRequestEvent.getSocket().getPort()), + new SimpleEntry("message", invalidRequestEvent.getMessage().replaceAll("\n", "\t"))); + } else if (event instanceof ConnectionInputStreamCloseExceptionEvent) { + ConnectionInputStreamCloseExceptionEvent socketInputStreamCloseException = (ConnectionInputStreamCloseExceptionEvent) event; + votifier.getLogger().printlnTranslation("s9", + new SimpleEntry("ip", socketInputStreamCloseException.getSocket().getInetAddress().getHostAddress()), + new SimpleEntry("port", socketInputStreamCloseException.getSocket().getPort()), + new SimpleEntry("exception", socketInputStreamCloseException.getException())); + } else if (event instanceof ConnectionClosedEvent) { + ConnectionClosedEvent connectionClosedEvent = (ConnectionClosedEvent) event; + votifier.getLogger().printlnTranslation("s10", + new SimpleEntry("ip", connectionClosedEvent.getSocket().getInetAddress().getHostAddress()), + new SimpleEntry("port", connectionClosedEvent.getSocket().getPort())); + } else if (event instanceof ConnectionCloseExceptionEvent) { + ConnectionCloseExceptionEvent connectionCloseException = (ConnectionCloseExceptionEvent) event; + votifier.getLogger().printlnTranslation("s11", + new SimpleEntry("ip", connectionCloseException.getSocket().getInetAddress().getHostAddress()), + new SimpleEntry("port", connectionCloseException.getSocket().getPort()), + new SimpleEntry("exception", connectionCloseException.getException())); + } else if (event instanceof DecryptInputExceptionEvent) { + votifier.getLogger().printlnTranslation("s46"); + } else if (event instanceof ComunicationExceptionEvent) { + votifier.getLogger().printlnTranslation("s29", new SimpleEntry("exception", ((ComunicationExceptionEvent) event).getException())); + } else if (event instanceof ConnectionEstablishExceptionEvent) { + votifier.getLogger().printlnTranslation("s30", new SimpleEntry("exception", ((ConnectionEstablishExceptionEvent) event).getException())); + } else if (event instanceof ServerStoppingEvent) { + votifier.getLogger().printlnTranslation("s22"); + } else if (event instanceof ServerAwaitingTaskCompletionEvent) { + votifier.getLogger().printlnTranslation("s54"); + } else if (event instanceof ServerStoppedEvent) { + votifier.getLogger().printlnTranslation("s14"); + } + } +} diff --git a/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/help.txt b/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/help.txt index 3405503..1591fc8 100644 --- a/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/help.txt +++ b/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/help.txt @@ -1,14 +1,14 @@ -=============================== AVAIABLE COMMANDS ============================== -gen-key-pair [key-size] Generates new RSA keys for the VanillaVotifier server. - Key-size defaults to 2048. -help Shows all avaiable commands with usage explanation. -info Shows information about VanillaVotifier. -license Displays VanillaVotifier's license. -manual Displays a detailed manual of VanillaVotifier. -restart Restarts the VanillaVotifier server. -stop Stops the VanillaVotifier server. -test-query Sends an enciphered query to VanillaVotifier itself. For - debugging purposes only. -test-vote Sends a vote as to VanillaVotifier itself. For - debugging purposes only. +=============================== AVAIABLE COMMANDS ============================== +gen-key-pair [key-size] Generates new RSA keys for the VanillaVotifier server. + Key-size defaults to 2048. +help Shows all avaiable commands with usage explanation. +info Shows information about VanillaVotifier. +license Displays VanillaVotifier's license. +manual Displays a detailed manual of VanillaVotifier. +restart Restarts the VanillaVotifier server. +stop Stops the VanillaVotifier server. +test-query Sends an enciphered query to VanillaVotifier itself. For + debugging purposes only. +test-vote Sends a vote as IGN to VanillaVotifier itself. For + debugging purposes only. ================================================================================ \ No newline at end of file diff --git a/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/help_it.txt b/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/help_it.txt new file mode 100644 index 0000000..c515ce6 --- /dev/null +++ b/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/help_it.txt @@ -0,0 +1,17 @@ +=============================== AVAIABLE COMMANDS ============================== +gen-key-pair [key-size] Genera una nuova coppia di chiavi per il server di + VanillaVotifier. La dimensione della chiave, key-size, + ha come valore predefinito 2048. +help Mostra tutti i comandi disponibili. +info Mostra informazioni su VanillaVotifier. +license Mostra la licenza di VanillaVotifier (disponibile solo + in inglese). +manual Visualizza un manuale dettagliato di VanillaVotifier + (disponibile solo in inglese). +restart Riavvia il server di VanillaVotifier. +stop Arresta il server di VanillaVotifier. +test-query Invia una query cifrata al server di VanillaVotifier + stesso. Da usare solo a scopo di debug. +test-vote Emula l'invio di un voto da parte di IGN a + VanillaVotifier stesso. Da usare solo a scopo di debug. +================================================================================ \ No newline at end of file diff --git a/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/lang.properties b/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/lang.properties index 7048b41..491b03e 100644 --- a/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/lang.properties +++ b/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/lang.properties @@ -1,71 +1,71 @@ -# -# Copyright (C) 2015 VirtualDragon -# -# 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 . -# -s1=Starting VanillaVotifier server... -s2=VanillaVotifier server started. -s3=${ip}:${port} established a connection. -s4=Received vote query from ${ip}:${port}: VOTE\t${service-name}\t${user-name}\t${address}\t${time-stamp} -s5=Sending RCon command to ${ip}:${port}: ${command} -s6=RCon command sent. Response: ${response} -s7=Can't send RCon command: invalid password! -s8=Received invalid query from ${ip}:${port}: ${message} -s9=Unexpected exception while closing input stream of ${ip}:${port}: ${exception} -s10=${ip}:${port} disconnected. -s11=Unexpected exception while closing socket connection with ${ip}:${port}: ${exception} -s12=Unexpected exception while closing server socket: ${exception} -s13=Unexpected exception while initializing server socket: ${exception} -s14=VanillaVotifier server stopped. -s15=Unexpected exception while loading config: ${exception} -s16=Generating key pair... -s17=Wrong arguments! Correct usage: stop -s18=Can't load config file: access is denied! -s19=Key-size has to be a valid integer number! -s20=Wrong arguments! Correct usage: gen-key-pair [key-size] -s21=Unexpected exception while saving configuration: ${exception} -s22=Stopping VanillaVotifier server... -s23=Key pair generated. -s24=Loading config... -s25=Config loaded. -s26=Wrong arguments! Correct usage: test-vote -s27=Unexpected exception while sending test vote: ${exception} -s28=Unexpected exception while sending RCon command: ${exception} -s29=Unexpected exception while comunicating with client: ${exception} -s30=Unexpected exception while establishing connection: ${exception} -s31-location=help.txt -s32=Wrong arguments! Correct usage: restart -s32=Wrong arguments! Correct usage: help -s33=Unknown command. Type "help" for help. -s34=Wrong arguments! Correct usage: test-query -s35=Unexpected exception while sending test query: ${exception} -s36-location=manual.txt -s37=Wrong arguments! Correct usage: manual -s38=Failed to start server! Perhaps a server is already running on port ${port}? -s39=Can't send RCon command: connection refused! Perhaps the Minecraft RCon server is offline or running on another port? -s40-location=info.txt -s41=Wrong arguments! Correct usage: info -s42-location=license-notice.txt -s43-location=license.txt -s44=Wrong arguments! Correct usage: license -s45=Can't load config: JSON is malformed! ${exception} -s46=Error while while decrypting client input! Perhaps the client has the wrong public key? -s47=Invalid public key file! -s48=Invalid public key file! -s49=Can't find public key file! -s50=Can't find private key file! -s51=Key-size has to be at least 512. -s52=Key-size can't be greater than 16384. -s53=RCon command sent. +# +# Copyright (C) 2015 VirtualDragon +# +# 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 . +# +s1=Starting VanillaVotifier server... +s2=VanillaVotifier server started. +s3=${ip}:${port} established a connection. +s4=Received vote query from ${ip}:${port}: VOTE\t${service-name}\t${user-name}\t${address}\t${time-stamp} +s5=Sending RCon command to ${ip}:${port}: ${command} +s6=RCon command sent. Response: ${response} +s7=Can't send RCon command: invalid password! +s8=Received invalid query from ${ip}:${port}: ${message} +s9=Unexpected exception while closing input stream of ${ip}:${port}: ${exception} +s10=${ip}:${port} disconnected. +s11=Unexpected exception while closing socket connection with ${ip}:${port}: ${exception} +s12=Unexpected exception while closing server socket: ${exception} +s13=Unexpected exception while initializing server socket: ${exception} +s14=VanillaVotifier server stopped. +s15=Unexpected exception while loading config: ${exception} +s16=Generating key pair... +s17=Wrong arguments! Correct usage: stop +s18=Can't load config file: access is denied! +s19=Key-size has to be a valid integer number! +s20=Wrong arguments! Correct usage: gen-key-pair [key-size] +s21=Unexpected exception while saving configuration: ${exception} +s22=Stopping VanillaVotifier server... +s23=Key pair generated. +s24=Loading config... +s25=Config loaded. +s26=Wrong arguments! Correct usage: test-vote +s27=Unexpected exception while sending test vote: ${exception} +s28=Unexpected exception while sending RCon command: ${exception} +s29=Unexpected exception while comunicating with client: ${exception} +s30=Unexpected exception while establishing connection: ${exception} +s31-location=help.txt +s32=Wrong arguments! Correct usage: restart +s32=Wrong arguments! Correct usage: help +s33=Unknown command. Type "help" for help. +s34=Wrong arguments! Correct usage: test-query +s35=Unexpected exception while sending test query: ${exception} +s36-location=manual.txt +s37=Wrong arguments! Correct usage: manual +s38=Failed to start server! Perhaps a server is already running on port ${port}? +s39=Can't send RCon command: connection refused! Perhaps the Minecraft RCon server is offline or running on another port? +s40-location=info.txt +s41=Wrong arguments! Correct usage: info +s42-location=license-notice.txt +s43-location=license.txt +s44=Wrong arguments! Correct usage: license +s45=Can't load config: JSON is malformed! ${exception} +s46=Error while while decrypting client input! Perhaps the client has the wrong public key? +s47=Invalid public key file! +s48=Invalid public key file! +s49=Public key file not found! +s50=Private key file not found! +s51=Key-size has to be at least 512. +s52=Key-size can't be greater than 16384. +s53=RCon command sent. s54=Waiting for all tasks to complete... \ No newline at end of file diff --git a/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/lang_it.properties b/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/lang_it.properties new file mode 100644 index 0000000..4205e75 --- /dev/null +++ b/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/lang_it.properties @@ -0,0 +1,67 @@ +# +# Copyright (C) 2015 VirtualDragon +# +# 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 . +# +s1=Avvio di VanillaVotifier in corso... +s2=VanillaVotifier avviato. +s3=${ip}:${port} ha stabilito una connessione. +s4=Richesta di voto ricevuta da ${ip}:${port}: VOTE\t${service-name}\t${user-name}\t${address}\t${time-stamp} +s5=Invio comando RCon a ${ip}:${port}: ${command} +s6=Comando RCon inviato. Risposta: ${response} +s7=Impossibile inviare il comando RCon: password errata! +s8=Richiesta invalida ricevuta da ${ip}:${port}: ${message} +s9=Errore imprevisto nella chiusura dell'input stream di ${ip}:${port}: ${exception} +s10=${ip}:${port} si \u00e8 sconnesso. +s11=Errore imprevisto nella chiusura della connessione socket con ${ip}:${port}: ${exception} +s12=Errore imprevisto nella chiusura del server socket: ${exception} +s13=Errore imprevisto nell'inizializzazione del server socket: ${exception} +s14=Server di VanillaVotifier arrestato. +s15=Errore imprevisto nel caricamento della configurazione: ${exception} +s16=Generazione della coppia di chiavi in corso... +s17=Argomenti invalidi! Uso corretto: stop +s18=Impossibile caricare il file di configurazione: accesso negato! +s19=Per essere valido, l'argomento key-size dev'essere un numero intero! +s20=Argomenti invalidi! Uso corretto: gen-key-pair [key-size] +s21=Errore imprevisto nel salvataggio della configurazione: ${exception} +s22=Arresto del server di VanillaVotifier in corso... +s23=Coppia di chiavi generata con successo. +s24=Caricamento della configurazione in corso... +s25=Configurazione caricata. +s26=Argomento errati! Uso corretto: test-vote +s27=Errore imprevisto nell'invio del voto di prova: ${exception} +s28=Errore imprevisto nell'invio del comando RCon: ${exception} +s29=Errore imprevisto nella comunicazione con il client: ${exception} +s30=Errore imprevisto nello stabilimento di connessione: ${exception} +s32=Argomenti invalidi! Uso corretto: help +s33=Comando sconosciuto. Digitare "help" per ottenere aiuto. +s34=Argomenti invalidi! Uso corretto: test-query +s35=Errore imprevisto nell'invio della richiesta di prova: ${exception} +s37=Argomenti invalidi! Uso corretto: manual +s38=Impossibile avviare il server! Pu\u00f2 essere che la porta ${port} sia gi\u00e0 occupata dall'esecuzione di un altro server? +s39=Impossibile inviare il comando RCon: connessione rifiutata! Pu\u00f2 essere che il server RCon di Minecraft sia offline o in esecuzione su un'altra porta? +s41=Argomenti invalidi! Uso corretto: info +s42-location=license-notice_it.txt +s44=Argomenti invalidi! Uso corretto: license +s45=Impossibile caricare la configurazione: il file JSON \u00e8 mal formattato! ${exception} +s46=Errore nella decriptazione dell'input del client! Pu\u00f2 essere che il client abbia la chiave pubblica errata? +s47=Il file della chiave pubblica \u00e8 invalido! +s48=Il file della chiave privata \u00e8 invalido! +s49=Impossibile trovare il file della chiave privata! +s50=Impossibile trovare il file della chiave pubblica! +s51=Key-size dev'essere almeno 512. +s52=Key-size dev'essere al massimo 16384. +s53=Comando RCon inviato. +s54=In attesa del completamento di tutte le attivit\u00e0 in esecuzione... +s31-location=help_it.txt diff --git a/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/license-notice_it.txt b/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/license-notice_it.txt new file mode 100644 index 0000000..c8d535b --- /dev/null +++ b/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/license-notice_it.txt @@ -0,0 +1,7 @@ +================================ LICENSE NOTICE ================================ +VanillaVotifier Copyright (C) 2015 VirtualDragon +Questo programma viene fornito SENZA ALCUNA GARANZIA. Questo è software libeto, +ed è libero, ed è possibile apportarne modifiche sotto certe condizioni. +Per maggiori informazioni, digitare "license" o visitare il sito +http://www.gnu.org/licenses/gpl-3.0-standalone.html +================================================================================ \ No newline at end of file diff --git a/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/manual.txt b/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/manual.txt index 6a0d90b..f92bc07 100644 --- a/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/manual.txt +++ b/src/main/resources/co/virtualdragon/vanillaVotifier/impl/lang/manual.txt @@ -1,67 +1,67 @@ -==================================== MANUAL ==================================== -This is the VanillaVotifier manual. Reading this is recommended if it's the -first time you're using VanillaVotifier. - ---------------------------------- CONFIGURATION -------------------------------- -The configuration for VanillaVotifier is automatically generated on the first -startup and located in the same folder you started the program from, with name -"config.json". Since the configuration file gets read by VanillaVotifier only on -startup, you may use the "restart" command to force the program to reload the -settings. -Here is a short description of all settings you can configure in the file. - -"config-version" is simply the version of the config. If VanillaVotifier gets -updated, it can rely on this setting to see if the config is outdated. Please -don't touch this setting! - -"ip" and "port" are the IP and the port you want VanillaVotifier to run on. -Nothing special here. - -"key-pair-files/public" and "key-pair-files/private" are the locations of the -public and private RSA POM files VanillaVotifier needs to work (for more -information, please read the COMMANDS section). Best thing would be to leave -the default ones or to generate new ones through the "gen-key-pair" command. -You can also look up some on-line tools to generate some RSA keys for yourself. - -"rcon/ip" and "rcon/port" are the IP and the port of the RCon-enabled Minecraft -server you want VanillaVotifier to send commands to. Again, nothing special -here. - -"rcon/password" is the password VanillaVotifier has to utilize to connect to -RCon. - -"rcon/commands" are the Minecraft commands VanillaVotifier has to execute -through RCon when somebody votes for your server. "${service-name}", -"${user-name}", "${address}", "${time-stamp}" will be replaced with the value of -the service the user has voted on (like MCSL), his IGN, his address, and the -time stamp he has voted on (for more information, please read the COMMANDS -section). - ------------------------------------ COMMANDS ----------------------------------- -To start out, there are three really basic and simple commands: "stop", -"restart" and "info". There's not much to say here: "stop" stops the -VanillaVotifier server, "restart" restarts VanillaVotifier, thus updting its -config, "info" displays some information about VanillaVotifier itself (like its -version and the third party libraries used). - -A more advanced command is "gen-key-pair". All votifier sites and servers (like -this one) use RSA, basically a way to send data securely between hosts. To -work, RSA needs to have two keys, a public and a private one. It's not really -important to know what this keys are or what they do, but it's important to know -that they are needed to make votifier programs work. When you first start -VanillaVotifier, the two RSA keys are generated for you, but if you want to -generate new ones, use the command "gen-key-pair". - -Lastly, there are two debugging comands: "test-vote" and "test-query". -Basically, whenever someone votes for your server, a string of data is sent to -the votifier server. The string of data looks something like this: -VOTE